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 /* Creates a new wxbDataParser, and register it in wxbMainFrame */
33 wxbDataParser::wxbDataParser(bool lineanalysis) {
34 wxbMainFrame::GetInstance()->Register(this);
35 this->lineanalysis = lineanalysis;
39 /* Destroy a wxbDataParser, and unregister it in wxbMainFrame */
40 wxbDataParser::~wxbDataParser() {
41 wxbMainFrame::GetInstance()->Unregister(this);
45 * Receives director information, forwarded by wxbMainFrame, and sends it
46 * line by line to the virtual function Analyse.
48 bool wxbDataParser::Print(wxString str, int status) {
51 bool allnewline = true;
52 for (unsigned int i = 0; i < str.Length(); i++) {
53 if (!(allnewline = (str.GetChar(i) == '\n')))
58 ret = Analyse(buffer << "\n", CS_DATA);
60 for (unsigned int i = 1; i < str.Length(); i++) {
61 ret = Analyse("\n", status);
65 wxStringTokenizer tkz(str, "\n",
66 wxTOKEN_RET_DELIMS | wxTOKEN_RET_EMPTY | wxTOKEN_RET_EMPTY_ALL);
68 while ( tkz.HasMoreTokens() ) {
69 buffer << tkz.GetNextToken();
70 if (buffer.Length() != 0) {
71 if ((buffer.GetChar(buffer.Length()-1) == '\n') ||
72 (buffer.GetChar(buffer.Length()-1) == '\r')) {
73 ret = Analyse(buffer, status);
80 if (buffer == "$ ") { // Restore console
81 ret = Analyse(buffer, status);
85 if (status != CS_DATA) {
86 if (buffer.Length() != 0) {
87 ret = Analyse(buffer, CS_DATA);
90 ret = Analyse("", status);
94 ret = Analyse(wxString(str), status);
99 /* Creates a new wxbDataTokenizer */
100 wxbDataTokenizer::wxbDataTokenizer(bool linebyline): wxbDataParser(linebyline), wxArrayString() {
104 /* Destroy a wxbDataTokenizer */
105 wxbDataTokenizer::~wxbDataTokenizer() {
110 * Receives director information, forwarded by wxbMainFrame.
112 bool wxbDataTokenizer::Analyse(wxString str, int status) {
113 finished = ((status == CS_END) || (status == CS_PROMPT) || (status == CS_DISCONNECTED));
121 /* Returns true if the last signal received was an end signal,
122 * indicating that no more data is available */
123 bool wxbDataTokenizer::hasFinished() {
127 /* Creates a new wxbDataTokenizer */
128 wxbPromptParser::wxbPromptParser(): wxbDataParser(false) {
136 /* Destroy a wxbDataTokenizer */
137 wxbPromptParser::~wxbPromptParser() {
144 * Receives director information, forwarded by wxbMainFrame.
146 bool wxbPromptParser::Analyse(wxString str, int status) {
147 if (status == CS_DATA) {
148 if (finished || prompt) { /* New question */
161 if (((i = str.Find(": ")) > 0) && (str.Mid(0, i).Trim(false).ToLong(&num))) { /* List element */
163 choices = new wxArrayString();
164 choices->Add(""); /* index 0 is never used by multiple choice questions */
167 if (choices->GetCount() != num) { /* new choice has begun */
169 choices = new wxArrayString(num);
170 choices->Add("", num); /* fill until this number */
173 choices->Add(str.Mid(i+2).RemoveLast());
175 else if (!choices) { /* Introduction, no list received yet */
176 introStr << questionStr;
177 questionStr = wxString(str);
179 else { /* List receveived, get the question now */
180 introStr << questionStr;
181 questionStr = wxString(str);
185 finished = ((status == CS_PROMPT) || (status == CS_END) || (status == CS_DISCONNECTED));
186 if (prompt = ((status == CS_PROMPT) && (questionStr != "$ "))) { // && (str.Find(": ") == str.Length())
187 if ((introStr != "") && (questionStr == "")) {
188 questionStr = introStr;
191 if (introStr.Last() == '\n') {
192 introStr.RemoveLast();
196 else { /* ended or (dis)connected */
208 /* Returns true if the last signal received was an prompt signal,
209 * indicating that the answer must be sent */
210 bool wxbPromptParser::hasFinished() {
214 /* Returns true if the last message received is a prompt message */
215 bool wxbPromptParser::isPrompt() {
219 /* Returns multiple choice question's introduction */
220 wxString wxbPromptParser::getIntroString() {
224 /* Returns question string */
225 wxString wxbPromptParser::getQuestionString() {
229 /* Return a wxArrayString containing the indexed choices we have
230 * to answer the question, or NULL if this question is not a multiple
232 wxArrayString* wxbPromptParser::getChoices() {