// // (c) J. Dean Brock, 1999, 2002 // #include using std::ios_base ; #include "btree.hxx" bTree::bTree() { btState = 0 ; } bTree::~bTree() { if (btState) btFile.close() ; btState = 0 ; } void bTree::create(const char *name) { if (btState) { btFile.close() ; btState = 0 ; } btFile.open(name, ios_base::in | ios_base::out | ios_base::trunc ) ; if (btFile.fail()) { return ; } // Create btree with a single empty leaf node setUsedRecords(&btRoot, 0) ; setLeaf(&btRoot) ; btRoot.recs[METAPNTR].value = -1 ; btFile.write((char *)&btRoot, sizeof(blockRecT)) ; if (btFile.fail()) btState = 0 ; else btState = 1 ; } void bTree::open(const char *name) { if (btState) { btFile.close() ; btState = 0 ; } // ISO C++ does not allow ios::nocreate. Open read_only and see if it exits btFile.open(name, ios_base::in) ; if (btFile.fail()) { return ; } btFile.close() ; btFile.open(name, ios_base::in | ios_base::out) ; if (btFile.fail()) { return ; } // read in the root btFile.seekg(0, ios_base::beg) ; btFile.read((char *)&btRoot, sizeof(blockRecT)) ; // Ought to add some sanity checks in here ... if (btFile.fail()) btState = 0 ; else btState = 1 ; } int bTree::io_ok() const { return btState ; }