OpenASIP 2.2
Loading...
Searching...
No Matches
LineReader.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 LineReader.cc
26 *
27 * Declaration of LineReader 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 reviewed 2 June 2004 by jm, pj, tr, jn
32 * @note rating: red
33 */
34
35#include <string>
36#include <fstream>
37#include <istream>
38#include <ostream>
39
40#include "LineReader.hh"
41#include "Application.hh"
42
43using std::string;
44
45/**
46 * Constructor.
47 */
48LineReader::LineReader(std::istream& iStream, std::ostream& oStream) :
49 initialized_(false), inputHistorySize_(DEFAULT_INPUT_HISTORY_SIZE),
50 saveHistoryToFile_(false), historyFile_(NULL),
51 historyFilename_("commandhistory.txt"), iStream_(iStream),
52 oStream_(oStream) {
53}
54
55/**
56 * Destructor.
57 */
59 if (historyFile_ != NULL) {
60 historyFile_->close();
61 delete historyFile_;
62 historyFile_ = NULL;
63 }
64}
65
66/**
67 * Asks confirmation from the user.
68 *
69 * Question is asked from user, who should answer with a single character.
70 *
71 * @param question The question that is asked from the user.
72 * @param defaultAnswer The default answer.
73 * @param yesChar Character meaning "yes" answer.
74 * @param noChar Character meaning "no" answer.
75 * @return True, if answer is yesChar, false otherwise.
76 * @exception ObjectNotInitialized If LineReader is not initialized.
77 */
78bool
80 std::string question, char defaultAnswer, char yesChar, char noChar) {
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}
92
93/**
94 * Returns the output stream which can be used to print information to
95 * the user.
96 *
97 * This stream should be used to ask confirmation, etc. from the user.
98 *
99 * @return The output stream (std::cout by default).
100 */
101std::ostream&
105
106/**
107 * Sets whether command history should be also saved to a stream.
108 *
109 * The file name should be set with setInputHistoryLog().
110 *
111 * @param flag Status of this option.
112 */
113void
117
118/**
119 * Sets the output file name to save the command history to.
120 *
121 * @param historyFilename The command log stream.
122 */
123void
124LineReader::setInputHistoryLog(const std::string& historyFilename) {
125
126 if (historyFile_ != NULL) {
127 historyFile_->close();
128 delete historyFile_;
129 historyFile_ = NULL;
130 }
131 historyFilename_ = historyFilename;
132}
133
134/**
135 * Sets the maximum size of the command history log.
136 *
137 * @param length New length for the command history log.
138 */
139void
141 inputHistorySize_ = length;
142}
143
144/**
145 * Returns the count of entries in the input history log.
146 *
147 * @return The count of entries in the log.
148 */
149std::size_t
151 return inputHistory_.size();
152}
153
154
155/**
156 * Returns the string at input history log with the given age.
157 *
158 * In case the given age is 0, the newest entry is returned, if it's 1,
159 * the 2nd newest, and so on.
160 *
161 * @param age The age of the requested history entry. An empty string is
162 * is returned in case there's no entry with the given age.
163 */
164std::string
165LineReader::inputHistoryEntry(std::size_t age) const {
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}
171
172/**
173 * Puts an input to the input history log.
174 *
175 * Also writes the input to the input history log stream, if there's one, and
176 * input history logging to a file is enabled.
177 *
178 * Expects that the terminating '\n' is in place in case it's wanted to be
179 * stored in the log.
180 *
181 * @param inputLine The line to store.
182 */
183void
184LineReader::putInInputHistory(const std::string& inputLine) {
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}
#define debugLog(text)
find Finds info of the inner loops in the false
#define DEFAULT_INPUT_HISTORY_SIZE
virtual void setInputHistoryLength(std::size_t length)
void putInInputHistory(const std::string &inputLine)
virtual ~LineReader()
Definition LineReader.cc:58
bool initialized_
Flag indicating whether LineReader is initialized.
Definition LineReader.hh:95
virtual void setInputHistoryLog(const std::string &historyFilename)
std::string historyFilename_
The filename to write the command history to.
std::ofstream * historyFile_
The output stream to write the command history to.
virtual std::size_t inputsInHistory() const
LineReader(std::istream &iStream=std::cin, std::ostream &oStream=std::cout)
Definition LineReader.cc:48
bool confirmation(std::string question, char defaultAnswer='n', char yesChar='y', char noChar='n')
Definition LineReader.cc:79
virtual void setSaveInputHistoryToFile(bool flag)
bool saveHistoryToFile_
Should the history be appended in a file?
virtual std::ostream & outputStream()
virtual std::string inputHistoryEntry(std::size_t age) const
std::deque< std::string > inputHistory_
The input history.
Definition LineReader.hh:97
virtual char charQuestion(std::string question, std::string allowedChars, bool caseSensitive=false, char defaultAnswer='\0')=0
std::size_t inputHistorySize_
The maximum size for input history.
Definition LineReader.hh:99
std::ostream & oStream_
The output stream.