2012-11-27 16:17:29 +00:00
|
|
|
/*
|
|
|
|
* TmpFiles.cc
|
|
|
|
*
|
|
|
|
* Collect and clean-up temporary files
|
|
|
|
*
|
|
|
|
* implemented by WangLu
|
|
|
|
* split off by Filodej <philodej@gmail.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
2013-06-24 20:45:07 +00:00
|
|
|
#include <cstdio>
|
2013-11-22 21:39:28 +00:00
|
|
|
#include <sys/stat.h>
|
2013-06-24 20:45:07 +00:00
|
|
|
|
2012-11-26 21:38:13 +00:00
|
|
|
#include "TmpFiles.h"
|
|
|
|
#include "Param.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2013-12-05 20:20:53 +00:00
|
|
|
#ifndef _WIN32
|
2014-01-11 08:36:19 +00:00
|
|
|
# include <unistd.h>
|
2013-12-05 20:20:53 +00:00
|
|
|
# define STAT stat
|
2014-01-09 13:14:12 +00:00
|
|
|
# define RMDIR rmdir
|
2013-12-05 20:20:53 +00:00
|
|
|
#else
|
2014-01-09 13:14:12 +00:00
|
|
|
# include <direct.h>
|
2013-12-05 20:20:53 +00:00
|
|
|
# define STAT _stat
|
2014-01-09 13:14:12 +00:00
|
|
|
# define RMDIR _rmdir
|
2013-12-05 20:20:53 +00:00
|
|
|
#endif
|
|
|
|
|
2012-11-26 21:38:13 +00:00
|
|
|
namespace pdf2htmlEX {
|
|
|
|
|
|
|
|
|
2012-11-29 13:20:26 +00:00
|
|
|
TmpFiles::TmpFiles( const Param& param )
|
2013-11-22 21:39:28 +00:00
|
|
|
: param( param )
|
2012-11-29 13:20:26 +00:00
|
|
|
{ }
|
2012-11-26 21:38:13 +00:00
|
|
|
|
|
|
|
TmpFiles::~TmpFiles()
|
2013-11-22 21:39:28 +00:00
|
|
|
{
|
2012-11-26 21:38:13 +00:00
|
|
|
clean();
|
|
|
|
}
|
|
|
|
|
2012-11-27 16:17:29 +00:00
|
|
|
void TmpFiles::add( const string & fn)
|
2012-11-26 21:38:13 +00:00
|
|
|
{
|
|
|
|
if(!param.clean_tmp)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(tmp_files.insert(fn).second && param.debug)
|
|
|
|
cerr << "Add new temporary file: " << fn << endl;
|
|
|
|
}
|
|
|
|
|
2014-01-11 08:25:09 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-26 21:38:13 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:14:12 +00:00
|
|
|
RMDIR(param.tmp_dir.c_str());
|
2012-11-26 21:38:13 +00:00
|
|
|
if(param.debug)
|
|
|
|
cerr << "Remove temporary directory: " << param.tmp_dir << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace pdf2htmlEX
|
2013-11-24 15:20:25 +00:00
|
|
|
|