11 static void write_index(const char *filename
)
13 struct index_node
*index
;
16 unsigned int linenum
= 0;
18 cfile
= fopen(filename
, "w");
20 fatal("Could not open %s for writing: %s\n",
21 filename
, strerror(errno
));
23 index
= index_create();
25 while((line
= getline_wrapped(stdin
, &linenum
))) {
26 pos
= strchr(line
, ' ');
28 index_insert(index
, line
, pos
, linenum
);
32 index_write(index
, cfile
);
37 static struct index_file
*open_index(const char *filename
)
39 struct index_file
*index
;
41 index
= index_file_open(filename
);
44 fatal("%s has wrong magic or version number", filename
);
46 fatal("Could not open %s for reading: %s\n",
47 filename
, strerror(errno
));
53 static void dump_index(const char *filename
)
55 struct index_file
*index
= open_index(filename
);
57 index_dump(index
, stdout
, "");
59 index_file_close(index
);
62 static void search_index(const char *filename
, char *key
)
64 struct index_file
*index
= open_index(filename
);
67 value
= index_search(index
, key
);
69 printf("Found value:\n%s\n", value
);
71 printf("Not found.\n");
74 index_file_close(index
);
77 static void searchwild_index(const char *filename
, char *key
)
79 struct index_file
*index
= open_index(filename
);
80 struct index_value
*values
, *v
;
82 values
= index_searchwild(index
, key
);
84 printf("Found value(s):\n");
86 printf("Not found.\n");
88 for (v
= values
; v
; v
= v
->next
)
89 printf("%s\n", v
->value
);
91 index_values_free(values
);
92 index_file_close(index
);
95 static void print_usage(const char *progname
)
98 "Usage: %s [MODE] [FILE] ...\n"
99 " -o, --output <outfile>\n"
100 " -d, --dump <infile>\n"
101 " -s, --search <key> <infile>\n"
102 " -w, --searchwild <key> <infile>\n"
107 static struct option options
[] = {
108 { "output", 0, NULL
, 'o' },
109 { "dump", 0, NULL
, 'd' },
110 { "search", 1, NULL
, 's' },
111 { "searchwild", 1, NULL
, 'w' },
114 int main(int argc
, char *argv
[])
118 char *filename
= NULL
;
121 while ((opt
= getopt_long(argc
, argv
, "ods:w:", options
, NULL
))
139 print_usage(argv
[0]);
143 print_usage(argv
[0]);
146 print_usage(argv
[0]);
147 filename
= argv
[optind
];
151 write_index(filename
);
154 dump_index(filename
);
157 search_index(filename
, key
);
160 searchwild_index(filename
, key
);