]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/wxbtableparser.cpp
- wxbUtils : Created this class, containing general functions like WaitForEnd
[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  */
15 /*
16    Copyright (C) 2004 Kern Sibbald and John Walker
17
18    This program is free software; you can redistribute it and/or
19    modify it under the terms of the GNU General Public License
20    as published by the Free Software Foundation; either version 2
21    of the License, or (at your option) any later version.
22
23    This program is distributed in the hope that it will be useful,
24    but WITHOUT ANY WARRANTY; without even the implied warranty of
25    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26    GNU General Public License for more details.
27
28    You should have received a copy of the GNU General Public License
29    along with this program; if not, write to the Free Software
30    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
31  */
32
33 #include "wxbtableparser.h" // class's header file
34
35 #include "csprint.h"
36
37 #include <wx/tokenzr.h>
38
39 #include "wxbmainframe.h"
40
41 #include <wx/arrimpl.cpp>
42
43 WX_DEFINE_OBJARRAY(wxbTable);
44
45 wxbArrayString::wxbArrayString(int n) : wxArrayString(), wxObject() {
46    Alloc(n);
47 }
48
49 wxbArrayString::~wxbArrayString() {
50    
51 }
52
53 /*
54  *   wxbTableParser constructor
55  */
56 wxbTableParser::wxbTableParser() : wxbTable(), wxbDataParser(true) {
57    separatorNum = 0;
58    tableHeader = wxbArrayString();
59 }
60
61 /*
62  *   wxbTableParser destructor
63  */
64 wxbTableParser::~wxbTableParser() {
65
66 }
67
68 /*
69  *   Returns table header as an array of wxStrings.
70  */
71 const wxbArrayString& wxbTableParser::GetHeader() {
72    return tableHeader;
73 }
74
75 /*
76  *   Receives data to analyse.
77  */
78 bool wxbTableParser::Analyse(wxString str, int status) {
79    if ((status == CS_END) && (separatorNum > 0)) {
80       separatorNum = 3;
81    }
82
83    if (separatorNum == 3) return false;
84
85    if (str.Length() > 4) {
86       if ((str.GetChar(0) == '+') && (str.GetChar(str.Length()-2) == '+') && (str.GetChar(str.Length()-1) == '\n')) {
87          separatorNum++;
88          return false;
89       }
90
91       if ((str.GetChar(0) == '|') && (str.GetChar(str.Length()-2) == '|') && (str.GetChar(str.Length()-1) == '\n')) {
92          str.RemoveLast();
93          wxStringTokenizer tkz(str, "|", wxTOKEN_STRTOK);
94
95          if (separatorNum == 1) {
96             while ( tkz.HasMoreTokens() ) {
97                tableHeader.Add(tkz.GetNextToken().Trim(true).Trim(false));
98             }
99          }
100          else if (separatorNum == 2) {
101             wxbArrayString tablerow(tableHeader.GetCount());
102             while ( tkz.HasMoreTokens() ) {
103                tablerow.Add(tkz.GetNextToken().Trim(true).Trim(false));
104             }
105             Add(tablerow);
106          }
107       }
108    }
109    return false;
110 }
111
112 /*
113  *   Return true table parsing has finished.
114  */
115 bool wxbTableParser::hasFinished() {
116    return (separatorNum == 3);
117 }