Argo  1.0
A C++ library for handling JSON.
json_utf8_exception.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Andrew Haisley
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 
24 
25 #include <string>
26 
27 #include "common.hpp"
28 #include "json_utf8_exception.hpp"
29 
30 using namespace std;
31 using namespace NAMESPACE;
32 
33 json_utf8_exception::json_utf8_exception(exception_type et, const string &s) noexcept :
34  json_exception(et), m_byte_index(0)
35 {
36  snprintf(m_message, max_message_length, "%s : %s", get_main_message(), s.c_str());
37 }
38 
39 json_utf8_exception::json_utf8_exception(exception_type et, char c) noexcept :
40  json_exception(et), m_byte_index(0)
41 {
42  snprintf(m_message, max_message_length, "%s : 0x%02x", get_main_message(), c);
43 }
44 
45 json_utf8_exception::json_utf8_exception(exception_type et, unsigned int i) noexcept :
46  json_exception(et), m_byte_index(0)
47 {
48  snprintf(m_message, max_message_length, "%s : %d", get_main_message(), i);
49 }
50 
51 void json_utf8_exception::add_byte_index(size_t byte_index) noexcept
52 {
53  snprintf(m_message, max_message_length, "%s at or near byte %ld", get_main_message(), byte_index);
54 }
55 
56 size_t json_utf8_exception::get_byte_index() const noexcept
57 {
58  return m_byte_index;
59 }
60 
61 const char *json_utf8_exception::get_main_message()
62 {
63  switch (m_type)
64  {
65  case invalid_string_escape_e:
66  return "invalid escape character found in string";
67  case invalid_string_encoding_e:
68  return "string encoding is invalid";
69  case invalid_unicode_e:
70  return "invalid unicode value";
71  case invalid_utf8_sequence_length_e:
72  return "invalid UTF-8 sequence length";
73  case invalid_hex_char_e:
74  return "invalid hex character";
75  case invalid_hex_number_e:
76  return "invalid hex number (must be 0-15)";
77  case invalid_utf8_char_e:
78  return "0 byte detected in UTF-8 sequence other than code point 0";
79  default:
80  return "generic";
81  }
82 }
Base class for all exceptions thrown by the library.
#define NAMESPACE
You can change the namespace of the whole library by changing this value.
Definition: common.hpp:29
STL namespace.
Common defs needed everywhere and, as far as is possible, platform specific changes.
The json_utf8_exception class.