]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/wxbutils.cpp
- wxbUtils : Created this class, containing general functions like WaitForEnd
[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 #include "wxbtableparser.h"
33
34
35 bool wxbUtils::inited = false;
36
37 /* Initialization */
38 void wxbUtils::Init() {
39    inited = true;
40 }
41
42 /* Reset state */
43 void wxbUtils::Reset() {
44    inited = false;
45 }
46
47 /* Parse a table in tableParser */
48 wxbTableParser* wxbUtils::CreateAndWaitForParser(wxString cmd) {
49    wxbTableParser* tableParser = new wxbTableParser();
50
51    wxbMainFrame::GetInstance()->Send(cmd);
52
53    //time_t base = wxDateTime::Now().GetTicks();
54    while (!tableParser->hasFinished()) {
55       //innerThread->Yield();
56       wxTheApp->Yield(true);
57       ::wxUsleep(100);
58       //if (base+15 < wxDateTime::Now().GetTicks()) break;
59    }
60    return tableParser;
61 }
62
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();
68    
69    wxbMainFrame::GetInstance()->Send(cmd);
70     
71    //time_t base = wxDateTime::Now().GetTicks();
72    while (!promptParser->hasFinished()) {
73       //innerThread->Yield();
74       wxTheApp->Yield(true);
75       ::wxUsleep(100);
76       //if (base+15 < wxDateTime::Now().GetTicks()) break;
77    }
78      
79    if (keepresults) {
80       return promptParser;
81    }
82    else {
83       delete promptParser;
84       return NULL;
85    }  
86 }
87
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);
91
92    wxbMainFrame::GetInstance()->Send(cmd);
93    
94    //wxbMainFrame::GetInstance()->Print("(<WFE)", CS_DEBUG);
95    
96    //time_t base = wxDateTime::Now().GetTicks();
97    while (!datatokenizer->hasFinished()) {
98       //innerThread->Yield();
99       wxTheApp->Yield(true);
100       ::wxUsleep(100);
101       //if (base+15 < wxDateTime::Now().GetTicks()) break;
102    }
103    
104    //wxbMainFrame::GetInstance()->Print("(>WFE)", CS_DEBUG);
105    
106    if (keepresults) {
107       return datatokenizer;
108    }
109    else {
110       delete datatokenizer;
111       return NULL;
112    }
113 }
114
115
116 /* Creates a new wxbDataParser, and register it in wxbMainFrame */
117 wxbDataParser::wxbDataParser(bool lineanalysis) {
118    wxbMainFrame::GetInstance()->Register(this);
119    this->lineanalysis = lineanalysis;
120 //   buffer = "";
121 }
122
123 /* Destroy a wxbDataParser, and unregister it in wxbMainFrame */
124 wxbDataParser::~wxbDataParser() {
125    wxbMainFrame::GetInstance()->Unregister(this);
126 }
127
128 /*
129  * Receives director information, forwarded by wxbMainFrame, and sends it
130  * line by line to the virtual function Analyse.
131  */
132 bool wxbDataParser::Print(wxString str, int status) {
133    bool ret = false;
134    if (lineanalysis) {
135       bool allnewline = true;
136       for (unsigned int i = 0; i < str.Length(); i++) {
137          if (!(allnewline = (str.GetChar(i) == '\n')))
138             break;
139       }
140    
141       if (allnewline) {
142          ret = Analyse(buffer << "\n", CS_DATA);
143          buffer = "";
144          for (unsigned int i = 1; i < str.Length(); i++) {
145             ret = Analyse("\n", status);
146          }
147       }
148       else {
149          wxStringTokenizer tkz(str, "\n", 
150             (wxStringTokenizerMode)(wxTOKEN_RET_DELIMS | wxTOKEN_RET_EMPTY | wxTOKEN_RET_EMPTY_ALL));
151    
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);
158                   buffer = "";
159                }
160             }
161          }
162       }
163    
164       if (buffer == "$ ") { // Restore console
165          ret = Analyse(buffer, status);
166          buffer = "";
167       }
168    
169       if (status != CS_DATA) {
170          if (buffer.Length() != 0) {
171             ret = Analyse(buffer, CS_DATA);
172          }
173          buffer = "";
174          ret = Analyse("", status);
175       }
176    }
177    else {
178       ret = Analyse(wxString(str), status);
179    }
180    return ret;
181 }
182
183 /* Creates a new wxbDataTokenizer */
184 wxbDataTokenizer::wxbDataTokenizer(bool linebyline): wxbDataParser(linebyline), wxArrayString() {
185    finished = false;
186 }
187
188 /* Destroy a wxbDataTokenizer */
189 wxbDataTokenizer::~wxbDataTokenizer() {
190
191 }
192
193 /*
194  *   Receives director information, forwarded by wxbMainFrame.
195  */
196 bool wxbDataTokenizer::Analyse(wxString str, int status) {
197    finished = ((status == CS_END) || (status == CS_PROMPT) || (status == CS_DISCONNECTED));
198
199    if (str != "") {
200       Add(str);
201    }
202    return false;
203 }
204
205 /* Returns true if the last signal received was an end signal, 
206  * indicating that no more data is available */
207 bool wxbDataTokenizer::hasFinished() {
208    return finished;
209 }
210
211 /* Creates a new wxbDataTokenizer */
212 wxbPromptParser::wxbPromptParser(): wxbDataParser(false) {
213    finished = false;
214    prompt = false;
215    introStr = "";
216    choices = NULL;
217    questionStr = "";
218 }
219
220 /* Destroy a wxbDataTokenizer */
221 wxbPromptParser::~wxbPromptParser() {
222    if (choices) {
223       delete choices;
224    }
225 }
226
227 /*
228  *   Receives director information, forwarded by wxbMainFrame.
229  */
230 bool wxbPromptParser::Analyse(wxString str, int status) {
231    if (status == CS_DATA) {
232       if (finished || prompt) { /* New question */
233          finished = false;
234          prompt = false;
235          if (choices) {
236             delete choices;
237             choices = NULL;
238          }
239          questionStr = "";
240          introStr = "";
241       }
242       int i;
243       long num;
244       
245       if (((i = str.Find(": ")) > 0) && (str.Mid(0, i).Trim(false).ToLong(&num))) { /* List element */
246          if (!choices) {
247             choices = new wxArrayString();
248             choices->Add(""); /* index 0 is never used by multiple choice questions */
249          }
250          
251          if ((long)choices->GetCount() != num) { /* new choice has begun */
252             delete choices;
253             choices = new wxArrayString(num);
254             choices->Add("", num); /* fill until this number */
255          }
256          
257          choices->Add(str.Mid(i+2).RemoveLast());
258       }
259       else if (!choices) { /* Introduction, no list received yet */
260          introStr << questionStr;
261          questionStr = wxString(str);
262       }
263       else { /* List receveived, get the question now */
264          introStr << questionStr;
265          questionStr = wxString(str);
266       }
267    }
268    else {
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;
273             introStr = "";
274          }
275          if (introStr.Last() == '\n') {
276             introStr.RemoveLast();
277          }
278          return true;
279       }
280       else { /* ended or (dis)connected */
281          if (choices) {
282             delete choices;
283             choices = NULL;
284          }
285          questionStr = "";
286          introStr = "";
287       }
288    }
289    return false;
290 }
291
292 /* Returns true if the last signal received was an prompt signal, 
293  * indicating that the answer must be sent */
294 bool wxbPromptParser::hasFinished() {
295    return finished;
296 }
297
298 /* Returns true if the last message received is a prompt message */
299 bool wxbPromptParser::isPrompt() {
300    return prompt;
301 }
302
303 /* Returns multiple choice question's introduction */
304 wxString wxbPromptParser::getIntroString() {
305    return introStr;
306 }
307
308 /* Returns question string */
309 wxString wxbPromptParser::getQuestionString() {
310    return questionStr;
311 }
312
313 /* Return a wxArrayString containing the indexed choices we have
314  * to answer the question, or NULL if this question is not a multiple
315  * choice one. */
316 wxArrayString* wxbPromptParser::getChoices() {
317    return choices;
318 }