ttree_test.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /********************************************************************** <BR>
  2. This file is part of Crack dot Com's free source code release of
  3. Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
  4. information about compiling & licensing issues visit this URL</a>
  5. <PRE> If that doesn't help, contact Jonathan Clark at
  6. golgotha_source@usa.net (Subject should have "GOLG" in it)
  7. ***********************************************************************/
  8. #include "ttree.hh"
  9. #include "init/init.hh"
  10. #include "main/main.hh"
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <ctype.h>
  14. const char *delim=" ,.?!;:\"()-`'<>{}[]|-=+\n\t";
  15. void test()
  16. {
  17. i4_ternary_tree<int> t;
  18. int count=0;
  19. char buff[256];
  20. int val, cont=1;
  21. while (cont)
  22. {
  23. printf("> ");
  24. gets(buff);
  25. switch (buff[0])
  26. {
  27. case 'q':
  28. cont = 0;
  29. break;
  30. case 'a':
  31. if (t.add(&buff[2], count++, &val))
  32. printf("[%s] repeated at %d\n", &buff[2], val);
  33. break;
  34. case 's':
  35. if (t.find(&buff[2],&val))
  36. printf("found [%s] = %d\n", &buff[2], val);
  37. else
  38. printf("[%s] not found\n", &buff[2]);
  39. break;
  40. case 'd':
  41. if (t.remove(&buff[2],&val))
  42. printf("removed [%s] = %d\n", &buff[2], val);
  43. else
  44. printf("[%s] not removed\n", &buff[2]);
  45. break;
  46. case 'f':
  47. {
  48. char *name = &buff[2];
  49. if (!buff[1] || !buff[2])
  50. name = "/usr/dict/words";
  51. FILE *f = fopen(name,"rt");
  52. char line[1024], *s;
  53. if (f)
  54. {
  55. printf("Loading file '%s'\n", name);
  56. while (fgets(line, sizeof(line), f))
  57. {
  58. for (s = strtok(line, delim); s; s = strtok(0,delim))
  59. {
  60. for (char *c=s; *c; c++)
  61. *c = (char)tolower(*c);
  62. if (t.add(s, count++, &val))
  63. printf("[%s] repeated from %d.\n",s,val);
  64. }
  65. }
  66. fclose(f);
  67. }
  68. } break;
  69. case 'c':
  70. {
  71. FILE *f = fopen(&buff[2],"rt");
  72. char line[1024], *s;
  73. if (f)
  74. {
  75. printf("Checking file '%s'\n", &buff[2]);
  76. while (fgets(line, sizeof(line), f))
  77. {
  78. for (s = strtok(line, delim); s; s = strtok(0,delim))
  79. {
  80. for (char *c=s; *c; c++)
  81. *c = (char)tolower(*c);
  82. if (!t.find(s))
  83. printf("[%s] not found.\n", s);
  84. }
  85. }
  86. fclose(f);
  87. }
  88. } break;
  89. case 'u':
  90. {
  91. FILE *f = fopen(&buff[2],"rt");
  92. char line[1024], *s;
  93. if (f)
  94. {
  95. printf("Unloading file '%s'\n", &buff[2]);
  96. while (fgets(line, sizeof(line), f))
  97. {
  98. for (s = strtok(line, delim); s; s = strtok(0,delim))
  99. {
  100. for (char *c=s; *c; c++)
  101. *c = (char)tolower(*c);
  102. if (t.remove(s, &val))
  103. printf("Removing [%s] = %d\n", s, val);
  104. }
  105. }
  106. fclose(f);
  107. }
  108. } break;
  109. case 'l':
  110. {
  111. char name[256];
  112. i4_ternary_tree<int>::iterator i(t, name, sizeof(name));
  113. for (; !i.end(); i++)
  114. printf("[%s] = %d\n", i.key(), *i);
  115. } break;
  116. }
  117. }
  118. }
  119. void i4_main(w32 argc, i4_const_str *argv)
  120. {
  121. i4_init();
  122. test();
  123. i4_uninit();
  124. }