cli_readline.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2006-2017, 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=".cli_hist", size_t max_size=100) {
61  line_read=0;
62  msize=max_size;
63 
64  histfile=fname;
65  read_history(histfile.c_str());
66  }
67 
68  ~cli_readline() {
69  stifle_history(((int)msize));
70  write_history(histfile.c_str());
71  }
72 
73  /** \brief Function to get a string from the user
74  \nothing
75  */
76  virtual char *cli_gets(const char *c) {
77 
78  /* If the buffer has already been allocated, return the memory to
79  the free pool.
80  */
81  if (line_read) {
82  free(line_read);
83  line_read=(char *)0;
84  }
85 
86  line_read=readline(c);
87 
88  /* If the line has any text in it, save it on the history.
89  */
90  if (line_read && *line_read) {
91  add_history(line_read);
92  }
93 
94  return(line_read);
95  }
96 
97  //#endif
98 
99  };
100 
101 #ifndef DOXYGEN_NO_O2NS
102 }
103 #endif
104 
105 #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:76
size_t msize
Maximum history file size.
Definition: cli_readline.h:56

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