3 * wxbDataParser, class that receives and analyses data
5 * Nicolas Boichat, April-July 2004
10 Copyright (C) 2004 Kern Sibbald and John Walker
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 /* Sleeps during milliseconds (wrapper for wxUsleep (2.4) or wxMilliSleep (2.6)) */
41 void wxbUtils::MilliSleep(unsigned long milliseconds) {
42 #if wxCHECK_VERSION(2, 6, 0)
43 ::wxMilliSleep(milliseconds);
45 ::wxUsleep(milliseconds);
50 void wxbUtils::Init() {
55 void wxbUtils::Reset() {
59 /* Parse a table in tableParser */
60 wxbTableParser* wxbUtils::CreateAndWaitForParser(wxString cmd) {
61 wxbTableParser* tableParser = new wxbTableParser();
63 wxbMainFrame::GetInstance()->Send(cmd);
65 //time_t base = wxDateTime::Now().GetTicks();
66 while (!tableParser->hasFinished()) {
67 //innerThread->Yield();
68 wxTheApp->Yield(true);
69 wxbUtils::MilliSleep(100);
70 //if (base+15 < wxDateTime::Now().GetTicks()) break;
75 /* Run a command, and waits until prompt result is fully received,
76 * if keepresults is true, returns a valid pointer to a wxbPromptParser
77 * containing the data. */
78 wxbPromptParser* wxbUtils::WaitForPrompt(wxString cmd, bool keepresults) {
79 wxbPromptParser* promptParser = new wxbPromptParser();
81 wxbMainFrame::GetInstance()->Send(cmd);
83 //time_t base = wxDateTime::Now().GetTicks();
84 while (!promptParser->hasFinished()) {
85 //innerThread->Yield();
86 wxTheApp->Yield(true);
87 wxbUtils::MilliSleep(100);
88 //if (base+15 < wxDateTime::Now().GetTicks()) break;
100 /* Run a command, and waits until result is fully received. */
101 wxbDataTokenizer* wxbUtils::WaitForEnd(wxString cmd, bool keepresults, bool linebyline) {
102 wxbDataTokenizer* datatokenizer = new wxbDataTokenizer(linebyline);
104 wxbMainFrame::GetInstance()->Send(cmd);
106 //wxbMainFrame::GetInstance()->Print("(<WFE)", CS_DEBUG);
108 //time_t base = wxDateTime::Now().GetTicks();
109 while (!datatokenizer->hasFinished()) {
110 //innerThread->Yield();
111 wxTheApp->Yield(true);
112 wxbUtils::MilliSleep(100);
113 //if (base+15 < wxDateTime::Now().GetTicks()) break;
116 //wxbMainFrame::GetInstance()->Print("(>WFE)", CS_DEBUG);
119 return datatokenizer;
122 delete datatokenizer;
128 /* Creates a new wxbDataParser, and register it in wxbMainFrame */
129 wxbDataParser::wxbDataParser(bool lineanalysis) {
130 wxbMainFrame::GetInstance()->Register(this);
131 this->lineanalysis = lineanalysis;
135 /* Destroy a wxbDataParser, and unregister it in wxbMainFrame */
136 wxbDataParser::~wxbDataParser() {
137 wxbMainFrame::GetInstance()->Unregister(this);
141 * Receives director information, forwarded by wxbMainFrame, and sends it
142 * line by line to the virtual function Analyse.
144 bool wxbDataParser::Print(wxString str, int status) {
147 bool allnewline = true;
148 for (unsigned int i = 0; i < str.Length(); i++) {
149 if (!(allnewline = (str.GetChar(i) == '\n')))
154 ret = Analyse(buffer << "\n", CS_DATA);
156 for (unsigned int i = 1; i < str.Length(); i++) {
157 ret = Analyse("\n", status);
161 wxStringTokenizer tkz(str, "\n",
162 (wxStringTokenizerMode)(wxTOKEN_RET_DELIMS | wxTOKEN_RET_EMPTY | wxTOKEN_RET_EMPTY_ALL));
164 while ( tkz.HasMoreTokens() ) {
165 buffer << tkz.GetNextToken();
166 if (buffer.Length() != 0) {
167 if ((buffer.GetChar(buffer.Length()-1) == '\n') ||
168 (buffer.GetChar(buffer.Length()-1) == '\r')) {
169 ret = Analyse(buffer, status);
176 if (buffer == "$ ") { // Restore console
177 ret = Analyse(buffer, status);
181 if (status != CS_DATA) {
182 if (buffer.Length() != 0) {
183 ret = Analyse(buffer, CS_DATA);
186 ret = Analyse("", status);
190 ret = Analyse(wxString(str), status);
195 /* Creates a new wxbDataTokenizer */
196 wxbDataTokenizer::wxbDataTokenizer(bool linebyline): wxbDataParser(linebyline), wxArrayString() {
200 /* Destroy a wxbDataTokenizer */
201 wxbDataTokenizer::~wxbDataTokenizer() {
206 * Receives director information, forwarded by wxbMainFrame.
208 bool wxbDataTokenizer::Analyse(wxString str, int status) {
209 finished = ((status == CS_END) || (status == CS_PROMPT) || (status == CS_DISCONNECTED));
217 /* Returns true if the last signal received was an end signal,
218 * indicating that no more data is available */
219 bool wxbDataTokenizer::hasFinished() {
223 /* Creates a new wxbDataTokenizer */
224 wxbPromptParser::wxbPromptParser(): wxbDataParser(false) {
233 /* Destroy a wxbDataTokenizer */
234 wxbPromptParser::~wxbPromptParser() {
241 * Receives director information, forwarded by wxbMainFrame.
243 bool wxbPromptParser::Analyse(wxString str, int status) {
244 if (status == CS_DATA) {
245 if (finished || prompt) { /* New question */
259 if (((i = str.Find(": ")) > 0) && (str.Mid(0, i).Trim(false).ToLong(&num))) { /* List element */
261 choices = new wxArrayString();
262 choices->Add(""); /* index 0 is never used by multiple choice questions */
266 if ((long)choices->GetCount() != num) { /* new choice has begun */
268 choices = new wxArrayString();
269 choices->Add("", num); /* fill until this number */
273 choices->Add(str.Mid(i+2).RemoveLast());
275 else if (!choices) { /* Introduction, no list received yet */
276 introStr << questionStr;
277 questionStr = wxString(str);
279 else { /* List receveived, get the question now */
280 introStr << questionStr;
281 questionStr = wxString(str);
285 finished = ((status == CS_PROMPT) || (status == CS_END) || (status == CS_DISCONNECTED));
286 if (prompt = ((status == CS_PROMPT) && (questionStr != "$ "))) { // && (str.Find(": ") == str.Length())
287 if (introStr.Last() == '\n') {
288 introStr.RemoveLast();
290 if ((introStr != "") && (questionStr == "")) {
291 questionStr = introStr;
295 if ((!choices) && (questionStr.Find("(yes/mod/no)") > -1)) {
296 choices = new wxArrayString();
305 else { /* ended or (dis)connected */
318 /* Returns true if the last signal received was an prompt signal,
319 * indicating that the answer must be sent */
320 bool wxbPromptParser::hasFinished() {
324 /* Returns true if the last message received is a prompt message */
325 bool wxbPromptParser::isPrompt() {
329 /* Returns multiple choice question's introduction */
330 wxString wxbPromptParser::getIntroString() {
334 /* Returns question string */
335 wxString wxbPromptParser::getQuestionString() {
339 /* Returns a wxArrayString containing the indexed choices we have
340 * to answer the question, or NULL if this question is not a multiple
342 wxArrayString* wxbPromptParser::getChoices() {
346 /* Returns true if the expected answer to the choice list is a number,
347 * false if it is a string (for example yes/mod/no). */
348 bool wxbPromptParser::isNumericalChoice() {