Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset 43788b2 in mainline


Ignore:
Timestamp:
2012-07-26T22:26:19Z (9 years ago)
Author:
Sean Bartell <wingedtachikoma@…>
Branches:
lfn, master
Children:
5a7c0e6
Parents:
842ed146
Message:

Bithenge: add transform context in preparation for parameters

Location:
uspace/app/bithenge
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/bithenge/test.c

    r842ed146 r43788b2  
    6969                bithenge_node_dec_ref(node);
    7070        } else {
     71                bithenge_transform_context_t context;
    7172                bithenge_transform_t *transform = NULL;
    7273                bithenge_node_t *node = NULL, *node2 = NULL;
     74
     75                bithenge_transform_context_init(&context);
     76
    7377                rc = bithenge_parse_script(argv[1], &transform);
    7478                if (rc != EOK) {
     
    8387                }
    8488
    85                 rc = bithenge_transform_apply(transform, node, &node2);
     89                rc = bithenge_transform_apply(transform, &context, node, &node2);
    8690                if (rc != EOK) {
    8791                        printf("Error applying transform: %s\n", str_error(rc));
     
    109113                bithenge_node_dec_ref(node2);
    110114                bithenge_transform_dec_ref(transform);
     115                bithenge_transform_context_destroy(&context);
    111116                return 1;
    112117        }
  • uspace/app/bithenge/transform.c

    r842ed146 r43788b2  
    6262
    6363static 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)
    6566{
    6667        int rc;
     
    102103};
    103104
    104 static int prefix_length_1(bithenge_transform_t *self, bithenge_blob_t *blob,
     105static int prefix_length_1(bithenge_transform_t *self,
     106    bithenge_transform_context_t *context, bithenge_blob_t *blob,
    105107    aoff64_t *out)
    106108{
     
    109111}
    110112
    111 static int prefix_length_2(bithenge_transform_t *self, bithenge_blob_t *blob,
     113static int prefix_length_2(bithenge_transform_t *self,
     114    bithenge_transform_context_t *context, bithenge_blob_t *blob,
    112115    aoff64_t *out)
    113116{
     
    116119}
    117120
    118 static int prefix_length_4(bithenge_transform_t *self, bithenge_blob_t *blob,
     121static int prefix_length_4(bithenge_transform_t *self,
     122    bithenge_transform_context_t *context, bithenge_blob_t *blob,
    119123    aoff64_t *out)
    120124{
     
    123127}
    124128
    125 static int prefix_length_8(bithenge_transform_t *self, bithenge_blob_t *blob,
     129static int prefix_length_8(bithenge_transform_t *self,
     130    bithenge_transform_context_t *context, bithenge_blob_t *blob,
    126131    aoff64_t *out)
    127132{
     
    132137#define MAKE_UINT_TRANSFORM(NAME, TYPE, ENDIAN, PREFIX_LENGTH_FUNC)            \
    133138        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)                                             \
    135141        {                                                                      \
    136142                int rc;                                                        \
     
    170176
    171177static 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)
    173180{
    174181        int rc;
     
    194201
    195202static 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)
    197205{
    198206        int rc;
     
    243251        bithenge_node_t base;
    244252        struct struct_transform *transform;
     253        bithenge_transform_context_t *context;
    245254        bithenge_blob_t *blob;
    246255} struct_node_t;
     
    272281
    273282static 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)
    276285{
    277286        int rc;
     
    279288
    280289        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);
    282292        if (rc != EOK)
    283293                goto error;
     
    289299                goto error;
    290300
    291         rc = bithenge_transform_apply(subxform, subblob_node,
     301        rc = bithenge_transform_apply(subxform, context, subblob_node,
    292302            &subxform_result);
    293303        bithenge_node_dec_ref(subblob_node);
     
    347357        for (size_t i = 0; subxforms[i].transform; i++) {
    348358                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);
    350361                if (rc != EOK)
    351362                        goto error;
     
    380391
    381392static 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)
    383395{
    384396        struct_transform_t *self = transform_as_struct(base);
     
    397409        node->transform = self;
    398410        bithenge_node_inc_ref(in);
     411        node->context = context;
    399412        node->blob = bithenge_node_as_blob(in);
    400413        *out = struct_as_node(node);
     
    403416
    404417static 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)
    406420{
    407421        struct_transform_t *self = transform_as_struct(base);
     
    419433                    self->subtransforms[i].transform;
    420434                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);
    422437                if (rc != EOK)
    423438                        goto error;
     
    502517}
    503518
    504 static int compose_apply(bithenge_transform_t *base, bithenge_node_t *in,
     519static int compose_apply(bithenge_transform_t *base,
     520    bithenge_transform_context_t *context, bithenge_node_t *in,
    505521    bithenge_node_t **out)
    506522{
     
    512528        for (size_t i = self->num; i--; ) {
    513529                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);
    515532                bithenge_node_dec_ref(in);
    516533                if (rc != EOK)
     
    524541
    525542static 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)
    527545{
    528546        compose_transform_t *self = transform_as_compose(base);
    529547        return bithenge_transform_prefix_length(self->xforms[self->num - 1],
    530             blob, out);
     548            context, blob, out);
    531549}
    532550
  • uspace/app/bithenge/transform.h

    r842ed146 r43788b2  
    4848} bithenge_transform_t;
    4949
     50/** Context and parameters used when applying transforms. */
     51typedef struct {
     52        /** @privatesection */
     53} bithenge_transform_context_t;
     54
    5055/** Operations that may be provided by a transform. */
    5156typedef struct bithenge_transform_ops {
    5257        /** @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);
    5461        /** @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);
    5665        /** Destroy the transform.
    5766         * @param self The transform. */
     
    5968} bithenge_transform_ops_t;
    6069
    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. */
     73static 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. */
     81static inline void bithenge_transform_context_destroy(
     82    bithenge_transform_context_t *context)
     83{
     84}
     85
     86/** Apply a transform. Takes ownership of nothing.
    6287 * @memberof bithenge_transform_t
    6388 * @param self The transform.
     89 * @param context The context.
    6490 * @param in The input tree.
    6591 * @param[out] out Where the output tree will be stored.
    6692 * @return EOK on success or an error code from errno.h. */
    6793static 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)
    6996{
    7097        assert(self);
    7198        assert(self->ops);
    72         return self->ops->apply(self, in, out);
     99        return self->ops->apply(self, context, in, out);
    73100}
    74101
     
    76103 * other words, figure out how many bytes this transform will use up.  This
    77104 * method is optional and can return an error, but it must succeed for struct
    78  * subtransforms.
     105 * subtransforms. Takes ownership of nothing.
    79106 * @memberof bithenge_transform_t
    80107 * @param self The transform.
     108 * @param context The context.
    81109 * @param blob The blob.
    82110 * @param[out] out Where the prefix length will be stored.
     
    84112 * errno.h. */
    85113static 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)
    87116{
    88117        assert(self);
     
    90119        if (!self->ops->prefix_length)
    91120                return ENOTSUP;
    92         return self->ops->prefix_length(self, blob, out);
     121        return self->ops->prefix_length(self, context, blob, out);
    93122}
    94123
Note: See TracChangeset for help on using the changeset viewer.