File indexing completed on 2025-03-23 09:55:25
0001 /* 0002 This file is part of the KDE libraries 0003 SPDX-FileCopyrightText: 2008 Andreas Hartmetz <ahartmetz@gmail.com> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #ifndef PARSINGHELPERS_H 0009 #define PARSINGHELPERS_H 0010 0011 #include <QHash> 0012 #include <QList> 0013 0014 struct HeaderField { 0015 HeaderField(bool multiValued) 0016 { 0017 isMultiValued = multiValued; 0018 } 0019 // QHash requires a default constructor 0020 HeaderField() 0021 { 0022 isMultiValued = false; 0023 } 0024 0025 bool isMultiValued; 0026 struct Info { 0027 int startIndex = 0; 0028 int endIndex = 0; 0029 }; 0030 QList<Info> beginEnd; 0031 }; 0032 0033 class HeaderTokenizer; 0034 class TokenIterator 0035 { 0036 public: 0037 inline bool hasNext() const 0038 { 0039 return m_currentToken < m_tokens.count(); 0040 } 0041 0042 QByteArray next(); 0043 0044 QByteArray current() const; 0045 0046 QList<QByteArray> all() const; 0047 0048 private: 0049 friend class HeaderTokenizer; 0050 QList<HeaderField::Info> m_tokens; 0051 int m_currentToken; 0052 const char *m_buffer; 0053 TokenIterator(const QList<HeaderField::Info> &tokens, const char *buffer) 0054 : m_tokens(tokens) 0055 , m_currentToken(0) 0056 , m_buffer(buffer) 0057 { 0058 } 0059 }; 0060 0061 class HeaderTokenizer : public QHash<QByteArray, HeaderField> 0062 { 0063 public: 0064 explicit HeaderTokenizer(char *buffer); 0065 // note that buffer is not const - in the parsed area CR/LF will be overwritten 0066 // with spaces if there is a line continuation. 0067 /// @return: index of first char after header or end 0068 int tokenize(int begin, int end); 0069 0070 // after tokenize() has been called use the QHash part of this class to 0071 // ask for a list of begin-end indexes in buffer for header values. 0072 0073 TokenIterator iterator(const char *key) const; 0074 0075 private: 0076 char *m_buffer; 0077 struct HeaderFieldTemplate { 0078 const char *name; 0079 bool isMultiValued; 0080 }; 0081 QList<HeaderField::Info> m_nullTokens; // long-lived, allows us to pass out references. 0082 }; 0083 0084 #endif // PARSINGHELPERS_H