From: Ricardo Markiewicz Date: Mon, 12 Sep 2005 04:19:26 +0000 (+0000) Subject: Paso todo a C++ X-Git-Tag: 1_0-pre1~160 X-Git-Url: https://git.llucax.com/z.facultad/75.52/treemulator.git/commitdiff_plain/6fcd86dce909f05de818fdd78d76e70aabc3ad22?ds=inline Paso todo a C++ --- diff --git a/src/Makefile b/src/Makefile index b97eadb..5440d99 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,6 +1,7 @@ TARGET=btree -CFLAGS="-Wall" +CXXFLAGS=-Wall -g all: $(TARGET) $(TARGET): main.o btree.o + g++ -o $(TARGET) main.o btree.o diff --git a/src/btree.c b/src/btree.c deleted file mode 100644 index 1cdf037..0000000 --- a/src/btree.c +++ /dev/null @@ -1,58 +0,0 @@ - -#include "btree.h" - -static void write_tree_header (BTree *tree); -static void write_block (BTree *tree, uchar *block, uint num); - -BTree *btree_create (const char *filename, unsigned int block_size) -{ - BTree *tree; - uchar *node; - - tree = malloc (sizeof(BTree)); - if (!tree) - return NULL; - - tree->fp = fopen (filename, "wb"); - if (!tree->fp) { - free (tree); - return NULL; - } - - /* Nombre de archivo */ - tree->filename = malloc(sizeof(char)*strlen(filename)+1); - strcpy (tree->filename, filename); - - /* Inicializo el header */ - tree->header.block_size = block_size; - write_tree_header (tree); - - /* Creo el primer bloque vacio */ - node = calloc (1, block_size); - write_block (tree, node, 0); - free (node); - - return tree; -} - -int btree_close (BTree *tree) -{ - fclose (tree->fp); - free (tree->filename); - free (tree); - - return 0; -} - -static void write_tree_header (BTree *tree) -{ - fseek (tree->fp, 0L, SEEK_SET); - fwrite (&tree->header, 1, sizeof (BTreeHeader), tree->fp); -} - -static void write_block (BTree *tree, uchar *block, uint num) -{ - fseek (tree->fp, num*tree->header.block_size + sizeof (BTreeHeader), SEEK_SET); - fwrite (block, 1, tree->header.block_size, tree->fp); -} - diff --git a/src/btree.cpp b/src/btree.cpp new file mode 100644 index 0000000..1ad006d --- /dev/null +++ b/src/btree.cpp @@ -0,0 +1,47 @@ + +#include "btree.h" + +BTree::BTree (const char *name, unsigned int block_size, bool create_new_file) +{ + uchar *node; + + + fp = fopen (name, "wb"); + if (!fp) { + /* TODO : mandar una exception ? */ + return; + } + + /* Nombre de archivo */ + filename = name; + + /* Inicializo el header */ + header.block_size = block_size; + write_tree_header (); + + /* Creo el primer bloque vacio */ + node = new uchar[block_size]; + write_block (node, 0); + delete [] node; +} + +BTree::~BTree () +{ + fclose (fp); +} + +void BTree::write_tree_header () +{ + fseek (fp, 0L, SEEK_SET); + fwrite (&header, 1, sizeof (BTreeHeader), fp); +} + +void BTree::write_block (uchar *block, uint num) +{ + fseek (fp, num*header.block_size + sizeof (BTreeHeader), SEEK_SET); + fwrite (block, 1, header.block_size, fp); +} + +void BTree::AddKey () {} +void BTree::DelKey () {} + diff --git a/src/btree.h b/src/btree.h index 6764a63..cacd29b 100644 --- a/src/btree.h +++ b/src/btree.h @@ -44,9 +44,7 @@ * tomar una idea simimar) */ -#include -#include -#include +#include /* alias para codear menos :) */ typedef unsigned char uchar; @@ -75,46 +73,33 @@ typedef struct _btree_header_ { unsigned int item_count; } BTreeHeader; -typedef struct _btree_ { - char *filename; - BTreeFileHeader header; - - /** Apunta al archivo de datos, asi se abre solo 1 vez - * - * \TODO Ver si vale la pena - */ - FILE *fp; -} BTree; - /** Crea un nuevo arbol B * * \param filename Nombre del archivo a crear * \param block_size TamaƱo de bloque a utilizar * \return Un nuevo arbol B creado o NULL en caso de error */ -BTree *btree_create (const char *filename, unsigned int block_size); - -/** Abre un arbol B existente - * - * \param filename Nombre del archivo a abrir - * \return El arbol abierto o NULL en caso de error - */ -BTree *btree_open (const char *filename); - -/** Agrega una clave en el arbol - * - * \TODO Definir parametros - */ -int btree_add (BTree *); - -/** Borra una clave del arbol - * - * \TODO Definir parametros - */ -int btree_del (BTree *); - -/** Cierra el arbol y libera los recursos */ -int btree_close (BTree *); +class BTree { + public: + BTree (const char *filename, unsigned int block_size, bool create_new_file = false); + ~BTree (); + + void AddKey (); + void DelKey (); + + private: + void write_tree_header (); + void write_block (uchar *block, uint num); + + std::string filename; + BTreeFileHeader header; + + /** Apunta al archivo de datos, asi se abre solo 1 vez + * + * \TODO Ver si vale la pena + */ + FILE *fp; +}; #endif // _B_TREE_H diff --git a/src/main.c b/src/main.c index f058d58..0ad6845 100644 --- a/src/main.c +++ b/src/main.c @@ -6,9 +6,9 @@ int main (int argc, char *argv[]) { BTree *tree; - tree = btree_create ("test.idx", 1024); + tree = new BTree ("test.idx", 1024); - btree_close (tree); + delete tree; return 0; }