1
0
mirror of https://github.com/pdf2htmlEX/pdf2htmlEX.git synced 2024-09-12 11:24:34 +00:00
pdf2htmlEX/src/TmpFiles.cc
Marc Sanfacon 5fab160e05 Modifications following code review
Fixed rmdir under Windows/MINGW
2014-01-09 08:14:12 -05:00

82 lines
1.5 KiB
C++

/*
* TmpFiles.cc
*
* Collect and clean-up temporary files
*
* implemented by WangLu
* split off by Filodej <philodej@gmail.com>
*/
#include <iostream>
#include <cstdio>
#include <sys/stat.h>
#include "TmpFiles.h"
#include "Param.h"
using namespace std;
#ifndef _WIN32
# define STAT stat
# define RMDIR rmdir
#else
# include <direct.h>
# define STAT _stat
# define RMDIR _rmdir
#endif
namespace pdf2htmlEX {
TmpFiles::TmpFiles( const Param& param )
: param( param )
{ }
TmpFiles::~TmpFiles()
{
clean();
}
void TmpFiles::add( const string & fn)
{
if(!param.clean_tmp)
return;
if(tmp_files.insert(fn).second && param.debug)
cerr << "Add new temporary file: " << fn << endl;
}
void TmpFiles::clean()
{
if(!param.clean_tmp)
return;
for(auto iter = tmp_files.begin(); iter != tmp_files.end(); ++iter)
{
const string & fn = *iter;
remove(fn.c_str());
if(param.debug)
cerr << "Remove temporary file: " << fn << endl;
}
RMDIR(param.tmp_dir.c_str());
if(param.debug)
cerr << "Remove temporary directory: " << param.tmp_dir << endl;
}
// Return the total size of the temporary files in bytes
double TmpFiles::get_total_size() const
{
double total_size = 0;
struct STAT st;
for(auto iter = tmp_files.begin(); iter != tmp_files.end(); ++iter) {
STAT(iter->c_str(), &st);
total_size += st.st_size;
}
return total_size;
}
} // namespace pdf2htmlEX