|
1 /* GRAPHITE2 LICENSING |
|
2 |
|
3 Copyright 2011, SIL International |
|
4 All rights reserved. |
|
5 |
|
6 This library is free software; you can redistribute it and/or modify |
|
7 it under the terms of the GNU Lesser General Public License as published |
|
8 by the Free Software Foundation; either version 2.1 of License, or |
|
9 (at your option) any later version. |
|
10 |
|
11 This program is distributed in the hope that it will be useful, |
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 Lesser General Public License for more details. |
|
15 |
|
16 You should also have received a copy of the GNU Lesser General Public |
|
17 License along with this library in the file named "LICENSE". |
|
18 If not, write to the Free Software Foundation, 51 Franklin Street, |
|
19 Suite 500, Boston, MA 02110-1335, USA or visit their web page on the |
|
20 internet at http://www.fsf.org/licenses/lgpl.html. |
|
21 |
|
22 Alternatively, the contents of this file may be used under the terms of the |
|
23 Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public |
|
24 License, as published by the Free Software Foundation, either version 2 |
|
25 of the License or (at your option) any later version. |
|
26 */ |
|
27 // JSON pretty printer for graphite font debug output logging. |
|
28 // Created on: 15 Dec 2011 |
|
29 // Author: Tim Eves |
|
30 |
|
31 #pragma once |
|
32 #include "inc/Main.h" |
|
33 #include <cassert> |
|
34 #include <stdio.h> |
|
35 |
|
36 namespace graphite2 { |
|
37 |
|
38 class json |
|
39 { |
|
40 // Prevent copying |
|
41 json(const json &); |
|
42 json & operator = (const json &); |
|
43 |
|
44 typedef void (*_context_t)(json &); |
|
45 class _null_t {}; |
|
46 |
|
47 FILE * const _stream; |
|
48 char _contexts[128], // context stack |
|
49 * _context, // current context (top of stack) |
|
50 * _flatten; // if !0 points to context above which |
|
51 // pretty printed output should occur. |
|
52 |
|
53 void context(const char current) throw(); |
|
54 void indent(const int d=0) throw(); |
|
55 void push_context(const char, const char) throw(); |
|
56 void pop_context() throw(); |
|
57 |
|
58 public: |
|
59 class closer; |
|
60 |
|
61 typedef const char * string; |
|
62 typedef double number; |
|
63 typedef long signed int integer; |
|
64 typedef bool boolean; |
|
65 static const _null_t null; |
|
66 |
|
67 static void flat(json &) throw(); |
|
68 static void close(json &) throw(); |
|
69 static void object(json &) throw(); |
|
70 static void array(json &) throw(); |
|
71 static void item(json &) throw(); |
|
72 |
|
73 json(FILE * stream) throw(); |
|
74 ~json() throw (); |
|
75 |
|
76 FILE * stream() const throw(); |
|
77 |
|
78 json & operator << (string) throw(); |
|
79 json & operator << (number) throw(); |
|
80 json & operator << (integer) throw(); |
|
81 json & operator << (long unsigned int d) throw(); |
|
82 json & operator << (boolean) throw(); |
|
83 json & operator << (_null_t) throw(); |
|
84 json & operator << (_context_t) throw(); |
|
85 |
|
86 operator bool() const throw(); |
|
87 bool good() const throw(); |
|
88 bool eof() const throw(); |
|
89 |
|
90 CLASS_NEW_DELETE; |
|
91 }; |
|
92 |
|
93 class json::closer |
|
94 { |
|
95 // Prevent copying. |
|
96 closer(const closer &); |
|
97 closer & operator = (const closer &); |
|
98 |
|
99 json * const _j; |
|
100 public: |
|
101 closer(json * const j) : _j(j) {} |
|
102 ~closer() throw() { if (_j) *_j << close; } |
|
103 }; |
|
104 |
|
105 inline |
|
106 json::json(FILE * s) throw() |
|
107 : _stream(s), _context(_contexts), _flatten(0) |
|
108 { |
|
109 if (good()) |
|
110 fflush(s); |
|
111 } |
|
112 |
|
113 |
|
114 inline |
|
115 json::~json() throw () |
|
116 { |
|
117 while (_context > _contexts) pop_context(); |
|
118 } |
|
119 |
|
120 inline |
|
121 FILE * json::stream() const throw() { return _stream; } |
|
122 |
|
123 |
|
124 inline |
|
125 json & json::operator << (json::_context_t ctxt) throw() |
|
126 { |
|
127 ctxt(*this); |
|
128 return *this; |
|
129 } |
|
130 |
|
131 inline |
|
132 json & operator << (json & j, signed char d) throw() { return j << json::integer(d); } |
|
133 |
|
134 inline |
|
135 json & operator << (json & j, short signed int d) throw() { return j << json::integer(d); } |
|
136 |
|
137 inline |
|
138 json & operator << (json & j, signed int d) throw() { return j << json::integer(d); } |
|
139 |
|
140 inline |
|
141 json & operator << (json & j, unsigned char d) throw() { return j << json::integer(d); } |
|
142 |
|
143 inline |
|
144 json & operator << (json & j, short unsigned int d) throw() { return j << json::integer(d); } |
|
145 |
|
146 inline |
|
147 json & operator << (json & j, unsigned int d) throw() { return j << json::integer(d); } |
|
148 |
|
149 inline |
|
150 json & operator << (json & j, char c) throw () |
|
151 { |
|
152 const char str[2] = {c,0}; |
|
153 return j << str; |
|
154 } |
|
155 |
|
156 inline |
|
157 json::operator bool() const throw() { return good(); } |
|
158 |
|
159 inline |
|
160 bool json::good() const throw() { return _stream && ferror(_stream) == 0; } |
|
161 |
|
162 inline |
|
163 bool json::eof() const throw() { return feof(_stream) != 0; } |
|
164 |
|
165 } // namespace graphite2 |