OpenASIP  2.0
Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
LineReader Class Referenceabstract

#include <LineReader.hh>

Inheritance diagram for LineReader:
Inheritance graph
Collaboration diagram for LineReader:
Collaboration graph

Public Member Functions

 LineReader (std::istream &iStream=std::cin, std::ostream &oStream=std::cout)
 
virtual ~LineReader ()
 
virtual void initialize (std::string defPrompt="", FILE *in=stdin, FILE *out=stdout, FILE *err=stderr)=0
 
virtual std::string readLine (std::string prompt="")=0
 
virtual std::ostream & outputStream ()
 
virtual char charQuestion (std::string question, std::string allowedChars, bool caseSensitive=false, char defaultAnswer='\0')=0
 
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
 

Protected Member Functions

void setInitialized ()
 
bool initialized () const
 
void putInInputHistory (const std::string &inputLine)
 

Private Member Functions

 LineReader (const LineReader &)
 Copying not allowed. More...
 
LineReaderoperator= (const LineReader &)
 Assignment not allowed. More...
 

Private Attributes

bool initialized_
 Flag indicating whether LineReader is initialized. More...
 
std::deque< std::string > inputHistory_
 The input history. More...
 
std::size_t inputHistorySize_
 The maximum size for input history. More...
 
bool saveHistoryToFile_
 Should the history be appended in a file? More...
 
std::ofstream * historyFile_
 The output stream to write the command history to. More...
 
std::string historyFilename_
 The filename to write the command history to. More...
 
std::istream & iStream_
 The input stream. More...
 
std::ostream & oStream_
 The output stream. More...
 

Detailed Description

Abstract base class for line readers.

LineReader's purpose is to handle reading from command line and to make it possible for the user to edit the input given to program.

Definition at line 52 of file LineReader.hh.

Constructor & Destructor Documentation

◆ LineReader() [1/2]

LineReader::LineReader ( std::istream &  iStream = std::cin,
std::ostream &  oStream = std::cout 
)

Constructor.

Definition at line 48 of file LineReader.cc.

48  :
50  saveHistoryToFile_(false), historyFile_(NULL),
51  historyFilename_("commandhistory.txt"), iStream_(iStream),
52  oStream_(oStream) {
53 }

◆ ~LineReader()

LineReader::~LineReader ( )
virtual

Destructor.

Definition at line 58 of file LineReader.cc.

58  {
59  if (historyFile_ != NULL) {
60  historyFile_->close();
61  delete historyFile_;
62  historyFile_ = NULL;
63  }
64 }

References historyFile_.

◆ LineReader() [2/2]

LineReader::LineReader ( const LineReader )
private

Copying not allowed.

Member Function Documentation

◆ charQuestion()

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

Implemented in ProximLineReader, EditLineReader, and BaseLineReader.

Referenced by confirmation().

◆ confirmation()

bool LineReader::confirmation ( std::string  question,
char  defaultAnswer = 'n',
char  yesChar = 'y',
char  noChar = 'n' 
)

Asks confirmation from the user.

Question is asked from user, who should answer with a single character.

Parameters
questionThe question that is asked from the user.
defaultAnswerThe default answer.
yesCharCharacter meaning "yes" answer.
noCharCharacter meaning "no" answer.
Returns
True, if answer is yesChar, false otherwise.
Exceptions
ObjectNotInitializedIf LineReader is not initialized.

Definition at line 79 of file LineReader.cc.

80  {
81  if (!initialized_) {
82  string method = "LineReader::confirmation()";
83  string message = "LineReader not initialized.";
84  throw ObjectNotInitialized(__FILE__, __LINE__, method, message);
85  }
86 
87  string allowed = string(1, yesChar) + string(1, noChar);
88  char answer = charQuestion(question, allowed, false, defaultAnswer);
89 
90  return (answer == yesChar);
91 }

References charQuestion(), and initialized_.

Here is the call graph for this function:

◆ initialize()

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

◆ initialized()

bool LineReader::initialized ( ) const
protected

