osgjs_conv
diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/main.c Thu Jun 02 14:25:37 2016 +0300 1.3 @@ -0,0 +1,152 @@ 1.4 +#include <stdio.h> 1.5 +#include <stdlib.h> 1.6 +#include <string.h> 1.7 +#include <errno.h> 1.8 +#include <jansson.h> 1.9 + 1.10 +void proc_osg_node(const char *node_type, json_t *node, int level); 1.11 +void proc_geometry(json_t *node, int level); 1.12 +void proc_primitive(json_t *prim, int level); 1.13 +json_t *find_node(json_t *node, const char *key); 1.14 + 1.15 +int main(int argc, char **argv) 1.16 +{ 1.17 + FILE *fp = stdin; 1.18 + json_t *root, *osgnode; 1.19 + json_error_t err; 1.20 + 1.21 + if(argc > 1) { 1.22 + if(!(fp = fopen(argv[1], "r"))) { 1.23 + fprintf(stderr, "failed to open osgjs file: %s: %s\n", argv[1], strerror(errno)); 1.24 + return 1; 1.25 + } 1.26 + } 1.27 + 1.28 + if(!(root = json_loadf(fp, 0, &err))) { 1.29 + fprintf(stderr, "json parsing error at %d:%d: %s\n", err.line, err.column, err.text); 1.30 + return 1; 1.31 + } 1.32 + if(!json_is_object(root)) { 1.33 + fprintf(stderr, "root node is not an object!\n"); 1.34 + return 1; 1.35 + } 1.36 + if(!(osgnode = json_object_get(root, "osg.Node"))) { 1.37 + fprintf(stderr, "root doesn't have an osg.Node field!\n"); 1.38 + return 1; 1.39 + } 1.40 + proc_osg_node("osg.Node", osgnode, 0); 1.41 + 1.42 + json_decref(root); 1.43 + return 0; 1.44 +} 1.45 + 1.46 +void indent(int n) 1.47 +{ 1.48 + while(n--) { 1.49 + putchar(' '); 1.50 + } 1.51 +} 1.52 + 1.53 +void proc_osg_node(const char *node_type, json_t *node, int level) 1.54 +{ 1.55 + json_t *children, *name_field; 1.56 + const char *name = 0; 1.57 + 1.58 + if((name_field = json_object_get(node, "Name"))) { 1.59 + if(json_is_string(name_field)) { 1.60 + name = json_string_value(name_field); 1.61 + } 1.62 + } 1.63 + 1.64 + indent(level); 1.65 + printf("%s: %s\n", node_type, name ? name : "<unnamed>"); 1.66 + 1.67 + if(strcmp(node_type, "osg.Geometry") == 0) { 1.68 + proc_geometry(node, level); 1.69 + } 1.70 + 1.71 + if((children = json_object_get(node, "Children")) && json_is_array(children)) { 1.72 + int i, num = json_array_size(children); 1.73 + 1.74 + for(i=0; i<num; i++) { 1.75 + void *fiter; 1.76 + json_t *child = json_array_get(children, i); 1.77 + if(!child) continue; 1.78 + 1.79 + fiter = json_object_iter(child); 1.80 + while(fiter) { 1.81 + const char *key = json_object_iter_key(fiter); 1.82 + if(strstr(key, "osg") == key) { /* starts with osg */ 1.83 + // recurse into new OSG node 1.84 + proc_osg_node(key, json_object_iter_value(fiter), level + 1); 1.85 + } 1.86 + fiter = json_object_iter_next(child, fiter); 1.87 + } 1.88 + } 1.89 + } 1.90 +} 1.91 + 1.92 +void proc_geometry(json_t *node, int level) 1.93 +{ 1.94 + json_t *prim_list; 1.95 + int i, num; 1.96 + 1.97 + if(!(prim_list = json_object_get(node, "PrimitiveSetList"))) { 1.98 + fprintf(stderr, "geometry node without a PrimitiveSetList array\n"); 1.99 + return; 1.100 + } 1.101 + if(!json_is_array(prim_list)) { 1.102 + fprintf(stderr, "PrimitiveSetList should be an array\n"); 1.103 + return; 1.104 + } 1.105 + num = json_array_size(prim_list); 1.106 + 1.107 + for(i=0; i<num; i++) { 1.108 + json_t *prim; 1.109 + 1.110 + prim = json_array_get(prim_list, i); 1.111 + if(!json_is_object(prim)) continue; 1.112 + 1.113 + proc_primitive(prim, level); 1.114 + } 1.115 +} 1.116 + 1.117 +void proc_primitive(json_t *prim, int level) 1.118 +{ 1.119 + json_t *mode; 1.120 + const char *mode_str; 1.121 + 1.122 + if(!(mode = json_object_get(prim, "Mode"))) { 1.123 + fprintf(stderr, "skipping PrimitiveSetList item without Mode\n"); 1.124 + return; 1.125 + } 1.126 + if(!json_is_string(mode) || !(mode_str = json_string_value(mode))) { 1.127 + fprintf(stderr, "primitive mode should be a string!\n"); 1.128 + return; 1.129 + } 1.130 +} 1.131 + 1.132 +json_t *find_node(json_t *node, const char *key) 1.133 +{ 1.134 + json_t *res; 1.135 + if(json_is_object(node)) { 1.136 + void *iter = json_object_iter(node); 1.137 + while(iter) { 1.138 + if(strcmp(json_object_iter_key(iter), key) == 0) { 1.139 + return json_object_iter_value(iter); 1.140 + } 1.141 + if((res = find_node(json_object_iter_value(iter), key))) { 1.142 + return res; 1.143 + } 1.144 + iter = json_object_iter_next(node, iter); 1.145 + } 1.146 + } else if(json_is_array(node)) { 1.147 + int i, num = json_array_size(node); 1.148 + for(i=0; i<num; i++) { 1.149 + if((res = find_node(json_array_get(node, i), key))) { 1.150 + return res; 1.151 + } 1.152 + } 1.153 + } 1.154 + return 0; 1.155 +}