# HG changeset patch # User Sebastien Jodogne # Date 1458575248 -3600 # Node ID ff11ba08e5d0bbea03cdebf07a575ccca75f1762 # Parent 2d46e378960d20cd90e111a29856e6ca26378cd1 Toolbox::ReadHeader diff -r 2d46e378960d -r ff11ba08e5d0 Core/Toolbox.cpp --- 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) < headerSize) + { + headerSize = size; // Truncate to the size of the file + full = false; + } + } + + header.resize(headerSize); + if (headerSize != 0) + { + f.read(reinterpret_cast(&header[0]), headerSize); + } + + f.close(); + + return full; + } + + void Toolbox::WriteFile(const void* content, size_t size, const std::string& path) diff -r 2d46e378960d -r ff11ba08e5d0 Core/Toolbox.h --- 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); diff -r 2d46e378960d -r ff11ba08e5d0 UnitTestsSources/UnitTestsMain.cpp --- 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;