1 #include "configcache.h"
8 configcache::configcache(const string& cacheid, const int& nequi, const int& nskip, const string& datadir, char **configmem, const int& configMemSize){
14 configMem = (char*)malloc(configMemSize);
15 tmpConfig = (char*)malloc(configMemSize);
17 *configmem = configMem;
18 configSize = configMemSize;
23 refetchDataFiles = false;
26 string configcache::getFileId(const bool& shortid)
30 if(!shortid) fileid << CACHEID << "_" << NEQUI << "_" << NSKIP;
31 for(int ipara=0; ipara<Paras.size(); ipara++)
32 fileid << "_" << Paras[ipara].id << Paras[ipara].val;
37 void configcache::fetchDataFiles()
39 struct dirent *de=NULL;
42 d=opendir(DATADIR.c_str());
44 while(de = readdir(d)){
45 string filename = de->d_name;
46 if(isValidInFile(filename))
48 inFiles.push_back(filename);
54 bool configcache::isValidInFile(const string& infile)
56 char *inchar, *inParts;
57 string truncIn, truncOut;
59 if( infile.size() < 4 ) return false;
61 if( infile.substr(infile.size()-3) != "dat" ) return false;
63 inchar = new char [infile.size()+1];
64 strcpy (inchar, infile.c_str());
66 inParts = strtok( inchar, "_" );
67 for(int iPart=0; inParts!=NULL; iPart++)
69 if( iPart>3 ) { truncIn += "_"; truncIn += inParts; }
73 case 1: if(inParts != CACHEID) return false; break;
74 case 2: if(atoi(inParts) < NEQUI) return false; break;
75 case 3: if(atoi(inParts) < NSKIP) return false; break;
77 inParts = strtok( NULL, "_");
79 truncIn = truncIn.substr(0, truncIn.size()-4);
83 if( truncIn.find( getFileId(true) + "_" ) == string::npos ) return false;
88 bool configcache::readConfig()
90 if(DATADIR == "") return false;
93 refetchDataFiles = false;
99 if( (!inFile.is_open()) && inFiles.size() == 0 ) return false;
101 while( (!inFile.is_open()) && inFiles.size() > 0 )
103 inFile.open( (DATADIR + "/" + inFiles.back()).c_str(), std::ios::binary );
106 if( !inFile.is_open() ) continue;
108 inBuffer = new boost::iostreams::filtering_istreambuf;
109 inBuffer->push( boost::iostreams::bzip2_decompressor() );
110 inBuffer->push(inFile);
113 if( inFile.is_open() )
115 if( boost::iostreams::read(*inBuffer, tmpConfig, configSize) == configSize )
117 memcpy(configMem, tmpConfig, configSize);
125 void configcache::writeConfig()
127 if( DATADIR == "") return;
129 if(!outFile.is_open()){
130 time_t secstamp = time(NULL);
133 outFileName << DATADIR << "/" << secstamp << "_" << getFileId() << "_.dat.tmp";
134 outFile.open( outFileName.str().c_str(), std::ios::binary );
136 outBuffer = new boost::iostreams::filtering_ostreambuf;
137 outBuffer->push(boost::iostreams::bzip2_compressor());
138 outBuffer->push(outFile);
141 boost::iostreams::write(*outBuffer, configMem, configSize);
144 void configcache::addPara(const string& parid, const double& val){
148 Paras.push_back(newPara);
151 int configcache::getParIndex(const string& parid){
152 for(int ipara=0; ipara<Paras.size(); ipara++)
153 if(Paras[ipara].id == parid) return ipara;
156 void configcache::setPara(const string& parid, const double& value){
157 Paras[getParIndex(parid)].val = value;
159 if(inBuffer != NULL) { delete inBuffer; inBuffer=NULL; }
163 refetchDataFiles = true;
166 configcache::~configcache()
173 void configcache::finishOutFile()
175 if( outBuffer != NULL )
181 if( outFile.is_open() )
184 rename( outFileName.str().c_str(), outFileName.str().substr(0, outFileName.str().size()-4).c_str() );