OpenASIP 2.2
Loading...
Searching...
No Matches
Public Member Functions | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | List of all members
EditLineReader Class Reference

#include <EditLineReader.hh>

Inheritance diagram for EditLineReader:
Inheritance graph
Collaboration diagram for EditLineReader:
Collaboration graph

Public Member Functions

 EditLineReader (std::string program="")
 
virtual ~EditLineReader ()
 
virtual void initialize (std::string defPrompt="", FILE *in=stdin, FILE *out=stdout, FILE *err=stderr)
 
virtual std::string readLine (std::string prompt="")
 
virtual char charQuestion (std::string question, std::string allowedChars, bool caseSensitive=false, char defaultAnswer='\0')
 
- Public Member Functions inherited from LineReader
 LineReader (std::istream &iStream=std::cin, std::ostream &oStream=std::cout)
 
virtual ~LineReader ()
 
virtual std::ostream & outputStream ()
 
bool confirmation (std::string question, char defaultAnswer='n', char yesChar='y', char noChar='n')
 
virtual void setSaveInputHistoryToFile (bool flag)
 
virtual bool saveInputHistoryToFile () const
 
virtual void setInputHistoryLog (const std::string &historyFilename)
 
virtual void setInputHistoryLength (std::size_t length)
 
virtual size_t inputHistoryMaxLength () const
 
virtual std::size_t inputsInHistory () const
 
virtual std::string inputHistoryEntry (std::size_t age) const
 

Private Types

typedef std::map< EditLine *, EditLineReader * >::value_type ValType
 value_type for map.
 
typedef std::map< EditLine *, EditLineReader * >::iterator MapIt
 Iterator for map.
 

Private Member Functions

 EditLineReader (const EditLineReader &)
 Copying not allowed.
 
EditLineReaderoperator= (const EditLineReader &)
 Assignment not allowed.
 
char * prompt ()
 
void updateHistory (const char *c)
 

Static Private Member Functions

static char * wrapperToCallPrompt (EditLine *edit)
 

Private Attributes

char * prompt_
 Line reader prompt.
 
std::string program_
 The name of the invocating program.
 
EditLine * editLine_
 EditLine instance.
 
History * history_
 History instance.
 
FILE * in_
 Input stream is saved for end-of-file checking.
 

Static Private Attributes

static std::map< EditLine *, EditLineReader * > lineReaders_
 Map containing all (EditLine*, EditLineReader*) pairs to make prompt printing possible. This map offers to callback function 'wrapperToCallPrompt' a right instance of EditLineReader. This instance then returns the right prompt.
 

Additional Inherited Members

- Protected Member Functions inherited from LineReader
void setInitialized ()
 
bool initialized () const
 
void putInInputHistory (const std::string &inputLine)
 

Detailed Description

LineReader implementation using libedit library.

libedit offers functionality to edit a line as well as command history browsing functionality. In other words, user can edit the line he is writing as well as browse history of commands.

Definition at line 53 of file EditLineReader.hh.

Member Typedef Documentation

◆ MapIt

typedef std::map<EditLine*,EditLineReader*>::iterator EditLineReader::MapIt
private

Iterator for map.

Definition at line 72 of file EditLineReader.hh.

◆ ValType

typedef std::map<EditLine*,EditLineReader*>::value_type EditLineReader::ValType
private

value_type for map.

Definition at line 70 of file EditLineReader.hh.

Constructor & Destructor Documentation

◆ EditLineReader() [1/2]

EditLineReader::EditLineReader ( std::string  program = "")
explicit

Constructor.

Parameters
programThe name of the invocating program.

Definition at line 56 of file EditLineReader.cc.

56 :
57 LineReader(), prompt_(NULL), program_(program), editLine_(NULL),
58 history_(NULL) {
59}
find Finds info of the inner loops in the program
char * prompt_
Line reader prompt.
EditLine * editLine_
EditLine instance.
History * history_
History instance.
std::string program_
The name of the invocating program.

◆ ~EditLineReader()

EditLineReader::~EditLineReader ( )
virtual

Destructor.

Definition at line 64 of file EditLineReader.cc.

64 {
65
67
68 if (history_ != NULL) {
69 history_end(history_);
70 }
71
72 if (editLine_ != NULL) {
73 el_end(editLine_);
74 }
75
76 if (prompt_ != NULL) {
77 delete[] prompt_;
78 }
79}
static std::map< EditLine *, EditLineReader * > lineReaders_
Map containing all (EditLine*, EditLineReader*) pairs to make prompt printing possible....

References editLine_, history_, lineReaders_, and prompt_.

◆ EditLineReader() [2/2]

