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