handling parsing exceptions and check for error creating a backup
This commit is contained in:
@ -4,6 +4,7 @@
|
|||||||
#include <cerrno>
|
#include <cerrno>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <exception>
|
||||||
|
|
||||||
using json = LAMMPS_NS::json;
|
using json = LAMMPS_NS::json;
|
||||||
|
|
||||||
@ -28,20 +29,28 @@ int main(int argc, char **argv)
|
|||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
auto jsondata = json::parse(fp, nullptr, true, true);
|
auto jsondata = json::parse(fp, nullptr, true, true);
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
if (rename(file.c_str(), backup.c_str())) {
|
||||||
rename(file.c_str(), backup.c_str());
|
printf("Cannot create backup for file %s: %s\n", file.c_str(), strerror(errno));
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
|
||||||
fp = fopen(file.c_str(), "w");
|
fp = fopen(file.c_str(), "w");
|
||||||
if (!fp) {
|
if (!fp) {
|
||||||
printf("Cannot open file %s for writing: %s\n", file.c_str(), strerror(errno));
|
printf("Cannot open file %s for writing: %s\n", file.c_str(), strerror(errno));
|
||||||
return 3;
|
return 4;
|
||||||
}
|
}
|
||||||
std::string data = jsondata.dump(indent);
|
std::string data = jsondata.dump(indent);
|
||||||
data += '\n';
|
data += '\n';
|
||||||
fputs(data.c_str(), fp);
|
fputs(data.c_str(), fp);
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
} catch (std::exception &e) {
|
||||||
|
printf("%s: %s\nSkipping file...\n", argv[i], e.what());
|
||||||
|
fclose(fp);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user