-class BAK_FILE
-{
-public:
-
- //-------------------------------------------------------------------------
- BAK_FILE(const string & fileName, bool removeBak)
- : f(NULL),
- removeBak(false)
- {
- bakSuccessed = false;
- BAK_FILE::removeBak = removeBak;
- fileNameBak = fileName + ".bak";
- /*struct stat fileStat;
- if (stat(fileName.c_str(), &fileStat) == 0)
- {
- char * buff = new char[fileStat.st_size];
- f = fopen(fileName.c_str(), "rb");
- if(f)
- {
- fread(buff, 1, fileStat.st_size, f);
- fclose(f);
-
- fileNameBak = fileName + ".bak";
- f = fopen(fileNameBak.c_str(), "wb");
- if(f)
- {
- fwrite(buff, 1, fileStat.st_size, f);
- fclose(f);
- }
- }
-
- delete[] buff;
-
- bakSuccessed = true;
- }*/
- if (rename(fileName.c_str(), fileNameBak.c_str()))
- {
- printfd(__FILE__, "BAK_FILE::BAK_FILE - rename failed. Message: '%s'\n", strerror(errno));
- }
- else
- {
- bakSuccessed = true;
- }
-
- }
- //-------------------------------------------------------------------------
- ~BAK_FILE()
- {
- if(bakSuccessed && removeBak)
- {
- if (unlink(fileNameBak.c_str()))
- {
- printfd(__FILE__, "BAK_FILE::~BAK_FILE - unlink failed. Message: '%s'\n", strerror(errno));
- }
- }
- }
- //-------------------------------------------------------------------------
-
-private:
- FILE * f;
- bool bakSuccessed;
- string fileNameBak;
- bool removeBak;
-};