]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/wxbtableparser.cpp
f5cbf84d3cf6c11f8149c4c45806bda2ae6b75cf
[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 /*
42  *   wxbTableParser constructor
43  */
44 wxbTableParser::wxbTableParser() : wxbTable(5) {
45    separatorNum = 0;
46    tableHeader = wxbTableRow(5);
47 }
48
49 /*
50  *   wxbTableParser destructor
51  */
52 wxbTableParser::~wxbTableParser() {
53
54 }
55
56 /*
57  *   Returns table header as an array of wxStrings.
58  */
59 wxbTableRow* wxbTableParser::GetHeader() {
60    return &tableHeader;
61 }
62
63 /*
64  *   Receives director information, forwarded by the wxbPanel which
65  *  uses this parser.
66  */
67 void wxbTableParser::Print(wxString str, int status) {
68    if ((status == CS_END) && (separatorNum > 0)) {
69       separatorNum = 3;
70    }
71
72    if (separatorNum == 3) return;
73
74    if (str.Length() > 4) {
75       if ((str.GetChar(0) == '+') && (str.GetChar(str.Length()-2) == '+') && (str.GetChar(str.Length()-1) == '\n')) {
76          separatorNum++;
77          return;
78       }
79
80       if ((str.GetChar(0) == '|') && (str.GetChar(str.Length()-2) == '|') && (str.GetChar(str.Length()-1) == '\n')) {
81          str.RemoveLast();
82          wxStringTokenizer tkz(str, "|", wxTOKEN_STRTOK);
83
84          if (separatorNum == 1) {
85             int i = 0;
86             while ( tkz.HasMoreTokens() ) {
87                tableHeader[i++] = tkz.GetNextToken().Trim(true).Trim(false);
88             }
89          }
90          else if (separatorNum == 2) {
91             wxbTableRow tablerow(tableHeader.size());
92             int i = 0;
93             while ( tkz.HasMoreTokens() ) {
94                tablerow[i++] = tkz.GetNextToken().Trim(true).Trim(false);
95             }
96             (*this)[size()] = tablerow;
97          }
98       }
99    }
100 }
101
102 /*
103  *   Return true table parsing has finished.
104  */
105 bool wxbTableParser::hasFinished() {
106    return (separatorNum == 3);
107 }