1
0
cuberite-2a/src/HTTP/NameValueParser.h

71 lines
2.1 KiB
C
Raw Normal View History

2013-10-04 03:20:15 -04:00
// NameValueParser.h
// Declares the cNameValueParser class that parses strings in the "name=value;name2=value2" format into a stringmap
#pragma once
class cNameValueParser :
public std::map<AString, AString>
{
public:
2015-07-31 10:49:10 -04:00
/** Creates an empty parser */
2013-10-04 03:20:15 -04:00
cNameValueParser(bool a_AllowsKeyOnly = true);
2016-02-05 16:45:45 -05:00
2015-07-31 10:49:10 -04:00
/** Creates an empty parser, then parses the data given. Doesn't call Finish(), so more data can be parsed later */
cNameValueParser(const char * a_Data, size_t a_Size, bool a_AllowsKeyOnly = true);
2016-02-05 16:45:45 -05:00
2015-07-31 10:49:10 -04:00
/** Parses the data given */
void Parse(const char * a_Data, size_t a_Size);
2016-02-05 16:45:45 -05:00
2015-07-31 10:49:10 -04:00
/** Notifies the parser that no more data will be coming. Returns true if the parser state is valid */
2013-10-04 03:20:15 -04:00
bool Finish(void);
2016-02-05 16:45:45 -05:00
2015-07-31 10:49:10 -04:00
/** Returns true if the data parsed so far was valid */
2013-10-04 03:20:15 -04:00
bool IsValid(void) const { return (m_State != psInvalid); }
2016-02-05 16:45:45 -05:00
2015-07-31 10:49:10 -04:00
/** Returns true if the parser expects no more data */
2013-10-04 03:20:15 -04:00
bool IsFinished(void) const { return ((m_State == psInvalid) || (m_State == psFinished)); }
2016-02-05 16:45:45 -05:00
2013-10-04 03:20:15 -04:00
protected:
enum eState
{
psKeySpace, ///< Parsing the space in front of the next key
psKey, ///< Currently adding more chars to the key in m_CurrentKey
psEqualSpace, ///< Space after m_CurrentKey
psEqual, ///< Just parsed the = sign after a name
psValueInSQuotes, ///< Just parsed a Single-quote sign after the Equal sign
psValueInDQuotes, ///< Just parsed a Double-quote sign after the Equal sign
psValueRaw, ///< Just parsed a raw value without a quote
psAfterValue, ///< Just finished parsing the value, waiting for semicolon or data end
psInvalid, ///< The parser has encountered an invalid input; further parsing is skipped
psFinished, ///< The parser has already been instructed to finish and doesn't expect any more data
} ;
2016-02-05 16:45:45 -05:00
2015-07-31 10:49:10 -04:00
/** The current state of the parser */
2013-10-04 03:20:15 -04:00
eState m_State;
2016-02-05 16:45:45 -05:00
2015-07-31 10:49:10 -04:00
/** If true, the parser will accept keys without an equal sign and the value */
2013-10-04 03:20:15 -04:00
bool m_AllowsKeyOnly;
2016-02-05 16:45:45 -05:00
2015-07-31 10:49:10 -04:00
/** Buffer for the current Key */
2013-10-04 03:20:15 -04:00
AString m_CurrentKey;
2016-02-05 16:45:45 -05:00
2015-07-31 10:49:10 -04:00
/** Buffer for the current Value; */
2013-10-04 03:20:15 -04:00
AString m_CurrentValue;
2016-02-05 16:45:45 -05:00
2013-10-04 03:20:15 -04:00
} ;