simplify isChecksumMatching, move to filesystem::path

issue#70
Houtan Bastani 2019-10-07 15:20:07 +02:00
parent 6191c69d74
commit 6e92f79c13
No known key found for this signature in database
GPG Key ID: 000094FB955BE169
1 changed files with 18 additions and 23 deletions

View File

@ -6580,7 +6580,7 @@ DynamicModel::dynamicOnlyEquationsNbr() const
bool bool
DynamicModel::isChecksumMatching(const string &basename, bool block) const DynamicModel::isChecksumMatching(const string &basename, bool block) const
{ {
std::stringstream buffer; stringstream buffer;
// Write equation tags // Write equation tags
for (const auto & equation_tag : equation_tags) for (const auto & equation_tag : equation_tags)
@ -6632,40 +6632,35 @@ DynamicModel::isChecksumMatching(const string &basename, bool block) const
} }
} }
unsigned int result = hash<string>{}(buffer.str()); size_t result = hash<string>{}(buffer.str());
bool basename_dir_exists = !filesystem::create_directory(basename);
// check whether basename directory exist. If not, create it. // check whether basename directory exist. If not, create it.
// If it does, read old checksum if it exist // If it does, read old checksum if it exists, return if equal to result
fstream checksum_file; fstream checksum_file;
string filename = basename + "/checksum"; auto filename = filesystem::path{basename} / "checksum";
unsigned int old_checksum = 0; if (!filesystem::create_directory(basename))
// read old checksum if it exists
if (basename_dir_exists)
{ {
checksum_file.open(filename, ios::in | ios::binary); checksum_file.open(filename, ios::in | ios::binary);
if (checksum_file.is_open()) if (checksum_file.is_open())
{ {
size_t old_checksum;
checksum_file >> old_checksum; checksum_file >> old_checksum;
checksum_file.close(); checksum_file.close();
if (old_checksum == result)
return true;
} }
} }
// write new checksum file if none or different from old checksum
if (old_checksum != result)
{
checksum_file.open(filename, ios::out | ios::binary);
if (!checksum_file.is_open())
{
cerr << "ERROR: Can't open file " << filename << endl;
exit(EXIT_FAILURE);
}
checksum_file << result;
checksum_file.close();
return false;
}
return true; // write new checksum file if none or different from old checksum
checksum_file.open(filename, ios::out | ios::binary);
if (!checksum_file.is_open())
{
cerr << "ERROR: Can't open file " << filename << endl;
exit(EXIT_FAILURE);
}
checksum_file << result;
checksum_file.close();
return false;
} }
void void