Simple coroutine library / objects in plain C Snapshot
|
A class for conveniently passing of typed tuple of values. More...
Classes | |
struct | tagVALUES |
Typedefs | |
typedef struct tagVALUES | VALUES |
Functions | |
M_INLINE int | VALUES_init (VALUES *val) |
initialises a typed tuple of values object | |
M_INLINE void | VALUES_free (VALUES *val) |
frees a typed tuple of values object | |
int | VALUES_printv (VALUES *val, const char *format, va_list ap) |
insert typed tuple of values | |
int | VALUES_scanv (VALUES *val, const char *format, va_list ap) |
retrieve typed tuple of values; like scanf receives varying number of arguments | |
M_INLINE int | VALUES_print (VALUES *val, const char *format,...) |
set typed tuple of values; like prinf receives varying number of arguments | |
M_INLINE int | VALUES_scan (VALUES *val, const char *format,...) |
retrieve typed tuple of values; like scanf receives varying number of arguments | |
M_INLINE VAL * | VALUES_at (VALUES *val, size_t pos) |
M_INLINE size_t | VALUES_size (VALUES *val) |
A class for conveniently passing of typed tuple of values.
A typed tuple of values is stored into the VALUES object by a printf like function.
VALUES_print( values, "%hhu%hd%d%qd%s", n8, n16, n32, n64, sval );
Once inserted, the values can be retrieved back, also by a scanf like function
VALUES_scan( &val, "%hhu%hd%d%qd%s", &_n8, &_n16, &_n32, &_n64, &_sval );
Note that the type specifiers of values retrieved must be strictly the same as that of the values inserted previously.
Type specifiers:
hhd - signed byte (int8_t) hhu - unsigned byte (uint8_t) hd - signed short (int16_t) hhu - unsigned short (uint16_t) d - signed integer (int32_t) u - unsigned integer (uint32_t) qd - signed long long (int64_t) qu - unsiend long long (uint64_t s - null terminated string pointer p - void pointer.
M_INLINE void VALUES_free | ( | VALUES * | val | ) |
M_INLINE int VALUES_init | ( | VALUES * | val | ) |
M_INLINE int VALUES_print | ( | VALUES * | val, |
const char * | format, | ||
... | |||
) |
set typed tuple of values; like prinf receives varying number of arguments
val | - the VALUES object that is modified |
format | - Format specifier (for details see documentation of VALUES class) |
ap | - va_list - pointer to stack frame of variadic function. |
Definition at line 123 of file val.h.
{ va_list vlist; va_start( vlist, format ); return VALUES_printv( val, format, vlist ); }
int VALUES_printv | ( | VALUES * | val, |
const char * | format, | ||
va_list | ap | ||
) |
insert typed tuple of values
val | - the VALUES object that is modified |
format | - Format specifier (for details see documentation of VALUES class) |
ap | - |
Definition at line 62 of file val.c.
{ const char *pos; VAL_TYPE type; VAL val; int rt; ARRAY_reset( &values->values ); pos = format; while( (rt = parse_spec( &pos, &type ) ) == 0 ) { val.type = type; switch( type ) { case VAL_TYPE_UINT8: val.u.u8 = (uint8_t) va_arg( ap, int32_t ); break; case VAL_TYPE_INT8: val.u.n8 = (int8_t) va_arg( ap, int32_t ); break; case VAL_TYPE_UINT16: val.u.u16 = (uint16_t) va_arg( ap, uint32_t ); break; case VAL_TYPE_INT16: val.u.n16 = (int16_t) va_arg( ap, int32_t ); break; case VAL_TYPE_UINT32: val.u.u32 = va_arg( ap, uint32_t ); break; case VAL_TYPE_INT32: val.u.n32 = va_arg( ap, int32_t ); break; case VAL_TYPE_UINT64: val.u.u64 = va_arg( ap, uint64_t ); break; case VAL_TYPE_INT64: val.u.n64 = va_arg( ap, int64_t ); break; case VAL_TYPE_STRING: case VAL_TYPE_PTR: val.u.pval = va_arg( ap, void * ); break; } ARRAY_push_back( &values->values, &val, sizeof(val) ); } va_end( ap ); if (rt == -1) { return -1; } return 0; }
M_INLINE int VALUES_scan | ( | VALUES * | val, |
const char * | format, | ||
... | |||
) |
retrieve typed tuple of values; like scanf receives varying number of arguments
val | - the VALUES object that is modified |
format | - Format specifier (for details see documentation of VALUES class) |
Definition at line 135 of file val.h.
{ va_list vlist; va_start( vlist, format ); return VALUES_scanv( val, format, vlist ); }
int VALUES_scanv | ( | VALUES * | val, |
const char * | format, | ||
va_list | ap | ||
) |
retrieve typed tuple of values; like scanf receives varying number of arguments
val | - the VALUES object that is modified |
format | - Format specifier (for details see documentation of VALUES class) |
ap | - va_list - pointer to stack frame of variadic function. |
Definition at line 113 of file val.c.
{ VAL_TYPE type; VAL *val; size_t i; const char *pos; void *ptr; int rt; pos = format; for(i = 0; i < ARRAY_size(&values->values) && (rt = parse_spec( &pos, &type )) == 0; i++ ) { val = VALUES_at( values, i ); if (val->type != type) { va_end(ap); return -1; } ptr = va_arg( ap, void *); switch( type ) { case VAL_TYPE_UINT8: * ((uint8_t *) ptr) = val->u.u8; break; case VAL_TYPE_INT8: * ((int8_t *) ptr) = val->u.n8; break; case VAL_TYPE_UINT16: * ((uint16_t *) ptr) = val->u.u16; break; case VAL_TYPE_INT16: * ((int16_t *) ptr) = val->u.n16; break; case VAL_TYPE_UINT32: * ((uint32_t *) ptr) = val->u.u32; break; case VAL_TYPE_INT32: * ((int32_t *) ptr) = val->u.n32; break; case VAL_TYPE_UINT64: * ((uint64_t *) ptr) = val->u.u64; break; case VAL_TYPE_INT64: * ((int64_t *) ptr) = val->u.n64; break; case VAL_TYPE_STRING: case VAL_TYPE_PTR: * ((char **) ptr) = val->u.sval; break; } } va_end( ap ); if (rt == -1) { return -1; } if (*pos != '\0') { return -1; } return 0; }