| 64 | } |
| 65 | |
| 66 | int IndexedMzMLDecoder::parseOffsets(const String& filename, std::streampos indexoffset, OffsetVector& spectra_offsets, OffsetVector& chromatograms_offsets) |
| 67 | { |
| 68 | //------------------------------------------------------------- |
| 69 | // Open file, jump to end and read last indexoffset bytes into buffer. |
| 70 | //------------------------------------------------------------- |
| 71 | std::ifstream f(filename.c_str()); |
| 72 | if (!f.is_open()) |
| 73 | { |
| 74 | throw Exception::FileNotFound(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, filename); |
| 75 | } |
| 76 | |
| 77 | // get length of file: |
| 78 | f.seekg(0, f.end); |
| 79 | std::streampos length = f.tellg(); |
| 80 | |
| 81 | if (indexoffset < 0 || indexoffset > length) |
| 82 | { |
| 83 | std::cerr << "IndexedMzMLDecoder::parseOffsets Error: Offset was " << |
| 84 | indexoffset << " (not between 0 and " << length << ")." << std::endl; |
| 85 | return -1; |
| 86 | } |
| 87 | |
| 88 | //------------------------------------------------------------- |
| 89 | // Read full end of file to parse offsets for spectra and chroms |
| 90 | //------------------------------------------------------------- |
| 91 | // read data as a block into a buffer |
| 92 | |
| 93 | // allocate enough memory in buffer (+1 for string termination) |
| 94 | std::streampos readl = length - indexoffset; |
| 95 | char* buffer = new(std::nothrow) char[readl + std::streampos(1)]; |
| 96 | |
| 97 | // catch case where not enough memory is available |
| 98 | if (buffer == nullptr) |
| 99 | { |
| 100 | // Warning: Index takes up more than 10 % of the whole file, please check your input file." << std::endl; |
| 101 | std::cerr << "IndexedMzMLDecoder::parseOffsets Could not allocate enough memory to read in index of indexedMzML" << std::endl; |
| 102 | std::cerr << "IndexedMzMLDecoder::parseOffsets calculated index offset " << indexoffset << " and file length " << length << |
| 103 | ", consequently tried to read into memory " << readl << " bytes." << std::endl; |
| 104 | return -1; |
| 105 | } |
| 106 | |
| 107 | // read into memory |
| 108 | f.seekg(-readl, f.end); |
| 109 | f.read(buffer, readl); |
| 110 | buffer[readl] = '\0'; |
| 111 | |
| 112 | //------------------------------------------------------------- |
| 113 | // Add a sane start element and then give it to a DOM parser |
| 114 | //------------------------------------------------------------- |
| 115 | // http://stackoverflow.com/questions/4691039/making-xerces-parse-a-string-insted-of-a-file |
| 116 | std::string tmp_fixed_xml = "<indexedmzML>" + String(buffer) + "\n"; |
| 117 | int res = domParseIndexedEnd_(tmp_fixed_xml, spectra_offsets, chromatograms_offsets); |
| 118 | |
| 119 | delete[] buffer; |
| 120 | |
| 121 | return res; |
| 122 | } |
| 123 |
no test coverage detected