00001 /* 00002 ------------------------------------------------------------------- 00003 00004 Copyright (C) 2006, 2007, 2008, 2009, 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 /** 00036 \brief An extension to \ref cli which uses readline 00037 00038 This header-only class requires the GNU <tt>readline</tt> 00039 library for use, but is not referenced by \o2 code at the 00040 moment to make the library usable without <tt>readline</tt>. 00041 00042 */ 00043 class cli_readline : public cli { 00044 00045 protected: 00046 00047 /// Buffer for readline 00048 char *line_read; 00049 00050 /// String containing filename 00051 std::string histfile; 00052 00053 /// Maximum history file size 00054 size_t msize; 00055 00056 public: 00057 00058 cli_readline(std::string fname=".cli_hist", size_t max_size=100) { 00059 line_read=0; 00060 msize=max_size; 00061 00062 histfile=fname; 00063 read_history(histfile.c_str()); 00064 } 00065 00066 ~cli_readline() { 00067 stifle_history(msize); 00068 write_history(histfile.c_str()); 00069 } 00070 00071 #ifdef O2SCL_READLINE 00072 00073 /** \brief Function to get a string from the user 00074 \nothing 00075 */ 00076 virtual char *cli_gets(const char *c) { 00077 00078 /* If the buffer has already been allocated, return the memory to 00079 the free pool. 00080 */ 00081 if (line_read) { 00082 free(line_read); 00083 line_read=(char *)0; 00084 } 00085 00086 line_read=readline(c); 00087 00088 /* If the line has any text in it, save it on the history. 00089 */ 00090 if (line_read && *line_read) { 00091 add_history(line_read); 00092 } 00093 00094 return(line_read); 00095 } 00096 00097 #endif 00098 00099 }; 00100 00101 #ifndef DOXYGENP 00102 } 00103 #endif 00104 00105 #endif
Documentation generated with Doxygen and provided under the GNU Free Documentation License. See License Information for details.
Project hosting provided by
,
O2scl Sourceforge Project Page