1 #include "configcache.h"
9 configcache::configcache(const string& cacheid, const int& nequi, const int& nskip, const string& datadir, char **configmem, const int& configMemSize){
15 configMem = (char*)malloc(configMemSize);
16 tmpConfig = (char*)malloc(configMemSize);
18 *configmem = configMem;
19 configSize = configMemSize;
24 refetchDataFiles = false;
27 string configcache::getFileId(const bool& shortid)
31 if(!shortid) fileid << CACHEID << "_" << NEQUI << "_" << NSKIP;
32 for(int ipara=0; ipara<Paras.size(); ipara++)
33 fileid << "_" << Paras[ipara].id << Paras[ipara].val;
38 void configcache::fetchDataFiles()
40 struct dirent *de=NULL;
43 d=opendir(DATADIR.c_str());
45 while(de = readdir(d)){
46 string filename = de->d_name;
47 if(isValidInFile(filename))
49 inFiles.push_back(filename);
55 bool configcache::isValidInFile(const string& infile)
57 char *inchar, *inParts;
58 string truncIn, truncOut;
60 if( infile.size() < 4 ) return false;
62 if( infile.substr(infile.size()-3) != "dat" ) return false;
64 inchar = new char [infile.size()+1];
65 strcpy (inchar, infile.c_str());
67 inParts = strtok( inchar, "_" );
68 for(int iPart=0; inParts!=NULL; iPart++)
70 if( iPart>3 ) { truncIn += "_"; truncIn += inParts; }
74 case 1: if(inParts != CACHEID) return false; break;
75 case 2: if(atoi(inParts) < NEQUI) return false; break;
76 case 3: if(atoi(inParts) < NSKIP) return false; break;
78 inParts = strtok( NULL, "_");
80 truncIn = truncIn.substr(0, truncIn.size()-4);
84 if( truncIn.find( getFileId(true) ) == string::npos ) return false;
89 bool configcache::readConfig()
91 if(DATADIR == "") return false;
94 refetchDataFiles = false;
100 if( (!inFile.is_open()) && inFiles.size() == 0 ) return false;
102 while( (!inFile.is_open()) && inFiles.size() > 0 )
104 inFile.open( (DATADIR + "/" + inFiles.back()).c_str(), std::ios::binary );
107 if( !inFile.is_open() ) continue;
109 inBuffer = new boost::iostreams::filtering_istreambuf;
110 inBuffer->push( boost::iostreams::bzip2_decompressor() );
111 inBuffer->push(inFile);
114 if( inFile.is_open() )
116 if( boost::iostreams::read(*inBuffer, tmpConfig, configSize) == configSize )
118 memcpy(configMem, tmpConfig, configSize);
126 void configcache::writeConfig()
128 if( DATADIR == "") return;
130 if(!outFile.is_open()){
131 time_t secstamp = time(NULL);
134 outFileName << DATADIR << "/" << secstamp << "_" << getFileId() << ".dat.tmp";
135 outFile.open( outFileName.str().c_str(), std::ios::binary );
137 outBuffer = new boost::iostreams::filtering_ostreambuf;
138 outBuffer->push(boost::iostreams::bzip2_compressor());
139 outBuffer->push(outFile);
142 boost::iostreams::write(*outBuffer, configMem, configSize);
145 void configcache::addPara(const string& parid, const double& val){
149 Paras.push_back(newPara);
152 int configcache::getParIndex(const string& parid){
153 for(int ipara=0; ipara<Paras.size(); ipara++)
154 if(Paras[ipara].id == parid) return ipara;
157 void configcache::setPara(const string& parid, const double& value){
158 Paras[getParIndex(parid)].val = value;
160 if(inBuffer != NULL) { delete inBuffer; inBuffer=NULL; }
164 refetchDataFiles = true;
167 configcache::~configcache()
174 void configcache::finishOutFile()
176 if( outBuffer != NULL )
182 if( outFile.is_open() )
185 rename( outFileName.str().c_str(), outFileName.str().substr(0, outFileName.str().size()-4).c_str() );