opentechinstitute / commotiond

The system management daemon for the Commotion Wireless Project.
https://commotionwireless.net
GNU Affero General Public License v3.0
26 stars 21 forks source link

pretty print nested trees and lists #89

Closed dismantl closed 10 years ago

dismantl commented 10 years ago

updated and added printing functions for lists and trees to output fully formatted JSON-style nested objects

to test: write test program, build a complex nested object using lists and objects and various basic types, and print it using co_list_print() or co_tree_print()!

Here's code for a sample object:

CHECK_MEM((params = co_tree16_create()));
CHECK(co_tree_insert(params,"testint",sizeof("testint"),co_int8_create(-123,0)),"Failed to insert testint into request tree");
co_tree_insert(params,"testuint",sizeof("testuint"),co_uint8_create(123,0));
co_tree_insert(params,"testfloat",sizeof("testfloat"),co_float32_create(123.567,0));
co_obj_t *tree2 = co_tree16_create();
co_obj_t *tree3 = co_tree16_create();
co_tree_insert(tree3,"subtree2",sizeof("subtree2"),co_str8_create("subtree",sizeof("subtree"),0));
co_tree_insert(tree3,"blah",sizeof("blah"),co_str8_create("blah",sizeof("blah"),0));
co_tree_insert(tree2,"subtree",sizeof("subtree"),tree3);
co_tree_insert(tree2,"foo",sizeof("foo"),co_str8_create("foo",sizeof("foo"),0));
co_obj_t *list = co_list16_create();
co_list_append(list,co_str8_create("bar",sizeof("bar"),0));
co_tree_insert(tree2,"bar",sizeof("bar"),list);
co_obj_t *tree4 = co_tree16_create();
co_tree_insert(tree4,"baz",sizeof("baz"),co_str8_create("baz",sizeof("baz"),0));
co_list_append(list,tree4);
co_obj_t *list2 = co_list16_create();
co_list_append(list2,co_str8_create("egg",sizeof("egg"),0));
co_list_append(list,list2);
CHECK(co_tree_insert(params,"test3",sizeof("test3"),tree2),"Failed to insert tree2 into request tree");
co_tree_print(params);

And here's what it should print out:

  {
    "test3": 
      {
        "bar": 
          [
            "bar",
            {
              "baz": "baz"
            },
            [
              "egg"
            ]
          ],
        "foo": "foo",
        "subtree": 
          {
            "blah": "blah",
            "subtree2": "subtree"
          }
      },
    "testfloat": 123.567001,
    "testint": -123,
    "testuint": 123
  }