TARGET=btree
-CFLAGS="-Wall"
+CXXFLAGS=-Wall -g
all: $(TARGET)
$(TARGET): main.o btree.o
+ g++ -o $(TARGET) main.o btree.o
+++ /dev/null
-
-#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);
-}
-
--- /dev/null
+
+#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 () {}
+
* tomar una idea simimar)
*/
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
+#include <string>
/* alias para codear menos :) */
typedef unsigned char uchar;
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
{
BTree *tree;
- tree = btree_create ("test.idx", 1024);
+ tree = new BTree ("test.idx", 1024);
- btree_close (tree);
+ delete tree;
return 0;
}