]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/wxbtableparser.cpp
- Remove xpg4 lib from FreeBSD build as it is no longer needed and
[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-2005 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 #include "wxbtableparser.h" // class's header file
32
33 #include "csprint.h"
34
35 #include <wx/tokenzr.h>
36
37 #include "wxbmainframe.h"
38
39 #include <wx/arrimpl.cpp>
40
41 WX_DEFINE_OBJARRAY(wxbTable);
42
43 wxbArrayString::wxbArrayString(int n) : wxArrayString(), wxObject() {
44    Alloc(n);
45 }
46
47 wxbArrayString::~wxbArrayString() {
48    
49 }
50
51 /*
52  *   wxbTableParser constructor
53  */
54 wxbTableParser::wxbTableParser(bool header) : wxbTable(), wxbDataParser(true) {
55    separatorNum = header ? 0 : 2;
56    tableHeader = wxbArrayString();
57 }
58
59 /*
60  *   wxbTableParser destructor
61  */
62 wxbTableParser::~wxbTableParser() {
63
64 }
65
66 /*
67  *   Returns table header as an array of wxStrings.
68  */
69 const wxbArrayString& wxbTableParser::GetHeader() {
70    return tableHeader;
71 }
72
73 /*
74  *   Receives data to analyse.
75  */
76 bool wxbTableParser::Analyse(wxString str, int status) {
77    if ((status == CS_END) && (separatorNum > 0)) {
78       separatorNum = 3;
79    }
80
81    if (separatorNum == 3) return false;
82
83    if (str.Length() > 4) {
84       if ((str.GetChar(0) == '+') && (str.GetChar(str.Length()-2) == '+') && (str.GetChar(str.Length()-1) == '\n')) {
85          separatorNum++;
86          return false;
87       }
88
89       if ((str.GetChar(0) == '|') && (str.GetChar(str.Length()-2) == '|') && (str.GetChar(str.Length()-1) == '\n')) {
90          str.RemoveLast();
91          wxStringTokenizer tkz(str, wxT("|"), wxTOKEN_STRTOK);
92
93          if (separatorNum == 1) {
94             while ( tkz.HasMoreTokens() ) {
95                tableHeader.Add(tkz.GetNextToken().Trim(true).Trim(false));
96             }
97          }
98          else if (separatorNum == 2) {
99             wxbArrayString tablerow(tableHeader.GetCount());
100             while ( tkz.HasMoreTokens() ) {
101                tablerow.Add(tkz.GetNextToken().Trim(true).Trim(false));
102             }
103             Add(tablerow);
104          }
105       }
106    }
107    return false;
108 }
109
110 /*
111  *   Return true table parsing has finished.
112  */
113 bool wxbTableParser::hasFinished() {
114    return (separatorNum == 3);
115 }