1 #include <stdio.h>
2 #include <unistd.h>
3 #include <string.h>
4
5 #include <Judy.h>
6
7 #define MAXLENSTR 1000000
8
9 char Index[MAXLENSTR]; // string to store.
10
11 Pvoid_t PJHArray = (PWord_t)NULL; // Judy array.
12
13 // By Doug Baskins Apr 2004 - for JudyHS man page -- but too long
14
15 int // Usage: JudyString file_to_store
main(int argc,char * argv[])16 main(int argc, char *argv[])
17 {
18 Pvoid_t PJHArray = (PWord_t)NULL; // Judy array.
19 PWord_t PValue; // Judy array element.
20 Word_t Bytes; // size of JudySL array.
21 Word_t Len; // length of string
22 FILE *fid = NULL; // stream id
23 int Chr; // next char
24 long Lines; // number of lines input file
25 Word_t Dups; // Count duplicate lines
26
27 if (argc < 2)
28 {
29 printf("Must supply input file in arg\n");
30 exit(2);
31 }
32 if ((fid = fopen(argv[1], "r")) == NULL)
33 {
34 printf("Failed to open '%s'\n", argv[1]);
35 exit(2);
36 }
37 printf("Open '%s' and store strings in JudyHS array\n", argv[1]);
38
39 Lines = 0;
40 Len = 0;
41 Dups = 0;
42 while ((Chr = fgetc(fid)) != EOF)
43 {
44 if (Chr == '\n' && Len)
45 {
46 Index[Len] = '\0';
47
48 //printf("%3lu,%lu: %s\n", Lines, Len, Index);
49 //printf("%s\n", Index);
50
51 JHSI(PValue, PJHArray, Index, Len); // store string into array
52 if (*PValue != 0) Dups++;
53 *PValue += 1;
54 Lines++;
55 Len = 0;
56 }
57 else if (Len < MAXLENSTR)
58 {
59 Index[Len] = Chr;
60 Len++;
61 }
62 }
63
64 fclose(fid);
65 fid = NULL;
66
67 printf("'%s' has %lu lines, %lu duplicate lines\n", argv[1], Lines, Dups);
68
69 printf("Re-open '%s' and verify each string is in JudyHS array\n", argv[1]);
70
71 if ((fid = fopen(argv[1], "r")) == NULL)
72 {
73 printf("Failed to re-open '%s'\n", argv[1]);
74 exit(2);
75 }
76
77 Lines = 0;
78 Len = 0;
79 while ((Chr = fgetc(fid)) != EOF)
80 {
81 if (Len < MAXLENSTR) Index[Len] = Chr;
82
83 if (Chr == '\n' && Len)
84 {
85 Index[Len] = '\0';
86
87 JHSG(PValue, PJHArray, Index, Len); // store string into array
88 if (PValue == NULL)
89 {
90 printf("'%s'\n", Index);
91 printf("JHSG() failed at Line %lu\n", Lines);
92 exit(1);
93 }
94 Len = 0;
95 Lines++;
96 }
97 else Len++;
98 }
99
100 printf("Begin JHSFA (JudyHSFreeArray)\n");
101
102 JHSFA(Bytes, PJHArray); // free array
103
104 fprintf(stderr, "JHSFA() free'ed %lu bytes of memory\n", Bytes);
105 return (0);
106 }
107