sstream_t

3

sstream_t

input/output serialization stream

    #include <cds/serialize.h>

    typedef struct {
    	dstring_t out;
    	str_t in;
    	int in_pos;
    	enum { sstream_in, sstream_out } type;
    } sstream_t;

## Description

This structure represents input and output serialization stream.

### Members

`out`

- Dynamic string holding output data in the case of output stream.

`in`

- String holding input data in the case of input stream.

`in_pos`

- Actual position in input data (points to first unread char) in the case of input stream.

`type`

- Member holding stream type - input or output.

Warning - internals of this data structure may change! Use it only through manipulation functions.

## Example

    #include <cds/sstr.h>
    #include <cds/serialize.h>

    typedef struct {
    	str_t name;
    	int number;
    	char c;
    } test_t;

    int serialize_test_struct(sstream_t *store, test_t *t)
    {
    	if (serialize_str(store, &t->name) != 0) return -1;
    	if (serialize_int(store, &t->number) != 0) return -1;
    	if (serialize_char(store, &t->c) != 0) return -1;
    	return 0;
    }

    int main(int argc, char **argv)
    {
    	sstream_t store;
    	str_t data;
    	test_t a = { name: {"test A", 6}, number: 13, c: 'x' };
    	test_t b;

    	str_clear(&data);

    	/* storing structure to the string */

    	init_output_sstream(&store, 256);
    	if (serialize_test_struct(&store, &a) == 0) {
    		if (get_serialized_sstream(&store, &data) != 0)
    			printf("can't get data\n");
    	}
    	else printf("can't serialize\n");
    	destroy_sstream(&store);

    	/* loading structure from the string */

    	init_input_sstream(&store, data.s, data.len);
    	if (serialize_test_struct(&store, &b) == 0) {
    		printf("test_t = { %.*s, %d, %c }\n",
    			FMT_STR(b.name), b.number, b.c);

    		/* cleanup */
    		str_free_content(&b.name);
    	}
    	else printf("can't deserialize\n");
    	destroy_sstream(&store);

    	/* cleanup */
    	str_free_content(&data);

    	return 0;
    }