3 #define HEADER_READOK 0
4 #define HEADER_READERR 1
5 #define HEADER_READLAST 2
7 datread::datread (const unsigned int& _blocksize, ostream *_log) : blocksize(_blocksize), log(_log)
12 int datread::openFile (const string& filename)
14 infile.open(filename.c_str(), std::ios::binary);
16 if ( ! infile.is_open() ) return -1;
18 if( filename.substr(filename.size()-4) == ".dat" ) {
21 else if( filename.substr(filename.size()-4) == "edat" ) {
22 format = Format::EDAT;
24 else if( filename.substr(filename.size()-4) == "sdat" ) {
25 format = Format::SDAT;
26 getline(infile, parastring);
32 inbuffer = new boost::iostreams::filtering_istreambuf;
33 inbuffer->push( boost::iostreams::bzip2_decompressor() );
34 inbuffer->push(infile);
45 int datread::readDataToMem (char *tmpData, long unsigned int dataSize)
49 if ( dataSize == 0 ) return 0;
51 try { readturn = boost::iostreams::read(*inbuffer, tmpData, dataSize); }
52 catch(boost::iostreams::bzip2_error& error) {
53 if(log) *log << "DATREAD: Caught bzip2 exception with error code: " << error.error() << endl << flush;
56 catch (std::exception const& ex) {
57 if(log) *log << "DATREAD: Caught exception: " << ex.what() << endl << flush;
61 if(log) *log << "DATREAD: Caught unknown exception while reading." << endl << flush;
68 int datread::readHeader ()
70 long unsigned int headersize;
72 if( readDataToMem((char *)&headersize, sizeof(long unsigned int)) == sizeof(long unsigned int) && infile.is_open() ) {
73 if ( headersize == 0 )
74 return HEADER_READLAST;
76 pair<unsigned long, void *> newHeader;
78 if( readDataToMem((char *)&newHeader.first, sizeof(unsigned long)) == sizeof(unsigned long) && infile.is_open() ) {
79 newHeader.second = malloc(headersize);
81 if( readDataToMem((char *)newHeader.second, headersize) == headersize && infile.is_open() ) {
82 headerStore.push_back(newHeader);
86 if(log) *log << "DATREAD: Could not read heade-data! Closing dat-file." << endl << flush;
88 return HEADER_READERR;
92 if(log) *log << "DATREAD: Could not read headerid-hash! Closing dat-file." << endl << flush;
94 return HEADER_READERR;
98 if(log) *log << "DATREAD: Could not read header size. Closing dat-file." << endl << flush;
100 return HEADER_READERR;
104 void datread::deleteHeaderStore ()
106 while ( headerStore.size() > 0 ) {
107 free(headerStore.back().second);
108 headerStore.pop_back();
112 bool datread::readAllHeaders ()
114 int readHeaderStatus;
119 readHeaderStatus = readHeader();
121 while ( readHeaderStatus == HEADER_READOK );
123 if ( readHeaderStatus == HEADER_READLAST ) return true;
124 else if ( readHeaderStatus == HEADER_READERR ) return false;
127 int datread::readFullBlock (char *tmpData)
129 if ( ! infile.is_open() )
132 /* try to read header */
133 if ( format == Format::EDAT || format == Format::SDAT )
134 if ( ! readAllHeaders() ) {
140 if ( readDataToMem(tmpData, blocksize) != blocksize ) {
141 if(log) *log << "DATREAD: Could not read full datablock. Closing dat-file." << endl << flush;
146 if ( ! infile.is_open() )
152 void * datread::getHeader (const string& headerid) {
153 for (vector< pair<unsigned long, void *> >::iterator headerStoreIt = headerStore.begin(); headerStoreIt != headerStore.end(); ++headerStoreIt)
154 if ( headerStoreIt->first == hash(headerid) )
155 return headerStoreIt->second;
160 bool datread::fisopen ()
162 return infile.is_open();
165 unsigned long datread::hash(const string& str)
167 unsigned long hash = 5381;
169 for(string::const_iterator it=str.begin();it!=str.end();it++)
170 hash = ((hash << 5) + hash) + *it; /* hash * 33 + character */
175 string datread::getParaString ()
180 void datread::closeFile ()
182 if ( infile.is_open() )