messageheader.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2003 Tommi Maekitalo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 
30 #ifndef TNT_MESSAGEHEADER_H
31 #define TNT_MESSAGEHEADER_H
32 
33 #include <istream>
34 #include <string>
35 #include <cstring>
36 #include <utility>
37 
38 namespace tnt
39 {
42  {
43  public:
44  static const unsigned MAXHEADERSIZE = 4096;
45 
46  private:
47  char _rawdata[MAXHEADERSIZE]; // key_1\0value_1\0key_2\0value_2\0...key_n\0value_n\0\0
48  unsigned _endOffset;
49 
50  char* getEnd() { return _rawdata + _endOffset; }
51 
52  public:
53  class Parser;
54 
55  typedef std::pair<const char*, const char*> value_type;
56 
57  class const_iterator : public std::iterator<std::forward_iterator_tag, value_type>
58  {
59  friend class Messageheader;
60 
61  value_type _currentVal;
62 
63  void fixup()
64  {
65  if (*_currentVal.first)
66  _currentVal.second = _currentVal.first + std::strlen(_currentVal.first) + 1;
67  else
68  _currentVal.first = _currentVal.second = 0;
69  }
70 
71  void moveForward()
72  {
73  _currentVal.first = _currentVal.second + std::strlen(_currentVal.second) + 1;
74  fixup();
75  }
76 
77  public:
79  : _currentVal(static_cast<const char*>(0), static_cast<const char*>(0))
80  { }
81 
82  explicit const_iterator(const char* p)
83  : _currentVal(p, p)
84  { fixup(); }
85 
86  bool operator== (const const_iterator& it) const
87  { return _currentVal.first == it._currentVal.first; }
88 
89  bool operator!= (const const_iterator& it) const
90  { return _currentVal.first != it._currentVal.first; }
91 
93  {
94  moveForward();
95  return *this;
96  }
97 
99  {
100  const_iterator ret = *this;
101  moveForward();
102  return ret;
103  }
104 
105  const value_type& operator* () const { return _currentVal; }
106  const value_type* operator-> () const { return &_currentVal; }
107  };
108 
109  protected:
111  {
112  OK,
115  };
116 
117  virtual return_type onField(const char* name, const char* value);
118 
119  public:
121  { clear(); }
122 
123  virtual ~Messageheader() { }
124 
126  { return const_iterator(_rawdata); }
128  { return const_iterator(); }
129 
130  const_iterator find(const char* key) const;
131  const_iterator find(const std::string& key) const
132  { return find(key.c_str()); }
133 
134  bool hasHeader(const char* key) const
135  { return find(key) != end(); }
136  bool hasHeader(const std::string& key) const
137  { return hasHeader(key.c_str()); }
138 
139  bool compareHeader(const char* key, const char* value) const;
140  bool compareHeader(const std::string& key, const std::string& value) const
141  { return compareHeader(key.c_str(), value.c_str()); }
142 
143  void removeHeader(const char* key);
144  void removeHeader(const std::string& key)
145  { removeHeader(key.c_str()); }
146 
147  void clear();
148 
149  void setHeader(const char* key, const char* value, bool replace);
150 
151  void setHeader(const std::string& key, const std::string& value, bool replace)
152  { setHeader(key.c_str(), value.c_str(), replace); }
153  };
154 
155  std::istream& operator>> (std::istream& in, Messageheader& data);
156 }
157 
158 #endif // TNT_MESSAGEHEADER_H