OpenASIP  2.0
Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
BaseLineReader Class Reference

#include <BaseLineReader.hh>

Inheritance diagram for BaseLineReader:
Inheritance graph
Collaboration diagram for BaseLineReader:
Collaboration graph

Public Member Functions

 BaseLineReader (std::istream &iStream=std::cin, std::ostream &oStream=std::cout)
 
virtual ~BaseLineReader ()
 
virtual void initialize (std::string defPrompt="", FILE *in=stdin, FILE *out=stdout, FILE *err=stderr)
 
virtual void setPromptPrinting (bool flag)
 
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 Member Functions

 BaseLineReader (const BaseLineReader &)
 Copying not allowed. More...
 
BaseLineReaderoperator= (const BaseLineReader &)
 Assignment not allowed. More...
 
void printPrompt () const
 

Private Attributes

std::string prompt_
 Prompt of the reader. More...
 
FILE * in_
 Input stream. More...
 
FILE * out_
 Output stream. More...
 
FILE * error_
 Error stream. More...
 
std::istream & iStream_
 Input stream. More...
 
std::ostream & oStream_
 Output stream. More...
 
bool promptPrinting_
 Prompt printing flag. More...
 

Static Private Attributes

static const int MAX_LINE_LENGTH = 256
 

Additional Inherited Members

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

Detailed Description

Handles the basic line reading without a use of any fancy libraries.

This LineReader is used only when nothing else is available. This LineReader supports only reading from std::cin and outputing to std::cout.

Definition at line 47 of file BaseLineReader.hh.

Constructor & Destructor Documentation

◆ BaseLineReader() [1/2]

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

Constructor.

Definition at line 45 of file BaseLineReader.cc.

45  :
46  LineReader(iStream, oStream), iStream_(iStream), oStream_(oStream),
47  promptPrinting_(true) {
48 }

◆ ~BaseLineReader()

BaseLineReader::~BaseLineReader ( )
virtual

Destructor.

Definition at line 53 of file BaseLineReader.cc.

53  {
54 }

◆ BaseLineReader() [2/2]

BaseLineReader::BaseLineReader ( const BaseLineReader )
private

Copying not allowed.

Member Function Documentation

◆ charQuestion()

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

User is asked a question, and answer is excepted to be one character.

Parameters
questionQuestion to be asked.
allowedCharsChars allowed in answer.
caseSensitiveIf true answer is treated case sensitive.
defaultAnswerDefault answer for the question.
Returns
The answered character.
Exceptions
ObjectNotInitializedIf LineReader is not initialized.

Implements LineReader.

Definition at line 132 of file BaseLineReader.cc.

134  {
135  if (!initialized()) {
136  std::string method = "BaseLineReader::charQuestion()";
137  std::string msg = "LineReader not initialized.";
138  throw ObjectNotInitialized(__FILE__, __LINE__, method, msg);
139  }
140 
141  std::string origPrompt = prompt_;
142  prompt_ = question;
143  printPrompt();
144 
145  char answer;
146 
147  if (defaultAnswer == '\0') {
148  // default answer not given
149  do {
150  iStream_ >> answer;
151  } while (!StringTools::containsChar(
152  allowedChars, answer, caseSensitive) &&
153  !iStream_.eof() && !iStream_.fail());
154  prompt_ = origPrompt;
155  return answer;
156  } else {
157  // default answer given
158  iStream_ >> answer;
159 
160  if (!StringTools::containsChar(allowedChars, answer, caseSensitive)) {
161  prompt_ = origPrompt;
162  return defaultAnswer;
163  } else {
164  prompt_ = origPrompt;
165  return answer;
166  }
167  }
168 
169  assert(false);
170  return '!';
171 }

References assert, StringTools::containsChar(), LineReader::initialized(), iStream_, printPrompt(), and prompt_.

Here is the call graph for this function:

◆ initialize()

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

Initializes the reader.

Parameters
promptPrompt for the reader.
inInput stream. Not used, input is always cin.
outOutput stream. Not used, output is always cout.
errError stream. Not used, error output is always cerr.

Implements LineReader.

Definition at line 65 of file BaseLineReader.cc.

69  {
70 
71  prompt_ = prompt;
72 
74 }

References prompt_, and LineReader::setInitialized().

Referenced by DesignSpaceExplorer::simulate().

Here is the call graph for this function:

◆ operator=()

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

Assignment not allowed.

◆ printPrompt()

void BaseLineReader::printPrompt ( ) const
private

