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