summaryrefslogtreecommitdiff
path: root/src/main.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.cc')
-rw-r--r--src/main.cc201
1 files changed, 140 insertions, 61 deletions
diff --git a/src/main.cc b/src/main.cc
index 03b6c7ea242b..577708b48bfd 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -1,76 +1,155 @@
-/* Driver program for the Gen_Perf hash function generator
- Copyright (C) 1989-1998, 2000 Free Software Foundation, Inc.
- written by Douglas C. Schmidt (schmidt@ics.uci.edu)
-
-This file is part of GNU GPERF.
-
-GNU GPERF is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 1, or (at your option)
-any later version.
-
-GNU GPERF is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with GNU GPERF; see the file COPYING. If not, write to the Free
-Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111, USA. */
-
-/* Simple driver program for the Gen_Perf.hash function generator.
- Most of the hard work is done in class Gen_Perf and its class methods. */
-
-#include "config.h"
-#include <sys/types.h>
-#if LARGE_STACK_ARRAYS && defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT)
-#ifdef HAVE_SYS_TIME_H
-#include <sys/time.h>
-#endif
-#ifdef HAVE_SYS_RESOURCE_H
-#include <sys/resource.h>
-#endif
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-#endif
+/* Driver program for the hash function generator
+ Copyright (C) 1989-1998, 2000, 2002-2003 Free Software Foundation, Inc.
+ Written by Douglas C. Schmidt <schmidt@ics.uci.edu>
+ and Bruno Haible <bruno@clisp.org>.
+
+ This file is part of GNU GPERF.
+
+ GNU GPERF is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
+ GNU GPERF is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; see the file COPYING.
+ If not, write to the Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
#include "options.h"
-#include "gen-perf.h"
-#include "trace.h"
+#include "input.h"
+#include "search.h"
+#include "output.h"
+
+
+/* ------------------------------------------------------------------------- */
+
+/* This Keyword factory produces KeywordExt instances. */
+
+class KeywordExt_Factory : public Keyword_Factory
+{
+virtual Keyword * create_keyword (const char *allchars, int allchars_length,
+ const char *rest);
+};
+
+Keyword *
+KeywordExt_Factory::create_keyword (const char *allchars, int allchars_length, const char *rest)
+{
+ return new KeywordExt (allchars, allchars_length, rest);
+}
+
+/* ------------------------------------------------------------------------- */
int
main (int argc, char *argv[])
{
- T (Trace t ("main");)
+ int exitcode;
+
+ /* Set the Options. Open the input file and assign stdin to it. */
+ option.parse_options (argc, argv);
+
+ /* Open the input file. */
+ if (option.get_input_file_name ())
+ if (!freopen (option.get_input_file_name (), "r", stdin))
+ {
+ fprintf (stderr, "Cannot open input file '%s'\n",
+ option.get_input_file_name ());
+ exit (1);
+ }
-#if LARGE_STACK_ARRAYS && defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT) && defined(RLIMIT_STACK)
- /* Get rid of any avoidable limit on stack size. */
{
- struct rlimit rlim;
- if (getrlimit (RLIMIT_STACK, &rlim) == 0)
- if (rlim.rlim_cur < rlim.rlim_max)
- {
- rlim.rlim_cur = rlim.rlim_max;
- setrlimit (RLIMIT_STACK, &rlim);
- }
- }
-#endif /* RLIMIT_STACK */
+ /* Initialize the keyword list. */
+ KeywordExt_Factory factory;
+ Input inputter (stdin, &factory);
+ inputter.read_input ();
+ /* We can cast the keyword list to KeywordExt_List* because its list
+ elements were created by KeywordExt_Factory. */
+ KeywordExt_List* list = static_cast<KeywordExt_List*>(inputter._head);
+
+ {
+ /* Search for a good hash function. */
+ Search searcher (list);
+ searcher.optimize ();
+ list = searcher._head;
+
+ /* Open the output file. */
+ if (option.get_output_file_name ())
+ if (strcmp (option.get_output_file_name (), "-") != 0)
+ if (!freopen (option.get_output_file_name (), "w", stdout))
+ {
+ fprintf (stderr, "Cannot open output file '%s'\n",
+ option.get_output_file_name ());
+ exit (1);
+ }
- /* Sets the Options. */
- option (argc, argv);
+ {
+ /* Output the hash function code. */
+ Output outputter (searcher._head,
+ inputter._struct_decl,
+ inputter._struct_decl_lineno,
+ inputter._return_type,
+ inputter._struct_tag,
+ inputter._verbatim_declarations,
+ inputter._verbatim_declarations_end,
+ inputter._verbatim_declarations_lineno,
+ inputter._verbatim_code,
+ inputter._verbatim_code_end,
+ inputter._verbatim_code_lineno,
+ inputter._charset_dependent,
+ searcher._total_keys,
+ searcher._max_key_len,
+ searcher._min_key_len,
+ searcher._key_positions,
+ searcher._alpha_inc,
+ searcher._total_duplicates,
+ searcher._alpha_size,
+ searcher._asso_values);
+ outputter.output ();
- /* Initializes the key word list. */
- Gen_Perf generate_table;
+ /* Check for write error on stdout. */
+ exitcode = 0;
+ if (fflush (stdout) || ferror (stdout))
+ {
+ fprintf (stderr, "error while writing output file\n");
+ exitcode = 1;
+ }
- /* Generates and prints the Gen_Perf hash table. */
- int status = generate_table ();
+ /* Here we run the Output destructor. */
+ }
+ /* Here we run the Search destructor. */
+ }
- /* Check for write error on stdout. */
- if (fflush (stdout) || ferror (stdout))
- status = 1;
+ /* Also delete the list that was allocated inside Input and reordered
+ inside Search. */
+ for (KeywordExt_List *ptr = list; ptr; ptr = ptr->rest())
+ {
+ KeywordExt *keyword = ptr->first();
+ do
+ {
+ KeywordExt *next_keyword = keyword->_duplicate_link;
+ delete[] const_cast<unsigned int *>(keyword->_selchars);
+ if (keyword->_rest != empty_string)
+ delete[] const_cast<char*>(keyword->_rest);
+ if (!(keyword->_allchars >= inputter._input
+ && keyword->_allchars < inputter._input_end))
+ delete[] const_cast<char*>(keyword->_allchars);
+ delete keyword;
+ keyword = next_keyword;
+ }
+ while (keyword != NULL);
+ }
+ delete_list (list);
+
+ /* Here we run the Input destructor. */
+ }
- /* Don't use exit() here, it skips the destructors. */
- return status;
+ /* Don't use exit() here, it skips the destructors. */
+ return exitcode;
}