Changeset 5f679702 in mainline
- Timestamp:
- 2012-06-07T04:03:16Z (12 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 5c925ce
- Parents:
- 520acaf
- Location:
- uspace/app/bithenge
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bithenge/print.c
r520acaf r5f679702 68 68 } 69 69 70 static int print_internal(bithenge_print_type_t type, bithenge_ internal_node_t *node)70 static int print_internal(bithenge_print_type_t type, bithenge_node_t *node) 71 71 { 72 72 int rc; … … 80 80 } 81 81 82 static int print_boolean(bithenge_print_type_t type, bithenge_ boolean_node_t *node)82 static int print_boolean(bithenge_print_type_t type, bithenge_node_t *node) 83 83 { 84 84 bool value = bithenge_boolean_node_value(node); … … 94 94 } 95 95 96 static int print_integer(bithenge_print_type_t type, bithenge_ integer_node_t *node)96 static int print_integer(bithenge_print_type_t type, bithenge_node_t *node) 97 97 { 98 98 bithenge_int_t value = bithenge_integer_node_value(node); … … 101 101 } 102 102 103 static int print_string(bithenge_print_type_t type, bithenge_ string_node_t *node)103 static int print_string(bithenge_print_type_t type, bithenge_node_t *node) 104 104 { 105 105 size_t off = 0; … … 126 126 return EINVAL; 127 127 case BITHENGE_NODE_INTERNAL: 128 return print_internal(type, bithenge_as_internal_node(tree));128 return print_internal(type, tree); 129 129 case BITHENGE_NODE_BOOLEAN: 130 return print_boolean(type, bithenge_as_boolean_node(tree));130 return print_boolean(type, tree); 131 131 case BITHENGE_NODE_INTEGER: 132 return print_integer(type, bithenge_as_integer_node(tree));132 return print_integer(type, tree); 133 133 case BITHENGE_NODE_STRING: 134 return print_string(type, bithenge_as_string_node(tree));134 return print_string(type, tree); 135 135 } 136 136 return ENOTSUP; -
uspace/app/bithenge/tree.c
r520acaf r5f679702 41 41 int bithenge_node_destroy(bithenge_node_t *node) 42 42 { 43 bithenge_string_node_t *string_node;44 43 switch (bithenge_node_type(node)) { 45 44 case BITHENGE_NODE_STRING: 46 string_node = bithenge_as_string_node(node); 47 if (string_node->needs_free) 48 free(string_node->value); 45 if (node->string_value.needs_free) 46 free(node->string_value.ptr); 49 47 break; 50 48 case BITHENGE_NODE_INTERNAL: … … 52 50 break; 53 51 case BITHENGE_NODE_BOOLEAN: 54 return EOK; 52 return EOK; // the boolean nodes are allocated statically below 55 53 case BITHENGE_NODE_INTEGER: /* pass-through */ 56 54 case BITHENGE_NODE_NONE: … … 63 61 typedef struct 64 62 { 65 bithenge_ internal_node_t base;63 bithenge_node_t base; 66 64 bithenge_node_t **nodes; 67 65 bithenge_int_t len; … … 69 67 } simple_internal_node_t; 70 68 71 static simple_internal_node_t * internal_as_simple(bithenge_internal_node_t *node)69 static simple_internal_node_t *node_as_simple(bithenge_node_t *node) 72 70 { 73 71 return (simple_internal_node_t *)node; 74 72 } 75 73 76 static int simple_internal_node_for_each(bithenge_ internal_node_t *base, bithenge_for_each_func_t func, void *data)74 static int simple_internal_node_for_each(bithenge_node_t *base, bithenge_for_each_func_t func, void *data) 77 75 { 78 76 int rc; 79 simple_internal_node_t *node = internal_as_simple(base);77 simple_internal_node_t *node = node_as_simple(base); 80 78 for (bithenge_int_t i = 0; i < node->len; i++) { 81 79 rc = func(node->nodes[2*i+0], node->nodes[2*i+1], data); … … 92 90 static bithenge_node_t *simple_internal_as_node(simple_internal_node_t *node) 93 91 { 94 return bithenge_internal_as_node(&node->base);92 return &node->base; 95 93 } 96 94 … … 101 99 if (!node) 102 100 return ENOMEM; 103 node->base. base.type = BITHENGE_NODE_INTERNAL;104 node->base. ops = &simple_internal_node_ops;101 node->base.type = BITHENGE_NODE_INTERNAL; 102 node->base.internal_ops = &simple_internal_node_ops; 105 103 node->nodes = nodes; 106 104 node->len = len; … … 110 108 } 111 109 112 static bithenge_ boolean_node_t false_node = { {BITHENGE_NODE_BOOLEAN},false };113 static bithenge_ boolean_node_t true_node = { {BITHENGE_NODE_BOOLEAN},true };110 static bithenge_node_t false_node = { BITHENGE_NODE_BOOLEAN, .boolean_value = false }; 111 static bithenge_node_t true_node = { BITHENGE_NODE_BOOLEAN, .boolean_value = true }; 114 112 115 113 int bithenge_new_boolean_node(bithenge_node_t **out, bool value) 116 114 { 117 115 assert(out); 118 *out = bithenge_boolean_as_node(value ? &true_node : &false_node);116 *out = value ? &true_node : &false_node; 119 117 return EOK; 120 118 } … … 123 121 { 124 122 assert(out); 125 bithenge_ integer_node_t *node = malloc(sizeof(*node));123 bithenge_node_t *node = malloc(sizeof(*node)); 126 124 if (!node) 127 125 return ENOMEM; 128 node-> base.type = BITHENGE_NODE_INTEGER;129 node-> value = value;130 *out = bithenge_integer_as_node(node);126 node->type = BITHENGE_NODE_INTEGER; 127 node->integer_value = value; 128 *out = node; 131 129 return EOK; 132 130 } … … 135 133 { 136 134 assert(out); 137 bithenge_ string_node_t *node = malloc(sizeof(*node));135 bithenge_node_t *node = malloc(sizeof(*node)); 138 136 if (!node) 139 137 return ENOMEM; 140 node-> base.type = BITHENGE_NODE_STRING;141 node-> value= value;142 node-> needs_free = needs_free;143 *out = bithenge_string_as_node(node);138 node->type = BITHENGE_NODE_STRING; 139 node->string_value.ptr = value; 140 node->string_value.needs_free = needs_free; 141 *out = node; 144 142 return EOK; 145 143 } -
uspace/app/bithenge/tree.h
r520acaf r5f679702 62 62 typedef struct bithenge_node_t { 63 63 bithenge_node_type_t type; 64 union { 65 struct bithenge_internal_node_ops_t *internal_ops; 66 bool boolean_value; 67 bithenge_int_t integer_value; 68 struct { 69 const char *ptr; 70 bool needs_free; 71 } string_value; 72 }; 64 73 } bithenge_node_t; 65 74 … … 71 80 typedef int (*bithenge_for_each_func_t)(bithenge_node_t *key, bithenge_node_t *value, void *data); 72 81 73 typedef struct {74 bithenge_node_t base;75 const struct bithenge_internal_node_ops_t *ops;76 } bithenge_internal_node_t;77 78 82 typedef struct bithenge_internal_node_ops_t { 79 int (*for_each)(bithenge_ internal_node_t *node, bithenge_for_each_func_t func, void *data);83 int (*for_each)(bithenge_node_t *node, bithenge_for_each_func_t func, void *data); 80 84 } bithenge_internal_node_ops_t; 81 85 82 typedef struct { 83 bithenge_node_t base; 84 bool value; 85 } bithenge_boolean_node_t; 86 87 typedef struct { 88 bithenge_node_t base; 89 bithenge_int_t value; 90 } bithenge_integer_node_t; 91 92 typedef struct { 93 bithenge_node_t base; 94 const char *value; 95 bool needs_free; 96 } bithenge_string_node_t; 97 98 static inline bithenge_node_t *bithenge_internal_as_node(bithenge_internal_node_t *node) 86 static inline int bithenge_node_for_each(bithenge_node_t *node, bithenge_for_each_func_t func, void *data) 99 87 { 100 return &node->base;88 return node->internal_ops->for_each(node, func, data); 101 89 } 102 90 103 static inline b ithenge_internal_node_t *bithenge_as_internal_node(bithenge_node_t *node)91 static inline bool bithenge_boolean_node_value(bithenge_node_t *node) 104 92 { 105 assert(node->type == BITHENGE_NODE_INTERNAL); 106 return (bithenge_internal_node_t *)node; 93 return node->boolean_value; 107 94 } 108 95 109 static inline int bithenge_node_for_each(bithenge_internal_node_t *node, bithenge_for_each_func_t func, void *data)96 static inline bithenge_int_t bithenge_integer_node_value(bithenge_node_t *node) 110 97 { 111 return node-> ops->for_each(node, func, data);98 return node->integer_value; 112 99 } 113 100 114 static inline bithenge_node_t *bithenge_boolean_as_node(bithenge_boolean_node_t *node)101 static inline const char *bithenge_string_node_value(bithenge_node_t *node) 115 102 { 116 return &node->base; 117 } 118 119 static inline bithenge_boolean_node_t *bithenge_as_boolean_node(bithenge_node_t *node) 120 { 121 assert(node->type == BITHENGE_NODE_BOOLEAN); 122 return (bithenge_boolean_node_t *)node; 123 } 124 125 static inline bool bithenge_boolean_node_value(bithenge_boolean_node_t *node) 126 { 127 return node->value; 128 } 129 130 static inline bithenge_node_t *bithenge_integer_as_node(bithenge_integer_node_t *node) 131 { 132 return &node->base; 133 } 134 135 static inline bithenge_integer_node_t *bithenge_as_integer_node(bithenge_node_t *node) 136 { 137 assert(node->type == BITHENGE_NODE_INTEGER); 138 return (bithenge_integer_node_t *)node; 139 } 140 141 static inline bithenge_int_t bithenge_integer_node_value(bithenge_integer_node_t *node) 142 { 143 return node->value; 144 } 145 146 static inline bithenge_node_t *bithenge_string_as_node(bithenge_string_node_t *node) 147 { 148 return &node->base; 149 } 150 151 static inline bithenge_string_node_t *bithenge_as_string_node(bithenge_node_t *node) 152 { 153 assert(node->type == BITHENGE_NODE_STRING); 154 return (bithenge_string_node_t *)node; 155 } 156 157 static inline const char *bithenge_string_node_value(bithenge_string_node_t *node) 158 { 159 return node->value; 103 return node->string_value.ptr; 160 104 } 161 105
Note:
See TracChangeset
for help on using the changeset viewer.