cli_readline.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2006-2018, Andrew W. Steiner
5 
6  This file is part of O2scl.
7 
8  O2scl 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 3 of the License, or
11  (at your option) any later version.
12 
13  O2scl 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
19  along with O2scl. If not, see <http://www.gnu.org/licenses/>.
20 
21  -------------------------------------------------------------------
22 */
23 /** \file cli_readline.h
24  \brief File defining \ref o2scl::cli_readline
25 */
26 #ifndef O2SCL_CLI_READLINE_H
27 #define O2SCL_CLI_READLINE_H
28 
29 #include <readline/readline.h>
30 #include <readline/history.h>
31 
32 #include <o2scl/cli.h>
33 
34 #ifndef DOXYGEN_NO_O2NS
35 namespace o2scl {
36 #endif
37 
38  /** \brief An extension to \ref o2scl::cli which uses readline
39 
40  This header-only class requires the GNU <tt>readline</tt>
41  library for use, but is not referenced by \o2 code at the
42  moment to make the library usable without <tt>readline</tt>.
43 
44  */
45  class cli_readline : public cli {
46 
47  protected:
48 
49  /// Buffer for readline
50  char *line_read;
51 
52  /// String containing filename
53  std::string histfile;
54 
55  /// Maximum history file size
56  size_t msize;
57 
58  public:
59 
60  cli_readline(std::string fname="", size_t max_size=100) {
61  line_read=0;
62  msize=max_size;
63 
64  histfile=fname;
65  if (histfile.size()>0) {
66  read_history(histfile.c_str());
67  }
68  }
69 
70  ~cli_readline() {
71  if (histfile.size()>0) {
72  stifle_history(((int)msize));
73  write_history(histfile.c_str());
74  }
75  }
76 
77  /** \brief Set history file
78  */
79  void set_histfile(std::string fname) {
80  histfile=fname;
81  if (histfile.size()>0) {
82  read_history(histfile.c_str());
83  }
84  return;
85  }
86 
87  /** \brief Function to get a string from the user
88  \nothing
89  */
90  virtual char *cli_gets(const char *c) {
91 
92  /* If the buffer has already been allocated, return the memory to
93  the free pool.
94  */
95  if (line_read) {
96  free(line_read);
97  line_read=(char *)0;
98  }
99 
100  line_read=readline(c);
101 
102  /* If the line has any text in it, save it on the history.
103  */
104  if (line_read && *line_read && histfile.size()>0) {
105  add_history(line_read);
106  }
107 
108  return(line_read);
109  }
110 
111  //#endif
112 
113  };
114 
115 #ifndef DOXYGEN_NO_O2NS
116 }
117 #endif
118 
119 #endif
An extension to o2scl::cli which uses readline.
Definition: cli_readline.h:45
The main O<span style=&#39;position: relative; top: 0.3em; font-size: 0.8em&#39;>2</span>scl O$_2$scl names...
Definition: anneal.h:42
Configurable command-line interface.
Definition: cli.h:230
std::string histfile
String containing filename.
Definition: cli_readline.h:53
char * line_read
Buffer for readline.
Definition: cli_readline.h:50
virtual char * cli_gets(const char *c)
Function to get a string from the user.
Definition: cli_readline.h:90
size_t msize
Maximum history file size.
Definition: cli_readline.h:56
void set_histfile(std::string fname)
Set history file.
Definition: cli_readline.h:79

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