Prints the prompt.

Definition at line 177 of file BaseLineReader.cc.

177  {
178  oStream_ << prompt_;
179 }

References oStream_, and prompt_.

Referenced by charQuestion(), and readLine().

◆ readLine()

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

Reads a line from the input stream.

Parameters
promptThe prompt to be used.
Returns
The read string.
Exceptions
ObjectNotInitializedIf LineReader is not initialized.
EndOfFileWhen end of file mark is received.
Todo:
Implement detection of end-of-file.

Implements LineReader.

Definition at line 96 of file BaseLineReader.cc.

96  {
97  if (!initialized()) {
98  std::string msg = "LineReader not initialized.";
99  throw ObjectNotInitialized(__FILE__, __LINE__, __func__, msg);
100  }
101 
102  std::string origPrompt = prompt_;
103  prompt_ = prompt;
104 
105  if (promptPrinting_)
106  printPrompt();
107 
108  std::string result;
109  getline(iStream_, result);
110 
111  prompt_ = origPrompt;
112 
113  if (iStream_.eof() || iStream_.fail()) {
114  std::string msg =
115  "End of file from input stream or input stream in bad state.";
116  throw EndOfFile(__FILE__, __LINE__, __func__, msg);
117  }
118  return result;
119 }

References __func__, LineReader::initialized(), iStream_, printPrompt(), prompt_, and promptPrinting_.

Referenced by DesignSpaceExplorer::simulate().

Here is the call graph for this function:

◆ setPromptPrinting()

void BaseLineReader::setPromptPrinting ( bool  flag)
virtual

Disables/enables printing of prompt altogether.

Parameters
flag

Definition at line 82 of file BaseLineReader.cc.

82  {
83  promptPrinting_ = flag;
84 }

References promptPrinting_.

Referenced by DesignSpaceExplorer::simulate().

Member Data Documentation

◆ error_

FILE* BaseLineReader::error_
private

Error stream.

Definition at line 84 of file BaseLineReader.hh.

◆ in_

FILE* BaseLineReader::in_
private

Input stream.

Definition at line 80 of file BaseLineReader.hh.

◆ iStream_

std::istream& BaseLineReader::iStream_
private

Input stream.

Definition at line 86 of file BaseLineReader.hh.

Referenced by charQuestion(), and readLine().

◆ MAX_LINE_LENGTH

const int BaseLineReader::MAX_LINE_LENGTH = 256
staticprivate

Definition at line 75 of file BaseLineReader.hh.

◆ oStream_

std::ostream& BaseLineReader::oStream_
private

Output stream.

Definition at line 88 of file BaseLineReader.hh.

Referenced by printPrompt().

◆ out_

FILE* BaseLineReader::out_
private

Output stream.

Definition at line 82 of file BaseLineReader.hh.

◆ prompt_

std::string BaseLineReader::prompt_
private

Prompt of the reader.

Definition at line 78 of file BaseLineReader.hh.

Referenced by charQuestion(), initialize(), printPrompt(), and readLine().

◆ promptPrinting_

bool BaseLineReader::promptPrinting_
private

Prompt printing flag.

Definition at line 90 of file BaseLineReader.hh.

Referenced by readLine(), and setPromptPrinting().


The documentation for this class was generated from the following files:
StringTools::containsChar
static bool containsChar(const std::string &source, char ch, bool caseSensitive=true)
Definition: StringTools.cc:101
BaseLineReader::oStream_
std::ostream & oStream_
Output stream.
Definition: BaseLineReader.hh:88
BaseLineReader::iStream_
std::istream & iStream_
Input stream.
Definition: BaseLineReader.hh:86
LineReader::setInitialized
void setInitialized()
assert
#define assert(condition)
Definition: Application.hh:86
ObjectNotInitialized
Definition: Exception.hh:640
BaseLineReader::prompt_
std::string prompt_
Prompt of the reader.
Definition: BaseLineReader.hh:78
__func__
#define __func__
Definition: Application.hh:67
LineReader::LineReader
LineReader(std::istream &iStream=std::cin, std::ostream &oStream=std::cout)
Definition: LineReader.cc:48
BaseLineReader::printPrompt
void printPrompt() const
Definition: BaseLineReader.cc:177
LineReader::initialized
bool initialized() const
BaseLineReader::promptPrinting_
bool promptPrinting_
Prompt printing flag.
Definition: BaseLineReader.hh:90
EndOfFile
Definition: Exception.hh:189