w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

json.h
Go to the documentation of this file.
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 
33 #include "inc/Main.h"
34 #include <cassert>
35 #include <cstdio>
36 #include <cstdint>
37 #include "inc/List.h"
38 
39 namespace graphite2 {
40 
41 class json
42 {
43  // Prevent copying
44  json(const json &);
45  json & operator = (const json &);
46 
47  typedef void (*_context_t)(json &);
48 
49  FILE * const _stream;
50  char _contexts[128], // context stack
51  * _context, // current context (top of stack)
52  * _flatten; // if !0 points to context above which
53  // pretty printed output should occur.
55 
56  void context(const char current) throw();
57  void indent(const int d=0) throw();
58  void push_context(const char, const char) throw();
59  void pop_context() throw();
60 
61 public:
62  class closer;
63 
64  using string = const char *;
65  using number = double;
66  enum class integer : std::intmax_t {};
67  enum class integer_u : std::uintmax_t {};
68  using boolean = bool;
69  static const std::nullptr_t null;
70 
71  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; }
72  void *getenv(unsigned int index) const { return _env[index]; }
73  const Vector<void *> &getenvs() const { return _env; }
74 
75  static void flat(json &) throw();
76  static void close(json &) throw();
77  static void object(json &) throw();
78  static void array(json &) throw();
79  static void item(json &) throw();
80 
81  json(FILE * stream) throw();
82  ~json() throw ();
83 
84  FILE * stream() const throw();
85 
86  json & operator << (string) throw();
87  json & operator << (number) throw();
88  json & operator << (integer) throw();
89  json & operator << (integer_u) throw();
90  json & operator << (boolean) throw();
91  json & operator << (std::nullptr_t) throw();
92  json & operator << (_context_t) throw();
93 
94  operator bool() const throw();
95  bool good() const throw();
96  bool eof() const throw();
97 
99 };
100 
101 class json::closer
102 {
103  // Prevent copying.
104  closer(const closer &);
106 
107  json * const _j;
108 public:
109  closer(json * const j) : _j(j) {}
110  ~closer() throw() { if (_j) *_j << close; }
111 };
112 
113 inline
114 json::json(FILE * s) throw()
116 {
117  if (good())
118  fflush(s);
119 }
120 
121 
122 inline
123 json::~json() throw ()
124 {
125  while (_context > _contexts) pop_context();
126 }
127 
128 inline
129 FILE * json::stream() const throw() { return _stream; }
130 
131 
132 inline
134 {
135  ctxt(*this);
136  return *this;
137 }
138 
139 inline
140 json & operator << (json & j, signed char d) throw() { return j << json::integer(d); }
141 
142 inline
143 json & operator << (json & j, unsigned char d) throw() { return j << json::integer_u(d); }
144 
145 inline
146 json & operator << (json & j, short int d) throw() { return j << json::integer(d); }
147 
148 inline
149 json & operator << (json & j, unsigned short int d) throw() { return j << json::integer_u(d); }
150 
151 inline
152 json & operator << (json & j, int d) throw() { return j << json::integer(d); }
153 
154 inline
155 json & operator << (json & j, unsigned int d) throw() { return j << json::integer_u(d); }
156 
157 inline
158 json & operator << (json & j, long int d) throw() { return j << json::integer(d); }
159 
160 inline
161 json & operator << (json & j, unsigned long int d) throw() { return j << json::integer_u(d); }
162 
163 inline
164 json & operator << (json & j, long long int d) throw() { return j << json::integer(d); }
165 
166 inline
167 json & operator << (json & j, unsigned long long int d) throw() { return j << json::integer_u(d); }
168 
169 inline
170 json::operator bool() const throw() { return good(); }
171 
172 inline
173 bool json::good() const throw() { return _stream && ferror(_stream) == 0; }
174 
175 inline
176 bool json::eof() const throw() { return feof(_stream) != 0; }
177 
178 } // namespace graphite2
#define bool
Definition: autosp.c:101
bool reserve(size_type n)
Definition: vectorv.cc:57
size_type size() const
Definition: vector.hh:180
iterator insert(iterator it, void *x)
iterator end()
Definition: vector.hh:176
closer(json *const j)
Definition: json.h:109
closer(const closer &)
bool eof() const
Definition: json.h:176
static void array(json &)
bool good() const
Definition: json.h:173
static void close(json &)
Definition: json.cpp:108
static void close(json &)
static void flat(json &)
char * _context
Definition: json.h:51
void indent(const int d=0)
Vector< void * > _env
Definition: json.h:54
void setenv(unsigned int index, void *val)
Definition: json.h:71
void context(const char current)
FILE *const _stream
Definition: json.h:49
void push_context(const char, const char)
Definition: json.cpp:77
void pop_context()
Definition: json.cpp:89
json & operator=(const json &)
json & operator<<(string)
Definition: json.cpp:118
void(* _context_t)(json &)
Definition: json.h:47
char * _flatten
Definition: json.h:52
static void item(json &)
const Vector< void * > & getenvs() const
Definition: json.h:73
void * getenv(unsigned int index) const
Definition: json.h:72
static void object(json &)
FILE * stream() const
Definition: json.h:129
char _contexts[128]
Definition: json.h:50
json(FILE *stream)
json(const json &)
CLASS_NEW_DELETE
Definition: json.h:98
#define fflush
Definition: xxstdio.h:24
static void
Definition: fpif.c:118
#define s
Definition: afcover.h:80
#define d(n)
Definition: gpos-common.c:151
#define const
Definition: ftzconf.h:91
int64_t intmax_t
Definition: stdint.h:123
Definition: bits.h:30
STL namespace.
static struct Point current
Definition: picttoppm.c:134
#define index(s, c)
Definition: plain2.h:351
struct stream_s stream
Definition: pts_fax.h:93
#define uintmax_t
Definition: stdint.in.h:352
Definition: mendex.h:20
Definition: epdf.c:232
Definition: strexpr.c:21
#define FILE
Definition: t1stdio.h:34
#define feof(f)
Definition: t1stdio.h:109
#define ferror(f)
Definition: t1stdio.h:110
int j
Definition: t4ht.c:1589
val
Definition: tex4ht.c:3227