CMS 3D CMS Logo

SimpleSAXParser.h
Go to the documentation of this file.
1 #ifndef __SIMPLE_SAX_PARSER_H_
2 #define __SIMPLE_SAX_PARSER_H_
3 /* A simple SAX-like parser.
4 
5  And yes, I know the S in SAX stands for Simple.
6 
7  Licensed under GPLv3 license.
8 
9  TODO: incomplete support for entities.
10  TODO: no support for DTD nor <?xml> preamble.
11  */
12 
13 #include <string>
14 #include <cstdio>
15 #include <cstdlib>
16 #include <cassert>
17 #include <cstring>
18 #include <iostream>
19 #include <algorithm>
20 #include <vector>
21 
22 bool fgettoken(std::istream &in, char **buffer, size_t *maxSize, const char *separators, int *firstChar);
23 
70 public:
71  struct Attribute {
74 
75  Attribute(const std::string &iKey, const std::string &iValue) : key(iKey), value(iValue) {}
76 
77  bool operator<(const Attribute &attribute) const { return this->key < attribute.key; }
78  };
79 
80  typedef std::vector<Attribute> Attributes;
81  class ParserError {
82  public:
84 
85  const char *error() { return m_error.c_str(); }
86 
87  private:
89  };
90 
106  };
107 
108  SimpleSAXParser(std::istream &f)
109  : m_in(f), m_bufferSize(1024), m_buffer(new char[m_bufferSize]), m_nextChar(m_in.get()) {}
110 
111  virtual ~SimpleSAXParser();
112 
113  void parse(void);
114 
115  virtual void startElement(const std::string & /*name*/, Attributes & /*attributes*/) {}
116  virtual void endElement(const std::string & /*name*/) {}
117  virtual void data(const std::string & /*data*/) {}
118 
119  SimpleSAXParser(const SimpleSAXParser &) = delete; // stop default
120  const SimpleSAXParser &operator=(const SimpleSAXParser &) = delete; // stop default
121 
122 private:
123  std::string parseEntity(const std::string &entity);
124  std::string getToken(const char *delim) {
126  return m_buffer;
127  }
128 
129  std::string getToken(const char delim) {
130  char buf[2] = {delim, 0};
132  m_nextChar = m_in.get();
133  return m_buffer;
134  }
135 
136  bool skipChar(int c) {
137  if (m_nextChar != c)
138  return false;
139  m_nextChar = m_in.get();
140  return true;
141  }
142 
143  int nextChar(void) { return m_nextChar; }
144 
145  std::istream &m_in;
146  size_t m_bufferSize;
147  char *m_buffer;
149  std::vector<std::string> m_elementTags;
151 };
152 
153 // NOTE: put in a .cc if this file is used in more than one place.
154 #endif // __SIMPLE_SAX_PARSER_H_
std::istream & m_in
std::vector< Attribute > Attributes
virtual void endElement(const std::string &)
std::string parseEntity(const std::string &entity)
virtual void data(const std::string &)
std::string getToken(const char delim)
Attribute(const std::string &iKey, const std::string &iValue)
bool fgettoken(std::istream &in, char **buffer, size_t *maxSize, const char *separators, int *firstChar)
ParserError(const std::string &error)
bool operator<(const Attribute &attribute) const
bool skipChar(int c)
double f[11][100]
Definition: value.py:1
std::string getToken(const char *delim)
std::vector< std::string > m_elementTags
virtual ~SimpleSAXParser()
const SimpleSAXParser & operator=(const SimpleSAXParser &)=delete
#define get
virtual void startElement(const std::string &, Attributes &)
Attributes m_attributes
SimpleSAXParser(std::istream &f)