1 /* Driver program for the hash function generator
2 Copyright (C) 1989-1998, 2000, 2002-2003 Free Software Foundation, Inc.
3 Written by Douglas C. Schmidt <schmidt@ics.uci.edu>
4 and Bruno Haible <bruno@clisp.org>.
6 This file is part of GNU GPERF.
8 GNU GPERF 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 2, or (at your option)
13 GNU GPERF 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.
18 You should have received a copy of the GNU General Public License
19 along with this program; see the file COPYING.
20 If not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
32 /* ------------------------------------------------------------------------- */
34 /* This Keyword factory produces KeywordExt instances. */
36 class KeywordExt_Factory : public Keyword_Factory
38 virtual Keyword * create_keyword (const char *allchars, int allchars_length,
43 KeywordExt_Factory::create_keyword (const char *allchars, int allchars_length, const char *rest)
45 return new KeywordExt (allchars, allchars_length, rest);
48 /* ------------------------------------------------------------------------- */
51 main (int argc, char *argv[])
55 /* Set the Options. Open the input file and assign stdin to it. */
56 option.parse_options (argc, argv);
58 /* Open the input file. */
59 if (option.get_input_file_name ())
60 if (!freopen (option.get_input_file_name (), "r", stdin))
62 fprintf (stderr, "Cannot open input file '%s'\n",
63 option.get_input_file_name ());
68 /* Initialize the keyword list. */
69 KeywordExt_Factory factory;
70 Input inputter (stdin, &factory);
71 inputter.read_input ();
72 /* We can cast the keyword list to KeywordExt_List* because its list
73 elements were created by KeywordExt_Factory. */
74 KeywordExt_List* list = static_cast<KeywordExt_List*>(inputter._head);
77 /* Search for a good hash function. */
78 Search searcher (list);
80 list = searcher._head;
82 /* Open the output file. */
83 if (option.get_output_file_name ())
84 if (strcmp (option.get_output_file_name (), "-") != 0)
85 if (!freopen (option.get_output_file_name (), "w", stdout))
87 fprintf (stderr, "Cannot open output file '%s'\n",
88 option.get_output_file_name ());
93 /* Output the hash function code. */
94 Output outputter (searcher._head,
95 inputter._struct_decl,
96 inputter._struct_decl_lineno,
97 inputter._return_type,
99 inputter._verbatim_declarations,
100 inputter._verbatim_declarations_end,
101 inputter._verbatim_declarations_lineno,
102 inputter._verbatim_code,
103 inputter._verbatim_code_end,
104 inputter._verbatim_code_lineno,
105 inputter._charset_dependent,
106 searcher._total_keys,
107 searcher._max_key_len,
108 searcher._min_key_len,
109 searcher._key_positions,
111 searcher._total_duplicates,
112 searcher._alpha_size,
113 searcher._asso_values);
116 /* Check for write error on stdout. */
118 if (fflush (stdout) || ferror (stdout))
120 fprintf (stderr, "error while writing output file\n");
124 /* Here we run the Output destructor. */
126 /* Here we run the Search destructor. */
129 /* Also delete the list that was allocated inside Input and reordered
131 for (KeywordExt_List *ptr = list; ptr; ptr = ptr->rest())
133 KeywordExt *keyword = ptr->first();
136 KeywordExt *next_keyword = keyword->_duplicate_link;
137 delete[] const_cast<unsigned int *>(keyword->_selchars);
138 if (keyword->_rest != empty_string)
139 delete[] const_cast<char*>(keyword->_rest);
140 if (!(keyword->_allchars >= inputter._input
141 && keyword->_allchars < inputter._input_end))
142 delete[] const_cast<char*>(keyword->_allchars);
144 keyword = next_keyword;
146 while (keyword != NULL);
150 /* Here we run the Input destructor. */
153 /* Don't use exit() here, it skips the destructors. */