]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/wxbutils.cpp
ab69ad2cafa58abb6d93ae4e9e4f50dad827d84d
[bacula/bacula] / bacula / src / wx-console / wxbutils.cpp
1 /*
2  *
3  *   wxbDataParser, class that receives and analyses data
4  *
5  *    Nicolas Boichat, April 2004
6  *
7  */
8 /*
9    Copyright (C) 2004 Kern Sibbald and John Walker
10
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.
15
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.
20
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.
24  */
25  
26 #include "wxbutils.h"
27
28 #include "wxbmainframe.h"
29
30 #include "csprint.h"
31
32 /* Creates a new wxbDataParser, and register it in wxbMainFrame */
33 wxbDataParser::wxbDataParser(bool lineanalysis) {
34    wxbMainFrame::GetInstance()->Register(this);
35    this->lineanalysis = lineanalysis;
36 //   buffer = "";
37 }
38
39 /* Destroy a wxbDataParser, and unregister it in wxbMainFrame */
40 wxbDataParser::~wxbDataParser() {
41    wxbMainFrame::GetInstance()->Unregister(this);
42 }
43
44 /*
45  * Receives director information, forwarded by wxbMainFrame, and sends it
46  * line by line to the virtual function Analyse.
47  */
48 bool wxbDataParser::Print(wxString str, int status) {
49    bool ret = false;
50    if (lineanalysis) {
51       bool allnewline = true;
52       for (unsigned int i = 0; i < str.Length(); i++) {
53          if (!(allnewline = (str.GetChar(i) == '\n')))
54             break;
55       }
56    
57       if (allnewline) {
58          ret = Analyse(buffer << "\n", CS_DATA);
59          buffer = "";
60          for (unsigned int i = 1; i < str.Length(); i++) {
61             ret = Analyse("\n", status);
62          }
63       }
64       else {
65          wxStringTokenizer tkz(str, "\n", 
66             wxTOKEN_RET_DELIMS | wxTOKEN_RET_EMPTY | wxTOKEN_RET_EMPTY_ALL);
67    
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);
74                   buffer = "";
75                }
76             }
77          }
78       }
79    
80       if (buffer == "$ ") { // Restore console
81          ret = Analyse(buffer, status);
82          buffer = "";
83       }
84    
85       if (status != CS_DATA) {
86          if (buffer.Length() != 0) {
87             ret = Analyse(buffer, CS_DATA);
88          }
89          buffer = "";
90          ret = Analyse("", status);
91       }
92    }
93    else {
94       ret = Analyse(wxString(str), status);
95    }
96    return ret;
97 }
98
99 /* Creates a new wxbDataTokenizer */
100 wxbDataTokenizer::wxbDataTokenizer(bool linebyline): wxbDataParser(linebyline), wxArrayString() {
101    finished = false;
102 }
103
104 /* Destroy a wxbDataTokenizer */
105 wxbDataTokenizer::~wxbDataTokenizer() {
106
107 }
108
109 /*
110  *   Receives director information, forwarded by wxbMainFrame.
111  */
112 bool wxbDataTokenizer::Analyse(wxString str, int status) {
113    finished = ((status == CS_END) || (status == CS_PROMPT) || (status == CS_DISCONNECTED));
114
115    if (str != "") {
116       Add(str);
117    }
118    return false;
119 }
120
121 /* Returns true if the last signal received was an end signal, 
122  * indicating that no more data is available */
123 bool wxbDataTokenizer::hasFinished() {
124    return finished;
125 }
126
127 /* Creates a new wxbDataTokenizer */
128 wxbPromptParser::wxbPromptParser(): wxbDataParser(false) {
129    finished = false;
130    prompt = false;
131    introStr = "";
132    choices = NULL;
133    questionStr = "";
134 }
135
136 /* Destroy a wxbDataTokenizer */
137 wxbPromptParser::~wxbPromptParser() {
138    if (choices) {
139       delete choices;
140    }
141 }
142
143 /*
144  *   Receives director information, forwarded by wxbMainFrame.
145  */
146 bool wxbPromptParser::Analyse(wxString str, int status) {
147    if (status == CS_DATA) {
148       if (finished || prompt) { /* New question */
149          finished = false;
150          prompt = false;
151          if (choices) {
152             delete choices;
153             choices = NULL;
154          }
155          questionStr = "";
156          introStr = "";
157       }
158       int i;
159       long num;
160       
161       if (((i = str.Find(": ")) > 0) && (str.Mid(0, i).Trim(false).ToLong(&num))) { /* List element */
162          if (!choices) {
163             choices = new wxArrayString();
164             choices->Add(""); /* index 0 is never used by multiple choice questions */
165          }
166          
167          if ((long)choices->GetCount() != num) { /* new choice has begun */
168             delete choices;
169             choices = new wxArrayString(num);
170             choices->Add("", num); /* fill until this number */
171          }
172          
173          choices->Add(str.Mid(i+2).RemoveLast());
174       }
175       else if (!choices) { /* Introduction, no list received yet */
176          introStr << questionStr;
177          questionStr = wxString(str);
178       }
179       else { /* List receveived, get the question now */
180          introStr << questionStr;
181          questionStr = wxString(str);
182       }
183    }
184    else {
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;
189             introStr = "";
190          }
191          if (introStr.Last() == '\n') {
192             introStr.RemoveLast();
193          }
194          return true;
195       }
196       else { /* ended or (dis)connected */
197          if (choices) {
198             delete choices;
199             choices = NULL;
200          }
201          questionStr = "";
202          introStr = "";
203       }
204    }
205    return false;
206 }
207
208 /* Returns true if the last signal received was an prompt signal, 
209  * indicating that the answer must be sent */
210 bool wxbPromptParser::hasFinished() {
211    return finished;
212 }
213
214 /* Returns true if the last message received is a prompt message */
215 bool wxbPromptParser::isPrompt() {
216    return prompt;
217 }
218
219 /* Returns multiple choice question's introduction */
220 wxString wxbPromptParser::getIntroString() {
221    return introStr;
222 }
223
224 /* Returns question string */
225 wxString wxbPromptParser::getQuestionString() {
226    return questionStr;
227 }
228
229 /* Return a wxArrayString containing the indexed choices we have
230  * to answer the question, or NULL if this question is not a multiple
231  * choice one. */
232 wxArrayString* wxbPromptParser::getChoices() {
233    return choices;
234 }