]> git.treefish.org Git - phys/latlib.git/blob - configcache.h
82fa3f6787ccba006b242f56b645e00afcbf4b9c
[phys/latlib.git] / configcache.h
1 #ifndef CONFIGCACHE_H
2 #define CONFIGCACHE_H
3
4 #include <vector>
5 #include <string>
6 #include <fstream>
7 #include <sstream>
8 #include <ostream>
9
10 #include <boost/iostreams/filtering_streambuf.hpp>
11 #include <boost/iostreams/stream.hpp>
12 #include <boost/iostreams/filter/bzip2.hpp>
13 #include <boost/iostreams/device/array.hpp>
14 #include <boost/iostreams/copy.hpp>
15
16 #define CACHE_MODE_DISABLED 0
17 #define CACHE_MODE_READ 1
18 #define CACHE_MODE_FULL 2
19
20 using namespace std;
21
22 struct parameter{
23   string id;
24   double val;
25 };
26
27 struct infiledesc {
28   string filename;
29   int nequi;
30   int nskip;
31   bool doVirtualEquilibration;
32   bool extended;
33 };
34
35 class configcache{
36  public:
37   ~configcache();
38   configcache(const string& cacheid, const int& nequi, const int& nskip, const string& datadir, char **configmem, const int& configMemSize, 
39               const int& cachemode=CACHE_MODE_FULL, ostream *_log=NULL);
40   int readConfig(vector<unsigned long> *excludeFileHashes=NULL);
41   void writeConfig();
42   void addPara(const string& parid, const double& val=0);
43   void setPara(const string& parid, const double& value);
44   void writeHeader(const string& headerid, const char *header, long unsigned int size);
45   void * getHeader(const string& headerid);
46   string getOutFileName() { return outFileName.str(); }
47   string getInFileName() { return DATADIR + "/" + openFileDesc.filename; }
48   static unsigned long hash(const string& str);
49   void closeInFile() { inFile.close(); }
50   int inFilesLeft() { return inFiles.size(); }
51
52  private:
53   ostream* log;
54   infiledesc openFileDesc;
55   void finishOutFile();
56   int getParIndex(const string& parid);
57   int NEQUI;
58   int NSKIP;
59   string DATADIR;
60   string CACHEID;
61   int MODE;
62   string getFileId(const bool& shortid=false);
63
64   ofstream outFile;
65   ifstream inFile;
66
67   stringstream outFileName;
68
69   int readnum;
70
71   boost::iostreams::filtering_istreambuf *inBuffer;
72   boost::iostreams::filtering_ostreambuf *outBuffer;
73
74   int inSize;
75
76   int configSize;
77   char *configMem;
78   char *tmpConfig;
79
80   bool refetchDataFiles;
81
82   void fetchDataFiles();
83   
84   bool isValidInFile(const string& infile, infiledesc *filedesc);
85
86   vector<infiledesc> inFiles;
87
88   vector<parameter> Paras;
89
90   int readDataToMem(char *tmpData, long unsigned int dataSize);
91
92   void openOutFile();
93
94   int readHeader();
95
96   bool headerWritten;
97
98   int readFullBlock(char *tmpData, long unsigned int dataSize);
99
100   vector< pair<unsigned long, void *> > headerStore;
101
102   void deleteHeaderStore();
103
104   bool readAllHeaders();
105
106   vector<infiledesc>::iterator getNextInfile(vector<unsigned long> *excludeFileHashes);
107 };
108
109 #endif