#include "datread.h"

#define HEADER_READOK   0
#define HEADER_READERR  1
#define HEADER_READLAST 2

datread::datread (const unsigned int& _blocksize, ostream *_log) : blocksize(_blocksize), log(_log)
{
  inbuffer = NULL;
}

int datread::openFile (const string& filename)
{ 
  infile.open(filename.c_str(), std::ios::binary);

  if ( ! infile.is_open() ) return -1;
  
  if( filename.substr(filename.size()-4) == ".dat" ) {
    format = DAT;
  }
  else if( filename.substr(filename.size()-4) == "edat" ) {
    format = EDAT;
  }
  else if( filename.substr(filename.size()-4) == "sdat" ) {
    format = SDAT;
    getline(infile, parastring);
  }

  if ( inbuffer != NULL )
    delete inbuffer;
  
  inbuffer = new boost::iostreams::filtering_istreambuf;
  inbuffer->push( boost::iostreams::bzip2_decompressor() );
  inbuffer->push(infile);

  return 0;
}

int datread::readDataToMem (char *tmpData, long unsigned int dataSize)
{
  int readturn = -1;

  if ( dataSize == 0 ) return 0;

  try { readturn = boost::iostreams::read(*inbuffer, tmpData, dataSize); }
  catch(boost::iostreams::bzip2_error& error) { 
    if(log) *log << "DATREAD: Caught bzip2 exception with error code: " << error.error() << endl << flush;
    infile.close();
  } 
  catch (std::exception const& ex) {
    if(log) *log << "DATREAD: Caught exception: " << ex.what() << endl << flush;
    infile.close();
  }
  catch( ... ) {
    if(log) *log << "DATREAD: Caught unknown exception while reading." << endl << flush;
    infile.close();
  }

  return readturn;
}

int datread::readHeader ()
{
  long unsigned int headersize;
  
  if( readDataToMem((char *)&headersize, sizeof(long unsigned int)) == sizeof(long unsigned int) && infile.is_open() ) {
    if ( headersize == 0 )
      return HEADER_READLAST;

    pair<unsigned long, void *> newHeader;

    if( readDataToMem((char *)&newHeader.first, sizeof(unsigned long)) == sizeof(unsigned long) && infile.is_open() ) {
      newHeader.second = malloc(headersize);

      if( readDataToMem((char *)newHeader.second, headersize) == headersize && infile.is_open() ) {
	headerStore.push_back(newHeader);
	return HEADER_READOK;
      }
      else {
	if(log) *log << "DATREAD: Could not read heade-data! Closing dat-file." << endl << flush;
	infile.close();
	return HEADER_READERR;
      }
    }
    else {
      if(log) *log << "DATREAD: Could not read headerid-hash! Closing dat-file." << endl << flush;
      infile.close();
      return HEADER_READERR;
    }
  }
  else {
    if(log) *log << "DATREAD: Could not read header size. Closing dat-file." << endl << flush;
    infile.close();
    return HEADER_READERR;
  }
}

void datread::deleteHeaderStore ()
{
  while ( headerStore.size() > 0 ) {
    free(headerStore.back().second);
    headerStore.pop_back();
  }
}

bool datread::readAllHeaders ()
{
  int readHeaderStatus;

  deleteHeaderStore();
  
  do {
    readHeaderStatus = readHeader();
  }
  while ( readHeaderStatus == HEADER_READOK );

  if ( readHeaderStatus == HEADER_READLAST ) return true;
  else if ( readHeaderStatus == HEADER_READERR ) return false;
}

int datread::readFullBlock (char *tmpData)
{
  if ( ! infile.is_open() )
    return -4;
    
  /* try to read header */
  if ( format == EDAT || format == SDAT )
    if ( ! readAllHeaders() ) {
      infile.close();
      return -1;
    }
      
  /* read data */
  if ( readDataToMem(tmpData, blocksize) != blocksize ) {
    if(log) *log << "DATREAD: Could not read full datablock. Closing dat-file." << endl << flush;
    infile.close();
    return -2;
  }
    
  if ( ! infile.is_open() )
    return -3;

  return 0;
}

void * datread::getHeader (const string& headerid) {
  for (vector< pair<unsigned long, void *> >::iterator headerStoreIt = headerStore.begin(); headerStoreIt != headerStore.end(); ++headerStoreIt)
    if ( headerStoreIt->first == hash(headerid) )
      return headerStoreIt->second;
  
  return NULL;
}

bool datread::fisopen ()
{
  return infile.is_open();
}

unsigned long datread::hash(const string& str)
{
  unsigned long hash = 5381;

  for(string::const_iterator it=str.begin();it!=str.end();it++) 
    hash = ((hash << 5) + hash) + *it; /* hash * 33 + character */

  return hash;
}

string datread::getParaString ()
{
  return parastring;
}

void datread::closeFile ()
{
  if ( infile.is_open() )
    infile.close();
}

datread::~datread ()
{
  if ( inbuffer != NULL )
    delete inbuffer;
}
