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