◆ inputHistoryEntry()

std::string LineReader::inputHistoryEntry ( std::size_t  age) const
virtual

Returns the string at input history log with the given age.

In case the given age is 0, the newest entry is returned, if it's 1, the 2nd newest, and so on.

Parameters
ageThe age of the requested history entry. An empty string is is returned in case there's no entry with the given age.

Definition at line 165 of file LineReader.cc.

165  {
166  if (inputHistory_.size() == 0 ||
167  age > static_cast<std::size_t>(inputHistory_.size() - 1))
168  return "";
169  return inputHistory_.at(inputHistory_.size() - age - 1);
170 }

References inputHistory_.

Referenced by ConsoleWindow::onInputKey(), ProximCmdHistoryWindow::onSave(), and ProximCmdHistoryWindow::updateCommandList().

◆ inputHistoryMaxLength()

virtual size_t LineReader::inputHistoryMaxLength ( ) const
virtual

◆ inputsInHistory()

std::size_t LineReader::inputsInHistory ( ) const
virtual

Returns the count of entries in the input history log.

Returns
The count of entries in the log.

Definition at line 150 of file LineReader.cc.

150  {
151  return inputHistory_.size();
152 }

References inputHistory_.

Referenced by ConsoleWindow::onInputKey(), ProximCmdHistoryWindow::onSave(), and ProximCmdHistoryWindow::updateCommandList().

◆ operator=()

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

Assignment not allowed.

◆ outputStream()

std::ostream & LineReader::outputStream ( )
virtual

Returns the output stream which can be used to print information to the user.

This stream should be used to ask confirmation, etc. from the user.

Returns
The output stream (std::cout by default).

Reimplemented in ProximLineReader.

Definition at line 102 of file LineReader.cc.

102  {
103  return oStream_;
104 }

References oStream_.

Referenced by SetHistoryFilename::execute(), and SimControlLanguageCommand::outputStream().

◆ putInInputHistory()

void LineReader::putInInputHistory ( const std::string &  inputLine)
protected

Puts an input to the input history log.

Also writes the input to the input history log stream, if there's one, and input history logging to a file is enabled.

Expects that the terminating '
' is in place in case it's wanted to be stored in the log.

Parameters
inputLineThe line to store.

Definition at line 184 of file LineReader.cc.

184  {
185 
186  // make room for the entry in case the input history is full
187  if (inputHistory_.size() >= inputHistorySize_)
188  inputHistory_.pop_front();
189 
190  inputHistory_.push_back(inputLine);
191 
192  if (saveHistoryToFile_) {
193  // initialize the log file in case it hasn't been initialized before
194  if (historyFile_ == NULL) {
195  historyFile_ =
196  new std::ofstream(historyFilename_.c_str(), std::ios::app);
197  if (!historyFile_->is_open()) {
198  debugLog("Could not open history file for writing.");
199  return;
200  }
201  }
202  *historyFile_ << inputLine;
203  }
204 }

References debugLog, historyFile_, historyFilename_, inputHistory_, inputHistorySize_, and saveHistoryToFile_.

Referenced by ProximLineReader::readLine(), and EditLineReader::updateHistory().

◆ readLine()

virtual std::string LineReader::readLine ( std::string  prompt = "")
pure virtual

◆ saveInputHistoryToFile()

virtual bool LineReader::saveInputHistoryToFile ( ) const
virtual

◆ setInitialized()

void LineReader::setInitialized ( )
protected

◆ setInputHistoryLength()

void LineReader::setInputHistoryLength ( std::size_t  length)
virtual

Sets the maximum size of the command history log.

Parameters
lengthNew length for the command history log.

Definition at line 140 of file LineReader.cc.

140  {
141  inputHistorySize_ = length;
142 }

References inputHistorySize_.

Referenced by SetHistorySize::execute().

◆ setInputHistoryLog()

void LineReader::setInputHistoryLog ( const std::string &  historyFilename)
virtual

Sets the output file name to save the command history to.

Parameters
historyFilenameThe command log stream.

