//   (c) J. Dean Brock, 1998, 1999, 2002
//   Written for UNCA CSCI 343
//   Various I/O routines for the B+-tree

#include <fstream>
using namespace std ;

#include "btree.hxx"

void bTree::updateBlock(
	int p,				// position for block
	const blockRecT &b)		// new contents for block
{
  btFile.seekp(p * sizeof(blockRecT), ios::beg) ;
  if ( btFile.fail() ) {
    btState = 0 ;
    return ;
  }
  btFile.write((char *)&b, sizeof(blockRecT)) ;
  if ( btFile.fail() )
    btState = 0 ;
  if (p==0)	// I wasn't thinking....
    btRoot = b;
  return ;
}

int bTree::addBlock(		// returns position where block was added
	const blockRecT &b)		// block to be added
{
  int rPos ;
  btFile.seekp(0, ios::end) ;
  if (btFile.fail()) {
    btState = 0 ;
    return -1 ;
  }
  rPos = btFile.tellp() / sizeof(blockRecT) ;
  btFile.write((char *)&b, sizeof(blockRecT)) ;
  if (btFile.fail()) {
    btState = 0 ;
    return -1 ;
  }
  return(rPos) ;
}


void bTree::readBlock(
	int p,			// position for block
	blockRecT &b)		// buffer for block
{
  btFile.seekg(p*sizeof(blockRecT), ios::beg) ;
  if (btFile.fail()) {
    btState = 0 ;
    return ;
  }
  btFile.read((char *)&b, sizeof(blockRecT)) ;
  if (btFile.fail())
    btState = 0 ;
  return ;
}
