presage 0.9.1
presageSimulator.cpp
Go to the documentation of this file.
1
2/******************************************************
3 * Presage, an extensible predictive text entry system
4 * ---------------------------------------------------
5 *
6 * Copyright (C) 2008 Matteo Vescovi <matteo.vescovi@yahoo.co.uk>
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 2 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 along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 **********(*)*/
23
24
25#ifdef HAVE_CONFIG_H
26# include "config.h"
27#endif
28
29#ifdef HAVE_STDLIB_H
30# include <stdlib.h>
31#endif
32#ifdef HAVE_STRING_H
33# include <string.h>
34#endif
35
36#include <getopt.h>
37
38#include <iostream>
39#include <fstream>
40#include <sstream>
41
42#include "core/charsets.h"
44#include "simulator/simulator.h"
45
46const char PROGRAM_NAME[] = "presage_simulator";
47
48void parseCommandLineArgs(int argc, char* argv[]);
49void printUsage();
50void printVersion();
51
52bool silent_mode = false;
53bool case_insensitive = false;
54std::string config;
55
56// Simple callback class, stores past context stream in a stringstream
57// passed in at construction time.
58//
59// getPastStream() returns a string of size() up to 'width'.
60// getFutureStream() return an empty string.
61//
63 : public PresageCallback
64{
65public:
66 SimulatorPresageCallback(std::stringstream& sstream) : m_sstream(sstream) { }
68
69 std::string get_past_stream() const { return m_sstream.str(); }
70 std::string get_future_stream() const { return m_empty; }
71
72private:
73 std::stringstream& m_sstream;
74 const std::string m_empty;
75};
76
77int main(int argc, char* argv[])
78{
79 parseCommandLineArgs(argc, argv);
80
81 // check we have enough arguments
82 if( argc - optind < 1 ) {
83 printUsage();
84 exit (0);
85 }
86
87 // open infile for input
88 std::ifstream infile( argv[ optind ], std::ios::in );
89 if( !infile ) {
90 std::cerr << "\aError: could not open file " << argv[ optind ] << std::endl;
91 return 1;
92 }
93
94 std::stringstream sstream; // stringstream to hold past context stream
95 Simulator simulator(new SimulatorPresageCallback(sstream), sstream, config);
96 simulator.silentMode(silent_mode);
97
98 ForwardTokenizer tokenizer(infile,
102 while(tokenizer.hasMoreTokens()) {
103 simulator.simulate(tokenizer.nextToken());
104 }
105
106 simulator.results();
107
108 infile.close();
109
110 return 0;
111}
112
113
114void parseCommandLineArgs(int argc, char* argv[])
115{
116 int next_option;
117
118 // getopt structures
119 const char* const short_options = "c:sqihv";
120
121 const struct option long_options[] = {
122 { "config", required_argument, 0, 'c' },
123 { "silent", no_argument, 0, 's' },
124 { "quiet", no_argument, 0, 'q' },
125 { "insensitive", no_argument, 0, 'i' },
126 { "help", no_argument, 0, 'h' },
127 { "version", no_argument, 0, 'v' },
128 { 0, 0, 0, 0 }
129 };
130
131 do {
132 next_option = getopt_long( argc, argv,
133 short_options, long_options, NULL );
134
135 switch( next_option ) {
136 case 'c': // -- config of -f option
137 config = optarg;
138 break;
139 case 's': // --silent or -s option
140 case 'q': // --quiet or -q option
141 silent_mode = true;
142 break;
143 case 'i': // --insensitive or -i option
144 case_insensitive = true;
145 break;
146 case 'h': // --help or -h option
147 printUsage();
148 exit (0);
149 break;
150 case 'v': // --version or -v option
151 printVersion();
152 exit (0);
153 break;
154 case '?': // unknown option
155 printUsage();
156 exit (0);
157 break;
158 case -1:
159 break;
160 default:
161 abort();
162 }
163
164 } while( next_option != -1 );
165}
166
168{
169 std::cout << PROGRAM_NAME << " (" << PACKAGE << ") version " << VERSION << std::endl
170 << "Copyright (C) Matteo Vescovi." << std::endl
171 << "This is free software; see the source for copying conditions. There is NO" << std::endl
172 << "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE," << std::endl
173 << "to the extent permitted by law." << std::endl;
174}
175
177{
178 std::cout << "Usage: " << PROGRAM_NAME << " [OPTION]... FILE" << std::endl
179 << std::endl
180 << " -c, --config CONFIG use config file CONFIG" << std::endl
181 << " -i, --insensitive case insensitive mode" << std::endl
182 << " -q, --quiet quiet mode, no verbose output, same as silent" << std::endl
183 << " -s, --silent silent mode, no verbose output, same as quiet" << std::endl
184 << " -h, --help display this help and exit" << std::endl
185 << " -v, --version output version information and exit" << std::endl
186 << std::endl
187 << "Direct your bug reports to: " << PACKAGE_BUGREPORT << std::endl;
188}
const char DEFAULT_SEPARATOR_CHARS[]
Definition: charsets.h:176
const char DEFAULT_BLANKSPACE_CHARS[]
Definition: charsets.h:211
virtual bool hasMoreTokens() const
virtual std::string nextToken()
std::stringstream & m_sstream
std::string get_past_stream() const
std::string get_future_stream() const
SimulatorPresageCallback(std::stringstream &sstream)
void silentMode(bool)
Definition: simulator.cpp:246
void simulate(std::string)
Definition: simulator.cpp:52
void results() const
Definition: simulator.cpp:164
void lowercaseMode(const bool)
Definition: tokenizer.cpp:81
const Logger< _charT, _Traits > & endl(const Logger< _charT, _Traits > &lgr)
Definition: logger.h:278
int main(int argc, char *argv[])
std::string config
bool case_insensitive
const char PROGRAM_NAME[]
void printVersion()
bool silent_mode
void parseCommandLineArgs(int argc, char *argv[])
void printUsage()