#include "cache.h" #include "indexer.h" #include std::string GetCachedFileName(std::string source_file) { // TODO/FIXME const char* kCacheDirectory = "C:/Users/jacob/Desktop/superindex/indexer/CACHE/"; std::replace(source_file.begin(), source_file.end(), '\\', '_'); std::replace(source_file.begin(), source_file.end(), '/', '_'); std::replace(source_file.begin(), source_file.end(), ':', '_'); std::replace(source_file.begin(), source_file.end(), '.', '_'); return kCacheDirectory + source_file + ".json"; } std::unique_ptr LoadCachedFile(std::string filename) { std::string cache_file = GetCachedFileName(filename); std::ifstream cache; cache.open(GetCachedFileName(filename)); if (!cache.good()) return nullptr; std::string file_content = std::string( std::istreambuf_iterator(cache), std::istreambuf_iterator()); optional indexed = Deserialize(filename, file_content); if (indexed) return MakeUnique(indexed.value()); return nullptr; } void WriteToCache(std::string filename, IndexedFile& file) { std::string indexed_content = Serialize(file); std::ofstream cache; cache.open(GetCachedFileName(filename)); assert(cache.good()); cache << indexed_content; cache.close(); }