Webmaster  |  Imprint 
C++ Server Pages
Main  |  License  |  Documentation  |  Download 

/home/tommi/tntnet/framework/common/tnt/messageheader.h

00001 /*
00002  * Copyright (C) 2003 Tommi Maekitalo
00003  * 
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or (at your option) any later version.
00008  * 
00009  * As a special exception, you may use this file as part of a free
00010  * software library without restriction. Specifically, if other files
00011  * instantiate templates or use macros or inline functions from this
00012  * file, or you compile this file and link it with other files to
00013  * produce an executable, this file does not by itself cause the
00014  * resulting executable to be covered by the GNU General Public
00015  * License. This exception does not however invalidate any other
00016  * reasons why the executable file might be covered by the GNU Library
00017  * General Public License.
00018  * 
00019  * This library is distributed in the hope that it will be useful,
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00022  * Lesser General Public License for more details.
00023  * 
00024  * You should have received a copy of the GNU Lesser General Public
00025  * License along with this library; if not, write to the Free Software
00026  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00027  */
00028 
00029 
00030 #ifndef TNT_MESSAGEHEADER_H
00031 #define TNT_MESSAGEHEADER_H
00032 
00033 #include <istream>
00034 #include <string>
00035 #include <cstring>
00036 #include <utility>
00037 
00038 namespace tnt
00039 {
00041   class Messageheader
00042   {
00043     public:
00044       static const unsigned MAXHEADERSIZE = 4096;
00045 
00046     private:
00047       char rawdata[MAXHEADERSIZE];  // key_1\0value_1\0key_2\0value_2\0...key_n\0value_n\0\0
00048       unsigned endOffset;
00049       char* getEnd() { return rawdata + endOffset; }
00050 
00051     public:
00052       class Parser;
00053       friend class Parser;
00054 
00055       typedef std::pair<const char*, const char*> value_type;
00056 
00057       class const_iterator
00058         : public std::iterator<std::forward_iterator_tag, value_type>
00059       {
00060           friend class Messageheader;
00061 
00062           value_type current_value;
00063 
00064           void fixup()
00065           {
00066             if (*current_value.first)
00067               current_value.second = current_value.first + std::strlen(current_value.first) + 1;
00068             else
00069               current_value.first = current_value.second = 0;
00070           }
00071 
00072           void moveForward()
00073           {
00074             current_value.first = current_value.second + std::strlen(current_value.second) + 1;
00075             fixup();
00076           }
00077 
00078         public:
00079           const_iterator()
00080             : current_value(static_cast<const char*>(0), static_cast<const char*>(0))
00081             { }
00082 
00083           explicit const_iterator(const char* p)
00084             : current_value(p, p)
00085           {
00086             fixup();
00087           }
00088 
00089           bool operator== (const const_iterator& it) const
00090           { return current_value.first == it.current_value.first; }
00091 
00092           bool operator!= (const const_iterator& it) const
00093           { return current_value.first != it.current_value.first; }
00094 
00095           const_iterator& operator++()
00096           {
00097             moveForward();
00098             return *this;
00099           }
00100 
00101           const_iterator operator++(int)
00102           {
00103             const_iterator ret = *this;
00104             moveForward();
00105             return ret;
00106           }
00107 
00108           const value_type& operator* () const   { return current_value; }
00109           const value_type* operator-> () const  { return &current_value; }
00110       };
00111 
00112     protected:
00113       enum return_type
00114       {
00115         OK,
00116         FAIL,
00117         END
00118       };
00119 
00120       virtual return_type onField(const char* name, const char* value);
00121 
00122     public:
00123       Messageheader()
00124         { clear(); }
00125 
00126       virtual ~Messageheader()   { }
00127 
00128       const_iterator begin() const
00129         { return const_iterator(rawdata); }
00130       const_iterator end() const
00131         { return const_iterator(); }
00132 
00133       const_iterator find(const char* key) const;
00134       const_iterator find(const std::string& key) const
00135         { return find(key.c_str()); }
00136 
00137       bool hasHeader(const char* key) const
00138         { return find(key) != end(); }
00139       bool hasHeader(const std::string& key) const
00140         { return hasHeader(key.c_str()); }
00141 
00142       bool compareHeader(const char* key, const char* value) const;
00143       bool compareHeader(const std::string& key, const std::string& value) const
00144         { return compareHeader(key.c_str(), value.c_str()); }
00145 
00146       void removeHeader(const char* key);
00147       void removeHeader(const std::string& key)
00148         { removeHeader(key.c_str()); }
00149 
00150       void clear();
00151 
00152       void setHeader(const char* key, const char* value, bool replace);
00153 
00154       void setHeader(const std::string& key, const std::string& value, bool replace)
00155         { setHeader(key.c_str(), value.c_str(), replace); }
00156   };
00157 
00158   std::istream& operator>> (std::istream& in, Messageheader& data);
00159 
00160 }
00161 
00162 #endif // TNT_MESSAGEHEADER_H
00163 
Copyright © 2008 The Tntnet Development Team
Tntnet 1.6