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);
29 inbuffer = unique_ptr<boost::iostreams::filtering_istreambuf>(new boost::iostreams::filtering_istreambuf);
30 inbuffer->push( boost::iostreams::bzip2_decompressor() );
31 inbuffer->push(infile);
36 int datread::readDataToMem (char *tmpData, long unsigned int dataSize)
40 if ( dataSize == 0 ) return 0;
42 try { readturn = boost::iostreams::read(*inbuffer, tmpData, dataSize); }
43 catch(boost::iostreams::bzip2_error& error) {
44 if(log) *log << "DATREAD: Caught bzip2 exception with error code: " << error.error() << endl << flush;
47 catch (std::exception const& ex) {
48 if(log) *log << "DATREAD: Caught exception: " << ex.what() << endl << flush;
52 if(log) *log << "DATREAD: Caught unknown exception while reading." << endl << flush;
59 int datread::readHeader ()
61 long unsigned int headersize;
63 if( readDataToMem((char *)&headersize, sizeof(long unsigned int)) == sizeof(long unsigned int) && infile.is_open() ) {
64 if ( headersize == 0 )
65 return HEADER_READLAST;
67 pair<unsigned long, void *> newHeader;
69 if( readDataToMem((char *)&newHeader.first, sizeof(unsigned long)) == sizeof(unsigned long) && infile.is_open() ) {
70 newHeader.second = malloc(headersize);
72 if( readDataToMem((char *)newHeader.second, headersize) == headersize && infile.is_open() ) {
73 headerStore.push_back(newHeader);
77 if(log) *log << "DATREAD: Could not read heade-data! Closing dat-file." << endl << flush;
79 return HEADER_READERR;
83 if(log) *log << "DATREAD: Could not read headerid-hash! Closing dat-file." << endl << flush;
85 return HEADER_READERR;
89 if(log) *log << "DATREAD: Could not read header size. Closing dat-file." << endl << flush;
91 return HEADER_READERR;
95 void datread::deleteHeaderStore ()
97 while ( headerStore.size() > 0 ) {
98 free(headerStore.back().second);
99 headerStore.pop_back();
103 bool datread::readAllHeaders ()
105 int readHeaderStatus;
110 readHeaderStatus = readHeader();
112 while ( readHeaderStatus == HEADER_READOK );
114 if ( readHeaderStatus == HEADER_READLAST ) return true;
115 else if ( readHeaderStatus == HEADER_READERR ) return false;
118 int datread::readFullBlock (char *tmpData)
120 if ( ! infile.is_open() )
123 /* try to read header */
124 if ( format == Format::EDAT || format == Format::SDAT )
125 if ( ! readAllHeaders() ) {
131 if ( readDataToMem(tmpData, blocksize) != blocksize ) {
132 if(log) *log << "DATREAD: Could not read full datablock. Closing dat-file." << endl << flush;
137 if ( ! infile.is_open() )
143 void * datread::getHeader (const string& headerid) {
144 for (vector< pair<unsigned long, void *> >::iterator headerStoreIt = headerStore.begin(); headerStoreIt != headerStore.end(); ++headerStoreIt)
145 if ( headerStoreIt->first == hash(headerid) )
146 return headerStoreIt->second;
151 bool datread::fisopen ()
153 return infile.is_open();
156 unsigned long datread::hash(const string& str)
158 unsigned long hash = 5381;
160 for(string::const_iterator it=str.begin();it!=str.end();it++)
161 hash = ((hash << 5) + hash) + *it; /* hash * 33 + character */
166 string datread::getParaString ()
171 void datread::closeFile ()
173 if ( infile.is_open() )