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