BasicExample.cpp

Go to the documentation of this file.
00001 
00005 //
00006 // Date:      22 December  2005
00007 //
00008 // Copyright (c) Sergey Satskiy 2005
00009 //               <sergesatsky@yahoo.com>
00010 //
00011 // Permission to copy, use, modify, sell and distribute this software 
00012 // is granted provided this copyright notice appears in all copies. 
00013 // This software is provided "as is" without express or implied
00014 // warranty, and with no claim as to its suitability for any purpose.
00015 //
00016 
00017 
00018 #include <iostream>
00019 #include <vector>
00020 #include <string>
00021 #include <map>
00022 #include <list>
00023 using namespace std;
00024 
00025     // The wrapper is in a single header file.
00026 #include "SReadline.h"
00027 using namespace swift;
00028 
00029     
00030  
00031 
00032 int  main( int  argc, char **  argv )
00033 { 
00034         // First parameter is a file name to save / load the
00035         // commands history. The second is a max number of stored commands
00036         // Both parameters could be ommited. In this case the history size
00037         // will be 64 and no save/restore operations will be performed.
00038     SReadline           Reader( "/tmp/.testhist", 32 );
00039 
00040 
00041         // Prepare the list of my own completers
00042     vector< string >    Completers;
00043         
00044         // The following is supported:
00045         // - "identifiers"
00046         // - special identifier %file - means to perform a file name completion
00047     Completers.push_back( "command1 opt1" );
00048     Completers.push_back( "command1 opt2" );
00049     Completers.push_back( "command1 opt3 %file" );
00050     Completers.push_back( "command2 opt4" );
00051     Completers.push_back( "command2 opt5 %file %file" );
00052 
00053 
00054         // Now register the completers.
00055         // Actually it is possible to re-register another set at any time
00056     Reader.RegisterCompletions( Completers );
00057         
00058         // Now we can ask user for a line
00059     string      UserInput;
00060     bool        EndOfInput( false );
00061 
00062     for ( ; ; )
00063     {
00064             // The last parameter could be ommited
00065         UserInput = Reader.GetLine( "Please input your command> ", EndOfInput );
00066         if ( EndOfInput )
00067         {
00068             cout << "End of the session. Exiting." << endl;
00069             break;
00070         }
00071         cout << "User input: '" << UserInput << "'." << endl;
00072         cout << "Press Ctrl+D for gracefull exit" << endl;
00073     }
00074         
00075         // The history could be saved to an arbitrary file at any time
00076     Reader.SaveHistory( "/tmp/BackupFileJustInCase" );
00077 
00078         // The history could be cleared
00079     Reader.ClearHistory();
00080 
00081         // And the history could be loaded at any time
00082     Reader.LoadHistory( "/tmp/BackupFileJustInCase" );
00083 
00084     return 0;
00085 }
00086 

Generated on Mon May 8 21:57:35 2006 for Basic SReadline usage example by  doxygen 1.4.5