source: golgotha/src/i4/inc/ttree_test.cc

Last change on this file was 80, checked in by Sam Hocevar, 12 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 3.5 KB
Line 
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
9#include "ttree.hh"
10#include "init/init.hh"
11#include "main/main.hh"
12#include <stdio.h>
13#include <string.h>
14#include <ctype.h>
15
16const char *delim=" ,.?!;:\"()-`'<>{}[]|-=+\n\t";
17
18void test()
19{
20  i4_ternary_tree<int> t;
21
22  int count=0;
23
24  char buff[256];
25  int val, cont=1;
26
27  while (cont)
28  {
29    printf("> ");
30    gets(buff);
31   
32    switch (buff[0])
33    {
34      case 'q':
35        cont = 0;
36        break;
37      case 'a':
38        if (t.add(&buff[2], count++, &val))
39          printf("[%s] repeated at %d\n", &buff[2], val);
40        break;
41      case 's':
42        if (t.find(&buff[2],&val))
43          printf("found [%s] = %d\n", &buff[2], val);
44        else
45          printf("[%s] not found\n", &buff[2]);
46        break;
47      case 'd':
48        if (t.remove(&buff[2],&val))
49          printf("removed [%s] = %d\n", &buff[2], val);
50        else
51          printf("[%s] not removed\n", &buff[2]);
52        break;
53      case 'f':
54      {
55        char *name = &buff[2];
56
57        if (!buff[1] || !buff[2])
58          name = "/usr/dict/words";
59
60        FILE *f = fopen(name,"rt");
61        char line[1024], *s;
62
63        if (f)
64        {
65          printf("Loading file '%s'\n", name);
66          while (fgets(line, sizeof(line), f))
67          {
68            for (s = strtok(line, delim); s; s = strtok(0,delim))
69            {
70              for (char *c=s; *c; c++)
71                *c = (char)tolower(*c);
72              if (t.add(s, count++, &val))
73                printf("[%s] repeated from %d.\n",s,val);
74            }
75          }
76          fclose(f);
77        }
78      } break;
79      case 'c':
80      {
81        FILE *f = fopen(&buff[2],"rt");
82        char line[1024], *s;
83
84        if (f)
85        {
86          printf("Checking file '%s'\n", &buff[2]);
87          while (fgets(line, sizeof(line), f))
88          {
89            for (s = strtok(line, delim); s; s = strtok(0,delim))
90            {
91              for (char *c=s; *c; c++)
92                *c = (char)tolower(*c);
93              if (!t.find(s))
94                printf("[%s] not found.\n", s);
95            }
96          }
97          fclose(f);
98        }
99      } break;
100      case 'u':
101      {
102        FILE *f = fopen(&buff[2],"rt");
103        char line[1024], *s;
104
105        if (f)
106        {
107          printf("Unloading file '%s'\n", &buff[2]);
108          while (fgets(line, sizeof(line), f))
109          {
110            for (s = strtok(line, delim); s; s = strtok(0,delim))
111            {
112              for (char *c=s; *c; c++)
113                *c = (char)tolower(*c);
114              if (t.remove(s, &val))
115                printf("Removing [%s] = %d\n", s, val);
116            }
117          }
118          fclose(f);
119        }
120      } break;
121      case 'l':
122      {
123        char name[256];
124        i4_ternary_tree<int>::iterator i(t, name, sizeof(name));
125
126        for (; !i.end(); i++)
127          printf("[%s] = %d\n", i.key(), *i);
128      } break;
129    }
130  }
131}
132
133void i4_main(w32 argc, i4_const_str *argv)
134{
135  i4_init();
136 
137  test();
138
139  i4_uninit();
140}
Note: See TracBrowser for help on using the repository browser.