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" ) {
24 else if( filename.substr(filename.size()-4) == "sdat" ) {
26 getline(infile, parastring);
29 if ( inbuffer != NULL )
32 inbuffer = new boost::iostreams::filtering_istreambuf;
33 inbuffer->push( boost::iostreams::bzip2_decompressor() );
34 inbuffer->push(infile);
39 int datread::readDataToMem (char *tmpData, long unsigned int dataSize)
43 if ( dataSize == 0 ) return 0;
45 try { readturn = boost::iostreams::read(*inbuffer, tmpData, dataSize); }
46 catch(boost::iostreams::bzip2_error& error) {
47 if(log) *log << "DATREAD: Caught bzip2 exception with error code: " << error.error() << endl << flush;
50 catch (std::exception const& ex) {
51 if(log) *log << "DATREAD: Caught exception: " << ex.what() << endl << flush;
55 if(log) *log << "DATREAD: Caught unknown exception while reading." << endl << flush;
62 int datread::readHeader ()
64 long unsigned int headersize;
66 if( readDataToMem((char *)&headersize, sizeof(long unsigned int)) == sizeof(long unsigned int) && infile.is_open() ) {
67 if ( headersize == 0 )
68 return HEADER_READLAST;
70 pair<unsigned long, void *> newHeader;
72 if( readDataToMem((char *)&newHeader.first, sizeof(unsigned long)) == sizeof(unsigned long) && infile.is_open() ) {
73 newHeader.second = malloc(headersize);
75 if( readDataToMem((char *)newHeader.second, headersize) == headersize && infile.is_open() ) {
76 headerStore.push_back(newHeader);
80 if(log) *log << "DATREAD: Could not read heade-data! Closing dat-file." << endl << flush;
82 return HEADER_READERR;
86 if(log) *log << "DATREAD: Could not read headerid-hash! Closing dat-file." << endl << flush;
88 return HEADER_READERR;
92 if(log) *log << "DATREAD: Could not read header size. Closing dat-file." << endl << flush;
94 return HEADER_READERR;
98 void datread::deleteHeaderStore ()
100 while ( headerStore.size() > 0 ) {
101 free(headerStore.back().second);
102 headerStore.pop_back();
106 bool datread::readAllHeaders ()
108 int readHeaderStatus;
113 readHeaderStatus = readHeader();
115 while ( readHeaderStatus == HEADER_READOK );
117 if ( readHeaderStatus == HEADER_READLAST ) return true;
118 else if ( readHeaderStatus == HEADER_READERR ) return false;
121 int datread::readFullBlock (char *tmpData)
123 if ( ! infile.is_open() )
126 /* try to read header */
127 if ( format == EDAT || format == SDAT )
128 if ( ! readAllHeaders() ) {
134 if ( readDataToMem(tmpData, blocksize) != blocksize ) {
135 if(log) *log << "DATREAD: Could not read full datablock. Closing dat-file." << endl << flush;
140 if ( ! infile.is_open() )
146 void * datread::getHeader (const string& headerid) {
147 for (vector< pair<unsigned long, void *> >::iterator headerStoreIt = headerStore.begin(); headerStoreIt != headerStore.end(); ++headerStoreIt)
148 if ( headerStoreIt->first == hash(headerid) )
149 return headerStoreIt->second;
154 bool datread::fisopen ()
156 return infile.is_open();
159 unsigned long datread::hash(const string& str)
161 unsigned long hash = 5381;
163 for(string::const_iterator it=str.begin();it!=str.end();it++)
164 hash = ((hash << 5) + hash) + *it; /* hash * 33 + character */
169 string datread::getParaString ()
174 void datread::closeFile ()
176 if ( infile.is_open() )
182 if ( inbuffer != NULL )