osgjs_conv
changeset 0:75807c6b939b tip
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 02 Jun 2016 14:25:37 +0300 |
parents | |
children | |
files | Makefile src/main.c |
diffstat | 2 files changed, 166 insertions(+), 0 deletions(-) [+] |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/Makefile Thu Jun 02 14:25:37 2016 +0300 1.3 @@ -0,0 +1,14 @@ 1.4 +src = $(wildcard src/*.c) 1.5 +obj = $(src:.c=.o) 1.6 + 1.7 +bin = test 1.8 + 1.9 +CFLAGS = -pedantic -Wall -g 1.10 +LDFLAGS = -ljansson 1.11 + 1.12 +$(bin): $(obj) 1.13 + $(CC) -o $@ $(obj) $(LDFLAGS) 1.14 + 1.15 +.PHONY: clean 1.16 +clean: 1.17 + rm -f $(obj) $(bin)
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/src/main.c Thu Jun 02 14:25:37 2016 +0300 2.3 @@ -0,0 +1,152 @@ 2.4 +#include <stdio.h> 2.5 +#include <stdlib.h> 2.6 +#include <string.h> 2.7 +#include <errno.h> 2.8 +#include <jansson.h> 2.9 + 2.10 +void proc_osg_node(const char *node_type, json_t *node, int level); 2.11 +void proc_geometry(json_t *node, int level); 2.12 +void proc_primitive(json_t *prim, int level); 2.13 +json_t *find_node(json_t *node, const char *key); 2.14 + 2.15 +int main(int argc, char **argv) 2.16 +{ 2.17 + FILE *fp = stdin; 2.18 + json_t *root, *osgnode; 2.19 + json_error_t err; 2.20 + 2.21 + if(argc > 1) { 2.22 + if(!(fp = fopen(argv[1], "r"))) { 2.23 + fprintf(stderr, "failed to open osgjs file: %s: %s\n", argv[1], strerror(errno)); 2.24 + return 1; 2.25 + } 2.26 + } 2.27 + 2.28 + if(!(root = json_loadf(fp, 0, &err))) { 2.29 + fprintf(stderr, "json parsing error at %d:%d: %s\n", err.line, err.column, err.text); 2.30 + return 1; 2.31 + } 2.32 + if(!json_is_object(root)) { 2.33 + fprintf(stderr, "root node is not an object!\n"); 2.34 + return 1; 2.35 + } 2.36 + if(!(osgnode = json_object_get(root, "osg.Node"))) { 2.37 + fprintf(stderr, "root doesn't have an osg.Node field!\n"); 2.38 + return 1; 2.39 + } 2.40 + proc_osg_node("osg.Node", osgnode, 0); 2.41 + 2.42 + json_decref(root); 2.43 + return 0; 2.44 +} 2.45 + 2.46 +void indent(int n) 2.47 +{ 2.48 + while(n--) { 2.49 + putchar(' '); 2.50 + } 2.51 +} 2.52 + 2.53 +void proc_osg_node(const char *node_type, json_t *node, int level) 2.54 +{ 2.55 + json_t *children, *name_field; 2.56 + const char *name = 0; 2.57 + 2.58 + if((name_field = json_object_get(node, "Name"))) { 2.59 + if(json_is_string(name_field)) { 2.60 + name = json_string_value(name_field); 2.61 + } 2.62 + } 2.63 + 2.64 + indent(level); 2.65 + printf("%s: %s\n", node_type, name ? name : "<unnamed>"); 2.66 + 2.67 + if(strcmp(node_type, "osg.Geometry") == 0) { 2.68 + proc_geometry(node, level); 2.69 + } 2.70 + 2.71 + if((children = json_object_get(node, "Children")) && json_is_array(children)) { 2.72 + int i, num = json_array_size(children); 2.73 + 2.74 + for(i=0; i<num; i++) { 2.75 + void *fiter; 2.76 + json_t *child = json_array_get(children, i); 2.77 + if(!child) continue; 2.78 + 2.79 + fiter = json_object_iter(child); 2.80 + while(fiter) { 2.81 + const char *key = json_object_iter_key(fiter); 2.82 + if(strstr(key, "osg") == key) { /* starts with osg */ 2.83 + // recurse into new OSG node 2.84 + proc_osg_node(key, json_object_iter_value(fiter), level + 1); 2.85 + } 2.86 + fiter = json_object_iter_next(child, fiter); 2.87 + } 2.88 + } 2.89 + } 2.90 +} 2.91 + 2.92 +void proc_geometry(json_t *node, int level) 2.93 +{ 2.94 + json_t *prim_list; 2.95 + int i, num; 2.96 + 2.97 + if(!(prim_list = json_object_get(node, "PrimitiveSetList"))) { 2.98 + fprintf(stderr, "geometry node without a PrimitiveSetList array\n"); 2.99 + return; 2.100 + } 2.101 + if(!json_is_array(prim_list)) { 2.102 + fprintf(stderr, "PrimitiveSetList should be an array\n"); 2.103 + return; 2.104 + } 2.105 + num = json_array_size(prim_list); 2.106 + 2.107 + for(i=0; i<num; i++) { 2.108 + json_t *prim; 2.109 + 2.110 + prim = json_array_get(prim_list, i); 2.111 + if(!json_is_object(prim)) continue; 2.112 + 2.113 + proc_primitive(prim, level); 2.114 + } 2.115 +} 2.116 + 2.117 +void proc_primitive(json_t *prim, int level) 2.118 +{ 2.119 + json_t *mode; 2.120 + const char *mode_str; 2.121 + 2.122 + if(!(mode = json_object_get(prim, "Mode"))) { 2.123 + fprintf(stderr, "skipping PrimitiveSetList item without Mode\n"); 2.124 + return; 2.125 + } 2.126 + if(!json_is_string(mode) || !(mode_str = json_string_value(mode))) { 2.127 + fprintf(stderr, "primitive mode should be a string!\n"); 2.128 + return; 2.129 + } 2.130 +} 2.131 + 2.132 +json_t *find_node(json_t *node, const char *key) 2.133 +{ 2.134 + json_t *res; 2.135 + if(json_is_object(node)) { 2.136 + void *iter = json_object_iter(node); 2.137 + while(iter) { 2.138 + if(strcmp(json_object_iter_key(iter), key) == 0) { 2.139 + return json_object_iter_value(iter); 2.140 + } 2.141 + if((res = find_node(json_object_iter_value(iter), key))) { 2.142 + return res; 2.143 + } 2.144 + iter = json_object_iter_next(node, iter); 2.145 + } 2.146 + } else if(json_is_array(node)) { 2.147 + int i, num = json_array_size(node); 2.148 + for(i=0; i<num; i++) { 2.149 + if((res = find_node(json_array_get(node, i), key))) { 2.150 + return res; 2.151 + } 2.152 + } 2.153 + } 2.154 + return 0; 2.155 +}