OpenASIP 2.2
Loading...
Searching...
No Matches
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
41
42/**
43 * Constructor.
44 */
45BaseLineReader::BaseLineReader(std::istream& iStream, std::ostream& oStream) :
46 LineReader(iStream, oStream), iStream_(iStream), oStream_(oStream),
47 promptPrinting_(true) {
48}
49
50/**
51 * Destructor.
52 */
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 */
64void
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 */
81void
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 */
95std::string
96BaseLineReader::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 */
131char
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;
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 */
176void
#define __func__
#define assert(condition)
virtual char charQuestion(std::string question, std::string allowedChars, bool caseSensitive=false, char defaultAnswer='\0')
virtual std::string readLine(std::string prompt="")
std::istream & iStream_
Input stream.
virtual void setPromptPrinting(bool flag)
static const int MAX_LINE_LENGTH
std::string prompt_
Prompt of the reader.
virtual ~BaseLineReader()
std::ostream & oStream_
Output stream.
bool promptPrinting_
Prompt printing flag.
void printPrompt() const
virtual void initialize(std::string defPrompt="", FILE *in=stdin, FILE *out=stdout, FILE *err=stderr)
BaseLineReader(std::istream &iStream=std::cin, std::ostream &oStream=std::cout)
void setInitialized()
bool initialized() const
static bool containsChar(const std::string &source, char ch, bool caseSensitive=true)