source: mainline/uspace/app/bithenge/transform.h@ 02dcb20

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 02dcb20 was 600f5d1, checked in by Sean Bartell <wingedtachikoma@…>, 13 years ago

Bithenge: add composition, ascii, and zero_terminated

  • Property mode set to 100644
File size: 4.7 KB
RevLine 
[d5070ef]1/*
2 * Copyright (c) 2012 Sean Bartell
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup bithenge
30 * @{
31 */
32/**
33 * @file
34 * Transforms.
35 */
36
37#ifndef BITHENGE_TRANSFORM_H_
38#define BITHENGE_TRANSFORM_H_
39
[03b2b2c]40#include "blob.h"
[d5070ef]41#include "tree.h"
42
43/** A transform that creates a new tree from an old tree. */
44typedef struct {
45 /** @privatesection */
[04a7435f]46 const struct bithenge_transform_ops *ops;
[d5070ef]47 unsigned int refs;
48} bithenge_transform_t;
49
50/** Operations that may be provided by a transform. */
[04a7435f]51typedef struct bithenge_transform_ops {
[d5070ef]52 /** @copydoc bithenge_transform_t::bithenge_transform_apply */
[978ccaf1]53 int (*apply)(bithenge_transform_t *self, bithenge_node_t *in, bithenge_node_t **out);
[d5070ef]54 /** @copydoc bithenge_transform_t::bithenge_transform_prefix_length */
[978ccaf1]55 int (*prefix_length)(bithenge_transform_t *self, bithenge_blob_t *blob, aoff64_t *out);
[d5070ef]56 /** Destroy the transform.
[978ccaf1]57 * @param self The transform. */
58 void (*destroy)(bithenge_transform_t *self);
[d5070ef]59} bithenge_transform_ops_t;
60
61/** Apply a transform.
62 * @memberof bithenge_transform_t
[978ccaf1]63 * @param self The transform.
[d5070ef]64 * @param in The input tree.
65 * @param[out] out Where the output tree will be stored.
66 * @return EOK on success or an error code from errno.h. */
[978ccaf1]67static inline int bithenge_transform_apply(bithenge_transform_t *self,
[d5070ef]68 bithenge_node_t *in, bithenge_node_t **out)
69{
[978ccaf1]70 assert(self);
71 assert(self->ops);
72 return self->ops->apply(self, in, out);
[d5070ef]73}
74
75/** Find the length of the prefix of a blob this transform can use as input. In
76 * other words, figure out how many bytes this transform will use up. This
[da0fef6]77 * method is optional and can return an error, but it must succeed for struct
[d5070ef]78 * subtransforms.
79 * @memberof bithenge_transform_t
[978ccaf1]80 * @param self The transform.
[d5070ef]81 * @param blob The blob.
82 * @param[out] out Where the prefix length will be stored.
83 * @return EOK on success, ENOTSUP if not supported, or another error code from
84 * errno.h. */
[978ccaf1]85static inline int bithenge_transform_prefix_length(bithenge_transform_t *self,
[d5070ef]86 bithenge_blob_t *blob, aoff64_t *out)
87{
[978ccaf1]88 assert(self);
89 assert(self->ops);
90 if (!self->ops->prefix_length)
[d5070ef]91 return ENOTSUP;
[978ccaf1]92 return self->ops->prefix_length(self, blob, out);
[d5070ef]93}
94
95/** Increment a transform's reference count.
[978ccaf1]96 * @param self The transform to reference. */
97static inline void bithenge_transform_inc_ref(bithenge_transform_t *self)
[d5070ef]98{
[978ccaf1]99 assert(self);
100 self->refs++;
[d5070ef]101}
102
[f2da0bb]103/** Decrement a transform's reference count and free it if appropriate.
[978ccaf1]104 * @param self The transform to dereference, or NULL. */
105static inline void bithenge_transform_dec_ref(bithenge_transform_t *self)
[d5070ef]106{
[978ccaf1]107 if (!self)
108 return;
109 assert(self->ops);
110 if (--self->refs == 0)
111 self->ops->destroy(self);
[d5070ef]112}
113
[0d1a8fd]114/** A transform with a name. */
115typedef struct {
116 const char *name;
117 bithenge_transform_t *transform;
118} bithenge_named_transform_t;
119
[600f5d1]120extern bithenge_transform_t bithenge_ascii_transform;
[0d1a8fd]121extern bithenge_transform_t bithenge_uint32le_transform;
122extern bithenge_transform_t bithenge_uint32be_transform;
[600f5d1]123extern bithenge_transform_t bithenge_zero_terminated_transform;
[0d1a8fd]124extern bithenge_named_transform_t *bithenge_primitive_transforms;
[d5070ef]125
[978ccaf1]126int bithenge_init_transform(bithenge_transform_t *self,
[04a7435f]127 const bithenge_transform_ops_t *ops);
128int bithenge_new_struct(bithenge_transform_t **out,
129 bithenge_named_transform_t *subtransforms);
[600f5d1]130int bithenge_new_composed_transform(bithenge_transform_t **,
131 bithenge_transform_t **, size_t);
[04a7435f]132
[d5070ef]133#endif
134
135/** @}
136 */
Note: See TracBrowser for help on using the repository browser.