Joedb 10.3.0
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
minimal_runtime_io.cpp
Go to the documentation of this file.
1#include "joedb/ui/type_io.h"
3
4namespace joedb
5{
6 /////////////////////////////////////////////////////////////////////////////
7 char get_hex_char_from_digit(uint8_t n)
8 /////////////////////////////////////////////////////////////////////////////
9 {
10 n &= 0x0f;
11
12 if (n < 10)
13 return char('0' + n);
14 else
15 return char('a' + n - 10);
16 }
17
18 /////////////////////////////////////////////////////////////////////////////
19 void write_octal_character(std::ostream &out, uint8_t c)
20 /////////////////////////////////////////////////////////////////////////////
21 {
22 out.put('\\');
23 out.put(char('0' + ((c >> 6) & 7)));
24 out.put(char('0' + ((c >> 3) & 7)));
25 out.put(char('0' + ((c >> 0) & 7)));
26 }
27
28 ////////////////////////////////////////////////////////////////////////////
29 static void write_octal_character(std::ostream &out, uint8_t c, bool json)
30 ////////////////////////////////////////////////////////////////////////////
31 {
32 if (json)
33 throw Exception("json can't handle non-utf8 strings");
34 else
36 }
37
38 /////////////////////////////////////////////////////////////////////////////
39 void write_string(std::ostream &out, const std::string &s, bool json)
40 /////////////////////////////////////////////////////////////////////////////
41 {
42 out.put('"');
43
44 for (size_t i = 0; i < s.size(); i++)
45 {
46 const uint8_t c = uint8_t(s[i]);
47
48 #if 0
50 #else
51 if (c < 0x20 || c == 0x7f)
52 {
53 if (json)
54 {
55 out.put('\\');
56 out.put('u');
57 out.put('0');
58 out.put('0');
59 out.put(get_hex_char_from_digit(uint8_t(c >> 4)));
60 out.put(get_hex_char_from_digit(uint8_t(c & 0x0f)));
61 }
62 else
64 }
65 else if (c == '?' && i > 0 && s[i - 1] == '?' && !json) // C++ trigraphs
66 out.put('\\').put('?');
67 else if (c == '"')
68 out.put('\\').put('"');
69 else if (c == '\\')
70 out.put('\\').put('\\');
71 else if ((c & 0xe0) == 0xc0 &&
72 i + 1 < s.size() &&
73 (s[i + 1] & 0xc0) == 0x80)
74 {
75 out.put(c);
76 out.put(s[++i]);
77 }
78 else if // UTF-16 surrogate halves
79 (
80 c == 0xed &&
81 i + 1 < s.size() &&
82 (uint8_t(s[i + 1]) & 0xa0) == 0xa0
83 )
84 {
85 write_octal_character(out, c, json);
86 }
87 else if ((c & 0xf0) == 0xe0 &&
88 i + 2 < s.size() &&
89 (s[i + 1] & 0xc0) == 0x80 &&
90 (s[i + 2] & 0xc0) == 0x80)
91 {
92 out.put(c);
93 out.put(s[++i]);
94 out.put(s[++i]);
95 }
96 else if ((c & 0xf8) == 0xf0 &&
97 i + 3 < s.size() &&
98 (s[i + 1] & 0xc0) == 0x80 &&
99 (s[i + 2] & 0xc0) == 0x80 &&
100 (s[i + 3] & 0xc0) == 0x80)
101 {
102 out.put(char(c));
103 out.put(s[++i]);
104 out.put(s[++i]);
105 out.put(s[++i]);
106 }
107 else if (c & 0x80)
108 {
109 write_octal_character(out, c, json);
110 }
111 else
112 {
113 out.put(char(c));
114 }
115 #endif
116 }
117
118 out.put('"');
119 }
120
121 /////////////////////////////////////////////////////////////////////////////
122 void write_int8(std::ostream &out, int8_t value)
123 /////////////////////////////////////////////////////////////////////////////
124 {
125 out << int(value);
126 }
127}
void write_octal_character(std::ostream &out, uint8_t c)
void write_string(std::ostream &out, const std::string &s, bool json)
void write_int8(std::ostream &out, int8_t value)
char get_hex_char_from_digit(uint8_t n)