mirror of
https://github.com/monero-project/monero.git
synced 2024-12-15 21:06:32 +02:00
151 lines
5.9 KiB
C
151 lines
5.9 KiB
C
|
// Copyright (C) 2011 Milo Yip
|
||
|
//
|
||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||
|
// of this software and associated documentation files (the "Software"), to deal
|
||
|
// in the Software without restriction, including without limitation the rights
|
||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||
|
// copies of the Software, and to permit persons to whom the Software is
|
||
|
// furnished to do so, subject to the following conditions:
|
||
|
//
|
||
|
// The above copyright notice and this permission notice shall be included in
|
||
|
// all copies or substantial portions of the Software.
|
||
|
//
|
||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||
|
// THE SOFTWARE.
|
||
|
|
||
|
#ifndef RAPIDJSON_ERROR_ERROR_H__
|
||
|
#define RAPIDJSON_ERROR_ERROR_H__
|
||
|
|
||
|
/*! \file error.h */
|
||
|
|
||
|
/*! \defgroup RAPIDJSON_ERRORS RapidJSON error handling */
|
||
|
|
||
|
///////////////////////////////////////////////////////////////////////////////
|
||
|
// RAPIDJSON_ERROR_CHARTYPE
|
||
|
|
||
|
//! Character type of error messages.
|
||
|
/*! \ingroup RAPIDJSON_ERRORS
|
||
|
The default character type is \c char.
|
||
|
On Windows, user can define this macro as \c TCHAR for supporting both
|
||
|
unicode/non-unicode settings.
|
||
|
*/
|
||
|
#ifndef RAPIDJSON_ERROR_CHARTYPE
|
||
|
#define RAPIDJSON_ERROR_CHARTYPE char
|
||
|
#endif
|
||
|
|
||
|
///////////////////////////////////////////////////////////////////////////////
|
||
|
// RAPIDJSON_ERROR_STRING
|
||
|
|
||
|
//! Macro for converting string literial to \ref RAPIDJSON_ERROR_CHARTYPE[].
|
||
|
/*! \ingroup RAPIDJSON_ERRORS
|
||
|
By default this conversion macro does nothing.
|
||
|
On Windows, user can define this macro as \c _T(x) for supporting both
|
||
|
unicode/non-unicode settings.
|
||
|
*/
|
||
|
#ifndef RAPIDJSON_ERROR_STRING
|
||
|
#define RAPIDJSON_ERROR_STRING(x) x
|
||
|
#endif
|
||
|
|
||
|
namespace rapidjson {
|
||
|
|
||
|
///////////////////////////////////////////////////////////////////////////////
|
||
|
// ParseErrorCode
|
||
|
|
||
|
//! Error code of parsing.
|
||
|
/*! \ingroup RAPIDJSON_ERRORS
|
||
|
\see GenericReader::Parse, GenericReader::GetParseErrorCode
|
||
|
*/
|
||
|
enum ParseErrorCode {
|
||
|
kParseErrorNone = 0, //!< No error.
|
||
|
|
||
|
kParseErrorDocumentEmpty, //!< The document is empty.
|
||
|
kParseErrorDocumentRootNotSingular, //!< The document root must not follow by other values.
|
||
|
|
||
|
kParseErrorValueInvalid, //!< Invalid value.
|
||
|
|
||
|
kParseErrorObjectMissName, //!< Missing a name for object member.
|
||
|
kParseErrorObjectMissColon, //!< Missing a colon after a name of object member.
|
||
|
kParseErrorObjectMissCommaOrCurlyBracket, //!< Missing a comma or '}' after an object member.
|
||
|
|
||
|
kParseErrorArrayMissCommaOrSquareBracket, //!< Missing a comma or ']' after an array element.
|
||
|
|
||
|
kParseErrorStringUnicodeEscapeInvalidHex, //!< Incorrect hex digit after \\u escape in string.
|
||
|
kParseErrorStringUnicodeSurrogateInvalid, //!< The surrogate pair in string is invalid.
|
||
|
kParseErrorStringEscapeInvalid, //!< Invalid escape character in string.
|
||
|
kParseErrorStringMissQuotationMark, //!< Missing a closing quotation mark in string.
|
||
|
kParseErrorStringInvalidEncoding, //!< Invalid encoding in string.
|
||
|
|
||
|
kParseErrorNumberTooBig, //!< Number too big to be stored in double.
|
||
|
kParseErrorNumberMissFraction, //!< Miss fraction part in number.
|
||
|
kParseErrorNumberMissExponent, //!< Miss exponent in number.
|
||
|
|
||
|
kParseErrorTermination, //!< Parsing was terminated.
|
||
|
kParseErrorUnspecificSyntaxError, //!< Unspecific syntax error.
|
||
|
};
|
||
|
|
||
|
//! Result of parsing (wraps ParseErrorCode)
|
||
|
/*!
|
||
|
\ingroup RAPIDJSON_ERRORS
|
||
|
\code
|
||
|
Document doc;
|
||
|
ParseResult ok = doc.Parse("[42]");
|
||
|
if (!ok) {
|
||
|
fprintf(stderr, "JSON parse error: %s (%u)",
|
||
|
GetParseError_En(ok.Code()), ok.Offset());
|
||
|
exit(EXIT_FAILURE);
|
||
|
}
|
||
|
\endcode
|
||
|
\see GenericReader::Parse, GenericDocument::Parse
|
||
|
*/
|
||
|
struct ParseResult {
|
||
|
|
||
|
//! Default constructor, no error.
|
||
|
ParseResult() : code_(kParseErrorNone), offset_(0) {}
|
||
|
//! Constructor to set an error.
|
||
|
ParseResult(ParseErrorCode code, size_t offset) : code_(code), offset_(offset) {}
|
||
|
|
||
|
//! Get the error code.
|
||
|
ParseErrorCode Code() const { return code_; }
|
||
|
//! Get the error offset, if \ref IsError(), 0 otherwise.
|
||
|
size_t Offset() const { return offset_; }
|
||
|
|
||
|
//! Conversion to \c bool, returns \c true, iff !\ref IsError().
|
||
|
operator bool() const { return !IsError(); }
|
||
|
//! Whether the result is an error.
|
||
|
bool IsError() const { return code_ != kParseErrorNone; }
|
||
|
|
||
|
bool operator==(const ParseResult& that) const { return code_ == that.code_; }
|
||
|
bool operator==(ParseErrorCode code) const { return code_ == code; }
|
||
|
friend bool operator==(ParseErrorCode code, const ParseResult & err) { return code == err.code_; }
|
||
|
|
||
|
//! Reset error code.
|
||
|
void Clear() { Set(kParseErrorNone); }
|
||
|
//! Update error code and offset.
|
||
|
void Set(ParseErrorCode code, size_t offset = 0) { code_ = code; offset_ = offset; }
|
||
|
|
||
|
private:
|
||
|
ParseErrorCode code_;
|
||
|
size_t offset_;
|
||
|
};
|
||
|
|
||
|
//! Function pointer type of GetParseError().
|
||
|
/*! \ingroup RAPIDJSON_ERRORS
|
||
|
|
||
|
This is the prototype for \c GetParseError_X(), where \c X is a locale.
|
||
|
User can dynamically change locale in runtime, e.g.:
|
||
|
\code
|
||
|
GetParseErrorFunc GetParseError = GetParseError_En; // or whatever
|
||
|
const RAPIDJSON_ERROR_CHARTYPE* s = GetParseError(document.GetParseErrorCode());
|
||
|
\endcode
|
||
|
*/
|
||
|
typedef const RAPIDJSON_ERROR_CHARTYPE* (*GetParseErrorFunc)(ParseErrorCode);
|
||
|
|
||
|
} // namespace rapidjson
|
||
|
|
||
|
#endif // RAPIDJSON_ERROR_ERROR_H__
|