// // Created by quentin on 8/4/22. // #include "data.h" #include #include #include #include #include template Data::Data(std::string file) : fileName(std::move(file)) { // Create file if it doesnt exist std::ifstream chkExistIfs(getFilePath(), std::ios::in | std::ios::binary | std::ios::ate); if (chkExistIfs) { // File exists, were not creating one std::ifstream::pos_type fileSize = chkExistIfs.tellg(); chkExistIfs.seekg(0, std::ios::beg); std::vector bytes(fileSize); chkExistIfs.read(bytes.data(), fileSize); document.Parse(std::string(bytes.data(), fileSize).c_str()); chkExistIfs.close(); } else { // File doesnt exist we need to create one // This is the job of the derives constructor. chkExistIfs.close(); }; } template std::string Data::getFilePath() { return fileName; } template void Data::flushToFile() { rapidjson::StringBuffer buffer; rapidjson::Writer writer(buffer); document.Accept(writer); std::ofstream file(getFilePath()); file << buffer.GetString(); file.close(); } template Data::~Data() { flushToFile(); }