GRASS Programmer's Manual  6.4.2(2012)
btree/try.c
Go to the documentation of this file.
00001 
00002 /****************************************************************************
00003  *
00004  * MODULE:       btree
00005  * AUTHOR(S):    CERL (present in ver 4.x)
00006  *               Radim Blazek <radim.blazek gmail.com>
00007  *               Glynn Clements <glynn gclements.plus.com>
00008  * PURPOSE:      balanced tree - possibly duplicating libavl functionality; see
00009  *               http://grass.itc.it/pipermail/grass-dev/2007-April/030396.html
00010  * COPYRIGHT:    (C) 2002-2007 by the GRASS Development Team
00011  *
00012  *               This program is free software under the GNU General Public
00013  *               License (>=v2). Read the file COPYING that comes with GRASS
00014  *               for details.
00015  *
00016  *****************************************************************************/
00017 #include <stdio.h>
00018 #include <string.h>
00019 #include <grass/btree.h>
00020 
00021 static int cmp(const void *a, const void *b)
00022 {
00023     return strcmp(a, b);
00024 }
00025 
00026 int main(void)
00027 {
00028     char key[100], data[100];
00029     void *k, *d;
00030     BTREE B;
00031 
00032     btree_create(&B, strcmp, 10);
00033     while (1) {
00034         fprintf(stdout, "enter key (or RETURN if done): ");
00035         if (!gets(key))
00036             exit(0);
00037         if (*key == 0)
00038             break;
00039         fprintf(stdout, "    ");
00040         if (btree_find(&B, key, &d))
00041             fprintf(stdout, "%s = %s\n", key, d);
00042         else
00043             fprintf(stdout, "%s - not found\n", key);
00044         fprintf(stdout, "    ");
00045         fprintf(stdout, "enter new value (or RETURN if none): ");
00046         if (!gets(data))
00047             exit(0);
00048         if (*data)
00049             btree_update(&B, key, strlen(key) + 1, data, strlen(data) + 1);
00050     }
00051 
00052     fprintf(stdout, "final tree\n");
00053     btree_rewind(&B);
00054     while (btree_next(&B, &k, &d))
00055         fprintf(stdout, "%s:%s\n", (const char *)k, (const char *)d);
00056 
00057     return 0;
00058 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines