Mercurial > hg > orthanc
comparison OrthancFramework/Sources/SystemToolbox.cpp @ 4342:52166629239f
SystemToolbox::ReadFileRange()
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 03 Dec 2020 18:48:06 +0100 |
parents | 785a2713323e |
children | 80fd140b12ba |
comparison
equal
deleted
inserted
replaced
4341:977c2759eb0a | 4342:52166629239f |
---|---|
222 log); | 222 log); |
223 } | 223 } |
224 | 224 |
225 std::streamsize size = GetStreamSize(f); | 225 std::streamsize size = GetStreamSize(f); |
226 content.resize(static_cast<size_t>(size)); | 226 content.resize(static_cast<size_t>(size)); |
227 | |
228 if (static_cast<std::streamsize>(content.size()) != size) | |
229 { | |
230 throw OrthancException(ErrorCode_InternalError, | |
231 "Reading a file that is too large for a 32bit architecture"); | |
232 } | |
233 | |
227 if (size != 0) | 234 if (size != 0) |
228 { | 235 { |
229 f.read(&content[0], size); | 236 f.read(&content[0], size); |
230 } | 237 } |
231 | 238 |
805 else | 812 else |
806 { | 813 { |
807 return (base / relative).string(); | 814 return (base / relative).string(); |
808 } | 815 } |
809 } | 816 } |
817 | |
818 | |
819 void SystemToolbox::ReadFileRange(std::string& content, | |
820 const std::string& path, | |
821 uint64_t start, // Inclusive | |
822 uint64_t end, // Exclusive | |
823 bool throwIfOverflow) | |
824 { | |
825 if (start > end) | |
826 { | |
827 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
828 } | |
829 | |
830 if (!IsRegularFile(path)) | |
831 { | |
832 throw OrthancException(ErrorCode_RegularFileExpected, | |
833 "The path does not point to a regular file: " + path); | |
834 } | |
835 | |
836 boost::filesystem::ifstream f; | |
837 f.open(path, std::ifstream::in | std::ifstream::binary); | |
838 if (!f.good()) | |
839 { | |
840 throw OrthancException(ErrorCode_InexistentFile, | |
841 "File not found: " + path); | |
842 } | |
843 | |
844 uint64_t fileSize = static_cast<uint64_t>(GetStreamSize(f)); | |
845 if (end > fileSize) | |
846 { | |
847 if (throwIfOverflow) | |
848 { | |
849 throw OrthancException(ErrorCode_ParameterOutOfRange, | |
850 "Reading beyond the end of a file"); | |
851 } | |
852 else | |
853 { | |
854 end = fileSize; | |
855 } | |
856 } | |
857 | |
858 if (start <= end) | |
859 { | |
860 content.resize(static_cast<size_t>(end - start)); | |
861 | |
862 if (static_cast<uint64_t>(content.size()) != (end - start)) | |
863 { | |
864 throw OrthancException(ErrorCode_InternalError, | |
865 "Reading a file that is too large for a 32bit architecture"); | |
866 } | |
867 | |
868 if (!content.empty()) | |
869 { | |
870 f.seekg(start, std::ios::beg); | |
871 f.read(&content[0], static_cast<std::streamsize>(content.size())); | |
872 } | |
873 } | |
874 else | |
875 { | |
876 content.clear(); | |
877 } | |
878 | |
879 f.close(); | |
880 } | |
810 } | 881 } |