]> git.treefish.org Git - phys/latlib.git/blob - configcache.h
forgot to close dir.
[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 extended;
32 };
33
34 class configcache{
35  public:
36   ~configcache();
37   configcache(const string& cacheid, const int& nequi, const int& nskip, const string& datadir, char **configmem, const int& configMemSize, 
38               const int& cachemode=CACHE_MODE_FULL, ostream *_log=NULL);
39   void readConfig(bool *readnewconfig, int *nequileft, vector<unsigned long> *excludeFileHashes=NULL);
40   void writeConfig(int actnequi);
41   void addPara(const string& parid, const double& val=0);
42   void setPara(const string& parid, const double& value);
43   void writeHeader(const string& headerid, const char *header, long unsigned int size, int actnequi);
44   void * getHeader(const string& headerid);
45   string getOutFileName() { return outFileName.str(); }
46   string getInFileName() { return DATADIR + "/" + openFileDesc.filename; }
47   static unsigned long hash(const string& str);
48   void closeInFile() { inFile.close(); }
49   int inFilesLeft() { return inFiles.size(); }
50   char* getConfigMem() { return configMem; }
51   int getConfigSize() { return configSize; }
52   bool isOutFileOpen () { return outFile.is_open(); }
53   void finishOutFile();
54
55  private:
56   ostream* log;
57   infiledesc openFileDesc;
58   int getParIndex(const string& parid);
59   int NEQUI;
60   int NSKIP;
61   string DATADIR;
62   string CACHEID;
63   int MODE;
64   string getFileId(int actnequi, const bool& shortid=false);
65
66   ofstream outFile;
67   ifstream inFile;
68
69   stringstream outFileName;
70
71   int readnum;
72
73   boost::iostreams::filtering_istreambuf *inBuffer;
74   boost::iostreams::filtering_ostreambuf *outBuffer;
75
76   int inSize;
77
78   int configSize;
79   char *configMem;
80   char *tmpConfig;
81
82   bool refetchDataFiles;
83
84   void fetchDataFiles();
85   
86   bool isValidInFile(const string& infile, infiledesc *filedesc);
87
88   vector<infiledesc> inFiles;
89
90   vector<parameter> Paras;
91
92   int readDataToMem(char *tmpData, long unsigned int dataSize);
93
94   void openOutFile(int actnequi);
95
96   int readHeader();
97
98   bool headerWritten;
99
100   int readFullBlock(char *tmpData, long unsigned int dataSize);
101
102   vector< pair<unsigned long, void *> > headerStore;
103
104   void deleteHeaderStore();
105
106   bool readAllHeaders();
107
108   vector<infiledesc>::iterator getNextInfile(vector<unsigned long> *excludeFileHashes);
109
110   int nequileft_internal;
111
112   bool doVirtualEquilibration, firstUsedConfig;
113 };
114
115 #endif