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.cpp
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 debug logging
28 // Author: Tim Eves
29 
30 #if !defined GRAPHITE2_NTRACING
31 
32 #include <cstdio>
33 #include <limits>
34 #include "inc/json.h"
35 
36 #if defined(_MSC_VER)
37 #define FORMAT_INTMAX "%lli"
38 #define FORMAT_UINTMAX "%llu"
39 #else
40 #define FORMAT_INTMAX "%ji"
41 #define FORMAT_UINTMAX "%ju"
42 #endif
43 
44 using namespace graphite2;
45 
46 namespace
47 {
48  enum
49  {
50  seq = ',',
51  obj='}', member=':', empty_obj='{',
52  arr=']', empty_arr='['
53  };
54 }
55 
56 const std::nullptr_t json::null = nullptr;
57 
58 inline
59 void json::context(const char current) throw()
60 {
61  fprintf(_stream, "%c", *_context);
62  indent();
63  *_context = current;
64 }
65 
66 
67 void json::indent(const int d) throw()
68 {
69  if (*_context == member || (_flatten && _flatten < _context))
70  fputc(' ', _stream);
71  else
72  fprintf(_stream, "\n%*s", 4*int(_context - _contexts + d), "");
73 }
74 
75 
76 inline
77 void json::push_context(const char prefix, const char suffix) throw()
78 {
79  assert(_context - _contexts < ptrdiff_t(sizeof _contexts));
80 
81  if (_context == _contexts)
82  *_context = suffix;
83  else
84  context(suffix);
85  *++_context = prefix;
86 }
87 
88 
89 void json::pop_context() throw()
90 {
92 
93  if (*_context == seq) indent(-1);
94  else fputc(*_context, _stream);
95 
96  fputc(*--_context, _stream);
97  if (_context == _contexts) fputc('\n', _stream);
98  fflush(_stream);
99 
100  if (_flatten >= _context) _flatten = 0;
101  *_context = seq;
102 }
103 
104 
105 // These four functions cannot be inlined as pointers to these
106 // functions are needed for operator << (_context_t) to work.
107 void json::flat(json & j) throw() { if (!j._flatten) j._flatten = j._context; }
108 void json::close(json & j) throw() { j.pop_context(); }
109 void json::object(json & j) throw() { j.push_context('{', '}'); }
110 void json::array(json & j) throw() { j.push_context('[', ']'); }
111 void json::item(json & j) throw()
112 {
113  while (j._context > j._contexts+1 && j._context[-1] != arr)
114  j.pop_context();
115 }
116 
117 
119 {
120  const char ctxt = _context[-1] == obj ? *_context == member ? seq : member : seq;
121  context(ctxt);
122  fprintf(_stream, "\"%s\"", s);
123  if (ctxt == member) fputc(' ', _stream);
124 
125  return *this;
126 }
127 
129 {
130  context(seq);
132  fputs("Infinity", _stream);
134  fputs("-Infinity", _stream);
135  else if (std::numeric_limits<json::number>::quiet_NaN() == f ||
136  std::numeric_limits<json::number>::signaling_NaN() == f)
137  fputs("NaN", _stream);
138  else
139  fprintf(_stream, "%g", f);
140  return *this;
141 }
142 json & json::operator << (json::integer d) throw() { context(seq); fprintf(_stream, FORMAT_INTMAX, intmax_t(d)); return *this; }
143 json & json::operator << (json::integer_u d) throw() { context(seq); fprintf(_stream, FORMAT_UINTMAX, uintmax_t(d)); return *this; }
144 json & json::operator << (json::boolean b) throw() { context(seq); fputs(b ? "true" : "false", _stream); return *this; }
145 json & json::operator << (std::nullptr_t) throw() { context(seq); fputs("null",_stream); return *this; }
146 
147 #endif
int ptrdiff_t
Definition: CPAL.d:3845
#define infinity
#define null
Definition: aptex-macros.h:127
const char * string
Definition: json.h:64
static void close(json &)
Definition: json.cpp:108
static void flat(json &)
Definition: json.cpp:107
char * _context
Definition: json.h:51
static void array(json &)
Definition: json.cpp:110
void indent(const int d=0)
Definition: json.cpp:67
double number
Definition: json.h:65
FILE *const _stream
Definition: json.h:49
static void object(json &)
Definition: json.cpp:109
json & operator<<(string)
Definition: json.cpp:118
static void item(json &)
Definition: json.cpp:111
char * _flatten
Definition: json.h:52
char _contexts[128]
Definition: json.h:50
#define b
Definition: jpegint.h:372
#define fflush
Definition: xxstdio.h:24
mpz_t * f
Definition: gen-fib.c:34
#define s
Definition: afcover.h:80
#define d(n)
Definition: gpos-common.c:151
assert(pcxLoadImage24((char *)((void *) 0), fp, pinfo, hdr))
int indent
Definition: main.c:118
#define fputs
Definition: mendex.h:67
#define fprintf
Definition: mendex.h:64
#define prefix
Definition: cwebboot.c:154
Definition: bits.h:30
static struct Point current
Definition: picttoppm.c:134
const char * suffix
Definition: pkg_icu.cpp:27
#define FORMAT_UINTMAX
Definition: json.cpp:41
#define FORMAT_INTMAX
Definition: json.cpp:40
bool
#define uintmax_t
Definition: stdint.in.h:352
#define intmax_t
Definition: stdint.in.h:338
int j
Definition: t4ht.c:1589