Changeset 5a7c0e6 in mainline
- Timestamp:
- 2012-07-27T01:09:05Z (12 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 6e34bd0
- Parents:
- 43788b2
- Location:
- uspace/app/bithenge
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bithenge/test.c
r43788b2 r5a7c0e6 69 69 bithenge_node_dec_ref(node); 70 70 } else { 71 bithenge_ transform_context_t context;71 bithenge_scope_t scope; 72 72 bithenge_transform_t *transform = NULL; 73 73 bithenge_node_t *node = NULL, *node2 = NULL; 74 74 75 bithenge_ transform_context_init(&context);75 bithenge_scope_init(&scope); 76 76 77 77 rc = bithenge_parse_script(argv[1], &transform); … … 87 87 } 88 88 89 rc = bithenge_transform_apply(transform, & context, node, &node2);89 rc = bithenge_transform_apply(transform, &scope, node, &node2); 90 90 if (rc != EOK) { 91 91 printf("Error applying transform: %s\n", str_error(rc)); … … 113 113 bithenge_node_dec_ref(node2); 114 114 bithenge_transform_dec_ref(transform); 115 bithenge_ transform_context_destroy(&context);115 bithenge_scope_destroy(&scope); 116 116 return 1; 117 117 } -
uspace/app/bithenge/transform.c
r43788b2 r5a7c0e6 61 61 } 62 62 63 static int ascii_apply(bithenge_transform_t *self, 64 bithenge_transform_context_t *context, bithenge_node_t *in, 65 bithenge_node_t **out) 63 static int ascii_apply(bithenge_transform_t *self, bithenge_scope_t *scope, 64 bithenge_node_t *in, bithenge_node_t **out) 66 65 { 67 66 int rc; … … 103 102 }; 104 103 105 static int prefix_length_1(bithenge_transform_t *self, 106 bithenge_transform_context_t *context, bithenge_blob_t *blob, 107 aoff64_t *out) 104 static int prefix_length_1(bithenge_transform_t *self, bithenge_scope_t *scope, 105 bithenge_blob_t *blob, aoff64_t *out) 108 106 { 109 107 *out = 1; … … 111 109 } 112 110 113 static int prefix_length_2(bithenge_transform_t *self, 114 bithenge_transform_context_t *context, bithenge_blob_t *blob, 115 aoff64_t *out) 111 static int prefix_length_2(bithenge_transform_t *self, bithenge_scope_t *scope, 112 bithenge_blob_t *blob, aoff64_t *out) 116 113 { 117 114 *out = 2; … … 119 116 } 120 117 121 static int prefix_length_4(bithenge_transform_t *self, 122 bithenge_transform_context_t *context, bithenge_blob_t *blob, 123 aoff64_t *out) 118 static int prefix_length_4(bithenge_transform_t *self, bithenge_scope_t *scope, 119 bithenge_blob_t *blob, aoff64_t *out) 124 120 { 125 121 *out = 4; … … 127 123 } 128 124 129 static int prefix_length_8(bithenge_transform_t *self, 130 bithenge_transform_context_t *context, bithenge_blob_t *blob, 131 aoff64_t *out) 125 static int prefix_length_8(bithenge_transform_t *self, bithenge_scope_t *scope, 126 bithenge_blob_t *blob, aoff64_t *out) 132 127 { 133 128 *out = 8; … … 137 132 #define MAKE_UINT_TRANSFORM(NAME, TYPE, ENDIAN, PREFIX_LENGTH_FUNC) \ 138 133 static int NAME##_apply(bithenge_transform_t *self, \ 139 bithenge_ transform_context_t *context, bithenge_node_t *in,\134 bithenge_scope_t *scope, bithenge_node_t *in, \ 140 135 bithenge_node_t **out) \ 141 136 { \ … … 176 171 177 172 static int zero_terminated_apply(bithenge_transform_t *self, 178 bithenge_transform_context_t *context, bithenge_node_t *in, 179 bithenge_node_t **out) 173 bithenge_scope_t *scope, bithenge_node_t *in, bithenge_node_t **out) 180 174 { 181 175 int rc; … … 201 195 202 196 static int zero_terminated_prefix_length(bithenge_transform_t *self, 203 bithenge_transform_context_t *context, bithenge_blob_t *blob, 204 aoff64_t *out) 197 bithenge_scope_t *scope, bithenge_blob_t *blob, aoff64_t *out) 205 198 { 206 199 int rc; … … 251 244 bithenge_node_t base; 252 245 struct struct_transform *transform; 253 bithenge_ transform_context_t *context;246 bithenge_scope_t *scope; 254 247 bithenge_blob_t *blob; 255 248 } struct_node_t; … … 281 274 282 275 static int struct_node_for_one(const char *name, 283 bithenge_transform_t *subxform, bithenge_ transform_context_t *context,276 bithenge_transform_t *subxform, bithenge_scope_t *scope, 284 277 bithenge_blob_t **blob, bithenge_for_each_func_t func, void *data) 285 278 { … … 288 281 289 282 aoff64_t sub_size; 290 rc = bithenge_transform_prefix_length(subxform, context, *blob,283 rc = bithenge_transform_prefix_length(subxform, scope, *blob, 291 284 &sub_size); 292 285 if (rc != EOK) … … 299 292 goto error; 300 293 301 rc = bithenge_transform_apply(subxform, context, subblob_node,294 rc = bithenge_transform_apply(subxform, scope, subblob_node, 302 295 &subxform_result); 303 296 bithenge_node_dec_ref(subblob_node); … … 357 350 for (size_t i = 0; subxforms[i].transform; i++) { 358 351 rc = struct_node_for_one(subxforms[i].name, 359 subxforms[i].transform, struct_node-> context, &blob, func,352 subxforms[i].transform, struct_node->scope, &blob, func, 360 353 data); 361 354 if (rc != EOK) … … 391 384 392 385 static int struct_transform_apply(bithenge_transform_t *base, 393 bithenge_transform_context_t *context, bithenge_node_t *in, 394 bithenge_node_t **out) 386 bithenge_scope_t *scope, bithenge_node_t *in, bithenge_node_t **out) 395 387 { 396 388 struct_transform_t *self = transform_as_struct(base); … … 409 401 node->transform = self; 410 402 bithenge_node_inc_ref(in); 411 node-> context = context;403 node->scope = scope; 412 404 node->blob = bithenge_node_as_blob(in); 413 405 *out = struct_as_node(node); … … 416 408 417 409 static int struct_transform_prefix_length(bithenge_transform_t *base, 418 bithenge_transform_context_t *context, bithenge_blob_t *blob, 419 aoff64_t *out) 410 bithenge_scope_t *scope, bithenge_blob_t *blob, aoff64_t *out) 420 411 { 421 412 struct_transform_t *self = transform_as_struct(base); … … 433 424 self->subtransforms[i].transform; 434 425 aoff64_t sub_size; 435 rc = bithenge_transform_prefix_length(subxform, context, blob,426 rc = bithenge_transform_prefix_length(subxform, scope, blob, 436 427 &sub_size); 437 428 if (rc != EOK) … … 517 508 } 518 509 519 static int compose_apply(bithenge_transform_t *base, 520 bithenge_transform_context_t *context, bithenge_node_t *in, 521 bithenge_node_t **out) 510 static int compose_apply(bithenge_transform_t *base, bithenge_scope_t *scope, 511 bithenge_node_t *in, bithenge_node_t **out) 522 512 { 523 513 int rc; … … 528 518 for (size_t i = self->num; i--; ) { 529 519 bithenge_node_t *tmp; 530 rc = bithenge_transform_apply(self->xforms[i], context, in,520 rc = bithenge_transform_apply(self->xforms[i], scope, in, 531 521 &tmp); 532 522 bithenge_node_dec_ref(in); … … 541 531 542 532 static int compose_prefix_length(bithenge_transform_t *base, 543 bithenge_transform_context_t *context, bithenge_blob_t *blob, 544 aoff64_t *out) 533 bithenge_scope_t *scope, bithenge_blob_t *blob, aoff64_t *out) 545 534 { 546 535 compose_transform_t *self = transform_as_compose(base); 547 536 return bithenge_transform_prefix_length(self->xforms[self->num - 1], 548 context, blob, out);537 scope, blob, out); 549 538 } 550 539 -
uspace/app/bithenge/transform.h
r43788b2 r5a7c0e6 51 51 typedef struct { 52 52 /** @privatesection */ 53 } bithenge_transform_context_t; 53 bithenge_node_t **params; 54 int num_params; 55 } bithenge_scope_t; 54 56 55 57 /** Operations that may be provided by a transform. */ 56 58 typedef struct bithenge_transform_ops { 57 59 /** @copydoc bithenge_transform_t::bithenge_transform_apply */ 58 int (*apply)(bithenge_transform_t *self, 59 bithenge_transform_context_t *context, bithenge_node_t *in, 60 bithenge_node_t **out); 60 int (*apply)(bithenge_transform_t *self, bithenge_scope_t *scope, 61 bithenge_node_t *in, bithenge_node_t **out); 61 62 /** @copydoc bithenge_transform_t::bithenge_transform_prefix_length */ 62 63 int (*prefix_length)(bithenge_transform_t *self, 63 bithenge_transform_context_t *context, bithenge_blob_t *blob, 64 aoff64_t *out); 64 bithenge_scope_t *scope, bithenge_blob_t *blob, aoff64_t *out); 65 65 /** Destroy the transform. 66 66 * @param self The transform. */ 67 67 void (*destroy)(bithenge_transform_t *self); 68 /** The number of parameters required. */ 69 int num_params; 68 70 } bithenge_transform_ops_t; 69 71 70 /** Initialize a transform context. It must be destroyed with @a 71 * bithenge_transform_context_destroy after it is used. 72 * @param[out] context The context to initialize. */ 73 static inline void bithenge_transform_context_init( 74 bithenge_transform_context_t *context) 75 { 76 } 77 78 /** Destroy a transform context. 79 * @param context The context to destroy. 80 * @return EOK on success or an error code from errno.h. */ 81 static inline void bithenge_transform_context_destroy( 82 bithenge_transform_context_t *context) 83 { 72 /** Initialize a transform scope. It must be destroyed with @a 73 * bithenge_scope_destroy after it is used. 74 * @param[out] scope The scope to initialize. */ 75 static inline void bithenge_scope_init(bithenge_scope_t *scope) 76 { 77 scope->params = NULL; 78 scope->num_params = 0; 79 } 80 81 /** Destroy a transform scope. 82 * @param scope The scope to destroy. 83 * @return EOK on success or an error code from errno.h. */ 84 static inline void bithenge_scope_destroy(bithenge_scope_t *scope) 85 { 86 for (int i = 0; i < scope->num_params; i++) 87 bithenge_node_dec_ref(scope->params[i]); 88 free(scope->params); 89 } 90 91 /** Allocate parameters. The parameters must then be set with @a 92 * bithenge_scope_set_param. 93 * @param scope The scope in which to allocate parameters. 94 * @param num_params The number of parameters to allocate. 95 * @return EOK on success or an error code from errno.h. */ 96 static inline int bithenge_scope_alloc_params(bithenge_scope_t *scope, 97 int num_params) 98 { 99 scope->params = malloc(sizeof(*scope->params) * num_params); 100 if (!scope->params) 101 return ENOMEM; 102 scope->num_params = num_params; 103 return EOK; 104 } 105 106 /** Set a parameter. Takes a reference to @a value. Note that range checking is 107 * not done in release builds. 108 * @param scope The scope in which to allocate parameters. 109 * @param i The index of the parameter to set. 110 * @param value The value to store in the parameter. 111 * @return EOK on success or an error code from errno.h. */ 112 static inline int bithenge_scope_set_param( bithenge_scope_t *scope, int i, 113 bithenge_node_t *node) 114 { 115 assert(scope); 116 assert(i >= 0 && i < scope->num_params); 117 scope->params[i] = node; 118 return EOK; 119 } 120 121 /** Get a parameter. Note that range checking is not done in release builds. 122 * @param scope The scope to get the parameter from. 123 * @param i The index of the parameter to set. 124 * @param[out] out Stores a new reference to the parameter. 125 * @return EOK on success or an error code from errno.h. */ 126 static inline int bithenge_scope_get_param(bithenge_scope_t *scope, int i, 127 bithenge_node_t **out) 128 { 129 assert(scope); 130 assert(i >= 0 && i < scope->num_params); 131 *out = scope->params[i]; 132 bithenge_node_inc_ref(*out); 133 return EOK; 134 } 135 136 /** Get the number of parameters required by a transform. Takes ownership of 137 * nothing. 138 * @param self The transform. 139 * @return The number of parameters required. */ 140 static inline int bithenge_transform_num_params(bithenge_transform_t *self) 141 { 142 assert(self); 143 assert(self->ops); 144 return self->ops->num_params; 84 145 } 85 146 … … 87 148 * @memberof bithenge_transform_t 88 149 * @param self The transform. 89 * @param context The context.150 * @param scope The scope. 90 151 * @param in The input tree. 91 152 * @param[out] out Where the output tree will be stored. 92 153 * @return EOK on success or an error code from errno.h. */ 93 154 static inline int bithenge_transform_apply(bithenge_transform_t *self, 94 bithenge_transform_context_t *context, bithenge_node_t *in, 95 bithenge_node_t **out) 96 { 97 assert(self); 98 assert(self->ops); 99 return self->ops->apply(self, context, in, out); 155 bithenge_scope_t *scope, bithenge_node_t *in, bithenge_node_t **out) 156 { 157 assert(self); 158 assert(self->ops); 159 return self->ops->apply(self, scope, in, out); 100 160 } 101 161 … … 106 166 * @memberof bithenge_transform_t 107 167 * @param self The transform. 108 * @param context The context.168 * @param scope The scope. 109 169 * @param blob The blob. 110 170 * @param[out] out Where the prefix length will be stored. … … 112 172 * errno.h. */ 113 173 static inline int bithenge_transform_prefix_length(bithenge_transform_t *self, 114 bithenge_transform_context_t *context, bithenge_blob_t *blob, 115 aoff64_t *out) 174 bithenge_scope_t *scope, bithenge_blob_t *blob, aoff64_t *out) 116 175 { 117 176 assert(self); … … 119 178 if (!self->ops->prefix_length) 120 179 return ENOTSUP; 121 return self->ops->prefix_length(self, context, blob, out);180 return self->ops->prefix_length(self, scope, blob, out); 122 181 } 123 182
Note:
See TracChangeset
for help on using the changeset viewer.