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. fprintf(stdout, "enter key (or RETURN if done): ");
  31. if (!gets(key))
  32. exit(0);
  33. if (*key == 0)
  34. break;
  35. fprintf(stdout, " ");
  36. if (btree_find(&B, key, &d))
  37. fprintf(stdout, "%s = %s\n", key, d);
  38. else
  39. fprintf(stdout, "%s - not found\n", key);
  40. fprintf(stdout, " ");
  41. fprintf(stdout, "enter new value (or RETURN if none): ");
  42. if (!gets(data))
  43. exit(0);
  44. if (*data)
  45. btree_update(&B, key, strlen(key) + 1, data, strlen(data) + 1);
  46. }
  47. fprintf(stdout, "final tree\n");
  48. btree_rewind(&B);
  49. while (btree_next(&B, &k, &d))
  50. fprintf(stdout, "%s:%s\n", (const char *)k, (const char *)d);
  51. return 0;
  52. }