3 * wxbDataParser, class that receives and analyses data
5 * Nicolas Boichat, April 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"
35 bool wxbUtils::inited = false;
38 void wxbUtils::Init() {
43 void wxbUtils::Reset() {
47 /* Parse a table in tableParser */
48 wxbTableParser* wxbUtils::CreateAndWaitForParser(wxString cmd) {
49 wxbTableParser* tableParser = new wxbTableParser();
51 wxbMainFrame::GetInstance()->Send(cmd);
53 //time_t base = wxDateTime::Now().GetTicks();
54 while (!tableParser->hasFinished()) {
55 //innerThread->Yield();
56 wxTheApp->Yield(true);
58 //if (base+15 < wxDateTime::Now().GetTicks()) break;
63 /* Run a command, and waits until prompt result is fully received,
64 * if keepresults is true, returns a valid pointer to a wxbPromptParser
65 * containing the data. */
66 wxbPromptParser* wxbUtils::WaitForPrompt(wxString cmd, bool keepresults) {
67 wxbPromptParser* promptParser = new wxbPromptParser();
69 wxbMainFrame::GetInstance()->Send(cmd);
71 //time_t base = wxDateTime::Now().GetTicks();
72 while (!promptParser->hasFinished()) {
73 //innerThread->Yield();
74 wxTheApp->Yield(true);
76 //if (base+15 < wxDateTime::Now().GetTicks()) break;
88 /* Run a command, and waits until result is fully received. */
89 wxbDataTokenizer* wxbUtils::WaitForEnd(wxString cmd, bool keepresults, bool linebyline) {
90 wxbDataTokenizer* datatokenizer = new wxbDataTokenizer(linebyline);
92 wxbMainFrame::GetInstance()->Send(cmd);
94 //wxbMainFrame::GetInstance()->Print("(<WFE)", CS_DEBUG);
96 //time_t base = wxDateTime::Now().GetTicks();
97 while (!datatokenizer->hasFinished()) {
98 //innerThread->Yield();
99 wxTheApp->Yield(true);
101 //if (base+15 < wxDateTime::Now().GetTicks()) break;
104 //wxbMainFrame::GetInstance()->Print("(>WFE)", CS_DEBUG);
107 return datatokenizer;
110 delete datatokenizer;
116 /* Creates a new wxbDataParser, and register it in wxbMainFrame */
117 wxbDataParser::wxbDataParser(bool lineanalysis) {
118 wxbMainFrame::GetInstance()->Register(this);
119 this->lineanalysis = lineanalysis;
123 /* Destroy a wxbDataParser, and unregister it in wxbMainFrame */
124 wxbDataParser::~wxbDataParser() {
125 wxbMainFrame::GetInstance()->Unregister(this);
129 * Receives director information, forwarded by wxbMainFrame, and sends it
130 * line by line to the virtual function Analyse.
132 bool wxbDataParser::Print(wxString str, int status) {
135 bool allnewline = true;
136 for (unsigned int i = 0; i < str.Length(); i++) {
137 if (!(allnewline = (str.GetChar(i) == '\n')))
142 ret = Analyse(buffer << "\n", CS_DATA);
144 for (unsigned int i = 1; i < str.Length(); i++) {
145 ret = Analyse("\n", status);
149 wxStringTokenizer tkz(str, "\n",
150 (wxStringTokenizerMode)(wxTOKEN_RET_DELIMS | wxTOKEN_RET_EMPTY | wxTOKEN_RET_EMPTY_ALL));
152 while ( tkz.HasMoreTokens() ) {
153 buffer << tkz.GetNextToken();
154 if (buffer.Length() != 0) {
155 if ((buffer.GetChar(buffer.Length()-1) == '\n') ||
156 (buffer.GetChar(buffer.Length()-1) == '\r')) {
157 ret = Analyse(buffer, status);
164 if (buffer == "$ ") { // Restore console
165 ret = Analyse(buffer, status);
169 if (status != CS_DATA) {
170 if (buffer.Length() != 0) {
171 ret = Analyse(buffer, CS_DATA);
174 ret = Analyse("", status);
178 ret = Analyse(wxString(str), status);
183 /* Creates a new wxbDataTokenizer */
184 wxbDataTokenizer::wxbDataTokenizer(bool linebyline): wxbDataParser(linebyline), wxArrayString() {
188 /* Destroy a wxbDataTokenizer */
189 wxbDataTokenizer::~wxbDataTokenizer() {
194 * Receives director information, forwarded by wxbMainFrame.
196 bool wxbDataTokenizer::Analyse(wxString str, int status) {
197 finished = ((status == CS_END) || (status == CS_PROMPT) || (status == CS_DISCONNECTED));
205 /* Returns true if the last signal received was an end signal,
206 * indicating that no more data is available */
207 bool wxbDataTokenizer::hasFinished() {
211 /* Creates a new wxbDataTokenizer */
212 wxbPromptParser::wxbPromptParser(): wxbDataParser(false) {
220 /* Destroy a wxbDataTokenizer */
221 wxbPromptParser::~wxbPromptParser() {
228 * Receives director information, forwarded by wxbMainFrame.
230 bool wxbPromptParser::Analyse(wxString str, int status) {
231 if (status == CS_DATA) {
232 if (finished || prompt) { /* New question */
245 if (((i = str.Find(": ")) > 0) && (str.Mid(0, i).Trim(false).ToLong(&num))) { /* List element */
247 choices = new wxArrayString();
248 choices->Add(""); /* index 0 is never used by multiple choice questions */
251 if ((long)choices->GetCount() != num) { /* new choice has begun */
253 choices = new wxArrayString(num);
254 choices->Add("", num); /* fill until this number */
257 choices->Add(str.Mid(i+2).RemoveLast());
259 else if (!choices) { /* Introduction, no list received yet */
260 introStr << questionStr;
261 questionStr = wxString(str);
263 else { /* List receveived, get the question now */
264 introStr << questionStr;
265 questionStr = wxString(str);
269 finished = ((status == CS_PROMPT) || (status == CS_END) || (status == CS_DISCONNECTED));
270 if (prompt = ((status == CS_PROMPT) && (questionStr != "$ "))) { // && (str.Find(": ") == str.Length())
271 if ((introStr != "") && (questionStr == "")) {
272 questionStr = introStr;
275 if (introStr.Last() == '\n') {
276 introStr.RemoveLast();
280 else { /* ended or (dis)connected */
292 /* Returns true if the last signal received was an prompt signal,
293 * indicating that the answer must be sent */
294 bool wxbPromptParser::hasFinished() {
298 /* Returns true if the last message received is a prompt message */
299 bool wxbPromptParser::isPrompt() {
303 /* Returns multiple choice question's introduction */
304 wxString wxbPromptParser::getIntroString() {
308 /* Returns question string */
309 wxString wxbPromptParser::getQuestionString() {
313 /* Return a wxArrayString containing the indexed choices we have
314 * to answer the question, or NULL if this question is not a multiple
316 wxArrayString* wxbPromptParser::getChoices() {