]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/wxbtableparser.cpp
- wxbRestorePanel : now parsing '+' sent by the director when building tree.
[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 Kern Sibbald and John Walker
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    as published by the Free Software Foundation; either version 2
22    of the License, or (at your option) any later version.
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    GNU General Public License for more details.
28
29    You should have received a copy of the GNU General Public License
30    along with this program; if not, write to the Free Software
31    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
32  */
33
34 #include "wxbtableparser.h" // class's header file
35
36 #include "csprint.h"
37
38 #include <wx/tokenzr.h>
39
40 #include "wxbmainframe.h"
41
42 #include <wx/arrimpl.cpp>
43
44 WX_DEFINE_OBJARRAY(wxbTable);
45
46 wxbArrayString::wxbArrayString(int n) : wxArrayString(), wxObject() {
47    Alloc(n);
48 }
49
50 wxbArrayString::~wxbArrayString() {
51    
52 }
53
54 /*
55  *   wxbTableParser constructor
56  */
57 wxbTableParser::wxbTableParser(bool header) : wxbTable(), wxbDataParser(true) {
58    separatorNum = header ? 0 : 2;
59    tableHeader = wxbArrayString();
60 }
61
62 /*
63  *   wxbTableParser destructor
64  */
65 wxbTableParser::~wxbTableParser() {
66
67 }
68
69 /*
70  *   Returns table header as an array of wxStrings.
71  */
72 const wxbArrayString& wxbTableParser::GetHeader() {
73    return tableHeader;
74 }
75
76 /*
77  *   Receives data to analyse.
78  */
79 bool wxbTableParser::Analyse(wxString str, int status) {
80    if ((status == CS_END) && (separatorNum > 0)) {
81       separatorNum = 3;
82    }
83
84    if (separatorNum == 3) return false;
85
86    if (str.Length() > 4) {
87       if ((str.GetChar(0) == '+') && (str.GetChar(str.Length()-2) == '+') && (str.GetChar(str.Length()-1) == '\n')) {
88          separatorNum++;
89          return false;
90       }
91
92       if ((str.GetChar(0) == '|') && (str.GetChar(str.Length()-2) == '|') && (str.GetChar(str.Length()-1) == '\n')) {
93          str.RemoveLast();
94          wxStringTokenizer tkz(str, "|", wxTOKEN_STRTOK);
95
96          if (separatorNum == 1) {
97             while ( tkz.HasMoreTokens() ) {
98                tableHeader.Add(tkz.GetNextToken().Trim(true).Trim(false));
99             }
100          }
101          else if (separatorNum == 2) {
102             wxbArrayString tablerow(tableHeader.GetCount());
103             while ( tkz.HasMoreTokens() ) {
104                tablerow.Add(tkz.GetNextToken().Trim(true).Trim(false));
105             }
106             Add(tablerow);
107          }
108       }
109    }
110    return false;
111 }
112
113 /*
114  *   Return true table parsing has finished.
115  */
116 bool wxbTableParser::hasFinished() {
117    return (separatorNum == 3);
118 }