Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/graphite/src/inc/json.h
9906 views
1
// SPDX-License-Identifier: MIT OR MPL-2.0 OR LGPL-2.1-or-later OR GPL-2.0-or-later
2
// Copyright 2011, SIL International, All rights reserved.
3
4
// JSON pretty printer for graphite font debug output logging.
5
// Created on: 15 Dec 2011
6
// Author: Tim Eves
7
8
#pragma once
9
10
#include "inc/Main.h"
11
#include <cassert>
12
#include <cstdio>
13
#include <cstdint>
14
#include "inc/List.h"
15
16
namespace graphite2 {
17
18
class json
19
{
20
// Prevent copying
21
json(const json &);
22
json & operator = (const json &);
23
24
typedef void (*_context_t)(json &);
25
26
FILE * const _stream;
27
char _contexts[128], // context stack
28
* _context, // current context (top of stack)
29
* _flatten; // if !0 points to context above which
30
// pretty printed output should occur.
31
Vector<void *> _env;
32
33
void context(const char current) throw();
34
void indent(const int d=0) throw();
35
void push_context(const char, const char) throw();
36
void pop_context() throw();
37
38
public:
39
class closer;
40
41
using string = const char *;
42
using number = double;
43
enum class integer : std::intmax_t {};
44
enum class integer_u : std::uintmax_t {};
45
using boolean = bool;
46
static const std::nullptr_t null;
47
48
void setenv(unsigned int index, void *val) { _env.reserve(index + 1); if (index >= _env.size()) _env.insert(_env.end(), _env.size() - index + 1, 0); _env[index] = val; }
49
void *getenv(unsigned int index) const { return _env[index]; }
50
const Vector<void *> &getenvs() const { return _env; }
51
52
static void flat(json &) throw();
53
static void close(json &) throw();
54
static void object(json &) throw();
55
static void array(json &) throw();
56
static void item(json &) throw();
57
58
json(FILE * stream) throw();
59
~json() throw ();
60
61
FILE * stream() const throw();
62
63
json & operator << (string) throw();
64
json & operator << (number) throw();
65
json & operator << (integer) throw();
66
json & operator << (integer_u) throw();
67
json & operator << (boolean) throw();
68
json & operator << (std::nullptr_t) throw();
69
json & operator << (_context_t) throw();
70
71
operator bool() const throw();
72
bool good() const throw();
73
bool eof() const throw();
74
75
CLASS_NEW_DELETE;
76
};
77
78
class json::closer
79
{
80
// Prevent copying.
81
closer(const closer &);
82
closer & operator = (const closer &);
83
84
json * const _j;
85
public:
86
closer(json * const j) : _j(j) {}
87
~closer() throw() { if (_j) *_j << close; }
88
};
89
90
inline
91
json::json(FILE * s) throw()
92
: _stream(s), _context(_contexts), _flatten(0)
93
{
94
if (good())
95
fflush(s);
96
}
97
98
99
inline
100
json::~json() throw ()
101
{
102
while (_context > _contexts) pop_context();
103
}
104
105
inline
106
FILE * json::stream() const throw() { return _stream; }
107
108
109
inline
110
json & json::operator << (json::_context_t ctxt) throw()
111
{
112
ctxt(*this);
113
return *this;
114
}
115
116
inline
117
json & operator << (json & j, signed char d) throw() { return j << json::integer(d); }
118
119
inline
120
json & operator << (json & j, unsigned char d) throw() { return j << json::integer_u(d); }
121
122
inline
123
json & operator << (json & j, short int d) throw() { return j << json::integer(d); }
124
125
inline
126
json & operator << (json & j, unsigned short int d) throw() { return j << json::integer_u(d); }
127
128
inline
129
json & operator << (json & j, int d) throw() { return j << json::integer(d); }
130
131
inline
132
json & operator << (json & j, unsigned int d) throw() { return j << json::integer_u(d); }
133
134
inline
135
json & operator << (json & j, long int d) throw() { return j << json::integer(d); }
136
137
inline
138
json & operator << (json & j, unsigned long int d) throw() { return j << json::integer_u(d); }
139
140
inline
141
json & operator << (json & j, long long int d) throw() { return j << json::integer(d); }
142
143
inline
144
json & operator << (json & j, unsigned long long int d) throw() { return j << json::integer_u(d); }
145
146
inline
147
json::operator bool() const throw() { return good(); }
148
149
inline
150
bool json::good() const throw() { return _stream && ferror(_stream) == 0; }
151
152
inline
153
bool json::eof() const throw() { return feof(_stream) != 0; }
154
155
} // namespace graphite2
156
157