try.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /****************************************************************************
  2. *
  3. * MODULE: btree
  4. * AUTHOR(S): CERL (present in ver 4.x)
  5. * Radim Blazek <radim.blazek gmail.com>
  6. * Glynn Clements <glynn gclements.plus.com>
  7. * PURPOSE: balanced tree - possibly duplicating libavl functionality; see
  8. * http://grass.itc.it/pipermail/grass-dev/2007-April/030396.html
  9. * COPYRIGHT: (C) 2002-2007 by the GRASS Development Team
  10. *
  11. * This program is free software under the GNU General Public
  12. * License (>=v2). Read the file COPYING that comes with GRASS
  13. * for details.
  14. *
  15. *****************************************************************************/
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <grass/btree.h>
  19. static int cmp(const void *a, const void *b)
  20. {
  21. return strcmp(a, b);
  22. }
  23. int main (void)
  24. {
  25. char key[100], data[100];
  26. void *k, *d;
  27. BTREE B;
  28. btree_create (&B, strcmp, 10);
  29. while (1)
  30. {
  31. fprintf (stdout,"enter key (or RETURN if done): ");
  32. if (!gets(key)) exit(0);
  33. if (*key == 0) break;
  34. fprintf (stdout," ");
  35. if (btree_find (&B,key,&d))
  36. fprintf (stdout,"%s = %s\n", key, d);
  37. else
  38. fprintf (stdout,"%s - not found\n", key);
  39. fprintf (stdout," ");
  40. fprintf (stdout,"enter new value (or RETURN if none): ");
  41. if (!gets(data)) exit(0);
  42. if (*data)
  43. btree_update (&B, key, strlen(key)+1, data, strlen(data)+1);
  44. }
  45. fprintf (stdout,"final tree\n");
  46. btree_rewind (&B);
  47. while (btree_next (&B, &k, &d))
  48. fprintf (stdout,"%s:%s\n", (const char *) k, (const char *) d);
  49. return 0;
  50. }