Object-oriented Scientific Computing Library: Version 0.910
cli_readline.h
00001 /*
00002   -------------------------------------------------------------------
00003   
00004   Copyright (C) 2006-2012, Andrew W. Steiner
00005   
00006   This file is part of O2scl.
00007   
00008   O2scl 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 3 of the License, or
00011   (at your option) any later version.
00012   
00013   O2scl 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
00019   along with O2scl. If not, see <http://www.gnu.org/licenses/>.
00020 
00021   -------------------------------------------------------------------
00022 */
00023 #ifndef O2SCL_CLI_READLINE_H
00024 #define O2SCL_CLI_READLINE_H
00025 
00026 #include <readline/readline.h>
00027 #include <readline/history.h>
00028 
00029 #include <o2scl/cli.h>
00030 
00031 #ifndef DOXYGENP
00032 namespace o2scl {
00033 #endif
00034 
00035   /** \brief An extension to \ref cli which uses readline
00036       
00037       This header-only class requires the GNU <tt>readline</tt>
00038       library for use, but is not referenced by \o2 code at the
00039       moment to make the library usable without <tt>readline</tt>.
00040 
00041   */
00042   class cli_readline : public cli {
00043   
00044   protected:
00045 
00046     /// Buffer for readline
00047     char *line_read;
00048 
00049     /// String containing filename
00050     std::string histfile;
00051 
00052     /// Maximum history file size
00053     size_t msize;
00054 
00055   public:
00056     
00057     cli_readline(std::string fname=".cli_hist", size_t max_size=100) {
00058       line_read=0;
00059       msize=max_size;
00060       
00061       histfile=fname;
00062       read_history(histfile.c_str());
00063     }
00064     
00065     ~cli_readline() {
00066       stifle_history(msize);
00067       write_history(histfile.c_str());
00068     }
00069 
00070     // This define constant is no longer necessary since
00071     // this is a separate class now
00072     //#ifdef O2SCL_READLINE
00073   
00074     /** \brief Function to get a string from the user
00075         \nothing
00076     */
00077     virtual char *cli_gets(const char *c) {
00078     
00079       /* If the buffer has already been allocated, return the memory to
00080          the free pool.
00081       */
00082       if (line_read) {
00083         free(line_read);
00084         line_read=(char *)0;
00085       }
00086     
00087       line_read=readline(c);
00088     
00089       /* If the line has any text in it, save it on the history.
00090        */
00091       if (line_read && *line_read) {
00092         add_history(line_read);
00093       }
00094     
00095       return(line_read);
00096     }
00097 
00098     //#endif
00099   
00100   };
00101 
00102 #ifndef DOXYGENP
00103 }
00104 #endif
00105 
00106 #endif
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).

Get Object-oriented Scientific Computing
Lib at SourceForge.net. Fast, secure and Free Open Source software
downloads.