From: Ricardo Markiewicz Date: Tue, 11 Oct 2005 09:23:21 +0000 (+0000) Subject: Visor de arbol B en ncurses. X-Git-Tag: 1_0-pre1~58 X-Git-Url: https://git.llucax.com/z.facultad/75.52/treemulator.git/commitdiff_plain/467f3bcabe798b584e470038c3169c374874b18b?hp=f4c34c38f2c62087bbf7e0d6e618660f0e48023e Visor de arbol B en ncurses. --- diff --git a/nviewer/Makefile b/nviewer/Makefile new file mode 100644 index 0000000..8bd21a5 --- /dev/null +++ b/nviewer/Makefile @@ -0,0 +1,13 @@ +TARGET=nviewer +CXXFLAGS=-Wall -g -I../src +OBJECTS=main.o window.o w_node_header.o w_btree.o + +all: $(TARGET) + +$(TARGET): $(OBJECTS) + g++ -o $(TARGET) $(OBJECTS) ../src/libbtree.a -lcurses + +clean: + rm -rf *.o $(TARGET) + + diff --git a/nviewer/main.cpp b/nviewer/main.cpp new file mode 100644 index 0000000..15580ba --- /dev/null +++ b/nviewer/main.cpp @@ -0,0 +1,71 @@ + +#include "window.h" +#include "w_btree.h" +#include "w_node_header.h" +#include "btree.h" +#include "random.h" +#include + +int main (int argc, char *argv[]) +{ + std::list lst; + + BTree tree ("test.idx", 64); + + Random::Init (); + + Random::Ints (lst, 20); + + std::list::iterator it = lst.begin (); + while (it != lst.end ()) { + ClaveFija c(*it); + + tree.AddKey (c); + it++; + } + /* Inicio Curses */ + ///signal(SIGINT, finish); + initscr(); + keypad(stdscr, TRUE); + nonl(); + cbreak(); + noecho(); + /* Si se soporta color, los inicializo */ + if (has_colors()) { + start_color(); + /* Simple color assignment, often all we need. */ + init_pair(COLOR_BLACK, COLOR_BLACK, COLOR_BLACK); /* COLOR_PAIR(1) */ + init_pair(COLOR_GREEN, COLOR_GREEN, COLOR_BLACK); + init_pair(COLOR_RED, COLOR_RED, COLOR_BLACK); + init_pair(COLOR_CYAN, COLOR_CYAN, COLOR_BLACK); + init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK); + init_pair(COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK); + init_pair(COLOR_BLUE, COLOR_BLUE, COLOR_BLACK); + init_pair(COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK); + init_pair(COLOR_PAIR (1), COLOR_RED, COLOR_RED); + } + + wBTree *w = new wBTree (); + wNodeHeader *w1 = new wNodeHeader (10, 10); + + w->SetTree (&tree); + int n = 0; + do { + w->ShowNode (n); + w->Show (); + + w1->SetHeader (n, w->GetHeader ()); + w1->Show (); + + scanf ("%d", &n); + if (n == -1) break; + + } while (true); + + delete w; + delete w1; + + endwin(); + return 0; +} + diff --git a/nviewer/w_btree.cpp b/nviewer/w_btree.cpp new file mode 100644 index 0000000..ae14245 --- /dev/null +++ b/nviewer/w_btree.cpp @@ -0,0 +1,65 @@ + +#include +#include "w_btree.h" + +wBTree::wBTree ():Window ("TreeMulator", 0, 0) +{ + last_length = 0; +} + +wBTree::~wBTree () +{ +} + +void wBTree::SetTree (BTree *b) +{ + tree = b; +} + +void wBTree::ShowNode (uint node_num) +{ + uchar *node; + std::list node_keys; + + node = tree->ReadBlock (node_num); + tree->ReadNodoHeader (node, &node_header); + node_keys = tree->ReadKeys (node, node_header); + + int y, x; + y = 5; + x = 5; + /* Dibujo el bloque de color rojo */ + /*wattron (win, COLOR_PAIR (1)); + for (uint i=0; i < tree->header.block_size + 2; i++) + mvwaddstr (win, y+1, i+x-1, "."); + for (uint i=0; i < tree->header.block_size + 2; i++) + mvwaddstr (win, y-1, i+x-1, "."); + mvwaddstr (win, y, x-1, "."); + mvwaddstr (win, y, tree->header.block_size + x, "."); + wattroff (win, COLOR_PAIR (1));*/ + + for (uint o=0; o::iterator it = node_keys.begin (); + int n = 0; + while (it != node_keys.end ()) { + std::string s = *(*it); + + if (n%2) + wattron (win, COLOR_PAIR (2)); + else + wattron (win, COLOR_PAIR (3)); + mvwaddnstr (win, y, x, (const char *)(s.c_str ()), s.length ()); + if (n%2) + wattroff (win, COLOR_PAIR (2)); + else + wattroff (win, COLOR_PAIR (3)); + x += s.length (); + n++; + it++; + } + last_length = x; + mvwaddstr (win, 20, 5, "Ir al nodo (-1 para salir) : "); +} + diff --git a/nviewer/w_btree.h b/nviewer/w_btree.h new file mode 100644 index 0000000..5de908b --- /dev/null +++ b/nviewer/w_btree.h @@ -0,0 +1,24 @@ + + +#ifndef _W_B_TREE_ +#define _W_B_TREE_ + +#include "btree.h" +#include "window.h" + +class wBTree : public Window { + public: + wBTree (); + ~wBTree (); + + void SetTree (BTree *b); + void ShowNode (uint node_num); + BTreeNodeHeader& GetHeader () { return node_header; } + protected: + BTree *tree; + BTreeNodeHeader node_header; + uint last_length; +}; + +#endif + diff --git a/nviewer/w_node_header.cpp b/nviewer/w_node_header.cpp new file mode 100644 index 0000000..7f8b905 --- /dev/null +++ b/nviewer/w_node_header.cpp @@ -0,0 +1,25 @@ + +#include "w_node_header.h" + +wNodeHeader::wNodeHeader (int x, int y):Window ("Header", 25, 4, x, y) +{ + SetText (1, 1, "Nodo :"); + SetText (1, 2, "Nivel :"); + SetText (1, 3, "Libre :"); + SetText (1, 4, "Claves :"); +} + +void wNodeHeader::SetHeader (uint node_num, BTreeNodeHeader &h) +{ + SetText (1, 1, "Nodo : "); + SetText (1, 2, "Nivel : "); + SetText (1, 3, "Libre : "); + SetText (1, 4, "Claves : "); + + SetText (10, 1, node_num); + + SetText (10, 2, h.level); + SetText (10, 3, h.free_space); + SetText (10, 4, h.item_count); +} + diff --git a/nviewer/w_node_header.h b/nviewer/w_node_header.h new file mode 100644 index 0000000..22bff61 --- /dev/null +++ b/nviewer/w_node_header.h @@ -0,0 +1,16 @@ + +#ifndef _W_NODE_HEADER_ +#define _W_NODE_HEADER_ + +#include "btree.h" +#include "window.h" + +class wNodeHeader : public Window { + public: + wNodeHeader (int x, int y); + + void SetHeader (uint node_num, BTreeNodeHeader &h); +}; + +#endif + diff --git a/nviewer/window.cpp b/nviewer/window.cpp new file mode 100644 index 0000000..a3f3993 --- /dev/null +++ b/nviewer/window.cpp @@ -0,0 +1,60 @@ + +#include "window.h" + +Window::Window (const std::string &s, int w, int h, int x, int y, bool use_box) +{ + if (use_box == true) { + if (w) w +=2; + if (h) h +=2; + } + if (h) h++; + + width = w; + height = h; + win = newwin (h, w, y, x); + + if (use_box == true) { + wattron (win, COLOR_PAIR (4)); + box(win, ACS_VLINE, ACS_HLINE); + wattroff (win, COLOR_PAIR (4)); + } + + title = s; + wattron (win, COLOR_PAIR (1)); + mvwaddstr(win, 1, 1, title.c_str ()); + wattroff (win, COLOR_PAIR (1)); +} + +Window::~Window () +{ + delwin (win); +} + +void Window::SetText (int x, int y, const std::string &s) +{ + mvwaddstr (win, y+1, x+1, s.c_str ()); + wrefresh (win); +} + +void Window::SetText (int x, int y, int i) +{ + std::stringstream ss; + std::string s; + ss << i; + ss >> s; + SetText (x, y, s); +} + +void Window::SetText (int x, int y, uint i) +{ + std::stringstream ss; + std::string s; + ss << i; + ss >> s; + SetText (x, y, s); +} +void Window::Show () +{ + wrefresh (win); +} + diff --git a/nviewer/window.h b/nviewer/window.h new file mode 100644 index 0000000..d80871b --- /dev/null +++ b/nviewer/window.h @@ -0,0 +1,26 @@ + +#ifndef _WINDOW_H_ +#define _WINDOW_H_ + +#include +#include +#include + +class Window { + public: + Window (const std::string &s, int w, int h, int x=0, int y=0, bool box=true); + ~Window (); + + void SetText (int x, int y, const std::string &s); + void SetText (int x, int y, int i); + void SetText (int x, int y, uint i); + void Show (); + protected: + int width; + int height; + WINDOW *win; + std::string title; +}; + +#endif +