3 * wxbDataParser, class that receives and analyses data
5 * Nicolas Boichat, April-July 2004
9 Copyright (C) 2004 Kern Sibbald and John Walker
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License
13 as published by the Free Software Foundation; either version 2
14 of the License, or (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 #include "wxbmainframe.h"
32 #include "wxbtableparser.h"
34 /* A macro named Yield is defined under MinGW */
37 bool wxbUtils::inited = false;
40 void wxbUtils::Init() {
45 void wxbUtils::Reset() {
49 /* Parse a table in tableParser */
50 wxbTableParser* wxbUtils::CreateAndWaitForParser(wxString cmd) {
51 wxbTableParser* tableParser = new wxbTableParser();
53 wxbMainFrame::GetInstance()->Send(cmd);
55 //time_t base = wxDateTime::Now().GetTicks();
56 while (!tableParser->hasFinished()) {
57 //innerThread->Yield();
58 wxTheApp->Yield(true);
60 //if (base+15 < wxDateTime::Now().GetTicks()) break;
65 /* Run a command, and waits until prompt result is fully received,
66 * if keepresults is true, returns a valid pointer to a wxbPromptParser
67 * containing the data. */
68 wxbPromptParser* wxbUtils::WaitForPrompt(wxString cmd, bool keepresults) {
69 wxbPromptParser* promptParser = new wxbPromptParser();
71 wxbMainFrame::GetInstance()->Send(cmd);
73 //time_t base = wxDateTime::Now().GetTicks();
74 while (!promptParser->hasFinished()) {
75 //innerThread->Yield();
76 wxTheApp->Yield(true);
78 //if (base+15 < wxDateTime::Now().GetTicks()) break;
90 /* Run a command, and waits until result is fully received. */
91 wxbDataTokenizer* wxbUtils::WaitForEnd(wxString cmd, bool keepresults, bool linebyline) {
92 wxbDataTokenizer* datatokenizer = new wxbDataTokenizer(linebyline);
94 wxbMainFrame::GetInstance()->Send(cmd);
96 //wxbMainFrame::GetInstance()->Print("(<WFE)", CS_DEBUG);
98 //time_t base = wxDateTime::Now().GetTicks();
99 while (!datatokenizer->hasFinished()) {
100 //innerThread->Yield();
101 wxTheApp->Yield(true);
103 //if (base+15 < wxDateTime::Now().GetTicks()) break;
106 //wxbMainFrame::GetInstance()->Print("(>WFE)", CS_DEBUG);
109 return datatokenizer;
112 delete datatokenizer;
118 /* Creates a new wxbDataParser, and register it in wxbMainFrame */
119 wxbDataParser::wxbDataParser(bool lineanalysis) {
120 wxbMainFrame::GetInstance()->Register(this);
121 this->lineanalysis = lineanalysis;
125 /* Destroy a wxbDataParser, and unregister it in wxbMainFrame */
126 wxbDataParser::~wxbDataParser() {
127 wxbMainFrame::GetInstance()->Unregister(this);
131 * Receives director information, forwarded by wxbMainFrame, and sends it
132 * line by line to the virtual function Analyse.
134 bool wxbDataParser::Print(wxString str, int status) {
137 bool allnewline = true;
138 for (unsigned int i = 0; i < str.Length(); i++) {
139 if (!(allnewline = (str.GetChar(i) == '\n')))
144 ret = Analyse(buffer << "\n", CS_DATA);
146 for (unsigned int i = 1; i < str.Length(); i++) {
147 ret = Analyse("\n", status);
151 wxStringTokenizer tkz(str, "\n",
152 (wxStringTokenizerMode)(wxTOKEN_RET_DELIMS | wxTOKEN_RET_EMPTY | wxTOKEN_RET_EMPTY_ALL));
154 while ( tkz.HasMoreTokens() ) {
155 buffer << tkz.GetNextToken();
156 if (buffer.Length() != 0) {
157 if ((buffer.GetChar(buffer.Length()-1) == '\n') ||
158 (buffer.GetChar(buffer.Length()-1) == '\r')) {
159 ret = Analyse(buffer, status);
166 if (buffer == "$ ") { // Restore console
167 ret = Analyse(buffer, status);
171 if (status != CS_DATA) {
172 if (buffer.Length() != 0) {
173 ret = Analyse(buffer, CS_DATA);
176 ret = Analyse("", status);
180 ret = Analyse(wxString(str), status);
185 /* Creates a new wxbDataTokenizer */
186 wxbDataTokenizer::wxbDataTokenizer(bool linebyline): wxbDataParser(linebyline), wxArrayString() {
190 /* Destroy a wxbDataTokenizer */
191 wxbDataTokenizer::~wxbDataTokenizer() {
196 * Receives director information, forwarded by wxbMainFrame.
198 bool wxbDataTokenizer::Analyse(wxString str, int status) {
199 finished = ((status == CS_END) || (status == CS_PROMPT) || (status == CS_DISCONNECTED));
207 /* Returns true if the last signal received was an end signal,
208 * indicating that no more data is available */
209 bool wxbDataTokenizer::hasFinished() {
213 /* Creates a new wxbDataTokenizer */
214 wxbPromptParser::wxbPromptParser(): wxbDataParser(false) {
223 /* Destroy a wxbDataTokenizer */
224 wxbPromptParser::~wxbPromptParser() {
231 * Receives director information, forwarded by wxbMainFrame.
233 bool wxbPromptParser::Analyse(wxString str, int status) {
234 if (status == CS_DATA) {
235 if (finished || prompt) { /* New question */
249 if (((i = str.Find(": ")) > 0) && (str.Mid(0, i).Trim(false).ToLong(&num))) { /* List element */
251 choices = new wxArrayString();
252 choices->Add(""); /* index 0 is never used by multiple choice questions */
256 if ((long)choices->GetCount() != num) { /* new choice has begun */
258 choices = new wxArrayString();
259 choices->Add("", num); /* fill until this number */
263 choices->Add(str.Mid(i+2).RemoveLast());
265 else if (!choices) { /* Introduction, no list received yet */
266 introStr << questionStr;
267 questionStr = wxString(str);
269 else { /* List receveived, get the question now */
270 introStr << questionStr;
271 questionStr = wxString(str);
275 finished = ((status == CS_PROMPT) || (status == CS_END) || (status == CS_DISCONNECTED));
276 if (prompt = ((status == CS_PROMPT) && (questionStr != "$ "))) { // && (str.Find(": ") == str.Length())
277 if (introStr.Last() == '\n') {
278 introStr.RemoveLast();
280 if ((introStr != "") && (questionStr == "")) {
281 questionStr = introStr;
285 if ((!choices) && (questionStr.Find("(yes/mod/no)") > -1)) {
286 choices = new wxArrayString();
295 else { /* ended or (dis)connected */
308 /* Returns true if the last signal received was an prompt signal,
309 * indicating that the answer must be sent */
310 bool wxbPromptParser::hasFinished() {
314 /* Returns true if the last message received is a prompt message */
315 bool wxbPromptParser::isPrompt() {
319 /* Returns multiple choice question's introduction */
320 wxString wxbPromptParser::getIntroString() {
324 /* Returns question string */
325 wxString wxbPromptParser::getQuestionString() {
329 /* Returns a wxArrayString containing the indexed choices we have
330 * to answer the question, or NULL if this question is not a multiple
332 wxArrayString* wxbPromptParser::getChoices() {
336 /* Returns true if the expected answer to the choice list is a number,
337 * false if it is a string (for example yes/mod/no). */
338 bool wxbPromptParser::isNumericalChoice() {