EditLineReader::EditLineReader ( const EditLineReader )
private

Copying not allowed.

Member Function Documentation

◆ charQuestion()

char EditLineReader::charQuestion ( std::string  question,
std::string  allowedChars,
bool  caseSensitive = false,
char  defaultAnswer = '\0' 
)
virtual

Asks user a question and expects that answer is given as char.

Prompt is temporarily changed to 'question' during the execution of this function.

Parameters
questionThe asked question.
allowedCharsThe chars that are legal answers.
caseSensitiveFlag indicating whether answer is read case sensitive or not.
defaultAnswerThe default answer.
Returns
User given answer.
Exceptions
ObjectNotInitializedIf LineReader is not initialized.

Implements LineReader.

Definition at line 180 of file EditLineReader.cc.

182 {
183 if (!initialized()) {
184 string method = "EditLineReader::charQuestion()";
185 string message = "LineReader not initialized";
186 throw ObjectNotInitialized(__FILE__, __LINE__, method, message);
187 }
188
189 char* oldPrompt = prompt_;
191
192 if (defaultAnswer == '\0') {
193 const char* answer = nullptr;
194 do {
195 int count;
196 answer = el_gets(editLine_, &count);
197 } while (strlen(answer) != 1 &&
198 !StringTools::
199 containsChar(allowedChars, answer[0], caseSensitive));
200
201 updateHistory(answer);
202 delete[] prompt_;
203 prompt_ = oldPrompt;
204 return answer[0];
205 } else {
206 const char* answer = nullptr;
207 int count;
208 assert(editLine_ != nullptr);
209 answer = el_gets(editLine_, &count);
210 if (answer == nullptr) {
211 updateHistory("");
212 delete[] prompt_;
213 prompt_ = oldPrompt;
214 return defaultAnswer;
215 }
216 if (strlen(answer) != 1 &&
217 !StringTools::
218 containsChar(allowedChars, answer[0], caseSensitive)) {
219
220 updateHistory(&defaultAnswer);
221 delete[] prompt_;
222 prompt_ = oldPrompt;
223 return defaultAnswer;
224 } else {
225 updateHistory(answer);
226 delete[] prompt_;
227 prompt_ = oldPrompt;
228 return answer[0];
229 }
230 }
231}
#define assert(condition)
void updateHistory(const char *c)
bool initialized() const
static char * stringToCharPtr(const std::string &source)

References assert, editLine_, LineReader::initialized(), prompt_, StringTools::stringToCharPtr(), and updateHistory().

Here is the call graph for this function:

◆ initialize()

void EditLineReader::initialize ( std::string  defPrompt = "",
FILE *  in = stdin,
FILE *  out = stdout,
FILE *  err = stderr 
)
virtual

Initializes LineReader.

Sets the prompt printing function and initializes history.

Parameters
defPromptThe prompt for the program.
inInput file stream.
outOutput file stream.
errError file stream.

Implements LineReader.

Definition at line 92 of file EditLineReader.cc.

96 {
97
99 // initialize EditLine
100 editLine_ = el_init(program_.c_str(), in, out, err);
101 assert(editLine_!=NULL);
102 // define prompt printing function
103 el_set(editLine_, EL_PROMPT, wrapperToCallPrompt);
104 // bind all keys to the standard GNU emacs-like bindings.
105 //el_set(editLine_, EL_BIND, "-e");
106 el_set(editLine_, EL_EDITOR, "emacs");
107 if (in != stdin) {
108 // when reading from file, editing is set off
109 el_set(editLine_, EL_EDITMODE, 0);
110 }
111 // initialize History
112 HistEvent ev;
113 history_ = history_init();
114 if (history_!=NULL) {
115 // size of the history is set to 100
116 history(history_, &ev, H_SETSIZE, 100);
117 el_set(editLine_, EL_HIST, history, history_);
118 }
119 lineReaders_.insert(ValType(editLine_, this));
120 in_ = in;
122}
std::map< EditLine *, EditLineReader * >::value_type ValType
value_type for map.
FILE * in_
Input stream is saved for end-of-file checking.
static char * wrapperToCallPrompt(EditLine *edit)
void setInitialized()

References assert, editLine_, history_, in_, lineReaders_, program_, prompt_, LineReader::setInitialized(), StringTools::stringToCharPtr(), and wrapperToCallPrompt().

Here is the call graph for this function:

◆ operator=()

EditLineReader & EditLineReader::operator= ( const EditLineReader )
private

Assignment not allowed.

◆ prompt()

char * EditLineReader::prompt ( )
private

Returns the used prompt.

Returns
The prompt.

Definition at line 259 of file EditLineReader.cc.

