Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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 debug logging |
michael@0 | 28 | // Author: Tim Eves |
michael@0 | 29 | |
michael@0 | 30 | #if !defined GRAPHITE2_NTRACING |
michael@0 | 31 | |
michael@0 | 32 | #include <stdio.h> |
michael@0 | 33 | #include "inc/json.h" |
michael@0 | 34 | |
michael@0 | 35 | using namespace graphite2; |
michael@0 | 36 | |
michael@0 | 37 | namespace |
michael@0 | 38 | { |
michael@0 | 39 | enum |
michael@0 | 40 | { |
michael@0 | 41 | seq = ',', |
michael@0 | 42 | obj='}', member=':', empty_obj='{', |
michael@0 | 43 | arr=']', empty_arr='[' |
michael@0 | 44 | }; |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | const json::_null_t json::null = {}; |
michael@0 | 48 | |
michael@0 | 49 | inline |
michael@0 | 50 | void json::context(const char current) throw() |
michael@0 | 51 | { |
michael@0 | 52 | fprintf(_stream, "%c", *_context); |
michael@0 | 53 | indent(); |
michael@0 | 54 | *_context = current; |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | |
michael@0 | 58 | void json::indent(const int d) throw() |
michael@0 | 59 | { |
michael@0 | 60 | if (*_context == member || (_flatten && _flatten < _context)) |
michael@0 | 61 | fputc(' ', _stream); |
michael@0 | 62 | else |
michael@0 | 63 | fprintf(_stream, "\n%*s", 4*int(_context - _contexts + d), ""); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | |
michael@0 | 67 | inline |
michael@0 | 68 | void json::push_context(const char prefix, const char suffix) throw() |
michael@0 | 69 | { |
michael@0 | 70 | assert(_context - _contexts < ptrdiff_t(sizeof _contexts)); |
michael@0 | 71 | |
michael@0 | 72 | if (_context == _contexts) |
michael@0 | 73 | *_context = suffix; |
michael@0 | 74 | else |
michael@0 | 75 | context(suffix); |
michael@0 | 76 | *++_context = prefix; |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | |
michael@0 | 80 | void json::pop_context() throw() |
michael@0 | 81 | { |
michael@0 | 82 | assert(_context > _contexts); |
michael@0 | 83 | |
michael@0 | 84 | if (*_context == seq) indent(-1); |
michael@0 | 85 | else fputc(*_context, _stream); |
michael@0 | 86 | |
michael@0 | 87 | fputc(*--_context, _stream); |
michael@0 | 88 | if (_context == _contexts) fputc('\n', _stream); |
michael@0 | 89 | fflush(_stream); |
michael@0 | 90 | |
michael@0 | 91 | if (_flatten >= _context) _flatten = 0; |
michael@0 | 92 | *_context = seq; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | |
michael@0 | 96 | // These four functions cannot be inlined as pointers to these |
michael@0 | 97 | // functions are needed for operator << (_context_t) to work. |
michael@0 | 98 | void json::flat(json & j) throw() { if (!j._flatten) j._flatten = j._context; } |
michael@0 | 99 | void json::close(json & j) throw() { j.pop_context(); } |
michael@0 | 100 | void json::object(json & j) throw() { j.push_context('{', '}'); } |
michael@0 | 101 | void json::array(json & j) throw() { j.push_context('[', ']'); } |
michael@0 | 102 | void json::item(json & j) throw() |
michael@0 | 103 | { |
michael@0 | 104 | while (j._context > j._contexts+1 && j._context[-1] != arr) |
michael@0 | 105 | j.pop_context(); |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | |
michael@0 | 109 | json & json::operator << (json::string s) throw() |
michael@0 | 110 | { |
michael@0 | 111 | const char ctxt = _context[-1] == obj ? *_context == member ? seq : member : seq; |
michael@0 | 112 | context(ctxt); |
michael@0 | 113 | fprintf(_stream, "\"%s\"", s); |
michael@0 | 114 | if (ctxt == member) fputc(' ', _stream); |
michael@0 | 115 | |
michael@0 | 116 | return *this; |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | json & json::operator << (json::number f) throw() { context(seq); fprintf(_stream, "%g", f); return *this; } |
michael@0 | 120 | json & json::operator << (json::integer d) throw() { context(seq); fprintf(_stream, "%ld", d); return *this; } |
michael@0 | 121 | json & json::operator << (long unsigned d) throw() { context(seq); fprintf(_stream, "%ld", d); return *this; } |
michael@0 | 122 | json & json::operator << (json::boolean b) throw() { context(seq); fputs(b ? "true" : "false", _stream); return *this; } |
michael@0 | 123 | json & json::operator << (json::_null_t) throw() { context(seq); fputs("null",_stream); return *this; } |
michael@0 | 124 | |
michael@0 | 125 | #endif |
michael@0 | 126 |