--- /dev/null
+#include "configcache.h"
+
+#include <stdlib.h>
+#include <iostream>
+#include <time.h>
+#include <dirent.h>
+#include <string.h>
+
+configcache::configcache(const string& cacheid, const int& nequi, const int& nskip, const string& datadir, char **configmem, const int& configMemSize){
+ NEQUI = nequi;
+ NSKIP = nskip;
+ DATADIR = datadir;
+ CACHEID = cacheid;
+
+ configMem = (char*)malloc(configMemSize);
+ tmpConfig = (char*)malloc(configMemSize);
+
+ *configmem = configMem;
+ configSize = configMemSize;
+
+ outBuffer = NULL;
+ inBuffer = NULL;
+
+ refetchDataFiles = false;
+}
+
+string configcache::getFileId(const bool& shortid)
+{
+ stringstream fileid;
+
+ if(!shortid) fileid << CACHEID << "_" << NEQUI << "_" << NSKIP;
+ for(int ipara=0; ipara<Paras.size(); ipara++)
+ fileid << "_" << Paras[ipara].id << Paras[ipara].val;
+
+ return fileid.str();
+}
+
+void configcache::fetchDataFiles()
+{
+ struct dirent *de=NULL;
+ DIR *d=NULL;
+
+ d=opendir(DATADIR.c_str());
+ if(d != NULL){
+ while(de = readdir(d)){
+ string filename = de->d_name;
+ if(isValidInFile(filename))
+ {
+ inFiles.push_back(filename);
+ }
+ }
+ }
+}
+
+bool configcache::isValidInFile(const string& infile)
+{
+ char *inchar, *inParts;
+ string truncIn, truncOut;
+
+ if( infile.size() < 4 ) return false;
+
+ if( infile.substr(infile.size()-3) != "dat" ) return false;
+
+ inchar = new char [infile.size()+1];
+ strcpy (inchar, infile.c_str());
+
+ inParts = strtok( inchar, "_" );
+ for(int iPart=0; inParts!=NULL; iPart++)
+ {
+ if( iPart>3 ) { truncIn += "_"; truncIn += inParts; }
+
+ switch(iPart)
+ {
+ case 1: if(inParts != CACHEID) return false; break;
+ case 2: if(atoi(inParts) < NEQUI) return false; break;
+ case 3: if(atoi(inParts) < NSKIP) return false; break;
+ }
+ inParts = strtok( NULL, "_");
+ }
+ truncIn = truncIn.substr(0, truncIn.size()-4);
+
+ delete[] inchar;
+
+ if( truncIn.find( getFileId(true) ) == string::npos ) return false;
+
+ return true;
+}
+
+bool configcache::readConfig()
+{
+ if(DATADIR == "") return false;
+
+ if(refetchDataFiles){
+ refetchDataFiles = false;
+ fetchDataFiles();
+ }
+
+ while(true)
+ {
+ if( (!inFile.is_open()) && inFiles.size() == 0 ) return false;
+
+ while( (!inFile.is_open()) && inFiles.size() > 0 )
+ {
+ inFile.open( (DATADIR + "/" + inFiles.back()).c_str(), std::ios::binary );
+ inFiles.pop_back();
+
+ if( !inFile.is_open() ) continue;
+
+ inBuffer = new boost::iostreams::filtering_istreambuf;
+ inBuffer->push( boost::iostreams::bzip2_decompressor() );
+ inBuffer->push(inFile);
+ }
+
+ if( inFile.is_open() )
+ {
+ if( boost::iostreams::read(*inBuffer, tmpConfig, configSize) == configSize )
+ {
+ memcpy(configMem, tmpConfig, configSize);
+ return true;
+ }
+ else inFile.close();
+ }
+ }
+}
+
+void configcache::writeConfig()
+{
+ if( DATADIR == "") return;
+
+ if(!outFile.is_open()){
+ time_t secstamp = time(NULL);
+
+ outFileName.str("");
+ outFileName << DATADIR << "/" << secstamp << "_" << getFileId() << ".dat.tmp";
+ outFile.open( outFileName.str().c_str(), std::ios::binary );
+
+ outBuffer = new boost::iostreams::filtering_ostreambuf;
+ outBuffer->push(boost::iostreams::bzip2_compressor());
+ outBuffer->push(outFile);
+ }
+
+ boost::iostreams::write(*outBuffer, configMem, configSize);
+}
+
+void configcache::addPara(const string& parid, const double& val){
+ parameter newPara;
+ newPara.id = parid;
+ newPara.val = val;
+ Paras.push_back(newPara);
+}
+
+int configcache::getParIndex(const string& parid){
+ for(int ipara=0; ipara<Paras.size(); ipara++)
+ if(Paras[ipara].id == parid) return ipara;
+}
+
+void configcache::setPara(const string& parid, const double& value){
+ Paras[getParIndex(parid)].val = value;
+ finishOutFile();
+ if(inBuffer != NULL) { delete inBuffer; inBuffer=NULL; }
+ inFile.close();
+ inFiles.clear();
+
+ refetchDataFiles = true;
+}
+
+configcache::~configcache()
+{
+ finishOutFile();
+ delete inBuffer;
+ inBuffer = NULL;
+}
+
+void configcache::finishOutFile()
+{
+ if( outBuffer != NULL )
+ {
+ delete outBuffer;
+ outBuffer = NULL;
+ }
+
+ if( outFile.is_open() )
+ {
+ outFile.close();
+ rename( outFileName.str().c_str(), outFileName.str().substr(0, outFileName.str().size()-4).c_str() );
+ }
+}
--- /dev/null
+#ifndef CONFIGCACHE_H
+#define CONFIGCACHE_H
+
+#include <vector>
+#include <string>
+#include <fstream>
+#include <sstream>
+
+#include <boost/iostreams/filtering_streambuf.hpp>
+#include <boost/iostreams/stream.hpp>
+#include <boost/iostreams/filter/bzip2.hpp>
+#include <boost/iostreams/device/array.hpp>
+#include <boost/iostreams/copy.hpp>
+
+using namespace std;
+
+struct parameter{
+ string id;
+ double val;
+};
+
+class configcache{
+ public:
+ ~configcache();
+ configcache(const string& cacheid, const int& nequi, const int& nskip, const string& datadir, char **configmem, const int& configMemSize);
+ bool readConfig();
+ void writeConfig();
+ void addPara(const string& parid, const double& val=0);
+ void setPara(const string& parid, const double& value);
+
+ private:
+ void finishOutFile();
+ int getParIndex(const string& parid);
+ int NEQUI;
+ int NSKIP;
+ string DATADIR;
+ string CACHEID;
+ string getFileId(const bool& shortid=false);
+
+ ofstream outFile;
+ ifstream inFile;
+
+ stringstream outFileName;
+
+ int readnum;
+
+ boost::iostreams::filtering_istreambuf *inBuffer;
+ boost::iostreams::filtering_ostreambuf *outBuffer;
+
+ int inSize;
+
+ int configSize;
+ char *configMem;
+ char *tmpConfig;
+
+ bool refetchDataFiles;
+
+ void fetchDataFiles();
+
+ bool isValidInFile(const string& infile);
+
+ vector<string> inFiles;
+
+ vector<parameter> Paras;
+};
+
+#endif