summaryrefslogtreecommitdiff
path: root/src/main.cc
blob: 34b59ee974265e0bf9d0fd43f23d8ff06bf3de55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* Driver program for the hash function generator
   Copyright (C) 1989-1998, 2000, 2002-2003, 2009 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.

   This program 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 3 of the License, or
   (at your option) any later version.

   This program 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.  If not, see <http://www.gnu.org/licenses/>.  */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "options.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[])
{
  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);
      }

  {
    /* 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);
            }

      {
        /* 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._hash_includes_len,
                          searcher._key_positions,
                          searcher._alpha_inc,
                          searcher._total_duplicates,
                          searcher._alpha_size,
                          searcher._asso_values);
        outputter.output ();

        /* Check for write error on stdout.  */
        exitcode = 0;
        if (fflush (stdout) || ferror (stdout))
          {
            fprintf (stderr, "error while writing output file\n");
            exitcode = 1;
          }

        /* Here we run the Output destructor.  */
      }
      /* Here we run the Search destructor.  */
    }

    /* 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 exitcode;
}