From a55a5830c5c98e950d58593697dd4ee5e4384716 Mon Sep 17 00:00:00 2001 From: Alex Schmidt Date: Thu, 14 Mar 2013 18:55:20 +0100 Subject: [PATCH] ... --- configcache.cpp | 104 ++++++++++++++++++++++++++---------------------- configcache.h | 11 +++-- 2 files changed, 64 insertions(+), 51 deletions(-) diff --git a/configcache.cpp b/configcache.cpp index 1b22757..fcfdd8e 100644 --- a/configcache.cpp +++ b/configcache.cpp @@ -26,6 +26,8 @@ configcache::configcache(const string& cacheid, const int& nequi, const int& nsk MODE = cachemode; refetchDataFiles = false; + + readHeaderData = NULL; } string configcache::getFileId(const bool& shortid) @@ -109,53 +111,34 @@ bool configcache::isValidInFile(const string& infile, infiledesc *filedesc) 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() @@ -171,9 +154,19 @@ 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() ) { @@ -181,12 +174,12 @@ bool configcache::readConfig() 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; @@ -210,30 +203,36 @@ void configcache::openOutFile() 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){ @@ -280,6 +279,17 @@ void configcache::finishOutFile() } } +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; diff --git a/configcache.h b/configcache.h index 139218b..4aa245e 100644 --- a/configcache.h +++ b/configcache.h @@ -42,7 +42,7 @@ class configcache{ void writeConfig(); void addPara(const string& parid, const double& val=0); void setPara(const string& parid, const double& value); - void writeHeader(char *header, const int& size); + void writeHeader(char *header, long unsigned int size); void *getHeader(); private: @@ -87,11 +87,14 @@ class configcache{ int readDataToMem(char *tmpData, long unsigned int dataSize); void openOutFile(); - bool openInFile(); - char *headerData; + char *readHeaderData; - void readHeader(); + bool readHeader(); + + bool headerWritten; + + int readFullBlock(char *tmpData, long unsigned int dataSize); }; #endif -- 2.39.5