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

Changeset 5a7c0e6 in mainline


Ignore:
Timestamp:
2012-07-27T01:09:05Z (9 years ago)
Author:
Sean Bartell <wingedtachikoma@…>
Branches:
lfn, master
Children:
6e34bd0
Parents:
43788b2
Message:

Bithenge: rename transform parameters "scopes" and add room for parameters

Location:
uspace/app/bithenge
Files:
3 edited

Legend:

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

    r43788b2 r5a7c0e6  
    6969                bithenge_node_dec_ref(node);
    7070        } else {
    71                 bithenge_transform_context_t context;
     71                bithenge_scope_t scope;
    7272                bithenge_transform_t *transform = NULL;
    7373                bithenge_node_t *node = NULL, *node2 = NULL;
    7474
    75                 bithenge_transform_context_init(&context);
     75                bithenge_scope_init(&scope);
    7676
    7777                rc = bithenge_parse_script(argv[1], &transform);
     
    8787                }
    8888
    89                 rc = bithenge_transform_apply(transform, &context, node, &node2);
     89                rc = bithenge_transform_apply(transform, &scope, node, &node2);
    9090                if (rc != EOK) {
    9191                        printf("Error applying transform: %s\n", str_error(rc));
     
    113113                bithenge_node_dec_ref(node2);
    114114                bithenge_transform_dec_ref(transform);
    115                 bithenge_transform_context_destroy(&context);
     115                bithenge_scope_destroy(&scope);
    116116                return 1;
    117117        }
  • uspace/app/bithenge/transform.c

    r43788b2 r5a7c0e6  
    6161}
    6262
    63 static int ascii_apply(bithenge_transform_t *self,
    64     bithenge_transform_context_t *context, bithenge_node_t *in,
    65     bithenge_node_t **out)
     63static int ascii_apply(bithenge_transform_t *self, bithenge_scope_t *scope,
     64    bithenge_node_t *in, bithenge_node_t **out)
    6665{
    6766        int rc;
     
    103102};
    104103
    105 static int prefix_length_1(bithenge_transform_t *self,
    106     bithenge_transform_context_t *context, bithenge_blob_t *blob,
    107     aoff64_t *out)
     104static int prefix_length_1(bithenge_transform_t *self, bithenge_scope_t *scope,
     105    bithenge_blob_t *blob, aoff64_t *out)
    108106{
    109107        *out = 1;
     
    111109}
    112110
    113 static int prefix_length_2(bithenge_transform_t *self,
    114     bithenge_transform_context_t *context, bithenge_blob_t *blob,
    115     aoff64_t *out)
     111static int prefix_length_2(bithenge_transform_t *self, bithenge_scope_t *scope,
     112    bithenge_blob_t *blob, aoff64_t *out)
    116113{
    117114        *out = 2;
     
    119116}
    120117
    121 static int prefix_length_4(bithenge_transform_t *self,
    122     bithenge_transform_context_t *context, bithenge_blob_t *blob,
    123     aoff64_t *out)
     118static int prefix_length_4(bithenge_transform_t *self, bithenge_scope_t *scope,
     119    bithenge_blob_t *blob, aoff64_t *out)
    124120{
    125121        *out = 4;
     
    127123}
    128124
    129 static int prefix_length_8(bithenge_transform_t *self,
    130     bithenge_transform_context_t *context, bithenge_blob_t *blob,
    131     aoff64_t *out)
     125static int prefix_length_8(bithenge_transform_t *self, bithenge_scope_t *scope,
     126    bithenge_blob_t *blob, aoff64_t *out)
    132127{
    133128        *out = 8;
     
    137132#define MAKE_UINT_TRANSFORM(NAME, TYPE, ENDIAN, PREFIX_LENGTH_FUNC)            \
    138133        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,                      \
    140135            bithenge_node_t **out)                                             \
    141136        {                                                                      \
     
    176171
    177172static 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)
    180174{
    181175        int rc;
     
    201195
    202196static 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)
    205198{
    206199        int rc;
     
    251244        bithenge_node_t base;
    252245        struct struct_transform *transform;
    253         bithenge_transform_context_t *context;
     246        bithenge_scope_t *scope;
    254247        bithenge_blob_t *blob;
    255248} struct_node_t;
     
    281274
    282275static 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,
    284277    bithenge_blob_t **blob, bithenge_for_each_func_t func, void *data)
    285278{
     
    288281
    289282        aoff64_t sub_size;
    290         rc = bithenge_transform_prefix_length(subxform, context, *blob,
     283        rc = bithenge_transform_prefix_length(subxform, scope, *blob,
    291284            &sub_size);
    292285        if (rc != EOK)
     
    299292                goto error;
    300293
    301         rc = bithenge_transform_apply(subxform, context, subblob_node,
     294        rc = bithenge_transform_apply(subxform, scope, subblob_node,
    302295            &subxform_result);
    303296        bithenge_node_dec_ref(subblob_node);
     
    357350        for (size_t i = 0; subxforms[i].transform; i++) {
    358351                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,
    360353                    data);
    361354                if (rc != EOK)
     
    391384
    392385static 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)
    395387{
    396388        struct_transform_t *self = transform_as_struct(base);
     
    409401        node->transform = self;
    410402        bithenge_node_inc_ref(in);
    411         node->context = context;
     403        node->scope = scope;
    412404        node->blob = bithenge_node_as_blob(in);
    413405        *out = struct_as_node(node);
     
    416408
    417409static 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)
    420411{
    421412        struct_transform_t *self = transform_as_struct(base);
     
    433424                    self->subtransforms[i].transform;
    434425                aoff64_t sub_size;
    435                 rc = bithenge_transform_prefix_length(subxform, context, blob,
     426                rc = bithenge_transform_prefix_length(subxform, scope, blob,
    436427                    &sub_size);
    437428                if (rc != EOK)
     
    517508}
    518509
    519 static int compose_apply(bithenge_transform_t *base,
    520     bithenge_transform_context_t *context, bithenge_node_t *in,
    521     bithenge_node_t **out)
     510static int compose_apply(bithenge_transform_t *base, bithenge_scope_t *scope,
     511    bithenge_node_t *in, bithenge_node_t **out)
    522512{
    523513        int rc;
     
    528518        for (size_t i = self->num; i--; ) {
    529519                bithenge_node_t *tmp;
    530                 rc = bithenge_transform_apply(self->xforms[i], context, in,
     520                rc = bithenge_transform_apply(self->xforms[i], scope, in,
    531521                    &tmp);
    532522                bithenge_node_dec_ref(in);
     
    541531
    542532static 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)
    545534{
    546535        compose_transform_t *self = transform_as_compose(base);
    547536        return bithenge_transform_prefix_length(self->xforms[self->num - 1],
    548             context, blob, out);
     537            scope, blob, out);
    549538}
    550539
  • uspace/app/bithenge/transform.h

    r43788b2 r5a7c0e6  
    5151typedef struct {
    5252        /** @privatesection */
    53 } bithenge_transform_context_t;
     53        bithenge_node_t **params;
     54        int num_params;
     55} bithenge_scope_t;
    5456
    5557/** Operations that may be provided by a transform. */
    5658typedef struct bithenge_transform_ops {
    5759        /** @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);
    6162        /** @copydoc bithenge_transform_t::bithenge_transform_prefix_length */
    6263        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);
    6565        /** Destroy the transform.
    6666         * @param self The transform. */
    6767        void (*destroy)(bithenge_transform_t *self);
     68        /** The number of parameters required. */
     69        int num_params;
    6870} bithenge_transform_ops_t;
    6971
    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. */
     75static 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. */
     84static 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. */
     96static 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. */
     112static 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. */
     126static 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. */
     140static inline int bithenge_transform_num_params(bithenge_transform_t *self)
     141{
     142        assert(self);
     143        assert(self->ops);
     144        return self->ops->num_params;
    84145}
    85146
     
    87148 * @memberof bithenge_transform_t
    88149 * @param self The transform.
    89  * @param context The context.
     150 * @param scope The scope.
    90151 * @param in The input tree.
    91152 * @param[out] out Where the output tree will be stored.
    92153 * @return EOK on success or an error code from errno.h. */
    93154static 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);
    100160}
    101161
     
    106166 * @memberof bithenge_transform_t
    107167 * @param self The transform.
    108  * @param context The context.
     168 * @param scope The scope.
    109169 * @param blob The blob.
    110170 * @param[out] out Where the prefix length will be stored.
     
    112172 * errno.h. */
    113173static 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)
    116175{
    117176        assert(self);
     
    119178        if (!self->ops->prefix_length)
    120179                return ENOTSUP;
    121         return self->ops->prefix_length(self, context, blob, out);
     180        return self->ops->prefix_length(self, scope, blob, out);
    122181}
    123182
Note: See TracChangeset for help on using the changeset viewer.