MODE = cachemode;
refetchDataFiles = false;
+
+ readHeaderData = NULL;
}
string configcache::getFileId(const bool& shortid)
return true;
}
-bool configcache::openInFile() {
- while( (!inFile.is_open()) && inFiles.size() > 0 )
- {
- if(out) *out->log << "CCACHE: Opening dat-file: " << inFiles.back().filename << endl << flush;
-
- openFileDesc = inFiles.back();
- inFile.open( (DATADIR + "/" + inFiles.back().filename).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);
-
- return true;
- }
-
- return false;
-}
-
-void configcache::readHeader()
+bool configcache::readHeader()
{
- int headersize;
-
- if( readDataToMem((char*)&headersize, sizeof(int)) == sizeof(int) && inFile.is_open() )
+ long unsigned int headersize;
+
+ if( readDataToMem((char*)&headersize, sizeof(long unsigned int)) == sizeof(long unsigned int) && inFile.is_open() )
{
- if( headerData != NULL ) delete headerData;
+ if( readHeaderData != NULL ) free(readHeaderData);
- headerData = new char(headersize);
-
- if( readDataToMem(headerData, headersize) == headersize && inFile.is_open() ) {
- if(out) *out->log << "CCACHE: Read header information." << endl << flush;
+ readHeaderData = (char*) malloc(headersize);
+
+ if( readDataToMem(readHeaderData, headersize) == headersize && inFile.is_open() ) {
+ return true;
}
else {
if(out) *out->log << "CCACHE: Could not read header! Closing dat-file: " << openFileDesc.filename << endl << flush;
inFile.close();
+ return false;
}
}
else {
- if(out) *out->log << "CCACHE: Could not read header size! Closing dat-file: " << openFileDesc.filename << endl << flush;
+ if(out) *out->log << "CCACHE: Could not read header size. Closing dat-file: " << openFileDesc.filename << endl << flush;
inFile.close();
+ return false;
}
}
void *configcache::getHeader() {
- return headerData;
+ return readHeaderData;
}
bool configcache::readConfig()
{
if( (!inFile.is_open()) && inFiles.size() == 0 ) return false;
- /* read header information */
- if ( openInFile() && openFileDesc.extended )
- readHeader();
+ while( (!inFile.is_open()) && inFiles.size() > 0 ) {
+ if(out) *out->log << "CCACHE: Opening dat-file: " << inFiles.back().filename << endl << flush;
+
+ openFileDesc = inFiles.back();
+ inFile.open( (DATADIR + "/" + inFiles.back().filename).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(out) *out->log << "CCACHE: Trying virtual equilibration." << endl << flush;
openFileDesc.doVirtualEquilibration = false;
for (int iskip=0; iskip < (NEQUI-openFileDesc.nequi)/openFileDesc.nskip; iskip++) {
- if( readDataToMem(tmpConfig, configSize) != configSize || ! inFile.is_open() )
+ if( readFullBlock(tmpConfig, configSize) != configSize || ! inFile.is_open() )
break;
}
}
- if( readDataToMem(tmpConfig, configSize) == configSize && inFile.is_open() )
+ if( readFullBlock(tmpConfig, configSize) == configSize && inFile.is_open() )
{
memcpy(configMem, tmpConfig, configSize);
return true;
outBuffer = new boost::iostreams::filtering_ostreambuf;
outBuffer->push(boost::iostreams::bzip2_compressor());
outBuffer->push(outFile);
+
+ headerWritten = false;
}
-void configcache::writeHeader(char *header, const int& size) {
+void configcache::writeHeader(char *header, long unsigned int size) {
if( DATADIR == "" || MODE < 2 ) return;
- if(!outFile.is_open()) {
+ if(!outFile.is_open())
openOutFile();
- boost::iostreams::write(*outBuffer, header, size);
- }
- else
- if(out) *out->log << "CCACHE: Not writing header because outfile is already open!" << endl << flush;
+
+ boost::iostreams::write(*outBuffer, (char*)&size, sizeof(long unsigned int));
+ boost::iostreams::write(*outBuffer, header, size);
+
+ headerWritten = true;
}
void configcache::writeConfig()
{
- if( DATADIR == "" || MODE < 2 ) return;
+ if ( DATADIR == "" || MODE < 2 ) return;
- if(!outFile.is_open()){
- int zeroheader=0;
+ if ( ! outFile.is_open() )
openOutFile();
- boost::iostreams::write(*outBuffer, (char*)&zeroheader, sizeof(int));
+
+ if ( ! headerWritten ) {
+ long unsigned int zeroheader=0;
+ boost::iostreams::write(*outBuffer, (char*)&zeroheader, sizeof(long unsigned int));
}
boost::iostreams::write(*outBuffer, configMem, configSize);
+ headerWritten = false;
}
void configcache::addPara(const string& parid, const double& val){
}
}
+int configcache::readFullBlock(char *tmpData, long unsigned int dataSize)
+{
+ /* try to read header */
+ if ( openFileDesc.extended )
+ if ( ! readHeader() )
+ return -1;
+
+ /* read data */
+ return readDataToMem(tmpData, dataSize);
+}
+
int configcache::readDataToMem(char *tmpData, long unsigned int dataSize)
{
int readturn = -1;