OpenASIP  2.0
BaseLineReader.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file BaseLineReader.cc
26  *
27  * Definition of BaseLineReader class.
28  *
29  * @author Jussi Nykänen 2004 (nykanen-no.spam-cs.tut.fi)
30  * @author Pekka Jääskeläinen 2005 (pjaaskel-no.spam-cs.tut.fi)
31  * @note rating: red
32  */
33 
34 #include <iostream>
35 
36 #include "BaseLineReader.hh"
37 #include "StringTools.hh"
38 #include "Application.hh"
39 
40 const int BaseLineReader::MAX_LINE_LENGTH = 256;
41 
42 /**
43  * Constructor.
44  */
45 BaseLineReader::BaseLineReader(std::istream& iStream, std::ostream& oStream) :
46  LineReader(iStream, oStream), iStream_(iStream), oStream_(oStream),
47  promptPrinting_(true) {
48 }
49 
50 /**
51  * Destructor.
52  */
54 }
55 
56 /**
57  * Initializes the reader.
58  *
59  * @param prompt Prompt for the reader.
60  * @param in Input stream. Not used, input is always cin.
61  * @param out Output stream. Not used, output is always cout.
62  * @param err Error stream. Not used, error output is always cerr.
63  */
64 void
66  std::string prompt,
67  FILE*,
68  FILE*,
69  FILE*) {
70 
71  prompt_ = prompt;
72 
74 }
75 
76 /**
77  * Disables/enables printing of prompt altogether.
78  *
79  * @param flag
80  */
81 void
83  promptPrinting_ = flag;
84 }
85 
86 /**
87  * Reads a line from the input stream.
88  *
89  * @param prompt The prompt to be used.
90  * @return The read string.
91  * @exception ObjectNotInitialized If LineReader is not initialized.
92  * @exception EndOfFile When end of file mark is received.
93  * @todo Implement detection of end-of-file.
94  */
95 std::string
96 BaseLineReader::readLine(std::string prompt) {
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 }
120 
121 /**
122  * User is asked a question, and answer is excepted to be one character.
123  *
124  * @param question Question to be asked.
125  * @param allowedChars Chars allowed in answer.
126  * @param caseSensitive If true answer is treated case sensitive.
127  * @param defaultAnswer Default answer for the question.
128  * @return The answered character.
129  * @exception ObjectNotInitialized If LineReader is not initialized.
130  */
131 char
133  std::string question, std::string allowedChars, bool caseSensitive,
134  char defaultAnswer) {
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 }
172 
173 /**
174  * Prints the prompt.
175  */
176 void
178  oStream_ << prompt_;
179 }
BaseLineReader.hh
BaseLineReader::setPromptPrinting
virtual void setPromptPrinting(bool flag)
Definition: BaseLineReader.cc:82
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()
BaseLineReader::MAX_LINE_LENGTH
static const int MAX_LINE_LENGTH
Definition: BaseLineReader.hh:75
BaseLineReader::BaseLineReader
BaseLineReader(std::istream &iStream=std::cin, std::ostream &oStream=std::cout)
Definition: BaseLineReader.cc:45
StringTools.hh
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
Application.hh
__func__
#define __func__
Definition: Application.hh:67
BaseLineReader::printPrompt
void printPrompt() const
Definition: BaseLineReader.cc:177
BaseLineReader::readLine
virtual std::string readLine(std::string prompt="")
Definition: BaseLineReader.cc:96
LineReader::initialized
bool initialized() const
BaseLineReader::~BaseLineReader
virtual ~BaseLineReader()
Definition: BaseLineReader.cc:53
LineReader
Definition: LineReader.hh:52
BaseLineReader::promptPrinting_
bool promptPrinting_
Prompt printing flag.
Definition: BaseLineReader.hh:90
EndOfFile
Definition: Exception.hh:189
BaseLineReader::charQuestion
virtual char charQuestion(std::string question, std::string allowedChars, bool caseSensitive=false, char defaultAnswer='\0')
Definition: BaseLineReader.cc:132
BaseLineReader::initialize
virtual void initialize(std::string defPrompt="", FILE *in=stdin, FILE *out=stdout, FILE *err=stderr)
Definition: BaseLineReader.cc:65