| 128 | |
| 129 | |
| 130 | std::ostream* FileManager::createDataOutStream(const std::string& filename, |
| 131 | bool binary, bool truncate) const { |
| 132 | // choose open mode |
| 133 | std::ios::openmode mode = std::ios::out; |
| 134 | if (binary) { mode |= std::ios::binary; } |
| 135 | if (truncate) { mode |= std::ios::trunc; } |
| 136 | |
| 137 | const bool relative = !isAbsolute(filename); |
| 138 | if (relative) { |
| 139 | // try directory stored in DB |
| 140 | if (BZDB.isSet("directory")) { |
| 141 | const std::string path = catPath(BZDB.get("directory"), filename); |
| 142 | std::ofstream* stream = new std::ofstream(path.c_str(), mode); |
| 143 | if (stream && *stream) { |
| 144 | return stream; |
| 145 | } |
| 146 | delete stream; |
| 147 | } |
| 148 | |
| 149 | // try data directory |
| 150 | { |
| 151 | const std::string path = catPath("data", filename); |
| 152 | std::ofstream* stream = new std::ofstream(path.c_str(), mode); |
| 153 | if (stream && *stream) { |
| 154 | return stream; |
| 155 | } |
| 156 | delete stream; |
| 157 | } |
| 158 | } |
| 159 | else { |
| 160 | // try absolute path |
| 161 | int successMkdir = 0; |
| 162 | int i = 0; |
| 163 | #ifndef _WIN32 |
| 164 | // create all directories above the file |
| 165 | while ((i = filename.find('/', i + 1)) != -1) { |
| 166 | const std::string subDir = filename.substr(0, i); |
| 167 | if (!isDirectory(subDir)) { |
| 168 | successMkdir = mkdir(subDir.c_str(), 0755); |
| 169 | if (successMkdir != 0) { |
| 170 | perror("Unable to make directory"); |
| 171 | return NULL; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | #else |
| 176 | // create all directories above the file |
| 177 | i = 2; // don't stat on a drive, it will fail |
| 178 | while ((i = filename.find('\\', i + 1)) != -1) { |
| 179 | const std::string subDir = filename.substr(0, i); |
| 180 | if (!isDirectory(subDir)) { |
| 181 | successMkdir = _mkdir(subDir.c_str()); |
| 182 | /*if (successMkdir != 0) |
| 183 | { |
| 184 | perror("Unable to make directory"); |
| 185 | return NULL; |
| 186 | } */ |
| 187 | } |
no test coverage detected