rev |
line source |
nuclear@0
|
1 #include <stdio.h>
|
nuclear@0
|
2 #include <stdlib.h>
|
nuclear@0
|
3 #include <string.h>
|
nuclear@0
|
4 #include <errno.h>
|
nuclear@0
|
5 #include <jansson.h>
|
nuclear@0
|
6
|
nuclear@0
|
7 void proc_osg_node(const char *node_type, json_t *node, int level);
|
nuclear@0
|
8 void proc_geometry(json_t *node, int level);
|
nuclear@0
|
9 void proc_primitive(json_t *prim, int level);
|
nuclear@0
|
10 json_t *find_node(json_t *node, const char *key);
|
nuclear@0
|
11
|
nuclear@0
|
12 int main(int argc, char **argv)
|
nuclear@0
|
13 {
|
nuclear@0
|
14 FILE *fp = stdin;
|
nuclear@0
|
15 json_t *root, *osgnode;
|
nuclear@0
|
16 json_error_t err;
|
nuclear@0
|
17
|
nuclear@0
|
18 if(argc > 1) {
|
nuclear@0
|
19 if(!(fp = fopen(argv[1], "r"))) {
|
nuclear@0
|
20 fprintf(stderr, "failed to open osgjs file: %s: %s\n", argv[1], strerror(errno));
|
nuclear@0
|
21 return 1;
|
nuclear@0
|
22 }
|
nuclear@0
|
23 }
|
nuclear@0
|
24
|
nuclear@0
|
25 if(!(root = json_loadf(fp, 0, &err))) {
|
nuclear@0
|
26 fprintf(stderr, "json parsing error at %d:%d: %s\n", err.line, err.column, err.text);
|
nuclear@0
|
27 return 1;
|
nuclear@0
|
28 }
|
nuclear@0
|
29 if(!json_is_object(root)) {
|
nuclear@0
|
30 fprintf(stderr, "root node is not an object!\n");
|
nuclear@0
|
31 return 1;
|
nuclear@0
|
32 }
|
nuclear@0
|
33 if(!(osgnode = json_object_get(root, "osg.Node"))) {
|
nuclear@0
|
34 fprintf(stderr, "root doesn't have an osg.Node field!\n");
|
nuclear@0
|
35 return 1;
|
nuclear@0
|
36 }
|
nuclear@0
|
37 proc_osg_node("osg.Node", osgnode, 0);
|
nuclear@0
|
38
|
nuclear@0
|
39 json_decref(root);
|
nuclear@0
|
40 return 0;
|
nuclear@0
|
41 }
|
nuclear@0
|
42
|
nuclear@0
|
43 void indent(int n)
|
nuclear@0
|
44 {
|
nuclear@0
|
45 while(n--) {
|
nuclear@0
|
46 putchar(' ');
|
nuclear@0
|
47 }
|
nuclear@0
|
48 }
|
nuclear@0
|
49
|
nuclear@0
|
50 void proc_osg_node(const char *node_type, json_t *node, int level)
|
nuclear@0
|
51 {
|
nuclear@0
|
52 json_t *children, *name_field;
|
nuclear@0
|
53 const char *name = 0;
|
nuclear@0
|
54
|
nuclear@0
|
55 if((name_field = json_object_get(node, "Name"))) {
|
nuclear@0
|
56 if(json_is_string(name_field)) {
|
nuclear@0
|
57 name = json_string_value(name_field);
|
nuclear@0
|
58 }
|
nuclear@0
|
59 }
|
nuclear@0
|
60
|
nuclear@0
|
61 indent(level);
|
nuclear@0
|
62 printf("%s: %s\n", node_type, name ? name : "<unnamed>");
|
nuclear@0
|
63
|
nuclear@0
|
64 if(strcmp(node_type, "osg.Geometry") == 0) {
|
nuclear@0
|
65 proc_geometry(node, level);
|
nuclear@0
|
66 }
|
nuclear@0
|
67
|
nuclear@0
|
68 if((children = json_object_get(node, "Children")) && json_is_array(children)) {
|
nuclear@0
|
69 int i, num = json_array_size(children);
|
nuclear@0
|
70
|
nuclear@0
|
71 for(i=0; i<num; i++) {
|
nuclear@0
|
72 void *fiter;
|
nuclear@0
|
73 json_t *child = json_array_get(children, i);
|
nuclear@0
|
74 if(!child) continue;
|
nuclear@0
|
75
|
nuclear@0
|
76 fiter = json_object_iter(child);
|
nuclear@0
|
77 while(fiter) {
|
nuclear@0
|
78 const char *key = json_object_iter_key(fiter);
|
nuclear@0
|
79 if(strstr(key, "osg") == key) { /* starts with osg */
|
nuclear@0
|
80 // recurse into new OSG node
|
nuclear@0
|
81 proc_osg_node(key, json_object_iter_value(fiter), level + 1);
|
nuclear@0
|
82 }
|
nuclear@0
|
83 fiter = json_object_iter_next(child, fiter);
|
nuclear@0
|
84 }
|
nuclear@0
|
85 }
|
nuclear@0
|
86 }
|
nuclear@0
|
87 }
|
nuclear@0
|
88
|
nuclear@0
|
89 void proc_geometry(json_t *node, int level)
|
nuclear@0
|
90 {
|
nuclear@0
|
91 json_t *prim_list;
|
nuclear@0
|
92 int i, num;
|
nuclear@0
|
93
|
nuclear@0
|
94 if(!(prim_list = json_object_get(node, "PrimitiveSetList"))) {
|
nuclear@0
|
95 fprintf(stderr, "geometry node without a PrimitiveSetList array\n");
|
nuclear@0
|
96 return;
|
nuclear@0
|
97 }
|
nuclear@0
|
98 if(!json_is_array(prim_list)) {
|
nuclear@0
|
99 fprintf(stderr, "PrimitiveSetList should be an array\n");
|
nuclear@0
|
100 return;
|
nuclear@0
|
101 }
|
nuclear@0
|
102 num = json_array_size(prim_list);
|
nuclear@0
|
103
|
nuclear@0
|
104 for(i=0; i<num; i++) {
|
nuclear@0
|
105 json_t *prim;
|
nuclear@0
|
106
|
nuclear@0
|
107 prim = json_array_get(prim_list, i);
|
nuclear@0
|
108 if(!json_is_object(prim)) continue;
|
nuclear@0
|
109
|
nuclear@0
|
110 proc_primitive(prim, level);
|
nuclear@0
|
111 }
|
nuclear@0
|
112 }
|
nuclear@0
|
113
|
nuclear@0
|
114 void proc_primitive(json_t *prim, int level)
|
nuclear@0
|
115 {
|
nuclear@0
|
116 json_t *mode;
|
nuclear@0
|
117 const char *mode_str;
|
nuclear@0
|
118
|
nuclear@0
|
119 if(!(mode = json_object_get(prim, "Mode"))) {
|
nuclear@0
|
120 fprintf(stderr, "skipping PrimitiveSetList item without Mode\n");
|
nuclear@0
|
121 return;
|
nuclear@0
|
122 }
|
nuclear@0
|
123 if(!json_is_string(mode) || !(mode_str = json_string_value(mode))) {
|
nuclear@0
|
124 fprintf(stderr, "primitive mode should be a string!\n");
|
nuclear@0
|
125 return;
|
nuclear@0
|
126 }
|
nuclear@0
|
127 }
|
nuclear@0
|
128
|
nuclear@0
|
129 json_t *find_node(json_t *node, const char *key)
|
nuclear@0
|
130 {
|
nuclear@0
|
131 json_t *res;
|
nuclear@0
|
132 if(json_is_object(node)) {
|
nuclear@0
|
133 void *iter = json_object_iter(node);
|
nuclear@0
|
134 while(iter) {
|
nuclear@0
|
135 if(strcmp(json_object_iter_key(iter), key) == 0) {
|
nuclear@0
|
136 return json_object_iter_value(iter);
|
nuclear@0
|
137 }
|
nuclear@0
|
138 if((res = find_node(json_object_iter_value(iter), key))) {
|
nuclear@0
|
139 return res;
|
nuclear@0
|
140 }
|
nuclear@0
|
141 iter = json_object_iter_next(node, iter);
|
nuclear@0
|
142 }
|
nuclear@0
|
143 } else if(json_is_array(node)) {
|
nuclear@0
|
144 int i, num = json_array_size(node);
|
nuclear@0
|
145 for(i=0; i<num; i++) {
|
nuclear@0
|
146 if((res = find_node(json_array_get(node, i), key))) {
|
nuclear@0
|
147 return res;
|
nuclear@0
|
148 }
|
nuclear@0
|
149 }
|
nuclear@0
|
150 }
|
nuclear@0
|
151 return 0;
|
nuclear@0
|
152 }
|