]> git.saurik.com Git - apple/dyld.git/blob - dyld3/JSONWriter.h
dyld-635.2.tar.gz
[apple/dyld.git] / dyld3 / JSONWriter.h
1 /*
2 * Copyright (c) 2017 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #include <string.h>
25
26 #include <string>
27 #include <map>
28 #include <vector>
29
30 namespace dyld3 {
31 namespace json {
32
33 struct Node
34 {
35 std::string value;
36 std::map<std::string, Node> map;
37 std::vector<Node> array;
38 };
39
40 static inline std::string hex(uint64_t value) {
41 char buff[64];
42 sprintf(buff, "0x%llX", value);
43 return buff;
44 }
45
46 static inline std::string hex4(uint64_t value) {
47 char buff[64];
48 sprintf(buff, "0x%04llX", value);
49 return buff;
50 }
51
52 static inline std::string hex8(uint64_t value) {
53 char buff[64];
54 sprintf(buff, "0x%08llX", value);
55 return buff;
56 }
57
58 static inline std::string decimal(uint64_t value) {
59 char buff[64];
60 sprintf(buff, "%llu", value);
61 return buff;
62 }
63
64 static inline void indentBy(uint32_t spaces, FILE* out) {
65 for (int i=0; i < spaces; ++i) {
66 fprintf(out, " ");
67 }
68 }
69
70 static inline void printJSON(const Node& node, uint32_t indent, FILE* out)
71 {
72 if ( !node.map.empty() ) {
73 fprintf(out, "{");
74 bool needComma = false;
75 for (const auto& entry : node.map) {
76 if ( needComma )
77 fprintf(out, ",");
78 fprintf(out, "\n");
79 indentBy(indent+2, out);
80 fprintf(out, "\"%s\": ", entry.first.c_str());
81 printJSON(entry.second, indent+2, out);
82 needComma = true;
83 }
84 fprintf(out, "\n");
85 indentBy(indent, out);
86 fprintf(out, "}");
87 }
88 else if ( !node.array.empty() ) {
89 fprintf(out, "[");
90 bool needComma = false;
91 for (const auto& entry : node.array) {
92 if ( needComma )
93 fprintf(out, ",");
94 fprintf(out, "\n");
95 indentBy(indent+2, out);
96 printJSON(entry, indent+2, out);
97 needComma = true;
98 }
99 fprintf(out, "\n");
100 indentBy(indent, out);
101 fprintf(out, "]");
102 }
103 else {
104 fprintf(out, "\"%s\"", node.value.c_str());
105 }
106 if ( indent == 0 )
107 fprintf(out, "\n");
108 }
109
110
111 } // namespace json
112 } // namespace dyld3