FormatString

Offers formatted output to string_t.

Copyright

This program is free software.  You can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

Author

© 2012 Jörg Seebohn

Files

C-kern/api/string/format_string.h

Header file FormatString.

C-kern/string/format_string.c

Implementation file FormatString impl.

Types

struct string_field_t

typedef struct string_field_t string_field_t

Enumerations

string_field_e

Functions

Summary
test
unittest_string_formatTests formatted output to string_t.

test

unittest_string_format

int unittest_string_format(void)

Tests formatted output to string_t.

string_field_t

struct string_field_t
Summary
valType of the string field.
valValue of the field to be formatted.
lifetime
string_field_INIT_EMPTYStatic initializer.
initstring_stringfieldInits string_field_t describing a string value.
initfloat_stringfieldInits string_field_t describing a float value.
initdouble_stringfieldInits string_field_t describing a double value.

val

Type of the string field.  For supported values see string_field_e.

val

union { int32_t i32 ; uint32_t u32 ; ssize_t isz ; size_t usz ; int64_t i64 ; uint64_t u64 ; float f ; double d ; struct { const uint8_t * addr ; size_t size ; } str ; } val

Value of the field to be formatted.

lifetime

string_field_INIT_EMPTY

#define string_field_INIT_EMPTY { string_field_EMPTY, { 0 } }

Static initializer.  This field is formatted to generate the empty string.

initstring_stringfield

int initstring_stringfield(/*out*/string_field_t *strfield,
struct string_t *strval)

Inits string_field_t describing a string value.

initfloat_stringfield

int initfloat_stringfield(/*out*/string_field_t *strfield,
float fval)

Inits string_field_t describing a float value.

initdouble_stringfield

int initdouble_stringfield(/*out*/string_field_t *strfield,
double dval)

Inits string_field_t describing a double value.

string_t

Summary
query
sizeformat_stringReturns in string_t.size the size of the formatted output fields.
format
format_stringWrites formatted output to string_t.

query

sizeformat_string

int sizeformat_string(/*out*/struct string_t *str,
int dummy)

Returns in string_t.size the size of the formatted output fields.  No content is written to the string memory only the size in bytes is set to the sum of the size of the formatted field output as if it would have been written.

No other field in string_t is set or changed except string_t.size.

format

format_string

int format_string(struct string_t *str,
/*out*/size_t *nr_of_fully_written_fields,
int dummy)

Writes formatted output to string_t.  After return parameter nr_of_fully_written_fields is set to the number of fully formatted and written fields.  If the returned value is less then the number of input fields then there was no memory left in parameter str.  A value is either written fully or not at all.

On successfull return the str parameter’s size is set to the number of written bytes.  There are no more bytes written then the value provided in size on function entry.

Macros

initdouble_stringfield

#define initdouble_stringfield(
   strfield,
   dval
) ( __extension__ ({ (strfield)->type = string_field_DOUBLE ; (strfield)->val.d = dval ; 0 ; }))

Implements string_field_t.initdouble_stringfield.

initfloat_stringfield

#define initfloat_stringfield(
   strfield,
   fval
) ( __extension__ ({ (strfield)->type = string_field_FLOAT ; (strfield)->val.f = fval ; 0 ; }))

Implements string_field_t.initfloat_stringfield.

initstring_stringfield

#define initstring_stringfield(
   strfield,
   strval
) ( __extension__ ({ (strfield)->type = string_field_STRING ; (strfield)->val.str.addr = (strval)->addr ; (strfield)->val.str.size = (strval)->size ; 0 ; }))

Implements string_field_t.initstring_stringfield.

Offers formatted output to string_t.
Implements FormatString.
typedef struct string_field_t string_field_t
int unittest_string_format(void)
Tests formatted output to string_t.
struct string_field_t
union { int32_t i32 ; uint32_t u32 ; ssize_t isz ; size_t usz ; int64_t i64 ; uint64_t u64 ; float f ; double d ; struct { const uint8_t * addr ; size_t size ; } str ; } val
Value of the field to be formatted.
#define string_field_INIT_EMPTY { string_field_EMPTY, { 0 } }
Static initializer.
int initstring_stringfield(/*out*/string_field_t *strfield,
struct string_t *strval)
Inits string_field_t describing a string value.
int initfloat_stringfield(/*out*/string_field_t *strfield,
float fval)
Inits string_field_t describing a float value.
int initdouble_stringfield(/*out*/string_field_t *strfield,
double dval)
Inits string_field_t describing a double value.
int sizeformat_string(/*out*/struct string_t *str,
int dummy)
Returns in string_t.size the size of the formatted output fields.
size_t size
Size in bytes of memory.
int format_string(struct string_t *str,
/*out*/size_t *nr_of_fully_written_fields,
int dummy)
Writes formatted output to string_t.
#define initdouble_stringfield(
   strfield,
   dval
) ( __extension__ ({ (strfield)->type = string_field_DOUBLE ; (strfield)->val.d = dval ; 0 ; }))
Implements string_field_t.initdouble_stringfield.
#define initfloat_stringfield(
   strfield,
   fval
) ( __extension__ ({ (strfield)->type = string_field_FLOAT ; (strfield)->val.f = fval ; 0 ; }))
Implements string_field_t.initfloat_stringfield.
#define initstring_stringfield(
   strfield,
   strval
) ( __extension__ ({ (strfield)->type = string_field_STRING ; (strfield)->val.str.addr = (strval)->addr ; (strfield)->val.str.size = (strval)->size ; 0 ; }))
Implements string_field_t.initstring_stringfield.
Close