3 * wxbDataParser, class that receives and analyses data
5 * Nicolas Boichat, April-July 2004
10 Bacula® - The Network Backup Solution
12 Copyright (C) 2004-2006 Free Software Foundation Europe e.V.
14 The main author of Bacula is Kern Sibbald, with contributions from
15 many others, a complete list can be found in the file AUTHORS.
16 This program is Free Software; you can redistribute it and/or
17 modify it under the terms of version two of the GNU General Public
18 License as published by the Free Software Foundation plus additions
19 that are listed in the file LICENSE.
21 This program is distributed in the hope that it will be useful, but
22 WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
31 Bacula® is a registered trademark of John Walker.
32 The licensor of Bacula is the Free Software Foundation Europe
33 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
34 Switzerland, email:ftf@fsfeurope.org.
37 /* Windows debug builds set _DEBUG which is used by wxWidgets to select their
38 * debug memory allocator. Unfortunately it conflicts with Bacula's SmartAlloc.
39 * So we turn _DEBUG off since we aren't interested in things it enables.
48 #include "wxbmainframe.h"
52 #include "wxbtableparser.h"
54 /* A macro named Yield is defined under MinGW */
57 bool wxbUtils::inited = false;
59 wxString wxbUtils::ConvertToPrintable(wxString& str) {
60 /* FIXME : Unicode support should be added to fix this problem */
61 #if (wxUSE_UNICODE == 0) && __WXGTK20__
63 /* Convert the string to something printable without unicode */
64 for (unsigned int i = 0; i < strnew.Length(); i++) {
65 /* If the character is not ASCII, print a ? */
66 if (((unsigned char)strnew[i] > (unsigned char)127)) {
76 /* Sleeps during milliseconds (wrapper for wxUsleep (2.4) or wxMilliSleep (2.6)) */
77 void wxbUtils::MilliSleep(unsigned long milliseconds) {
78 #if wxCHECK_VERSION(2, 6, 0)
79 ::wxMilliSleep(milliseconds);
81 ::wxUsleep(milliseconds);
86 void wxbUtils::Init() {
91 void wxbUtils::Reset() {
95 /* Parse a table in tableParser */
96 wxbTableParser* wxbUtils::CreateAndWaitForParser(wxString cmd) {
97 wxbTableParser* tableParser = new wxbTableParser();
99 wxbMainFrame::GetInstance()->Send(cmd);
101 //time_t base = wxDateTime::Now().GetTicks();
102 while (!tableParser->hasFinished()) {
103 //innerThread->Yield();
104 wxTheApp->Yield(true);
105 wxbUtils::MilliSleep(100);
106 //if (base+15 < wxDateTime::Now().GetTicks()) break;
111 /* Run a command, and waits until prompt result is fully received,
112 * if keepresults is true, returns a valid pointer to a wxbPromptParser
113 * containing the data. */
114 wxbPromptParser* wxbUtils::WaitForPrompt(wxString cmd, bool keepresults) {
115 wxbPromptParser* promptParser = new wxbPromptParser();
117 wxbMainFrame::GetInstance()->Send(cmd);
119 //time_t base = wxDateTime::Now().GetTicks();
120 while (!promptParser->hasFinished()) {
121 //innerThread->Yield();
122 wxTheApp->Yield(true);
123 wxbUtils::MilliSleep(100);
124 //if (base+15 < wxDateTime::Now().GetTicks()) break;
136 /* Run a command, and waits until result is fully received. */
137 wxbDataTokenizer* wxbUtils::WaitForEnd(wxString cmd, bool keepresults, bool linebyline) {
138 wxbDataTokenizer* datatokenizer = new wxbDataTokenizer(linebyline);
140 wxbMainFrame::GetInstance()->Send(cmd);
142 //wxbMainFrame::GetInstance()->Print("(<WFE)", CS_DEBUG);
144 //time_t base = wxDateTime::Now().GetTicks();
145 while (!datatokenizer->hasFinished()) {
146 //innerThread->Yield();
147 wxTheApp->Yield(true);
148 wxbUtils::MilliSleep(100);
149 //if (base+15 < wxDateTime::Now().GetTicks()) break;
152 //wxbMainFrame::GetInstance()->Print("(>WFE)", CS_DEBUG);
155 return datatokenizer;
158 delete datatokenizer;
164 /* Creates a new wxbDataParser, and register it in wxbMainFrame */
165 wxbDataParser::wxbDataParser(bool lineanalysis) {
166 wxbMainFrame::GetInstance()->Register(this);
167 this->lineanalysis = lineanalysis;
171 /* Destroy a wxbDataParser, and unregister it in wxbMainFrame */
172 wxbDataParser::~wxbDataParser() {
173 wxbMainFrame::GetInstance()->Unregister(this);
177 * Receives director information, forwarded by wxbMainFrame, and sends it
178 * line by line to the virtual function Analyse.
180 bool wxbDataParser::Print(wxString str, int status) {
183 bool allnewline = true;
184 for (unsigned int i = 0; i < str.Length(); i++) {
185 if (!(allnewline = (str.GetChar(i) == '\n')))
190 ret = Analyse(buffer << wxT("\n"), CS_DATA);
192 for (unsigned int i = 1; i < str.Length(); i++) {
193 ret = Analyse(wxT("\n"), status);
197 wxStringTokenizer tkz(str, wxT("\n"),
198 (wxStringTokenizerMode)(wxTOKEN_RET_DELIMS | wxTOKEN_RET_EMPTY | wxTOKEN_RET_EMPTY_ALL));
200 while ( tkz.HasMoreTokens() ) {
201 buffer << tkz.GetNextToken();
202 if (buffer.Length() != 0) {
203 if ((buffer.GetChar(buffer.Length()-1) == '\n') ||
204 (buffer.GetChar(buffer.Length()-1) == '\r')) {
205 ret = Analyse(buffer, status);
212 if (buffer == wxT("$ ")) { // Restore console
213 ret = Analyse(buffer, status);
217 if (status != CS_DATA) {
218 if (buffer.Length() != 0) {
219 ret = Analyse(buffer, CS_DATA);
222 ret = Analyse(wxT(""), status);
226 ret = Analyse(wxString(str), status);
231 /* Creates a new wxbDataTokenizer */
232 wxbDataTokenizer::wxbDataTokenizer(bool linebyline): wxbDataParser(linebyline), wxArrayString() {
236 /* Destroy a wxbDataTokenizer */
237 wxbDataTokenizer::~wxbDataTokenizer() {
242 * Receives director information, forwarded by wxbMainFrame.
244 bool wxbDataTokenizer::Analyse(wxString str, int status) {
245 finished = ((status == CS_END) || (status == CS_PROMPT) || (status == CS_DISCONNECTED));
247 if (str != wxT("")) {
253 /* Returns true if the last signal received was an end signal,
254 * indicating that no more data is available */
255 bool wxbDataTokenizer::hasFinished() {
259 /* Creates a new wxbDataTokenizer */
260 wxbPromptParser::wxbPromptParser(): wxbDataParser(false) {
266 questionStr = wxT("");
269 /* Destroy a wxbDataTokenizer */
270 wxbPromptParser::~wxbPromptParser() {
277 * Receives director information, forwarded by wxbMainFrame.
279 bool wxbPromptParser::Analyse(wxString str, int status) {
280 if (status == CS_DATA) {
281 if (finished || prompt) { /* New question */
289 questionStr = wxT("");
295 if (((i = str.Find(wxT(": "))) > 0) && (str.Mid(0, i).Trim(false).ToLong(&num))) { /* List element */
297 choices = new wxArrayString();
298 choices->Add(wxT("")); /* index 0 is never used by multiple choice questions */
302 if ((long)choices->GetCount() != num) { /* new choice has begun */
304 choices = new wxArrayString();
305 choices->Add(wxT(""), num); /* fill until this number */
309 choices->Add(str.Mid(i+2).RemoveLast());
311 else if (!choices) { /* Introduction, no list received yet */
312 introStr << questionStr;
313 questionStr = wxString(str);
315 else { /* List receveived, get the question now */
316 introStr << questionStr;
317 questionStr = wxString(str);
321 finished = ((status == CS_PROMPT) || (status == CS_END) || (status == CS_DISCONNECTED));
322 if (prompt = ((status == CS_PROMPT) && (questionStr != wxT("$ ")))) { // && (str.Find(": ") == str.Length())
323 if (introStr.Last() == '\n') {
324 introStr.RemoveLast();
326 if ((introStr != wxT("")) && (questionStr == wxT(""))) {
327 questionStr = introStr;
331 if ((!choices) && (questionStr.Find(wxT("(yes/mod/no)")) > -1)) {
332 choices = new wxArrayString();
333 choices->Add(wxT("yes"));
334 choices->Add(wxT("mod"));
335 choices->Add(wxT("no"));
341 else { /* ended or (dis)connected */
347 questionStr = wxT("");
354 /* Returns true if the last signal received was an prompt signal,
355 * indicating that the answer must be sent */
356 bool wxbPromptParser::hasFinished() {
360 /* Returns true if the last message received is a prompt message */
361 bool wxbPromptParser::isPrompt() {
365 /* Returns multiple choice question's introduction */
366 wxString wxbPromptParser::getIntroString() {
370 /* Returns question string */
371 wxString wxbPromptParser::getQuestionString() {
375 /* Returns a wxArrayString containing the indexed choices we have
376 * to answer the question, or NULL if this question is not a multiple
378 wxArrayString* wxbPromptParser::getChoices() {
382 /* Returns true if the expected answer to the choice list is a number,
383 * false if it is a string (for example yes/mod/no). */
384 bool wxbPromptParser::isNumericalChoice() {