/*******************************************
 * Copyright(c) 2000-2003 Mikio Hirabayashi
 *******************************************/


#include "xadbm.h"
#include <new>
#include <cstdlib>
#include <cstring>

using namespace qdbm;



/*************************************************************************************************
 * private objects
 *************************************************************************************************/


namespace {
  char *xmalloc(size_t size) throw(std::bad_alloc);
  char *xrealloc(void *ptr, size_t size) throw(std::bad_alloc);
  char *xmalloc(size_t size) throw(std::bad_alloc){
    char *p;
    if(!(p = (char*)std::malloc(size))) throw std::bad_alloc();
    return p;
  }
  char *xrealloc(void *ptr, size_t size) throw(std::bad_alloc){
    char *p;
    if(!(p = (char*)std::realloc(ptr, size))) throw std::bad_alloc();
    return p;
  }
}



/*************************************************************************************************
 * DBM_error
 *************************************************************************************************/


DBM_error::DBM_error() throw(){
  errmsg = 0;
}


DBM_error::DBM_error(const char* message) throw(){
  int len = std::strlen(message);
  this->errmsg = xmalloc(len + 1);
  std::memcpy(this->errmsg, message, len);
  this->errmsg[len] = '\0';
}


DBM_error::~DBM_error() throw(){
  if(errmsg) std::free(errmsg);
}


const char* DBM_error::message() const throw(){
  if(!errmsg) return "database error";
  return errmsg;
}



/*************************************************************************************************
 * Datum
 *************************************************************************************************/


Datum::Datum(const char* dptr, int dsize){
  if(dsize < 0) dsize = std::strlen(dptr);
  this->dptr = xmalloc(dsize + 1);
  std::memcpy(this->dptr, dptr, dsize);
  this->dptr[dsize] = '\0';
  this->dsize = dsize;
}


Datum::Datum(int num){
  this->dptr = xmalloc(sizeof(num));
  std::memcpy(this->dptr, &num, sizeof(num));
  this->dsize = sizeof(num);
}


Datum::Datum(const Datum& datum){
  this->dptr = xmalloc(datum.dsize + 1);
  std::memcpy(this->dptr, datum.dptr, datum.dsize);
  this->dptr[datum.dsize] = '\0';
  this->dsize = datum.dsize;
}


Datum::~Datum() throw(){
  std::free(dptr);
}


Datum& Datum::operator=(const Datum& datum){
  if(this == &datum) return *this;
  std::free(this->dptr);
  this->dptr = xmalloc(datum.dsize + 1);
  std::memcpy(this->dptr, datum.dptr, datum.dsize);
  this->dptr[datum.dsize] = '\0';
  this->dsize = datum.dsize;
  return *this;
}


Datum& Datum::operator=(const char* str){
  std::free(this->dptr);
  dsize = std::strlen(str);
  dptr = xmalloc(dsize + 1);
  std::memcpy(dptr, str, dsize);
  dptr[dsize] = '\0';
  return *this;
}


Datum& Datum::operator<<(const Datum& datum){
  dptr = xrealloc(dptr, dsize + datum.dsize + 1);
  std::memcpy(dptr + dsize, datum.dptr, datum.dsize);
  dsize += datum.dsize;
  dptr[dsize] = '\0';
  return *this;
}


Datum& Datum::operator<<(const char* str){
  int len = strlen(str);
  dptr = xrealloc(dptr, dsize + len + 1);
  std::memcpy(dptr + dsize, str, len);
  dsize += len;
  dptr[dsize] = '\0';
  return *this;  
}


bool Datum::operator==(const Datum& datum) const {
  if(dsize != datum.dsize) return false;
  return std::memcmp(dptr, datum.dptr, dsize) == 0 ? true : false;
}


bool Datum::operator!=(const Datum& datum) const {
  if(dsize != datum.dsize) return true;
  return std::memcmp(dptr, datum.dptr, dsize) == 0 ? false : true;
}


bool Datum::operator==(const char* str) const {
  int len = strlen(str);
  if(dsize != len) return false;
  return std::memcmp(dptr, str, len) == 0 ? true : false;
}


bool Datum::operator!=(const char* str) const {
  int len = strlen(str);
  if(dsize != len) return true;
  return std::memcmp(dptr, str, len) == 0 ? false : true;
}


const char* Datum::ptr() const {
  return dptr;
}


int Datum::size() const {
  return dsize;
}


void Datum::cjack(char *dptr, int dsize){
  this->dsize = dsize < 0 ? std::strlen(dptr) : dsize;
  std::free(this->dptr);
  this->dptr = xrealloc(dptr, dsize + 1);
  this->dptr[dsize] = '\0';
}



/* END OF FILE */
