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;
41 void wxbUtils::Init() {
46 void wxbUtils::Reset() {
50 /* Parse a table in tableParser */
51 wxbTableParser* wxbUtils::CreateAndWaitForParser(wxString cmd) {
52 wxbTableParser* tableParser = new wxbTableParser();
54 wxbMainFrame::GetInstance()->Send(cmd);
56 //time_t base = wxDateTime::Now().GetTicks();
57 while (!tableParser->hasFinished()) {
58 //innerThread->Yield();
59 wxTheApp->Yield(true);
61 //if (base+15 < wxDateTime::Now().GetTicks()) break;
66 /* Run a command, and waits until prompt result is fully received,
67 * if keepresults is true, returns a valid pointer to a wxbPromptParser
68 * containing the data. */
69 wxbPromptParser* wxbUtils::WaitForPrompt(wxString cmd, bool keepresults) {
70 wxbPromptParser* promptParser = new wxbPromptParser();
72 wxbMainFrame::GetInstance()->Send(cmd);
74 //time_t base = wxDateTime::Now().GetTicks();
75 while (!promptParser->hasFinished()) {
76 //innerThread->Yield();
77 wxTheApp->Yield(true);
79 //if (base+15 < wxDateTime::Now().GetTicks()) break;
91 /* Run a command, and waits until result is fully received. */
92 wxbDataTokenizer* wxbUtils::WaitForEnd(wxString cmd, bool keepresults, bool linebyline) {
93 wxbDataTokenizer* datatokenizer = new wxbDataTokenizer(linebyline);
95 wxbMainFrame::GetInstance()->Send(cmd);
97 //wxbMainFrame::GetInstance()->Print("(<WFE)", CS_DEBUG);
99 //time_t base = wxDateTime::Now().GetTicks();
100 while (!datatokenizer->hasFinished()) {
101 //innerThread->Yield();
102 wxTheApp->Yield(true);
104 //if (base+15 < wxDateTime::Now().GetTicks()) break;
107 //wxbMainFrame::GetInstance()->Print("(>WFE)", CS_DEBUG);
110 return datatokenizer;
113 delete datatokenizer;
119 /* Creates a new wxbDataParser, and register it in wxbMainFrame */
120 wxbDataParser::wxbDataParser(bool lineanalysis) {
121 wxbMainFrame::GetInstance()->Register(this);
122 this->lineanalysis = lineanalysis;
126 /* Destroy a wxbDataParser, and unregister it in wxbMainFrame */
127 wxbDataParser::~wxbDataParser() {
128 wxbMainFrame::GetInstance()->Unregister(this);
132 * Receives director information, forwarded by wxbMainFrame, and sends it
133 * line by line to the virtual function Analyse.
135 bool wxbDataParser::Print(wxString str, int status) {
138 bool allnewline = true;
139 for (unsigned int i = 0; i < str.Length(); i++) {
140 if (!(allnewline = (str.GetChar(i) == '\n')))
145 ret = Analyse(buffer << "\n", CS_DATA);
147 for (unsigned int i = 1; i < str.Length(); i++) {
148 ret = Analyse("\n", status);
152 wxStringTokenizer tkz(str, "\n",
153 (wxStringTokenizerMode)(wxTOKEN_RET_DELIMS | wxTOKEN_RET_EMPTY | wxTOKEN_RET_EMPTY_ALL));
155 while ( tkz.HasMoreTokens() ) {
156 buffer << tkz.GetNextToken();
157 if (buffer.Length() != 0) {
158 if ((buffer.GetChar(buffer.Length()-1) == '\n') ||
159 (buffer.GetChar(buffer.Length()-1) == '\r')) {
160 ret = Analyse(buffer, status);
167 if (buffer == "$ ") { // Restore console
168 ret = Analyse(buffer, status);
172 if (status != CS_DATA) {
173 if (buffer.Length() != 0) {
174 ret = Analyse(buffer, CS_DATA);
177 ret = Analyse("", status);
181 ret = Analyse(wxString(str), status);
186 /* Creates a new wxbDataTokenizer */
187 wxbDataTokenizer::wxbDataTokenizer(bool linebyline): wxbDataParser(linebyline), wxArrayString() {
191 /* Destroy a wxbDataTokenizer */
192 wxbDataTokenizer::~wxbDataTokenizer() {
197 * Receives director information, forwarded by wxbMainFrame.
199 bool wxbDataTokenizer::Analyse(wxString str, int status) {
200 finished = ((status == CS_END) || (status == CS_PROMPT) || (status == CS_DISCONNECTED));
208 /* Returns true if the last signal received was an end signal,
209 * indicating that no more data is available */
210 bool wxbDataTokenizer::hasFinished() {
214 /* Creates a new wxbDataTokenizer */
215 wxbPromptParser::wxbPromptParser(): wxbDataParser(false) {
224 /* Destroy a wxbDataTokenizer */
225 wxbPromptParser::~wxbPromptParser() {
232 * Receives director information, forwarded by wxbMainFrame.
234 bool wxbPromptParser::Analyse(wxString str, int status) {
235 if (status == CS_DATA) {
236 if (finished || prompt) { /* New question */
250 if (((i = str.Find(": ")) > 0) && (str.Mid(0, i).Trim(false).ToLong(&num))) { /* List element */
252 choices = new wxArrayString();
253 choices->Add(""); /* index 0 is never used by multiple choice questions */
257 if ((long)choices->GetCount() != num) { /* new choice has begun */
259 choices = new wxArrayString();
260 choices->Add("", num); /* fill until this number */
264 choices->Add(str.Mid(i+2).RemoveLast());
266 else if (!choices) { /* Introduction, no list received yet */
267 introStr << questionStr;
268 questionStr = wxString(str);
270 else { /* List receveived, get the question now */
271 introStr << questionStr;
272 questionStr = wxString(str);
276 finished = ((status == CS_PROMPT) || (status == CS_END) || (status == CS_DISCONNECTED));
277 if (prompt = ((status == CS_PROMPT) && (questionStr != "$ "))) { // && (str.Find(": ") == str.Length())
278 if (introStr.Last() == '\n') {
279 introStr.RemoveLast();
281 if ((introStr != "") && (questionStr == "")) {
282 questionStr = introStr;
286 if ((!choices) && (questionStr.Find("(yes/mod/no)") > -1)) {
287 choices = new wxArrayString();
296 else { /* ended or (dis)connected */
309 /* Returns true if the last signal received was an prompt signal,
310 * indicating that the answer must be sent */
311 bool wxbPromptParser::hasFinished() {
315 /* Returns true if the last message received is a prompt message */
316 bool wxbPromptParser::isPrompt() {
320 /* Returns multiple choice question's introduction */
321 wxString wxbPromptParser::getIntroString() {
325 /* Returns question string */
326 wxString wxbPromptParser::getQuestionString() {
330 /* Returns a wxArrayString containing the indexed choices we have
331 * to answer the question, or NULL if this question is not a multiple
333 wxArrayString* wxbPromptParser::getChoices() {
337 /* Returns true if the expected answer to the choice list is a number,
338 * false if it is a string (for example yes/mod/no). */
339 bool wxbPromptParser::isNumericalChoice() {