Changeset 43788b2 in mainline
- Timestamp:
- 2012-07-26T22:26:19Z (12 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 5a7c0e6
- Parents:
- 842ed146
- Location:
- uspace/app/bithenge
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bithenge/test.c
r842ed146 r43788b2 69 69 bithenge_node_dec_ref(node); 70 70 } else { 71 bithenge_transform_context_t context; 71 72 bithenge_transform_t *transform = NULL; 72 73 bithenge_node_t *node = NULL, *node2 = NULL; 74 75 bithenge_transform_context_init(&context); 76 73 77 rc = bithenge_parse_script(argv[1], &transform); 74 78 if (rc != EOK) { … … 83 87 } 84 88 85 rc = bithenge_transform_apply(transform, node, &node2);89 rc = bithenge_transform_apply(transform, &context, node, &node2); 86 90 if (rc != EOK) { 87 91 printf("Error applying transform: %s\n", str_error(rc)); … … 109 113 bithenge_node_dec_ref(node2); 110 114 bithenge_transform_dec_ref(transform); 115 bithenge_transform_context_destroy(&context); 111 116 return 1; 112 117 } -
uspace/app/bithenge/transform.c
r842ed146 r43788b2 62 62 63 63 static int ascii_apply(bithenge_transform_t *self, 64 bithenge_node_t *in, bithenge_node_t **out) 64 bithenge_transform_context_t *context, bithenge_node_t *in, 65 bithenge_node_t **out) 65 66 { 66 67 int rc; … … 102 103 }; 103 104 104 static int prefix_length_1(bithenge_transform_t *self, bithenge_blob_t *blob, 105 static int prefix_length_1(bithenge_transform_t *self, 106 bithenge_transform_context_t *context, bithenge_blob_t *blob, 105 107 aoff64_t *out) 106 108 { … … 109 111 } 110 112 111 static int prefix_length_2(bithenge_transform_t *self, bithenge_blob_t *blob, 113 static int prefix_length_2(bithenge_transform_t *self, 114 bithenge_transform_context_t *context, bithenge_blob_t *blob, 112 115 aoff64_t *out) 113 116 { … … 116 119 } 117 120 118 static int prefix_length_4(bithenge_transform_t *self, bithenge_blob_t *blob, 121 static int prefix_length_4(bithenge_transform_t *self, 122 bithenge_transform_context_t *context, bithenge_blob_t *blob, 119 123 aoff64_t *out) 120 124 { … … 123 127 } 124 128 125 static int prefix_length_8(bithenge_transform_t *self, bithenge_blob_t *blob, 129 static int prefix_length_8(bithenge_transform_t *self, 130 bithenge_transform_context_t *context, bithenge_blob_t *blob, 126 131 aoff64_t *out) 127 132 { … … 132 137 #define MAKE_UINT_TRANSFORM(NAME, TYPE, ENDIAN, PREFIX_LENGTH_FUNC) \ 133 138 static int NAME##_apply(bithenge_transform_t *self, \ 134 bithenge_node_t *in, bithenge_node_t **out) \ 139 bithenge_transform_context_t *context, bithenge_node_t *in, \ 140 bithenge_node_t **out) \ 135 141 { \ 136 142 int rc; \ … … 170 176 171 177 static int zero_terminated_apply(bithenge_transform_t *self, 172 bithenge_node_t *in, bithenge_node_t **out) 178 bithenge_transform_context_t *context, bithenge_node_t *in, 179 bithenge_node_t **out) 173 180 { 174 181 int rc; … … 194 201 195 202 static int zero_terminated_prefix_length(bithenge_transform_t *self, 196 bithenge_blob_t *blob, aoff64_t *out) 203 bithenge_transform_context_t *context, bithenge_blob_t *blob, 204 aoff64_t *out) 197 205 { 198 206 int rc; … … 243 251 bithenge_node_t base; 244 252 struct struct_transform *transform; 253 bithenge_transform_context_t *context; 245 254 bithenge_blob_t *blob; 246 255 } struct_node_t; … … 272 281 273 282 static int struct_node_for_one(const char *name, 274 bithenge_transform_t *subxform, bithenge_ blob_t **blob,275 bithenge_ for_each_func_t func, void *data)283 bithenge_transform_t *subxform, bithenge_transform_context_t *context, 284 bithenge_blob_t **blob, bithenge_for_each_func_t func, void *data) 276 285 { 277 286 int rc; … … 279 288 280 289 aoff64_t sub_size; 281 rc = bithenge_transform_prefix_length(subxform, *blob, &sub_size); 290 rc = bithenge_transform_prefix_length(subxform, context, *blob, 291 &sub_size); 282 292 if (rc != EOK) 283 293 goto error; … … 289 299 goto error; 290 300 291 rc = bithenge_transform_apply(subxform, subblob_node,301 rc = bithenge_transform_apply(subxform, context, subblob_node, 292 302 &subxform_result); 293 303 bithenge_node_dec_ref(subblob_node); … … 347 357 for (size_t i = 0; subxforms[i].transform; i++) { 348 358 rc = struct_node_for_one(subxforms[i].name, 349 subxforms[i].transform, &blob, func, data); 359 subxforms[i].transform, struct_node->context, &blob, func, 360 data); 350 361 if (rc != EOK) 351 362 goto error; … … 380 391 381 392 static int struct_transform_apply(bithenge_transform_t *base, 382 bithenge_node_t *in, bithenge_node_t **out) 393 bithenge_transform_context_t *context, bithenge_node_t *in, 394 bithenge_node_t **out) 383 395 { 384 396 struct_transform_t *self = transform_as_struct(base); … … 397 409 node->transform = self; 398 410 bithenge_node_inc_ref(in); 411 node->context = context; 399 412 node->blob = bithenge_node_as_blob(in); 400 413 *out = struct_as_node(node); … … 403 416 404 417 static int struct_transform_prefix_length(bithenge_transform_t *base, 405 bithenge_blob_t *blob, aoff64_t *out) 418 bithenge_transform_context_t *context, bithenge_blob_t *blob, 419 aoff64_t *out) 406 420 { 407 421 struct_transform_t *self = transform_as_struct(base); … … 419 433 self->subtransforms[i].transform; 420 434 aoff64_t sub_size; 421 rc = bithenge_transform_prefix_length(subxform, blob, &sub_size); 435 rc = bithenge_transform_prefix_length(subxform, context, blob, 436 &sub_size); 422 437 if (rc != EOK) 423 438 goto error; … … 502 517 } 503 518 504 static int compose_apply(bithenge_transform_t *base, bithenge_node_t *in, 519 static int compose_apply(bithenge_transform_t *base, 520 bithenge_transform_context_t *context, bithenge_node_t *in, 505 521 bithenge_node_t **out) 506 522 { … … 512 528 for (size_t i = self->num; i--; ) { 513 529 bithenge_node_t *tmp; 514 rc = bithenge_transform_apply(self->xforms[i], in, &tmp); 530 rc = bithenge_transform_apply(self->xforms[i], context, in, 531 &tmp); 515 532 bithenge_node_dec_ref(in); 516 533 if (rc != EOK) … … 524 541 525 542 static int compose_prefix_length(bithenge_transform_t *base, 526 bithenge_blob_t *blob, aoff64_t *out) 543 bithenge_transform_context_t *context, bithenge_blob_t *blob, 544 aoff64_t *out) 527 545 { 528 546 compose_transform_t *self = transform_as_compose(base); 529 547 return bithenge_transform_prefix_length(self->xforms[self->num - 1], 530 blob, out);548 context, blob, out); 531 549 } 532 550 -
uspace/app/bithenge/transform.h
r842ed146 r43788b2 48 48 } bithenge_transform_t; 49 49 50 /** Context and parameters used when applying transforms. */ 51 typedef struct { 52 /** @privatesection */ 53 } bithenge_transform_context_t; 54 50 55 /** Operations that may be provided by a transform. */ 51 56 typedef struct bithenge_transform_ops { 52 57 /** @copydoc bithenge_transform_t::bithenge_transform_apply */ 53 int (*apply)(bithenge_transform_t *self, bithenge_node_t *in, bithenge_node_t **out); 58 int (*apply)(bithenge_transform_t *self, 59 bithenge_transform_context_t *context, bithenge_node_t *in, 60 bithenge_node_t **out); 54 61 /** @copydoc bithenge_transform_t::bithenge_transform_prefix_length */ 55 int (*prefix_length)(bithenge_transform_t *self, bithenge_blob_t *blob, aoff64_t *out); 62 int (*prefix_length)(bithenge_transform_t *self, 63 bithenge_transform_context_t *context, bithenge_blob_t *blob, 64 aoff64_t *out); 56 65 /** Destroy the transform. 57 66 * @param self The transform. */ … … 59 68 } bithenge_transform_ops_t; 60 69 61 /** Apply a transform. 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 { 84 } 85 86 /** Apply a transform. Takes ownership of nothing. 62 87 * @memberof bithenge_transform_t 63 88 * @param self The transform. 89 * @param context The context. 64 90 * @param in The input tree. 65 91 * @param[out] out Where the output tree will be stored. 66 92 * @return EOK on success or an error code from errno.h. */ 67 93 static inline int bithenge_transform_apply(bithenge_transform_t *self, 68 bithenge_node_t *in, bithenge_node_t **out) 94 bithenge_transform_context_t *context, bithenge_node_t *in, 95 bithenge_node_t **out) 69 96 { 70 97 assert(self); 71 98 assert(self->ops); 72 return self->ops->apply(self, in, out);99 return self->ops->apply(self, context, in, out); 73 100 } 74 101 … … 76 103 * other words, figure out how many bytes this transform will use up. This 77 104 * method is optional and can return an error, but it must succeed for struct 78 * subtransforms. 105 * subtransforms. Takes ownership of nothing. 79 106 * @memberof bithenge_transform_t 80 107 * @param self The transform. 108 * @param context The context. 81 109 * @param blob The blob. 82 110 * @param[out] out Where the prefix length will be stored. … … 84 112 * errno.h. */ 85 113 static inline int bithenge_transform_prefix_length(bithenge_transform_t *self, 86 bithenge_blob_t *blob, aoff64_t *out) 114 bithenge_transform_context_t *context, bithenge_blob_t *blob, 115 aoff64_t *out) 87 116 { 88 117 assert(self); … … 90 119 if (!self->ops->prefix_length) 91 120 return ENOTSUP; 92 return self->ops->prefix_length(self, blob, out);121 return self->ops->prefix_length(self, context, blob, out); 93 122 } 94 123
Note:
See TracChangeset
for help on using the changeset viewer.