#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "options.h"
#include "input.h"
#include "search.h"
#include "output.h"
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;
option.parse_options (argc, argv);
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);
}
{
KeywordExt_Factory factory;
Input inputter (stdin, &factory);
inputter.read_input ();
KeywordExt_List* list = static_cast<KeywordExt_List*>(inputter._head);
{
Search searcher (list);
searcher.optimize ();
list = searcher._head;
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 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 ();
exitcode = 0;
if (fflush (stdout) || ferror (stdout))
{
fprintf (stderr, "error while writing output file\n");
exitcode = 1;
}
}
}
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);
}
return exitcode;
}