]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/wxbutils.cpp
Fix compilation warnings with wxWidgets 2.6.0.
[bacula/bacula] / bacula / src / wx-console / wxbutils.cpp
1 /*
2  *
3  *   wxbDataParser, class that receives and analyses data
4  *
5  *    Nicolas Boichat, April-July 2004
6  *
7  *    Version $Id$
8  */
9 /*
10    Copyright (C) 2004 Kern Sibbald and John Walker
11
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.
16
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.
21
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.
25  */
26  
27 #include "wxbutils.h"
28
29 #include "wxbmainframe.h"
30
31 #include "csprint.h"
32
33 #include "wxbtableparser.h"
34
35 /* A macro named Yield is defined under MinGW */
36 #undef Yield
37
38 bool wxbUtils::inited = false;
39
40 /* Sleeps during milliseconds (wrapper for wxUsleep (2.4) or wxMilliSleep (2.6)) */
41 void wxbUtils::MilliSleep(unsigned long milliseconds) {
42 #if wxCHECK_VERSION(2, 6, 0)
43    ::wxMilliSleep(milliseconds);
44 #else
45    ::wxUsleep(milliseconds);
46 #endif
47 }
48
49 /* Initialization */
50 void wxbUtils::Init() {
51    inited = true;
52 }
53
54 /* Reset state */
55 void wxbUtils::Reset() {
56    inited = false;
57 }
58
59 /* Parse a table in tableParser */
60 wxbTableParser* wxbUtils::CreateAndWaitForParser(wxString cmd) {
61    wxbTableParser* tableParser = new wxbTableParser();
62
63    wxbMainFrame::GetInstance()->Send(cmd);
64
65    //time_t base = wxDateTime::Now().GetTicks();
66    while (!tableParser->hasFinished()) {
67       //innerThread->Yield();
68       wxTheApp->Yield(true);
69       wxbUtils::MilliSleep(100);
70       //if (base+15 < wxDateTime::Now().GetTicks()) break;
71    }
72    return tableParser;
73 }
74
75 /* Run a command, and waits until prompt result is fully received,
76  * if keepresults is true, returns a valid pointer to a wxbPromptParser
77  * containing the data. */
78 wxbPromptParser* wxbUtils::WaitForPrompt(wxString cmd, bool keepresults) {
79    wxbPromptParser* promptParser = new wxbPromptParser();
80    
81    wxbMainFrame::GetInstance()->Send(cmd);
82     
83    //time_t base = wxDateTime::Now().GetTicks();
84    while (!promptParser->hasFinished()) {
85       //innerThread->Yield();
86       wxTheApp->Yield(true);
87       wxbUtils::MilliSleep(100);
88       //if (base+15 < wxDateTime::Now().GetTicks()) break;
89    }
90      
91    if (keepresults) {
92       return promptParser;
93    }
94    else {
95       delete promptParser;
96       return NULL;
97    }  
98 }
99
100 /* Run a command, and waits until result is fully received. */
101 wxbDataTokenizer* wxbUtils::WaitForEnd(wxString cmd, bool keepresults, bool linebyline) {
102    wxbDataTokenizer* datatokenizer = new wxbDataTokenizer(linebyline);
103
104    wxbMainFrame::GetInstance()->Send(cmd);
105    
106    //wxbMainFrame::GetInstance()->Print("(<WFE)", CS_DEBUG);
107    
108    //time_t base = wxDateTime::Now().GetTicks();
109    while (!datatokenizer->hasFinished()) {
110       //innerThread->Yield();
111       wxTheApp->Yield(true);
112       wxbUtils::MilliSleep(100);
113       //if (base+15 < wxDateTime::Now().GetTicks()) break;
114    }
115    
116    //wxbMainFrame::GetInstance()->Print("(>WFE)", CS_DEBUG);
117    
118    if (keepresults) {
119       return datatokenizer;
120    }
121    else {
122       delete datatokenizer;
123       return NULL;
124    }
125 }
126
127
128 /* Creates a new wxbDataParser, and register it in wxbMainFrame */
129 wxbDataParser::wxbDataParser(bool lineanalysis) {
130    wxbMainFrame::GetInstance()->Register(this);
131    this->lineanalysis = lineanalysis;
132 //   buffer = "";
133 }
134
135 /* Destroy a wxbDataParser, and unregister it in wxbMainFrame */
136 wxbDataParser::~wxbDataParser() {
137    wxbMainFrame::GetInstance()->Unregister(this);
138 }
139
140 /*
141  * Receives director information, forwarded by wxbMainFrame, and sends it
142  * line by line to the virtual function Analyse.
143  */
144 bool wxbDataParser::Print(wxString str, int status) {
145    bool ret = false;
146    if (lineanalysis) {
147       bool allnewline = true;
148       for (unsigned int i = 0; i < str.Length(); i++) {
149          if (!(allnewline = (str.GetChar(i) == '\n')))
150             break;
151       }
152    
153       if (allnewline) {
154          ret = Analyse(buffer << "\n", CS_DATA);
155          buffer = "";
156          for (unsigned int i = 1; i < str.Length(); i++) {
157             ret = Analyse("\n", status);
158          }
159       }
160       else {
161          wxStringTokenizer tkz(str, "\n", 
162             (wxStringTokenizerMode)(wxTOKEN_RET_DELIMS | wxTOKEN_RET_EMPTY | wxTOKEN_RET_EMPTY_ALL));
163    
164          while ( tkz.HasMoreTokens() ) {
165             buffer << tkz.GetNextToken();
166             if (buffer.Length() != 0) {
167                if ((buffer.GetChar(buffer.Length()-1) == '\n') ||
168                   (buffer.GetChar(buffer.Length()-1) == '\r')) {
169                   ret = Analyse(buffer, status);
170                   buffer = "";
171                }
172             }
173          }
174       }
175    
176       if (buffer == "$ ") { // Restore console
177          ret = Analyse(buffer, status);
178          buffer = "";
179       }
180    
181       if (status != CS_DATA) {
182          if (buffer.Length() != 0) {
183             ret = Analyse(buffer, CS_DATA);
184          }
185          buffer = "";
186          ret = Analyse("", status);
187       }
188    }
189    else {
190       ret = Analyse(wxString(str), status);
191    }
192    return ret;
193 }
194
195 /* Creates a new wxbDataTokenizer */
196 wxbDataTokenizer::wxbDataTokenizer(bool linebyline): wxbDataParser(linebyline), wxArrayString() {
197    finished = false;
198 }
199
200 /* Destroy a wxbDataTokenizer */
201 wxbDataTokenizer::~wxbDataTokenizer() {
202
203 }
204
205 /*
206  *   Receives director information, forwarded by wxbMainFrame.
207  */
208 bool wxbDataTokenizer::Analyse(wxString str, int status) {
209    finished = ((status == CS_END) || (status == CS_PROMPT) || (status == CS_DISCONNECTED));
210
211    if (str != "") {
212       Add(str);
213    }
214    return false;
215 }
216
217 /* Returns true if the last signal received was an end signal, 
218  * indicating that no more data is available */
219 bool wxbDataTokenizer::hasFinished() {
220    return finished;
221 }
222
223 /* Creates a new wxbDataTokenizer */
224 wxbPromptParser::wxbPromptParser(): wxbDataParser(false) {
225    finished = false;
226    prompt = false;
227    introStr = "";
228    choices = NULL;
229    numerical = false;
230    questionStr = "";
231 }
232
233 /* Destroy a wxbDataTokenizer */
234 wxbPromptParser::~wxbPromptParser() {
235    if (choices) {
236       delete choices;
237    }
238 }
239
240 /*
241  *   Receives director information, forwarded by wxbMainFrame.
242  */
243 bool wxbPromptParser::Analyse(wxString str, int status) {
244    if (status == CS_DATA) {
245       if (finished || prompt) { /* New question */
246          finished = false;
247          prompt = false;
248          if (choices) {
249             delete choices;
250             choices = NULL;
251             numerical = false;
252          }
253          questionStr = "";
254          introStr = "";
255       }
256       int i;
257       long num;
258       
259       if (((i = str.Find(": ")) > 0) && (str.Mid(0, i).Trim(false).ToLong(&num))) { /* List element */
260          if (!choices) {
261             choices = new wxArrayString();
262             choices->Add(""); /* index 0 is never used by multiple choice questions */
263             numerical = true;
264          }
265          
266          if ((long)choices->GetCount() != num) { /* new choice has begun */
267             delete choices;
268             choices = new wxArrayString();
269             choices->Add("", num); /* fill until this number */
270             numerical = true;
271          }
272          
273          choices->Add(str.Mid(i+2).RemoveLast());
274       }
275       else if (!choices) { /* Introduction, no list received yet */
276          introStr << questionStr;
277          questionStr = wxString(str);
278       }
279       else { /* List receveived, get the question now */
280          introStr << questionStr;
281          questionStr = wxString(str);
282       }
283    }
284    else {
285       finished = ((status == CS_PROMPT) || (status == CS_END) || (status == CS_DISCONNECTED));
286       if (prompt = ((status == CS_PROMPT) && (questionStr != "$ "))) { // && (str.Find(": ") == str.Length())
287          if (introStr.Last() == '\n') {
288             introStr.RemoveLast();
289          }
290          if ((introStr != "") && (questionStr == "")) {
291             questionStr = introStr;
292             introStr = "";
293          }
294          
295          if ((!choices) && (questionStr.Find("(yes/mod/no)") > -1)) {
296             choices = new wxArrayString();
297             choices->Add("yes");
298             choices->Add("mod");
299             choices->Add("no");
300             numerical = false;
301          }
302          
303          return true;
304       }
305       else { /* ended or (dis)connected */
306          if (choices) {
307             delete choices;
308             choices = NULL;
309             numerical = false;
310          }
311          questionStr = "";
312          introStr = "";
313       }
314    }
315    return false;
316 }
317
318 /* Returns true if the last signal received was an prompt signal, 
319  * indicating that the answer must be sent */
320 bool wxbPromptParser::hasFinished() {
321    return finished;
322 }
323
324 /* Returns true if the last message received is a prompt message */
325 bool wxbPromptParser::isPrompt() {
326    return prompt;
327 }
328
329 /* Returns multiple choice question's introduction */
330 wxString wxbPromptParser::getIntroString() {
331    return introStr;
332 }
333
334 /* Returns question string */
335 wxString wxbPromptParser::getQuestionString() {
336    return questionStr;
337 }
338
339 /* Returns a wxArrayString containing the indexed choices we have
340  * to answer the question, or NULL if this question is not a multiple
341  * choice one. */
342 wxArrayString* wxbPromptParser::getChoices() {
343    return choices;
344 }
345
346 /* Returns true if the expected answer to the choice list is a number,
347  * false if it is a string (for example yes/mod/no). */
348 bool wxbPromptParser::isNumericalChoice() {
349    return numerical;
350 }