presage  0.8.7
presageSimulator.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************
00003  *  Presage, an extensible predictive text entry system
00004  *  ---------------------------------------------------
00005  *
00006  *  Copyright (C) 2008  Matteo Vescovi <matteo.vescovi@yahoo.co.uk>
00007 
00008     This program is free software; you can redistribute it and/or modify
00009     it under the terms of the GNU General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or
00011     (at your option) any later version.
00012 
00013     This program is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU General Public License for more details.
00017 
00018     You should have received a copy of the GNU General Public License along
00019     with this program; if not, write to the Free Software Foundation, Inc.,
00020     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00021                                                                              *
00022                                                                 **********(*)*/
00023 
00024 
00025 #ifdef HAVE_CONFIG_H
00026 # include "config.h"
00027 #endif
00028 
00029 #ifdef HAVE_STDLIB_H
00030 # include <stdlib.h>
00031 #endif
00032 #ifdef HAVE_STRING_H
00033 # include <string.h>
00034 #endif
00035 
00036 #include <getopt.h>
00037 
00038 #include <iostream>
00039 #include <fstream>
00040 #include <sstream>
00041 
00042 #include "core/charsets.h"
00043 #include "core/tokenizer/forwardTokenizer.h"
00044 #include "simulator/simulator.h"
00045 
00046 const char PROGRAM_NAME[] = "presage_simulator";
00047 
00048 void parseCommandLineArgs(int argc, char* argv[]);
00049 void printUsage();
00050 void printVersion();
00051 
00052 bool silent_mode = false;
00053 bool case_insensitive = false;
00054 std::string config;
00055 
00056 // Simple callback class, stores past context stream in a stringstream
00057 // passed in at construction time.
00058 // 
00059 // getPastStream() returns a string of size() up to 'width'.
00060 // getFutureStream() return an empty string.
00061 // 
00062 class SimulatorPresageCallback
00063     : public PresageCallback 
00064 {
00065 public:
00066     SimulatorPresageCallback(std::stringstream& sstream) : m_sstream(sstream) { }
00067     ~SimulatorPresageCallback() { };
00068     
00069     std::string get_past_stream() const { return m_sstream.str(); }
00070     std::string get_future_stream() const { return m_empty; }
00071 
00072 private:
00073     std::stringstream& m_sstream;
00074     const std::string m_empty;
00075 };
00076 
00077 int main(int argc, char* argv[])
00078 {
00079     parseCommandLineArgs(argc, argv);
00080 
00081     // check we have enough arguments
00082     if( argc - optind < 1 ) {
00083         printUsage();
00084         exit (0);
00085     }
00086 
00087     // open infile for input
00088     std::ifstream infile( argv[ optind ], std::ios::in );
00089     if( !infile ) {
00090         std::cerr << "\aError: could not open file " << argv[ optind ] << std::endl;
00091         return 1;
00092     }
00093     
00094     std::stringstream sstream;    // stringstream to hold past context stream
00095     Simulator simulator(new SimulatorPresageCallback(sstream), sstream, config);
00096     simulator.silentMode(silent_mode);
00097 
00098     ForwardTokenizer tokenizer(infile,
00099                                DEFAULT_BLANKSPACE_CHARS,
00100                                DEFAULT_SEPARATOR_CHARS);
00101     tokenizer.lowercaseMode(case_insensitive);
00102     while(tokenizer.hasMoreTokens()) {
00103         simulator.simulate(tokenizer.nextToken());
00104     }
00105                 
00106     simulator.results();
00107 
00108     infile.close();
00109 
00110     return 0;
00111 }
00112 
00113 
00114 void parseCommandLineArgs(int argc, char* argv[])
00115 {
00116     int next_option;
00117         
00118     // getopt structures
00119     const char* const short_options = "c:sqihv";
00120 
00121     const struct option long_options[] = {
00122         { "config",      required_argument, 0, 'c' },
00123         { "silent",      no_argument,       0, 's' },
00124         { "quiet",       no_argument,       0, 'q' },
00125         { "insensitive", no_argument,       0, 'i' },
00126         { "help",        no_argument,       0, 'h' },
00127         { "version",     no_argument,       0, 'v' },
00128         { 0, 0, 0, 0 }
00129     };
00130 
00131     do {
00132         next_option = getopt_long( argc, argv, 
00133                                    short_options, long_options, NULL );
00134                 
00135         switch( next_option ) {
00136           case 'c': // -- config of -f option
00137             config = optarg;
00138             break;
00139           case 's': // --silent or -s option
00140           case 'q': // --quiet or -q option
00141             silent_mode = true;
00142             break;
00143           case 'i': // --insensitive or -i option
00144             case_insensitive = true;
00145             break;
00146           case 'h': // --help or -h option
00147             printUsage();
00148             exit (0);
00149             break;
00150           case 'v': // --version or -v option
00151             printVersion();
00152             exit (0);
00153             break;
00154           case '?': // unknown option
00155             printUsage();
00156             exit (0);
00157             break;
00158           case -1:
00159             break;
00160           default:
00161             abort();
00162         }
00163 
00164     } while( next_option != -1 );
00165 }
00166 
00167 void printVersion()
00168 {
00169     std::cout << PROGRAM_NAME << " (" << PACKAGE << ") version " << VERSION << std::endl
00170               << "Copyright (C) 2004 Matteo Vescovi." << std::endl
00171               << "This is free software; see the source for copying conditions.  There is NO" << std::endl
00172               << "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE," << std::endl
00173               << "to the extent permitted by law." << std::endl;
00174 }
00175 
00176 void printUsage()
00177 {
00178     std::cout << "Usage: " << PROGRAM_NAME << " [OPTION]... FILE" << std::endl
00179               << std::endl
00180               << "  -c, --config CONFIG  use config file CONFIG" << std::endl
00181               << "  -i, --insensitive    case insensitive mode" << std::endl
00182               << "  -q, --quiet          quiet mode, no verbose output, same as silent" << std::endl
00183               << "  -s, --silent         silent mode, no verbose output, same as quiet" << std::endl
00184               << "  -h, --help           display this help and exit" << std::endl
00185               << "  -v, --version        output version information and exit" << std::endl
00186               << std::endl
00187               << "Direct your bug reports to: " << PACKAGE_BUGREPORT << std::endl;
00188 }