From 5adde094541b74abf79a902dc4b63141d281b818 Mon Sep 17 00:00:00 2001 From: Regina Kleinhappel Date: Mon, 14 May 2012 16:44:40 +0200 Subject: [PATCH] ... --- CMakeLists.txt | 5 +- configcache.cpp | 187 ++++++++++++++++++++++++++++++++++++++++++++++++ configcache.h | 67 +++++++++++++++++ 3 files changed, 258 insertions(+), 1 deletion(-) create mode 100644 configcache.cpp create mode 100644 configcache.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 7ef5aae..117984c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,3 +1,6 @@ project(latlib) -add_library(obs obs.cpp) \ No newline at end of file +add_library(obs obs.cpp) +add_library(configcache configcache.cpp) + +target_link_libraries(configcache boost_iostreams) \ No newline at end of file diff --git a/configcache.cpp b/configcache.cpp new file mode 100644 index 0000000..1c4aa05 --- /dev/null +++ b/configcache.cpp @@ -0,0 +1,187 @@ +#include "configcache.h" + +#include +#include +#include +#include +#include + +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; iparad_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 +#include +#include +#include + +#include +#include +#include +#include +#include + +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 inFiles; + + vector Paras; +}; + +#endif -- 2.39.5