diff --git a/concordia/concordia.cpp b/concordia/concordia.cpp index 41448e2..783de43 100644 --- a/concordia/concordia.cpp +++ b/concordia/concordia.cpp @@ -119,48 +119,52 @@ void Concordia::loadRAMIndexFromDisk() { if (boost::filesystem::exists(_getWordMapFilePath()) && boost::filesystem::exists(_getHashedIndexFilePath()) && boost::filesystem::exists(_getMarkersFilePath())) { - // reading index from file - _T->clear(); - std::ifstream hashedIndexFile; - hashedIndexFile.open( - _getHashedIndexFilePath().c_str(), std::ios::in - | std::ios::ate | std::ios::binary); - saidx_t hiFileSize = hashedIndexFile.tellg(); - if (hiFileSize > 0) { - hashedIndexFile.seekg(0, std::ios::beg); + try { + // reading index from file + _T->clear(); + std::ifstream hashedIndexFile; + hashedIndexFile.open( + _getHashedIndexFilePath().c_str(), std::ios::in + | std::ios::ate | std::ios::binary); + long hiFileSize = hashedIndexFile.tellg(); + if (hiFileSize > 0) { + hashedIndexFile.seekg(0, std::ios::beg); - while (!hashedIndexFile.eof()) { - INDEX_CHARACTER_TYPE character = - Utils::readIndexCharacter(hashedIndexFile); - Utils::appendCharToSaucharVector(_T, character); + while (!hashedIndexFile.eof()) { + INDEX_CHARACTER_TYPE character = + Utils::readIndexCharacter(hashedIndexFile); + Utils::appendCharToSaucharVector(_T, character); + } + hashedIndexFile.close(); + } else { + hashedIndexFile.close(); + throw ConcordiaException("Index corrupt: empty hash index file"); } - hashedIndexFile.close(); - } else { - hashedIndexFile.close(); - throw ConcordiaException("Index corrupt: empty hash index file"); - } - // reading markers from file - _markers->clear(); - std::ifstream markersFile; - markersFile.open(_getMarkersFilePath().c_str(), std::ios::in - | std::ios::ate | std::ios::binary); - long maFileSize = markersFile.tellg(); - if (maFileSize > 0) { - markersFile.seekg(0, std::ios::beg); + // reading markers from file + _markers->clear(); + std::ifstream markersFile; + markersFile.open(_getMarkersFilePath().c_str(), std::ios::in + | std::ios::ate | std::ios::binary); + long maFileSize = markersFile.tellg(); + if (maFileSize > 0) { + markersFile.seekg(0, std::ios::beg); - while (!markersFile.eof()) { - SUFFIX_MARKER_TYPE marker = - Utils::readMarker(markersFile); - _markers->push_back(marker); + while (!markersFile.eof()) { + SUFFIX_MARKER_TYPE marker = + Utils::readMarker(markersFile); + _markers->push_back(marker); + } + markersFile.close(); + } else { + markersFile.close(); + throw ConcordiaException("Index corrupt: empty markers file"); } - markersFile.close(); - } else { - markersFile.close(); - throw ConcordiaException("Index corrupt: empty markers file"); + // generating suffix array + _SA = _index->generateSuffixArray(_T); + } catch (const std::bad_alloc&) { + throw ConcordiaException("Error allocating memory, probably out of memory."); } - // generating suffix array - _SA = _index->generateSuffixArray(_T); } else { throw ConcordiaException("Index corrupt: missing files"); }