1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/graphite2/src/inc/json.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,165 @@ 1.4 +/* GRAPHITE2 LICENSING 1.5 + 1.6 + Copyright 2011, SIL International 1.7 + All rights reserved. 1.8 + 1.9 + This library is free software; you can redistribute it and/or modify 1.10 + it under the terms of the GNU Lesser General Public License as published 1.11 + by the Free Software Foundation; either version 2.1 of License, or 1.12 + (at your option) any later version. 1.13 + 1.14 + This program is distributed in the hope that it will be useful, 1.15 + but WITHOUT ANY WARRANTY; without even the implied warranty of 1.16 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1.17 + Lesser General Public License for more details. 1.18 + 1.19 + You should also have received a copy of the GNU Lesser General Public 1.20 + License along with this library in the file named "LICENSE". 1.21 + If not, write to the Free Software Foundation, 51 Franklin Street, 1.22 + Suite 500, Boston, MA 02110-1335, USA or visit their web page on the 1.23 + internet at http://www.fsf.org/licenses/lgpl.html. 1.24 + 1.25 +Alternatively, the contents of this file may be used under the terms of the 1.26 +Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public 1.27 +License, as published by the Free Software Foundation, either version 2 1.28 +of the License or (at your option) any later version. 1.29 +*/ 1.30 +// JSON pretty printer for graphite font debug output logging. 1.31 +// Created on: 15 Dec 2011 1.32 +// Author: Tim Eves 1.33 + 1.34 +#pragma once 1.35 +#include "inc/Main.h" 1.36 +#include <cassert> 1.37 +#include <stdio.h> 1.38 + 1.39 +namespace graphite2 { 1.40 + 1.41 +class json 1.42 +{ 1.43 + // Prevent copying 1.44 + json(const json &); 1.45 + json & operator = (const json &); 1.46 + 1.47 + typedef void (*_context_t)(json &); 1.48 + class _null_t {}; 1.49 + 1.50 + FILE * const _stream; 1.51 + char _contexts[128], // context stack 1.52 + * _context, // current context (top of stack) 1.53 + * _flatten; // if !0 points to context above which 1.54 + // pretty printed output should occur. 1.55 + 1.56 + void context(const char current) throw(); 1.57 + void indent(const int d=0) throw(); 1.58 + void push_context(const char, const char) throw(); 1.59 + void pop_context() throw(); 1.60 + 1.61 +public: 1.62 + class closer; 1.63 + 1.64 + typedef const char * string; 1.65 + typedef double number; 1.66 + typedef long signed int integer; 1.67 + typedef bool boolean; 1.68 + static const _null_t null; 1.69 + 1.70 + static void flat(json &) throw(); 1.71 + static void close(json &) throw(); 1.72 + static void object(json &) throw(); 1.73 + static void array(json &) throw(); 1.74 + static void item(json &) throw(); 1.75 + 1.76 + json(FILE * stream) throw(); 1.77 + ~json() throw (); 1.78 + 1.79 + FILE * stream() const throw(); 1.80 + 1.81 + json & operator << (string) throw(); 1.82 + json & operator << (number) throw(); 1.83 + json & operator << (integer) throw(); 1.84 + json & operator << (long unsigned int d) throw(); 1.85 + json & operator << (boolean) throw(); 1.86 + json & operator << (_null_t) throw(); 1.87 + json & operator << (_context_t) throw(); 1.88 + 1.89 + operator bool() const throw(); 1.90 + bool good() const throw(); 1.91 + bool eof() const throw(); 1.92 + 1.93 + CLASS_NEW_DELETE; 1.94 +}; 1.95 + 1.96 +class json::closer 1.97 +{ 1.98 + // Prevent copying. 1.99 + closer(const closer &); 1.100 + closer & operator = (const closer &); 1.101 + 1.102 + json * const _j; 1.103 +public: 1.104 + closer(json * const j) : _j(j) {} 1.105 + ~closer() throw() { if (_j) *_j << close; } 1.106 +}; 1.107 + 1.108 +inline 1.109 +json::json(FILE * s) throw() 1.110 +: _stream(s), _context(_contexts), _flatten(0) 1.111 +{ 1.112 + if (good()) 1.113 + fflush(s); 1.114 +} 1.115 + 1.116 + 1.117 +inline 1.118 +json::~json() throw () 1.119 +{ 1.120 + while (_context > _contexts) pop_context(); 1.121 +} 1.122 + 1.123 +inline 1.124 +FILE * json::stream() const throw() { return _stream; } 1.125 + 1.126 + 1.127 +inline 1.128 +json & json::operator << (json::_context_t ctxt) throw() 1.129 +{ 1.130 + ctxt(*this); 1.131 + return *this; 1.132 +} 1.133 + 1.134 +inline 1.135 +json & operator << (json & j, signed char d) throw() { return j << json::integer(d); } 1.136 + 1.137 +inline 1.138 +json & operator << (json & j, short signed int d) throw() { return j << json::integer(d); } 1.139 + 1.140 +inline 1.141 +json & operator << (json & j, signed int d) throw() { return j << json::integer(d); } 1.142 + 1.143 +inline 1.144 +json & operator << (json & j, unsigned char d) throw() { return j << json::integer(d); } 1.145 + 1.146 +inline 1.147 +json & operator << (json & j, short unsigned int d) throw() { return j << json::integer(d); } 1.148 + 1.149 +inline 1.150 +json & operator << (json & j, unsigned int d) throw() { return j << json::integer(d); } 1.151 + 1.152 +inline 1.153 +json & operator << (json & j, char c) throw () 1.154 +{ 1.155 + const char str[2] = {c,0}; 1.156 + return j << str; 1.157 +} 1.158 + 1.159 +inline 1.160 +json::operator bool() const throw() { return good(); } 1.161 + 1.162 +inline 1.163 +bool json::good() const throw() { return _stream && ferror(_stream) == 0; } 1.164 + 1.165 +inline 1.166 +bool json::eof() const throw() { return feof(_stream) != 0; } 1.167 + 1.168 +} // namespace graphite2