Reimplemented in ProximLineReader.

Definition at line 124 of file LineReader.cc.

124  {
125 
126  if (historyFile_ != NULL) {
127  historyFile_->close();
128  delete historyFile_;
129  historyFile_ = NULL;
130  }
131  historyFilename_ = historyFilename;
132 }

References historyFile_, and historyFilename_.

Referenced by SetHistoryFilename::execute(), ProximLineReader::setInputHistoryLog(), and SimulatorCLI::SimulatorCLI().

◆ setSaveInputHistoryToFile()

void LineReader::setSaveInputHistoryToFile ( bool  flag)
virtual

Sets whether command history should be also saved to a stream.

The file name should be set with setInputHistoryLog().

Parameters
flagStatus of this option.

Definition at line 114 of file LineReader.cc.

114  {
115  saveHistoryToFile_ = flag;
116 }

References saveHistoryToFile_.

Referenced by SetHistorySave::execute().

Member Data Documentation

◆ historyFile_

std::ofstream* LineReader::historyFile_
private

The output stream to write the command history to.

Definition at line 103 of file LineReader.hh.

Referenced by putInInputHistory(), setInputHistoryLog(), and ~LineReader().

◆ historyFilename_

std::string LineReader::historyFilename_
private

The filename to write the command history to.

Definition at line 105 of file LineReader.hh.

Referenced by putInInputHistory(), and setInputHistoryLog().

◆ initialized_

bool LineReader::initialized_
private

Flag indicating whether LineReader is initialized.

Definition at line 95 of file LineReader.hh.

Referenced by confirmation().

◆ inputHistory_

std::deque<std::string> LineReader::inputHistory_
private

The input history.

Definition at line 97 of file LineReader.hh.

Referenced by inputHistoryEntry(), inputsInHistory(), and putInInputHistory().

◆ inputHistorySize_

std::size_t LineReader::inputHistorySize_
private

The maximum size for input history.

Definition at line 99 of file LineReader.hh.

Referenced by putInInputHistory(), and setInputHistoryLength().

◆ iStream_

std::istream& LineReader::iStream_
private

The input stream.

Definition at line 107 of file LineReader.hh.

◆ oStream_

std::ostream& LineReader::oStream_
private

The output stream.

Definition at line 109 of file LineReader.hh.

Referenced by outputStream().

◆ saveHistoryToFile_

bool LineReader::saveHistoryToFile_
private

Should the history be appended in a file?

Definition at line 101 of file LineReader.hh.

Referenced by putInInputHistory(), and setSaveInputHistoryToFile().


The documentation for this class was generated from the following files:
LineReader::inputHistory_
std::deque< std::string > inputHistory_
The input history.
Definition: LineReader.hh:97
LineReader::saveHistoryToFile_
bool saveHistoryToFile_
Should the history be appended in a file?
Definition: LineReader.hh:101
LineReader::charQuestion
virtual char charQuestion(std::string question, std::string allowedChars, bool caseSensitive=false, char defaultAnswer='\0')=0
ObjectNotInitialized
Definition: Exception.hh:640
LineReader::historyFilename_
std::string historyFilename_
The filename to write the command history to.
Definition: LineReader.hh:105
LineReader::inputHistorySize_
std::size_t inputHistorySize_
The maximum size for input history.
Definition: LineReader.hh:99
LineReader::oStream_
std::ostream & oStream_
The output stream.
Definition: LineReader.hh:109
LineReader::initialized_
bool initialized_
Flag indicating whether LineReader is initialized.
Definition: LineReader.hh:95
LineReader::iStream_
std::istream & iStream_
The input stream.
Definition: LineReader.hh:107
debugLog
#define debugLog(text)
Definition: Application.hh:95
LineReader::historyFile_
std::ofstream * historyFile_
The output stream to write the command history to.
Definition: LineReader.hh:103
DEFAULT_INPUT_HISTORY_SIZE
#define DEFAULT_INPUT_HISTORY_SIZE
Definition: LineReader.hh:112