259 {
260 return prompt_;
261}

References prompt_.

Referenced by readLine(), and wrapperToCallPrompt().

◆ readLine()

string EditLineReader::readLine ( std::string  prompt = "")
virtual

Reads line from input stream.

If prompt is given, it is used as a prompt only for reading this one line.

Parameters
promptThe prompt.
Returns
The string that is read.
Exceptions
ObjectNotInitializedIf LineReader is not initialized.
EndOfFileWhen end of file mark (ctrl-d) is received.

Implements LineReader.

Definition at line 135 of file EditLineReader.cc.

135 {
136 if (!initialized()) {
137 string message = "LineReader not initialized";
138 throw ObjectNotInitialized(__FILE__, __LINE__, __func__, message);
139 }
140
141 char* oldPrompt = NULL;
142 if (prompt != "") {
143 oldPrompt = prompt_;
145 }
146
147 int count;
148 const char* c = el_gets(editLine_, &count);
149
150 bool endOfFile = (c == NULL || strlen(c) == 0 || feof(in_));
151 if (endOfFile) {
152 string message = "End of file.";
153 throw EndOfFile(__FILE__, __LINE__, __func__, message);
154 }
155
156 // add command to history
157 updateHistory(c);
158 if (oldPrompt != NULL) {
159 delete[] prompt_;
160 prompt_ = oldPrompt;
161 }
162 return c;
163}
#define __func__

References __func__, editLine_, in_, LineReader::initialized(), prompt(), prompt_, StringTools::stringToCharPtr(), and updateHistory().

Here is the call graph for this function:

◆ updateHistory()

void EditLineReader::updateHistory ( const char *  newEntry)
private

Inserts a new entry to history.

Only non-empty strings are saved to history.

Parameters
newEntryA new entry to be added.

Definition at line 271 of file EditLineReader.cc.

271 {
272 string entry(newEntry);
273 entry = StringTools::trim(entry);
274 if (entry != "") {
275 HistEvent ev;
276 history(history_, &ev, H_ENTER, newEntry);
277 putInInputHistory(newEntry);
278 }
279}
void putInInputHistory(const std::string &inputLine)
static std::string trim(const std::string &source)

References history_, LineReader::putInInputHistory(), and StringTools::trim().

Referenced by charQuestion(), and readLine().

Here is the call graph for this function:

◆ wrapperToCallPrompt()

char * EditLineReader::wrapperToCallPrompt ( EditLine *  edit)
staticprivate

Static wrapper function that calls function that returns the prompt.

If correct EditLine* instance is not found in the map, an empty string (null char*) is returned.

Parameters
editEditLine which prompt is needed.
Returns
The prompt.

Definition at line 243 of file EditLineReader.cc.

243 {
244
245 MapIt mi = lineReaders_.find(edit);
246 if (mi == lineReaders_.end()) {
247 return NULL;
248 }
249 EditLineReader* reader = (*mi).second;
250 return reader->prompt();
251}
std::map< EditLine *, EditLineReader * >::iterator MapIt
Iterator for map.

References lineReaders_, and prompt().

Referenced by initialize().

Here is the call graph for this function:

Member Data Documentation

◆ editLine_

EditLine* EditLineReader::editLine_
private

EditLine instance.

Definition at line 88 of file EditLineReader.hh.

Referenced by charQuestion(), initialize(), readLine(), and ~EditLineReader().

◆ history_

History* EditLineReader::history_
private

History instance.

Definition at line 90 of file EditLineReader.hh.

Referenced by initialize(), updateHistory(), and ~EditLineReader().

◆ in_

FILE* EditLineReader::in_
private

Input stream is saved for end-of-file checking.

Definition at line 98 of file EditLineReader.hh.

Referenced by initialize(), and readLine().

◆ lineReaders_

map< EditLine *, EditLineReader * > EditLineReader::lineReaders_
staticprivate

Map containing all (EditLine*, EditLineReader*) pairs to make prompt printing possible. This map offers to callback function 'wrapperToCallPrompt' a right instance of EditLineReader. This instance then returns the right prompt.

Definition at line 95 of file EditLineReader.hh.

Referenced by initialize(), wrapperToCallPrompt(), and ~EditLineReader().

◆ program_

std::string EditLineReader::program_
private

The name of the invocating program.

Definition at line 86 of file EditLineReader.hh.

Referenced by initialize().

◆ prompt_

char* EditLineReader::prompt_
private

Line reader prompt.

Definition at line 84 of file EditLineReader.hh.

Referenced by charQuestion(), initialize(), prompt(), readLine(), and ~EditLineReader().


The documentation for this class was generated from the following files: