OpenASIP 2.2
Loading...
Searching...
No Matches
ProximLineReader.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 ProximLineReader.hh
26 *
27 * Implementation of ProximLineReader class.
28 *
29 * @author Veli-Pekka Jääskeläinen 2005 (vjaaskel-no.spam-cs.tut.fi)
30 * @note rating: red
31 */
32
33#include <iostream>
34#include "ProximLineReader.hh"
35#include "WxConversion.hh"
36#include "Proxim.hh"
37#include "SimulatorEvent.hh"
39#include "Conversion.hh"
40#include "OperationBehavior.hh"
41#include "OperationGlobals.hh"
42
43const std::string ProximLineReader::DEFAULT_LOG_FILE_NAME = ".proximHistory";
44
45/**
46 * The Constructor.
47 */
49 LineReader(),
50 mutex_(new wxMutex()),
51 input_(new wxCondition(*mutex_)),
52 outputStream_(new ProximLROutputStream(this)) {
53
55}
56
57
58/**
59 * The Destructor.
60 */
62 mutex_->Unlock();
63 delete mutex_;
64 delete input_;
65}
66
67
68/**
69 * Initializes the linereader.
70 *
71 * The FILE* pointer parameters specified in the base class are not used.
72 *
73 * @param defPrompt Default prompt.
74 */
75void
77 std::string defPrompt, FILE*, FILE*, FILE*) {
78
79 gui_ = wxGetApp().GetTopWindow();
80 prompt_ = defPrompt;
82
83 // Set the linereader output stream as OSAL output stream.
85}
86
87
88/**
89 * Waits for input from the GUI thread.
90 *
91 * The input_ condition timeout is set as 100ms. After the timeout
92 * an empty input is returned. This is done because the worker thread
93 * needs to check its stop and delete requests at regular intervals.
94 *
95 * @param prompt Prompt used for requesting the user input.
96 */
97std::string
98ProximLineReader::readLine(std::string prompt) {
99 if (!initialized()) {
100 std::string method = "ProximLineReader::readLine";
101 throw ObjectNotInitialized(__FILE__, __LINE__, method);
102 }
103
104 if (prompt == "") {
105 prompt = prompt_;
106 }
107
108 if (inputQueue_.empty()) {
109 output(prompt);
110 mutex_->TryLock();
111 input_->Wait();
112 }
113 std::string input = inputQueue_.front();
114 inputQueue_.pop();
115
116 output(input + "\n");
117
118 // Append read input to the command history.
120 return input;
121}
122
123/**
124 * This function is called by the GUI thread to set the user input.
125 *
126 * The input_ condtion is signaled about the user input.
127 *
128 * @param command The user input.
129 */
130void
131ProximLineReader::input(std::string input) {
132 inputQueue_.push(input);
133 // Signal simulation about the input.
134 input_->Signal();
135}
136
137
138/**
139 * Requests answer to a char question from the GUI thread.
140 *
141 * @param question Question to ask from the user.
142 * @param allowedChars Allowed answers.
143 * @param caseSensitive True, if the answer characters are case sensitive.
144 * @param defaultAnswer Default answer to the question.
145 * @return Answer to the char question.
146 */
147char
149 std::string question, std::string allowedChars,
150 bool /* (case sensitive) UNUSED */, char /* (default answer) UNUSED */) {
151 if (!initialized()) {
152 throw ObjectNotInitialized(__FILE__, __LINE__);
153 }
154
155
156 // Char question answer is requested from the GUI thread until
157 // a valid answer is received.
158 std::string answer;
159 do {
160 if (inputQueue_.empty()) {
161 output(question + " [" + allowedChars + "]? ");
162 input_->Wait();
163 }
164 answer = inputQueue_.front();
165 inputQueue_.pop();
166
167 output(answer + "\n");
168
169 if (answer.length() != 1) {
170 continue;
171 }
172 } while (allowedChars.find(answer, 0) == std::string::npos);
173
174 return answer[0];
175}
176
177/**
178 * Returns an outputstream, which the worker thread can use to write text
179 * to the GUI.
180 *
181 * @return Reference to an output stream, which can be utilized to write text
182 * to the GUI.
183 */
184std::ostream&
188
189
190/**
191 *
192 */
193void
194ProximLineReader::output(std::string output) {
196 wxPostEvent(gui_, event);
197}
198
199
200/**
201 * Constructor.
202 *
203 * @param lineReader ProximLineReader used for output.
204 */
206 ProximLineReader* lineReader):
207 std::ostream(new ProximLROutputBuffer(lineReader)) {
208}
209
210
211/**
212 * The Destructor.
213 */
216
217
218/**
219 * The Constructor.
220 *
221 * @param lineReader ProximLineReader used for output.
222 */
224 std::streambuf(),
225 lineReader_(lineReader),
226 BUFFER_SIZE(1024) {
227
228 char* ptr = new char[BUFFER_SIZE];
229 setp(ptr, ptr + BUFFER_SIZE);
230 setg(0, 0, 0);
231}
232
233
234/**
235 * The Destructor.
236 */
240
241/**
242 * Puts a character at current put position.
243 *
244 * This function is called in case there is no room in the buffer to perform
245 * the output operation.
246 */
247int
249
250 flushBuffer();
251
252 if (c != EOF) {
253 if(pbase() == epptr()) {
254 // The buffer length is zero, character is sent directly to the
255 // linereader.
256 lineReader_->output(std::string("") + (char)c);
257 } else {
258 // Append char to the flushed buffer.
259 sputc(c);
260 }
261 }
262 return 0;
263}
264
265/**
266 * Synchronizes the streambuffer by flushing buffer contents to the linereader.
267 */
268int
270 flushBuffer();
271 return 0;
272}
273
274
275/**
276 * Sends the buffer contents to the linereader.
277 */
278void
280 if (pbase() != pptr()) {
281 // Buffer is not empty.
282 int len = pptr() - pbase();
283 char* buffer = new char[len+1];
284 strncpy(buffer, pbase(), len);
285 buffer[len] = '\0';
286 lineReader_->output(std::string(buffer));
287 setp(pbase(), epptr());
288 delete[] buffer;
289 }
290}
291
292
293/**
294 * Sets the name of the input history log file.
295 *
296 * @param hisoryFileName Name of the input history log file.
297 */
298void
299ProximLineReader::setInputHistoryLog(const std::string& historyFileName) {
300 historyFile_ = historyFileName;
301 LineReader::setInputHistoryLog(historyFileName);
302}
303
304/**
305 * Returns input history log file name.
306 *
307 * @return Name of the input history log file.
308 */
309std::string
313
void putInInputHistory(const std::string &inputLine)
virtual void setInputHistoryLog(const std::string &historyFilename)
void setInitialized()
bool initialized() const
static void setOutputStream(std::ostream &newOutputStream)
const unsigned int BUFFER_SIZE
ProximLineReader * lineReader_
ProximLROutputBuffer(ProximLineReader *lineReader)
ProximLROutputStream(ProximLineReader *lineReader)
wxCondition * input_
Condition, which is signaled when user input is received from the GUI.
virtual std::ostream & outputStream()
std::string prompt_
Input prompt.
virtual void initialize(std::string defPrompt="", FILE *in=stdin, FILE *out=stdout, FILE *err=stderr)
std::ostream * outputStream_
Output stream which converts the output to SimulatorEvents.
void input(std::string command)
wxEvtHandler * gui_
GUI to send the events to.
virtual void setInputHistoryLog(const std::string &historyFilename)
virtual std::string readLine(std::string prompt="")
std::queue< std::string > inputQueue_
Input queue.
void output(std::string text)
std::string historyFile_
Name of the input history file.
std::string inputHistoryFilename() const
virtual char charQuestion(std::string question, std::string allowedChars, bool caseSensitive=false, char defaultAnswer='\0')
static const std::string DEFAULT_LOG_FILE_NAME
virtual ~ProximLineReader()
static const wxEventType EVT_SIMULATOR_OUTPUT
Textual output event from simulator interpreter.