Mercurial > hg > orthanc
comparison Core/Toolbox.cpp @ 1933:ff11ba08e5d0
Toolbox::ReadHeader
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 21 Mar 2016 16:47:28 +0100 |
parents | 7a05144cb919 |
children | 325772dadcd6 |
comparison
equal
deleted
inserted
replaced
1932:2d46e378960d | 1933:ff11ba08e5d0 |
---|---|
204 result = source; | 204 result = source; |
205 ToLowerCase(result); | 205 ToLowerCase(result); |
206 } | 206 } |
207 | 207 |
208 | 208 |
209 void Toolbox::ReadFile(std::string& content, | 209 static std::streamsize GetStreamSize(std::istream& f) |
210 const std::string& path) | 210 { |
211 { | |
212 if (!IsRegularFile(path)) | |
213 { | |
214 LOG(ERROR) << std::string("The path does not point to a regular file: ") << path; | |
215 throw OrthancException(ErrorCode_RegularFileExpected); | |
216 } | |
217 | |
218 boost::filesystem::ifstream f; | |
219 f.open(path, std::ifstream::in | std::ifstream::binary); | |
220 if (!f.good()) | |
221 { | |
222 throw OrthancException(ErrorCode_InexistentFile); | |
223 } | |
224 | |
225 // http://www.cplusplus.com/reference/iostream/istream/tellg/ | 211 // http://www.cplusplus.com/reference/iostream/istream/tellg/ |
226 f.seekg(0, std::ios::end); | 212 f.seekg(0, std::ios::end); |
227 std::streamsize size = f.tellg(); | 213 std::streamsize size = f.tellg(); |
228 f.seekg(0, std::ios::beg); | 214 f.seekg(0, std::ios::beg); |
229 | 215 |
216 return size; | |
217 } | |
218 | |
219 | |
220 void Toolbox::ReadFile(std::string& content, | |
221 const std::string& path) | |
222 { | |
223 if (!IsRegularFile(path)) | |
224 { | |
225 LOG(ERROR) << std::string("The path does not point to a regular file: ") << path; | |
226 throw OrthancException(ErrorCode_RegularFileExpected); | |
227 } | |
228 | |
229 boost::filesystem::ifstream f; | |
230 f.open(path, std::ifstream::in | std::ifstream::binary); | |
231 if (!f.good()) | |
232 { | |
233 throw OrthancException(ErrorCode_InexistentFile); | |
234 } | |
235 | |
236 std::streamsize size = GetStreamSize(f); | |
230 content.resize(size); | 237 content.resize(size); |
231 if (size != 0) | 238 if (size != 0) |
232 { | 239 { |
233 f.read(reinterpret_cast<char*>(&content[0]), size); | 240 f.read(reinterpret_cast<char*>(&content[0]), size); |
234 } | 241 } |
235 | 242 |
236 f.close(); | 243 f.close(); |
244 } | |
245 | |
246 | |
247 bool Toolbox::ReadHeader(std::string& header, | |
248 const std::string& path, | |
249 size_t headerSize) | |
250 { | |
251 if (!IsRegularFile(path)) | |
252 { | |
253 LOG(ERROR) << std::string("The path does not point to a regular file: ") << path; | |
254 throw OrthancException(ErrorCode_RegularFileExpected); | |
255 } | |
256 | |
257 boost::filesystem::ifstream f; | |
258 f.open(path, std::ifstream::in | std::ifstream::binary); | |
259 if (!f.good()) | |
260 { | |
261 throw OrthancException(ErrorCode_InexistentFile); | |
262 } | |
263 | |
264 bool full = true; | |
265 | |
266 { | |
267 std::streamsize size = GetStreamSize(f); | |
268 if (size <= 0) | |
269 { | |
270 headerSize = 0; | |
271 full = false; | |
272 } | |
273 else if (static_cast<size_t>(size) < headerSize) | |
274 { | |
275 headerSize = size; // Truncate to the size of the file | |
276 full = false; | |
277 } | |
278 } | |
279 | |
280 header.resize(headerSize); | |
281 if (headerSize != 0) | |
282 { | |
283 f.read(reinterpret_cast<char*>(&header[0]), headerSize); | |
284 } | |
285 | |
286 f.close(); | |
287 | |
288 return full; | |
237 } | 289 } |
238 | 290 |
239 | 291 |
240 void Toolbox::WriteFile(const void* content, | 292 void Toolbox::WriteFile(const void* content, |
241 size_t size, | 293 size_t size, |