## ## OSSP val - Value Access ## Copyright (c) 2002-2003 Ralf S. Engelschall ## Copyright (c) 2002-2003 The OSSP Project ## Copyright (c) 2002-2003 Cable & Wireless Deutschland ## ## This file is part of OSSP val, a value access library which ## can be found at http://www.ossp.org/pkg/lib/val/. ## ## Permission to use, copy, modify, and distribute this software for ## any purpose with or without fee is hereby granted, provided that ## the above copyright notice and this permission notice appear in all ## copies. ## ## THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED ## WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF ## MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ## IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF ## USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ## ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, ## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT ## OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF ## SUCH DAMAGE. ## ## val.pod: library manual page ## =pod =head1 NAME B - Value Access =head1 SYNOPSIS =over 4 =item B C, C, C, C, C, C, C, C, C C, C, C, C, C, C, C. =item B C, C, C. =item B C, C, C, C, C, C, C, C, C, C. =back =head1 DESCRIPTION B is a flexible name to value mapping library for ISO-C variables. It allows one to access ISO-C variables through name strings, although the ISO-C language does neither provide such a dedicated facility nor an evaluation construct (which could be used to implement such a facility easily). In general, this is used for accessing ISO-C variables without having to know the actual symbol/address. The typical use cases are in combination with flexible configuration parsing and supporting loosely-coupled DSO-based module architectures. =head1 STRUCTURED NAMES Whenever the API calls for a name, it supports structured names where elements are separated by a dot. It is assumed that the leading elements are references to other C structures and only the very last element is a reference to an actual variable. =head1 APPLICATION PROGRAMMING INTERFACE (API) =head2 API CONSTANTS The following constants exist in the B API: =over 4 =item C The maximum length of a variable name. For structured variables this includes the concatenation of all elements within a path and their separating dots. =item CI Type of value when registering a variable using B() or querying for the type using B(). Most are self-explanatory because directly correspond to the basic ISO-C data types. C is used to mount a C structure into an existing C structure to support structured names (see example section for details). The following particular types exist: C (C), C (C), C (C), C (C), C (C), C (C), C (C), C (C). =item C, CI Return codes (of type C) for every API function. Signals success (C), invalid argument passed to function, bad usage of a function, memory usage reached C limit, error in internal hash function to be examined through C, internal error in storage as result from structure corruption, or system errors including out of memory to be examined through C. The following particular return codes exist: C, C, C, C, C, C, C. =back =head2 API DATA TYPES The following data types exist in the B API: =over 4 =item C Opaque handle data type created by B() and passed to all other functions to reference the the same group of values. =item C Function data type for the callback to be used with B(). =item C Data type returned by every function. See B C and CI. =back =head2 API FUNCTIONS The following functions exist in the B API: =over 4 =item val_rc_t B(val_t **I); Creates a new value access structure and updates the given pointer I to reference it. =item val_rc_t B(val_t *I); Destroys the value access structure referenced by I. =item val_rc_t B(val_t *I, const char *I, int I, const char *I, void *I); Registers a value under I of type I in I. An optional description or C can be passed through I which can be queried through B() and is also passed to the callback of B(). The value that belongs to the given I is expected to be found at I. Passing C as I will create an internal data storage in I so it can only be accessed through B(), B() or after the actual storage address was queried using B(). =item val_rc_t B(val_t *I, const char *I); Unregisters the value under I from I. =item val_rc_t B(val_t *I, const char *I, int *I, char **I, void **I); Queries a value I in I and returns its type, description and storage address. All of I, I and I are optional and C can be passed in if this information is not needed. Passing C to all query result pointers just checks for existence of the value I in I. =item val_rc_t B(val_t *I, const char *I, ...); Sets the value I in I to the data passed in as the variable argument (expected to be of the I specified at B() time). Unless the actual storage address was queried using B() this operation is mandatory for internally stored data. If external storage is used, not the value but a pointer to it is stored in the library, so the value is allowed to be be modified without explicit notice to the library. =item val_rc_t B(val_t *I, const char *I, ...); Gets the value I in I and stores it wherever the passed variable argument points to. The storage location is expected to be of the I specified at B() time. =item val_rc_t B(val_t *I, const char *I, va_list I); Exactly like B() but uses a C for the variable arguments. =item val_rc_t B(val_t *I, const char *I, va_list I); Exactly like B() but uses a C for the variable arguments. =item val_rc_t B(val_t *I, const char *I, int I, val_cb_t I, void *I); Iterates over all values in I, starting with I, which can be either a data storage or I reference, down to a given recursion I. If I is set to the empty string the search starts immediately at I. For every value, the callback I() is executed. The callback has to be a function with the following prototype: val_rc_t cb(void *I, const char *I, int I, const char *I, void *I); The I is the passed-through context I of B(). The I is the structured name relative to the I passed to B(), I signals the type of value I points to and I is the text which was optionally passed to B(). =back =head1 SEE ALSO B (Variable Expansion) =head1 EXAMPLES A few simple examples on how to use B are following. For easier reading all error checks are omitted. In a production program you have to check every val_xxx() call against C, of course. =head2 Simple Internal Value Source: #include #include "val.h" int main(void) { val_rc_t rc; val_t *v; int tmp; val_create(&v); val_reg(v, "foo", VAL_TYPE_INT, "foo variable", NULL); val_set(v, "foo", 123); val_get(v, "foo", &tmp); printf("foo=%d\n", tmp); val_destroy(v); return 0; } Output: foo=123 =head2 Simple External Value Source: #include #include "val.h" int main(void) { val_rc_t rc; val_t *v; int foo; int tmp; val_create(&v); val_reg(v, "foo", VAL_TYPE_INT, "foo variable", (void *)&foo); foo = 123; val_get(v, "foo", &tmp); printf("1. foo=%d tmp=%d\n", foo, tmp); val_set(v, "foo", 456); val_get(v, "foo", &tmp); printf("2. foo=%d tmp=%d\n", foo, tmp); example = 789; val_get(v, "foo", &tmp); printf("3. foo=%d tmp=%d\n", foo, tmp); val_destroy(v); return 0; } Output: 1. foo=123 tmp=123 2. foo=456 tmp=456 3. foo=789 tmp=789 =head2 Structured Internal Values Source: #include #include "val.h" int main(void) { val_rc_t rc; val_t *v1, *v2; int tmp; val_create(&v1); val_create(&v2); val_reg(v1, "bar", VAL_TYPE_VAL, "v2", (void *)&v2); val_reg(v1, "bar.foo", VAL_TYPE_INT, "foo variable", NULL); val_set(v2, "foo", 123); val_get(v2, "foo", &tmp); printf("1. foo=%d\n", tmp); val_get(v1, "bar.foo", &tmp); printf("2. bar.foo=%d\n", tmp); val_set(v1, "bar.foo", 456); val_get(v2, "foo", &tmp); printf("3. foo=%d\n", tmp); val_destroy(v2); val_destroy(v1); return 0; } Output: 1. foo=123 2. bar.foo=123 3. foo=456 =head1 HISTORY B was invented in January 2002 by Thomas Lotterer Ethomas@lotterer.netE and Ralf S. Engelschall Erse@engelschall.comE for use in the B project. Its creation was prompted by the requirement to locate values for B based expansions in B. =head1 AUTHORS Thomas Lotterer Ralf S. Engelschall =cut