Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
JShell.hh
Go to the documentation of this file.
1#ifndef __JSYSTEM__JSHELL__
2#define __JSYSTEM__JSHELL__
3
4#include <string>
5#include <istream>
6#include <ostream>
7
8#include "JSystem/JProcess.hh"
10#include "JLang/JNullStream.hh"
12
13
14/**
15 * \file
16 * Shell interaction via I/O streams.
17 * \author mdejong
18 */
19namespace JSYSTEM {}
20namespace JPP { using namespace JSYSTEM; }
21
22namespace JSYSTEM {
23
26 using JLANG::null;
27
28
29 /**
30 * The JShell clas can be used to interact with the shell via I/O streams.
31 */
32 class JShell :
33 public JProcess,
34 public JFileStream,
36 {
37 public:
38
39 using std::istream::get;
40 using JAbstractObjectStatus::operator bool;
41
42 /**
43 * Constructor.
44 *
45 * \param size size of internal buffer
46 */
47 JShell(const std::size_t size = 65536) :
48 JProcess(),
50 {
51 using namespace std;
52
53 prompt = "_abc321_";
54
55 static_cast<ostream&>(*this) << "set prompt=" << prompt << endl;
56 static_cast<ostream&>(*this) << "echo hello" << endl;
57
58 flush();
59 }
60
61
62 /**
63 * Destructor.
64 */
66 {
67 this->exit();
68 }
69
70
71 /**
72 * Get reference to unique instance of this class object.
73 *
74 * \return reference to this class object
75 */
77 {
78 static JShell shell;
79
80 return shell;
81 }
82
83
84 /**
85 * Get prompt.
86 *
87 * \return prompt
88 */
89 const std::string& getPrompt() const
90 {
91 return prompt;
92 }
93
94
95 /**
96 * Get value.
97 *
98 * Note that
99 * - in case of success, the input stream is flushed; and
100 * - in case of failure, the input stream is cleared and flushed.
101 *
102 * \param value value
103 * \return true if valid; else false
104 */
105 template<class T>
106 inline bool get(T& value)
107 {
108 using namespace std;
109
110 if (*this >> value) {
111
112 this->flush();
113
114 return true;
115
116 } else {
117
118 static_cast<istream&>(*this).clear();
119
120 this->flush();
121
122 return false;
123 }
124 }
125
126
127 /**
128 * Get line of text.
129 *
130 * Extracts characters from this shell and stores them into the given buffer
131 * until the end of line character is found or the prompt is read.
132 *
133 * \param buffer buffer
134 * \param eol end of line character
135 * \return true if valid; else false
136 */
137 inline bool getline(std::string& buffer, const char eol = '\n')
138 {
139 using namespace std;
140
141 buffer.clear();
142
143 while (this->getStatus()) {
144
145 const int c = this->get();
146
147 if (c == streambuf::traits_type::eof()) {
148
149 return !buffer.empty();
150
151 } else if (c == eol) {
152
153 return true;
154
155 } else {
156
157 buffer += (char) c;
158
159 if (buffer.size() >= prompt.size()) {
160
161 const size_t pos = buffer.size() - this->prompt.size();
162
163 if (buffer.substr(pos) == this->prompt) {
164
165 buffer.erase(pos);
166
167 return !buffer.empty();
168 }
169 }
170 }
171 }
172
173 return false;
174 }
175
176
177 /**
178 * Extracts characters from this shell and flush them to the given output stream
179 * until the prompt is read.
180 *
181 * \param out output stream
182 * \return this shell
183 */
184 JShell& flush(std::ostream& out = null)
185 {
186 using namespace std;
187
188 for (string buffer; this->getline(buffer); ) {
189 out << buffer << endl;
190 }
191
192 return *this;
193 }
194
195
196 /**
197 * Get status of this shell.
198 *
199 * \return status of this shell
200 */
201 virtual bool getStatus() const override
202 {
203 return ((bool) static_cast<const std::ostream&>(*this) &&
204 (bool) static_cast<const std::istream&>(*this));
205 }
206
207
208 /**
209 * Exit this shell.
210 */
211 void exit()
212 {
213 using namespace std;
214
215 static_cast<ostream&>(*this) << "exit" << endl;
216 }
217
218
219 protected:
220 std::string prompt;
221
222 private:
223 JShell(const JShell&);
227 };
228}
229
230#endif
int flush()
Flush internal buffer.
Streaming of input and output.
Streaming of input and output from Linux command.
Definition JProcess.hh:30
The JShell clas can be used to interact with the shell via I/O streams.
Definition JShell.hh:36
JShell & operator=(JShell &&)
JShell(JShell &&)
JShell & operator=(const JShell &)
JShell & flush(std::ostream &out=null)
Extracts characters from this shell and flush them to the given output stream until the prompt is rea...
Definition JShell.hh:184
void exit()
Exit this shell.
Definition JShell.hh:211
static JShell & getInstance()
Get reference to unique instance of this class object.
Definition JShell.hh:76
~JShell()
Destructor.
Definition JShell.hh:65
std::string prompt
Definition JShell.hh:220
JShell(const std::size_t size=65536)
Constructor.
Definition JShell.hh:47
const std::string & getPrompt() const
Get prompt.
Definition JShell.hh:89
bool getline(std::string &buffer, const char eol='\n')
Get line of text.
Definition JShell.hh:137
bool get(T &value)
Get value.
Definition JShell.hh:106
JShell(const JShell &)
virtual bool getStatus() const override
Get status of this shell.
Definition JShell.hh:201
static JNullStream null
Null I/O stream.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for operating system calls.
Interface for status of object.