1/* Driver program for the hash function generator
2 Copyright (C) 1989-1998, 2000, 2002-2003, 2009 Free Software Foundation, Inc.
3 Written by Douglas C. Schmidt <schmidt@ics.uci.edu>
4 and Bruno Haible <bruno@clisp.org>.
5
6 This file is part of GNU GPERF.
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include "options.h"
25#include "input.h"
26#include "search.h"
27#include "output.h"
28
29
30/* ------------------------------------------------------------------------- */
31
32/* This Keyword factory produces KeywordExt instances. */
33
34class KeywordExt_Factory : public Keyword_Factory
35{
36virtual Keyword * create_keyword (const char *allchars, int allchars_length,
37 const char *rest);
38};
39
40Keyword *
41KeywordExt_Factory::create_keyword (const char *allchars, int allchars_length, const char *rest)
42{
43 return new KeywordExt (allchars, allchars_length, rest);
44}
45
46/* ------------------------------------------------------------------------- */
47
48int
49main (int argc, char *argv[])
50{
51 int exitcode;
52
53 /* Set the Options. Open the input file and assign stdin to it. */
54 option.parse_options (argc, argv);
55
56 /* Open the input file. */
57 if (option.get_input_file_name ())
58 if (!freopen (option.get_input_file_name (), "r", stdin))
59 {
60 fprintf (stderr, "Cannot open input file '%s'\n",
61 option.get_input_file_name ());
62 exit (1);
63 }
64
65 {
66 /* Initialize the keyword list. */
67 KeywordExt_Factory factory;
68 Input inputter (stdin, &factory);
69 inputter.read_input ();
70 /* We can cast the keyword list to KeywordExt_List* because its list
71 elements were created by KeywordExt_Factory. */
72 KeywordExt_List* list = static_cast<KeywordExt_List*>(inputter._head);
73
74 {
75 /* Search for a good hash function. */
76 Search searcher (list);
77 searcher.optimize ();
78 list = searcher._head;
79
80 /* Open the output file. */
81 if (option.get_output_file_name ())
82 if (strcmp (option.get_output_file_name (), "-") != 0)
83 if (!freopen (option.get_output_file_name (), "w", stdout))
84 {
85 fprintf (stderr, "Cannot open output file '%s'\n",
86 option.get_output_file_name ());
87 exit (1);
88 }
89
90 {
91 /* Output the hash function code. */
92 Output outputter (searcher._head,
93 inputter._struct_decl,
94 inputter._struct_decl_lineno,
95 inputter._return_type,
96 inputter._struct_tag,
97 inputter._verbatim_declarations,
98 inputter._verbatim_declarations_end,
99 inputter._verbatim_declarations_lineno,
100 inputter._verbatim_code,
101 inputter._verbatim_code_end,
102 inputter._verbatim_code_lineno,
103 inputter._charset_dependent,
104 searcher._total_keys,
105 searcher._max_key_len,
106 searcher._min_key_len,
107 searcher._hash_includes_len,
108 searcher._key_positions,
109 searcher._alpha_inc,
110 searcher._total_duplicates,
111 searcher._alpha_size,
112 searcher._asso_values);
113 outputter.output ();
114
115 /* Check for write error on stdout. */
116 exitcode = 0;
117 if (fflush (stdout) || ferror (stdout))
118 {
119 fprintf (stderr, "error while writing output file\n");
120 exitcode = 1;
121 }
122
123 /* Here we run the Output destructor. */
124 }
125 /* Here we run the Search destructor. */
126 }
127
128 /* Also delete the list that was allocated inside Input and reordered
129 inside Search. */
130 for (KeywordExt_List *ptr = list; ptr; ptr = ptr->rest())
131 {
132 KeywordExt *keyword = ptr->first();
133 do
134 {
135 KeywordExt *next_keyword = keyword->_duplicate_link;
136 delete[] const_cast<unsigned int *>(keyword->_selchars);
137 if (keyword->_rest != empty_string)
138 delete[] const_cast<char*>(keyword->_rest);
139 if (!(keyword->_allchars >= inputter._input
140 && keyword->_allchars < inputter._input_end))
141 delete[] const_cast<char*>(keyword->_allchars);
142 delete keyword;
143 keyword = next_keyword;
144 }
145 while (keyword != NULL);
146 }
147 delete_list (list);
148
149 /* Here we run the Input destructor. */
150 }
151
152 /* Don't use exit() here, it skips the destructors. */
153 return exitcode;
154}
155