3 * wxbDataParser, class that receives and analyses data
5 * Nicolas Boichat, April-July 2004
10 Copyright (C) 2004-2005 Kern Sibbald
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License
14 as published by the Free Software Foundation; either version 2
15 of the License, or (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 #include "wxbmainframe.h"
33 #include "wxbtableparser.h"
35 /* A macro named Yield is defined under MinGW */
38 bool wxbUtils::inited = false;
40 wxString wxbUtils::ConvertToPrintable(wxString& str) {
41 /* FIXME : Unicode support should be added to fix this problem */
42 #if needed // wxUSE_UNICODE == 0
44 /* Convert the string to something printable without unicode */
45 for (unsigned int i = 0; i < strnew.Length(); i++) {
46 /* If the character is not ASCII, print a ? */
47 if (((unsigned char)strnew[i] > (unsigned char)127)) {
57 /* Sleeps during milliseconds (wrapper for wxUsleep (2.4) or wxMilliSleep (2.6)) */
58 void wxbUtils::MilliSleep(unsigned long milliseconds) {
59 #if wxCHECK_VERSION(2, 6, 0)
60 ::wxMilliSleep(milliseconds);
62 ::wxUsleep(milliseconds);
67 void wxbUtils::Init() {
72 void wxbUtils::Reset() {
76 /* Parse a table in tableParser */
77 wxbTableParser* wxbUtils::CreateAndWaitForParser(wxString cmd) {
78 wxbTableParser* tableParser = new wxbTableParser();
80 wxbMainFrame::GetInstance()->Send(cmd);
82 //time_t base = wxDateTime::Now().GetTicks();
83 while (!tableParser->hasFinished()) {
84 //innerThread->Yield();
85 wxTheApp->Yield(true);
86 wxbUtils::MilliSleep(100);
87 //if (base+15 < wxDateTime::Now().GetTicks()) break;
92 /* Run a command, and waits until prompt result is fully received,
93 * if keepresults is true, returns a valid pointer to a wxbPromptParser
94 * containing the data. */
95 wxbPromptParser* wxbUtils::WaitForPrompt(wxString cmd, bool keepresults) {
96 wxbPromptParser* promptParser = new wxbPromptParser();
98 wxbMainFrame::GetInstance()->Send(cmd);
100 //time_t base = wxDateTime::Now().GetTicks();
101 while (!promptParser->hasFinished()) {
102 //innerThread->Yield();
103 wxTheApp->Yield(true);
104 wxbUtils::MilliSleep(100);
105 //if (base+15 < wxDateTime::Now().GetTicks()) break;
117 /* Run a command, and waits until result is fully received. */
118 wxbDataTokenizer* wxbUtils::WaitForEnd(wxString cmd, bool keepresults, bool linebyline) {
119 wxbDataTokenizer* datatokenizer = new wxbDataTokenizer(linebyline);
121 wxbMainFrame::GetInstance()->Send(cmd);
123 //wxbMainFrame::GetInstance()->Print("(<WFE)", CS_DEBUG);
125 //time_t base = wxDateTime::Now().GetTicks();
126 while (!datatokenizer->hasFinished()) {
127 //innerThread->Yield();
128 wxTheApp->Yield(true);
129 wxbUtils::MilliSleep(100);
130 //if (base+15 < wxDateTime::Now().GetTicks()) break;
133 //wxbMainFrame::GetInstance()->Print("(>WFE)", CS_DEBUG);
136 return datatokenizer;
139 delete datatokenizer;
145 /* Creates a new wxbDataParser, and register it in wxbMainFrame */
146 wxbDataParser::wxbDataParser(bool lineanalysis) {
147 wxbMainFrame::GetInstance()->Register(this);
148 this->lineanalysis = lineanalysis;
152 /* Destroy a wxbDataParser, and unregister it in wxbMainFrame */
153 wxbDataParser::~wxbDataParser() {
154 wxbMainFrame::GetInstance()->Unregister(this);
158 * Receives director information, forwarded by wxbMainFrame, and sends it
159 * line by line to the virtual function Analyse.
161 bool wxbDataParser::Print(wxString str, int status) {
164 bool allnewline = true;
165 for (unsigned int i = 0; i < str.Length(); i++) {
166 if (!(allnewline = (str.GetChar(i) == '\n')))
171 ret = Analyse(buffer << wxT("\n"), CS_DATA);
173 for (unsigned int i = 1; i < str.Length(); i++) {
174 ret = Analyse(wxT("\n"), status);
178 wxStringTokenizer tkz(str, wxT("\n"),
179 (wxStringTokenizerMode)(wxTOKEN_RET_DELIMS | wxTOKEN_RET_EMPTY | wxTOKEN_RET_EMPTY_ALL));
181 while ( tkz.HasMoreTokens() ) {
182 buffer << tkz.GetNextToken();
183 if (buffer.Length() != 0) {
184 if ((buffer.GetChar(buffer.Length()-1) == '\n') ||
185 (buffer.GetChar(buffer.Length()-1) == '\r')) {
186 ret = Analyse(buffer, status);
193 if (buffer == wxT("$ ")) { // Restore console
194 ret = Analyse(buffer, status);
198 if (status != CS_DATA) {
199 if (buffer.Length() != 0) {
200 ret = Analyse(buffer, CS_DATA);
203 ret = Analyse(wxT(""), status);
207 ret = Analyse(wxString(str), status);
212 /* Creates a new wxbDataTokenizer */
213 wxbDataTokenizer::wxbDataTokenizer(bool linebyline): wxbDataParser(linebyline), wxArrayString() {
217 /* Destroy a wxbDataTokenizer */
218 wxbDataTokenizer::~wxbDataTokenizer() {
223 * Receives director information, forwarded by wxbMainFrame.
225 bool wxbDataTokenizer::Analyse(wxString str, int status) {
226 finished = ((status == CS_END) || (status == CS_PROMPT) || (status == CS_DISCONNECTED));
228 if (str != wxT("")) {
234 /* Returns true if the last signal received was an end signal,
235 * indicating that no more data is available */
236 bool wxbDataTokenizer::hasFinished() {
240 /* Creates a new wxbDataTokenizer */
241 wxbPromptParser::wxbPromptParser(): wxbDataParser(false) {
247 questionStr = wxT("");
250 /* Destroy a wxbDataTokenizer */
251 wxbPromptParser::~wxbPromptParser() {
258 * Receives director information, forwarded by wxbMainFrame.
260 bool wxbPromptParser::Analyse(wxString str, int status) {
261 if (status == CS_DATA) {
262 if (finished || prompt) { /* New question */
270 questionStr = wxT("");
276 if (((i = str.Find(wxT(": "))) > 0) && (str.Mid(0, i).Trim(false).ToLong(&num))) { /* List element */
278 choices = new wxArrayString();
279 choices->Add(wxT("")); /* index 0 is never used by multiple choice questions */
283 if ((long)choices->GetCount() != num) { /* new choice has begun */
285 choices = new wxArrayString();
286 choices->Add(wxT(""), num); /* fill until this number */
290 choices->Add(str.Mid(i+2).RemoveLast());
292 else if (!choices) { /* Introduction, no list received yet */
293 introStr << questionStr;
294 questionStr = wxString(str);
296 else { /* List receveived, get the question now */
297 introStr << questionStr;
298 questionStr = wxString(str);
302 finished = ((status == CS_PROMPT) || (status == CS_END) || (status == CS_DISCONNECTED));
303 if (prompt = ((status == CS_PROMPT) && (questionStr != wxT("$ ")))) { // && (str.Find(": ") == str.Length())
304 if (introStr.Last() == '\n') {
305 introStr.RemoveLast();
307 if ((introStr != wxT("")) && (questionStr == wxT(""))) {
308 questionStr = introStr;
312 if ((!choices) && (questionStr.Find(wxT("(yes/mod/no)")) > -1)) {
313 choices = new wxArrayString();
314 choices->Add(wxT("yes"));
315 choices->Add(wxT("mod"));
316 choices->Add(wxT("no"));
322 else { /* ended or (dis)connected */
328 questionStr = wxT("");
335 /* Returns true if the last signal received was an prompt signal,
336 * indicating that the answer must be sent */
337 bool wxbPromptParser::hasFinished() {
341 /* Returns true if the last message received is a prompt message */
342 bool wxbPromptParser::isPrompt() {
346 /* Returns multiple choice question's introduction */
347 wxString wxbPromptParser::getIntroString() {
351 /* Returns question string */
352 wxString wxbPromptParser::getQuestionString() {
356 /* Returns a wxArrayString containing the indexed choices we have
357 * to answer the question, or NULL if this question is not a multiple
359 wxArrayString* wxbPromptParser::getChoices() {
363 /* Returns true if the expected answer to the choice list is a number,
364 * false if it is a string (for example yes/mod/no). */
365 bool wxbPromptParser::isNumericalChoice() {