Mercurial > hg > orthanc
changeset 1933:ff11ba08e5d0
Toolbox::ReadHeader
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 21 Mar 2016 16:47:28 +0100 |
parents | 2d46e378960d |
children | 72a2fd7fed8b |
files | Core/Toolbox.cpp Core/Toolbox.h UnitTestsSources/UnitTestsMain.cpp |
diffstat | 3 files changed, 71 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/Core/Toolbox.cpp Mon Mar 21 15:21:23 2016 +0100 +++ b/Core/Toolbox.cpp Mon Mar 21 16:47:28 2016 +0100 @@ -206,6 +206,17 @@ } + static std::streamsize GetStreamSize(std::istream& f) + { + // http://www.cplusplus.com/reference/iostream/istream/tellg/ + f.seekg(0, std::ios::end); + std::streamsize size = f.tellg(); + f.seekg(0, std::ios::beg); + + return size; + } + + void Toolbox::ReadFile(std::string& content, const std::string& path) { @@ -222,11 +233,7 @@ throw OrthancException(ErrorCode_InexistentFile); } - // http://www.cplusplus.com/reference/iostream/istream/tellg/ - f.seekg(0, std::ios::end); - std::streamsize size = f.tellg(); - f.seekg(0, std::ios::beg); - + std::streamsize size = GetStreamSize(f); content.resize(size); if (size != 0) { @@ -237,6 +244,51 @@ } + bool Toolbox::ReadHeader(std::string& header, + const std::string& path, + size_t headerSize) + { + if (!IsRegularFile(path)) + { + LOG(ERROR) << std::string("The path does not point to a regular file: ") << path; + throw OrthancException(ErrorCode_RegularFileExpected); + } + + boost::filesystem::ifstream f; + f.open(path, std::ifstream::in | std::ifstream::binary); + if (!f.good()) + { + throw OrthancException(ErrorCode_InexistentFile); + } + + bool full = true; + + { + std::streamsize size = GetStreamSize(f); + if (size <= 0) + { + headerSize = 0; + full = false; + } + else if (static_cast<size_t>(size) < headerSize) + { + headerSize = size; // Truncate to the size of the file + full = false; + } + } + + header.resize(headerSize); + if (headerSize != 0) + { + f.read(reinterpret_cast<char*>(&header[0]), headerSize); + } + + f.close(); + + return full; + } + + void Toolbox::WriteFile(const void* content, size_t size, const std::string& path)
--- a/Core/Toolbox.h Mon Mar 21 15:21:23 2016 +0100 +++ b/Core/Toolbox.h Mon Mar 21 16:47:28 2016 +0100 @@ -66,6 +66,10 @@ void ReadFile(std::string& content, const std::string& path); + bool ReadHeader(std::string& header, + const std::string& path, + size_t headerSize); + void WriteFile(const std::string& content, const std::string& path);
--- a/UnitTestsSources/UnitTestsMain.cpp Mon Mar 21 15:21:23 2016 +0100 +++ b/UnitTestsSources/UnitTestsMain.cpp Mon Mar 21 16:47:28 2016 +0100 @@ -558,6 +558,16 @@ ASSERT_EQ(11u, t.size()); ASSERT_EQ(0, t[5]); ASSERT_EQ(0, memcmp(s.c_str(), t.c_str(), s.size())); + + std::string h; + ASSERT_EQ(true, Toolbox::ReadHeader(h, path.c_str(), 1)); + ASSERT_EQ(1, h.size()); + ASSERT_EQ('H', h[0]); + ASSERT_EQ(true, Toolbox::ReadHeader(h, path.c_str(), 0)); + ASSERT_EQ(0, h.size()); + ASSERT_EQ(false, Toolbox::ReadHeader(h, path.c_str(), 32)); + ASSERT_EQ(11u, h.size()); + ASSERT_EQ(0, memcmp(s.c_str(), h.c_str(), s.size())); } std::string u;