OpenASIP  2.0
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 
43 const 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  */
75 void
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  */
97 std::string
98 ProximLineReader::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  */
130 void
131 ProximLineReader::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  */
147 char
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  */
184 std::ostream&
186  return *outputStream_;
187 }
188 
189 
190 /**
191  *
192  */
193 void
194 ProximLineReader::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  */
215 }
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  */
238  sync();
239 }
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  */
247 int
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  */
268 int
270  flushBuffer();
271  return 0;
272 }
273 
274 
275 /**
276  * Sends the buffer contents to the linereader.
277  */
278 void
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  */
298 void
299 ProximLineReader::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  */
309 std::string
311  return historyFile_;
312 }
313 
ProximLROutputBuffer::flushBuffer
void flushBuffer()
Definition: ProximLineReader.cc:279
ProximLineReader::DEFAULT_LOG_FILE_NAME
static const std::string DEFAULT_LOG_FILE_NAME
Definition: ProximLineReader.hh:106
ProximLROutputBuffer::~ProximLROutputBuffer
virtual ~ProximLROutputBuffer()
Definition: ProximLineReader.cc:237
ProximLineReader::historyFile_
std::string historyFile_
Name of the input history file.
Definition: ProximLineReader.hh:104
ProximLROutputBuffer
Definition: ProximLineReader.hh:116
ProximLROutputBuffer::lineReader_
ProximLineReader * lineReader_
Definition: ProximLineReader.hh:125
ProximLROutputBuffer::ProximLROutputBuffer
ProximLROutputBuffer(ProximLineReader *lineReader)
Definition: ProximLineReader.cc:223
SimulatorEvent.hh
ProximLROutputBuffer::BUFFER_SIZE
const unsigned int BUFFER_SIZE
Definition: ProximLineReader.hh:126
ProximLineReader::outputStream_
std::ostream * outputStream_
Output stream which converts the output to SimulatorEvents.
Definition: ProximLineReader.hh:101
ProximLROutputBuffer::sync
int sync()
Definition: ProximLineReader.cc:269
OperationGlobals::setOutputStream
static void setOutputStream(std::ostream &newOutputStream)
Definition: OperationGlobals.cc:59
LineReader::putInInputHistory
void putInInputHistory(const std::string &inputLine)
Definition: LineReader.cc:184
ProximLineReader.hh
ProximLineReader::inputQueue_
std::queue< std::string > inputQueue_
Input queue.
Definition: ProximLineReader.hh:97
LineReader::setInputHistoryLog
virtual void setInputHistoryLog(const std::string &historyFilename)
Definition: LineReader.cc:124
LineReader::setInitialized
void setInitialized()
ProximSimulationThread.hh
Proxim.hh
ProximLineReader::input
void input(std::string command)
Definition: ProximLineReader.cc:131
ProximLROutputStream
Definition: ProximLineReader.hh:134
SimulatorEvent::EVT_SIMULATOR_OUTPUT
static const wxEventType EVT_SIMULATOR_OUTPUT
Textual output event from simulator interpreter.
Definition: SimulatorEvent.hh:60
ProximLineReader::setInputHistoryLog
virtual void setInputHistoryLog(const std::string &historyFilename)
Definition: ProximLineReader.cc:299
ProximLineReader::ProximLineReader
ProximLineReader()
Definition: ProximLineReader.cc:48
ObjectNotInitialized
Definition: Exception.hh:640
Conversion.hh
ProximLineReader::~ProximLineReader
virtual ~ProximLineReader()
Definition: ProximLineReader.cc:61
ProximLineReader::inputHistoryFilename
std::string inputHistoryFilename() const
Definition: ProximLineReader.cc:310
ProximLROutputBuffer::overflow
int overflow(int)
Definition: ProximLineReader.cc:248
ProximLineReader::charQuestion
virtual char charQuestion(std::string question, std::string allowedChars, bool caseSensitive=false, char defaultAnswer='\0')
Definition: ProximLineReader.cc:148
ProximLineReader
Definition: ProximLineReader.hh:60
ProximLineReader::initialize
virtual void initialize(std::string defPrompt="", FILE *in=stdin, FILE *out=stdout, FILE *err=stderr)
Definition: ProximLineReader.cc:76
ProximLineReader::output
void output(std::string text)
Definition: ProximLineReader.cc:194
ProximLineReader::gui_
wxEvtHandler * gui_
GUI to send the events to.
Definition: ProximLineReader.hh:99
LineReader::initialized
bool initialized() const
ProximLineReader::outputStream
virtual std::ostream & outputStream()
Definition: ProximLineReader.cc:185
ProximLineReader::readLine
virtual std::string readLine(std::string prompt="")
Definition: ProximLineReader.cc:98
ProximLROutputStream::ProximLROutputStream
ProximLROutputStream(ProximLineReader *lineReader)
Definition: ProximLineReader.cc:205
OperationBehavior.hh
ProximLineReader::mutex_
wxMutex * mutex_
Definition: ProximLineReader.hh:91
LineReader
Definition: LineReader.hh:52
ProximLROutputStream::~ProximLROutputStream
virtual ~ProximLROutputStream()
Definition: ProximLineReader.cc:214
WxConversion.hh
SimulatorEvent
Definition: SimulatorEvent.hh:42
ProximLineReader::prompt_
std::string prompt_
Input prompt.
Definition: ProximLineReader.hh:95
ProximLineReader::input_
wxCondition * input_
Condition, which is signaled when user input is received from the GUI.
Definition: ProximLineReader.hh:93
OperationGlobals.hh