KIOSlave
parsinghelpers.h
Go to the documentation of this file.00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2008 Andreas Hartmetz <ahartmetz@gmail.com> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #ifndef PARSINGHELPERS_H 00021 #define PARSINGHELPERS_H 00022 00023 #include <QtCore/QList> 00024 #include <QtCore/QPair> 00025 00026 struct HeaderField { 00027 HeaderField(bool multiValued) 00028 { isMultiValued = multiValued; } 00029 // QHash requires a default constructor 00030 HeaderField() 00031 { isMultiValued = false; } 00032 00033 bool isMultiValued; 00034 QList<QPair<int, int> > beginEnd; 00035 }; 00036 00037 class HeaderTokenizer; 00038 class TokenIterator 00039 { 00040 public: 00041 inline bool hasNext() const 00042 { 00043 return m_currentToken < m_tokens.count(); 00044 } 00045 00046 QByteArray next(); 00047 00048 QByteArray current() const; 00049 00050 QList<QByteArray> all() const; 00051 00052 private: 00053 friend class HeaderTokenizer; 00054 QList<QPair<int, int> > m_tokens; 00055 int m_currentToken; 00056 const char *m_buffer; 00057 TokenIterator(const QList<QPair<int, int> > &tokens, const char *buffer) 00058 : m_tokens(tokens), 00059 m_currentToken(0), 00060 m_buffer(buffer) {} 00061 }; 00062 00063 class HeaderTokenizer : public QHash<QByteArray, HeaderField> 00064 { 00065 public: 00066 HeaderTokenizer(char *buffer); 00067 // note that buffer is not const - in the parsed area CR/LF will be overwritten 00068 // with spaces if there is a line continuation. 00070 int tokenize(int begin, int end); 00071 00072 // after tokenize() has been called use the QHash part of this class to 00073 // ask for a list of begin-end indexes in buffer for header values. 00074 00075 TokenIterator iterator(const char *key); 00076 private: 00077 char *m_buffer; 00078 struct HeaderFieldTemplate { 00079 const char *name; 00080 bool isMultiValued; 00081 }; 00082 QList<QPair<int, int> > m_nullTokens; //long-lived, allows us to pass out references. 00083 }; 00084 00085 #endif //PARSINGHELPERS_H