]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/wxbtableparser.cpp
34fcb42e54c4608e2e9cdd08778e7d178649ce2f
[bacula/bacula] / bacula / src / wx-console / wxbtableparser.cpp
1 /*
2  *
3  *   Class used to parse tables received from director in this format :
4  *
5  *  +---------+---------+-------------------+
6  *  | Header1 | Header2 | ...               |
7  *  +---------+---------+-------------------+
8  *  |  Data11 | Data12  | ...               |
9  *  |  ....   | ...     | ...               |
10  *  +---------+---------+-------------------+
11  *
12  *    Nicolas Boichat, April 2004
13  *
14  *    Version $Id$
15  */
16 /*
17    Copyright (C) 2004-2006 Kern Sibbald
18
19    This program is free software; you can redistribute it and/or
20    modify it under the terms of the GNU General Public License
21    version 2 as amended with additional clauses defined in the
22    file LICENSE in the main source directory.
23
24    This program is distributed in the hope that it will be useful,
25    but WITHOUT ANY WARRANTY; without even the implied warranty of
26    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
27    the file LICENSE for additional details.
28
29  */
30
31 /*  Windows debug builds set _DEBUG which is used by wxWidgets to select their
32  *  debug memory allocator.  Unfortunately it conflicts with Bacula's SmartAlloc.
33  * So we turn _DEBUG off since we aren't interested in things it enables.
34  */
35
36 #undef _DEBUG
37
38 #include "bacula.h"
39
40 #include "wxbtableparser.h" // class's header file
41
42 #include "csprint.h"
43
44 #include <wx/tokenzr.h>
45
46 #include "wxbmainframe.h"
47
48 #include <wx/arrimpl.cpp>
49
50 WX_DEFINE_OBJARRAY(wxbTable);
51
52 wxbArrayString::wxbArrayString(int n) : wxArrayString(), wxObject() {
53    Alloc(n);
54 }
55
56 wxbArrayString::~wxbArrayString() {
57    
58 }
59
60 /*
61  *   wxbTableParser constructor
62  */
63 wxbTableParser::wxbTableParser(bool header) : wxbTable(), wxbDataParser(true) {
64    separatorNum = header ? 0 : 2;
65    tableHeader = wxbArrayString();
66 }
67
68 /*
69  *   wxbTableParser destructor
70  */
71 wxbTableParser::~wxbTableParser() {
72
73 }
74
75 /*
76  *   Returns table header as an array of wxStrings.
77  */
78 const wxbArrayString& wxbTableParser::GetHeader() {
79    return tableHeader;
80 }
81
82 /*
83  *   Receives data to analyse.
84  */
85 bool wxbTableParser::Analyse(wxString str, int status) {
86    if ((status == CS_END) && (separatorNum > 0)) {
87       separatorNum = 3;
88    }
89
90    if (separatorNum == 3) return false;
91
92    if (str.Length() > 4) {
93       if ((str.GetChar(0) == '+') && (str.GetChar(str.Length()-2) == '+') && (str.GetChar(str.Length()-1) == '\n')) {
94          separatorNum++;
95          return false;
96       }
97
98       if ((str.GetChar(0) == '|') && (str.GetChar(str.Length()-2) == '|') && (str.GetChar(str.Length()-1) == '\n')) {
99          str.RemoveLast();
100          wxStringTokenizer tkz(str, wxT("|"), wxTOKEN_STRTOK);
101
102          if (separatorNum == 1) {
103             while ( tkz.HasMoreTokens() ) {
104                tableHeader.Add(tkz.GetNextToken().Trim(true).Trim(false));
105             }
106          }
107          else if (separatorNum == 2) {
108             wxbArrayString tablerow(tableHeader.GetCount());
109             while ( tkz.HasMoreTokens() ) {
110                tablerow.Add(tkz.GetNextToken().Trim(true).Trim(false));
111             }
112             Add(tablerow);
113          }
114       }
115    }
116    return false;
117 }
118
119 /*
120  *   Return true table parsing has finished.
121  */
122 bool wxbTableParser::hasFinished() {
123    return (separatorNum == 3);
124 }