diff options
Diffstat (limited to 'oop2eindopdr/CacheManager.cpp')
-rw-r--r-- | oop2eindopdr/CacheManager.cpp | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/oop2eindopdr/CacheManager.cpp b/oop2eindopdr/CacheManager.cpp index fad7b65..b8c368e 100644 --- a/oop2eindopdr/CacheManager.cpp +++ b/oop2eindopdr/CacheManager.cpp @@ -35,20 +35,26 @@ void CacheManager::update_cache() { std::fstream& date = *cache_get("date"); this->age = std::time(nullptr); date << this->age; + date.close(); } +std::fstream* CacheManager::cache_get(std::string filename) { return cache_get(filename.c_str()); } std::fstream* CacheManager::cache_get(const char* filename) { - std::fstream* file = new std::fstream(prefix_cache_path(filename), std::ios::out | std::ios::in); - this->files.push_back(file); + std::filesystem::path fixed_path = prefix_cache_path(filename); // fix duplicate slashes and add prefix + std::filesystem::create_directories(fixed_path.parent_path()); // make sure folder exists + if (!std::filesystem::exists(fixed_path)) + std::fstream(fixed_path, std::ios::trunc | std::ios::out).close(); // touch file + std::fstream* file = new std::fstream(fixed_path.c_str(), std::ios::out | std::ios::in); // open file + this->files.push_back(file); // add file pointer (for closing all files on exit) return file; } +bool CacheManager::cache_exists(std::string filename) { return cache_exists(filename.c_str()); } bool CacheManager::cache_exists(const char* filename) { return std::filesystem::exists(prefix_cache_path(filename)); } + std::string CacheManager::prefix_cache_path(const char* filename) { - std::string out = this->cache_path; - out.append(filename); - return out; + return this->cache_path + "/" + std::string(filename); } |