]> git.treefish.org Git - phys/latlib.git/blob - configcache.cpp
added outfile-flush
[phys/latlib.git] / configcache.cpp
1 #include "configcache.h"
2
3 #include <stdlib.h>
4 #include <iostream>
5 #include <time.h>
6 #include <dirent.h>
7
8 configcache::configcache(const string& cacheid, const int& nequi, const int& nskip, const string& datadir, char **configmem, const int& configMemSize){
9   NEQUI = nequi;
10   NSKIP = nskip;
11   DATADIR = datadir;
12   CACHEID = cacheid;
13
14   configMem = (char*)malloc(configMemSize);
15   tmpConfig = (char*)malloc(configMemSize);
16
17   *configmem = configMem;
18   configSize = configMemSize;
19
20   outBuffer = NULL;
21   inBuffer = NULL;
22
23   refetchDataFiles = false;
24 }
25
26 string configcache::getFileId(const bool& shortid)
27 {
28   stringstream fileid;
29
30   if(!shortid) fileid << CACHEID << "_" << NEQUI << "_" << NSKIP;
31   for(int ipara=0; ipara<Paras.size(); ipara++)
32     fileid << "_" << Paras[ipara].id << Paras[ipara].val;
33
34   return fileid.str();
35 }
36
37 void configcache::fetchDataFiles()
38 {
39   struct dirent *de=NULL;
40   DIR *d=NULL;
41   
42   d=opendir(DATADIR.c_str());
43   if(d != NULL){
44     while(de = readdir(d)){
45       string filename = de->d_name;
46       if(isValidInFile(filename)) 
47         {
48           inFiles.push_back(filename);
49         }
50     }
51   }
52 }
53
54 bool configcache::isValidInFile(const string& infile)
55 {
56   char *inchar, *inParts;
57   string truncIn, truncOut;
58
59   if( infile.size() < 4 ) return false;
60
61   if( infile.substr(infile.size()-3) != "dat" ) return false;
62
63   inchar = new char [infile.size()+1];
64   strcpy (inchar, infile.c_str());
65   
66   inParts = strtok( inchar, "_" );
67   for(int iPart=0; inParts!=NULL; iPart++)
68     {
69       if( iPart>3 ) { truncIn += "_"; truncIn += inParts; }
70
71       switch(iPart)
72         {
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;
76         }
77       inParts = strtok( NULL, "_");
78     }
79   truncIn = truncIn.substr(0, truncIn.size()-4);
80
81   delete[] inchar;
82
83   if( truncIn.find( getFileId(true) + "_" ) == string::npos ) return false;
84
85   return true;
86 }
87
88 bool configcache::readConfig()
89 {
90   if(DATADIR == "") return false;
91
92   if(refetchDataFiles){
93     refetchDataFiles = false;
94     fetchDataFiles();
95   }
96
97   while(true)
98     {
99       if( (!inFile.is_open()) && inFiles.size() == 0 ) return false;
100
101       while( (!inFile.is_open()) && inFiles.size() > 0 )
102         {
103           inFile.open( (DATADIR + "/" + inFiles.back()).c_str(), std::ios::binary );
104           inFiles.pop_back();
105
106           if( !inFile.is_open() ) continue;
107
108           inBuffer = new boost::iostreams::filtering_istreambuf;
109           inBuffer->push( boost::iostreams::bzip2_decompressor() );
110           inBuffer->push(inFile);
111         }
112
113       if( inFile.is_open() ) 
114         {
115           if( boost::iostreams::read(*inBuffer, tmpConfig, configSize) == configSize )
116             {
117               memcpy(configMem, tmpConfig, configSize);
118               return true;
119             }
120           else inFile.close();
121         }
122     }
123 }
124
125 void configcache::writeConfig()
126 {
127   if( DATADIR == "") return;
128
129   if(!outFile.is_open()){
130     time_t secstamp = time(NULL);
131
132     outFileName.str("");
133     outFileName << DATADIR << "/" << secstamp << "_" << getFileId() << "_.dat.tmp";    
134     outFile.open( outFileName.str().c_str(), std::ios::binary );
135
136     outBuffer = new boost::iostreams::filtering_ostreambuf;
137     outBuffer->push(boost::iostreams::bzip2_compressor());
138     outBuffer->push(outFile);
139   }
140
141   boost::iostreams::write(*outBuffer, configMem, configSize);
142
143   outFile.flush();
144 }
145
146 void configcache::addPara(const string& parid, const double& val){
147   parameter newPara;
148   newPara.id = parid;
149   newPara.val = val;
150   Paras.push_back(newPara);
151 }
152
153 int configcache::getParIndex(const string& parid){
154   for(int ipara=0; ipara<Paras.size(); ipara++)
155     if(Paras[ipara].id == parid) return ipara;
156 }
157
158 void configcache::setPara(const string& parid, const double& value){
159   Paras[getParIndex(parid)].val = value;
160   finishOutFile();
161   if(inBuffer != NULL) { delete inBuffer; inBuffer=NULL; } 
162   inFile.close();
163   inFiles.clear();
164
165   refetchDataFiles = true;
166 }
167
168 configcache::~configcache()
169 {
170   finishOutFile();
171   delete inBuffer;
172   inBuffer = NULL;
173 }
174
175 void configcache::finishOutFile()
176 {
177   if( outBuffer != NULL )
178     {
179       delete outBuffer;
180       outBuffer = NULL;
181     }
182
183   if( outFile.is_open() )
184     {
185       outFile.close();
186       rename( outFileName.str().c_str(), outFileName.str().substr(0, outFileName.str().size()-4).c_str() );
187     }
188 }