From 2d1cd95f901b0bb22f8b2defac80d88c7e5ff822 Mon Sep 17 00:00:00 2001 From: Lennart Buhl Date: Sat, 2 Nov 2013 14:44:17 +0100 Subject: [PATCH] added structs for serializing --- msg.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ msg.h | 18 ++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 msg.c create mode 100644 msg.h diff --git a/msg.c b/msg.c new file mode 100644 index 0000000..275a592 --- /dev/null +++ b/msg.c @@ -0,0 +1,52 @@ +#include +#include +#include +#include "msg.h" + +#if INTERFACE + +#define INITIAL_SIZE 32 + +struct Buffer { + int size; + void *data; +}; + +struct message { + long timestamp; + int width; // varies + int height; // normally 80 + char **image; // dimension is width x height +}; + + +#endif + +struct Buffer *new_buffer() { + struct Buffer *b = malloc(sizeof(Buffer)); + + b->data = malloc(INITIAL_SIZE); + b->size = 0; + + return b; +} + +void append_space(Buffer * b, int bytes) { + b->size += bytes; + b->data = realloc(b->data, b->size); +} + +void serialize_int(int x, Buffer * b) { + // htonl :: uint32_t -> uint32_t + x = htonl(x); + + append_space(b, sizeof(int)); + + memcpy(((char *)b->data) + b->next, &x, sizeof(int)); + b->next += sizeof(int); +} + +//serialize_string +//serialize_message + + diff --git a/msg.h b/msg.h new file mode 100644 index 0000000..de76560 --- /dev/null +++ b/msg.h @@ -0,0 +1,18 @@ +/* This file was automatically generated. Do not edit! */ +typedef struct Buffer Buffer; +void serialize_int(int x,Buffer *b); +void append_space(Buffer *b,int bytes); +struct Buffer *new_buffer(); +typedef struct message message; +struct message { + long timestamp; + int width; // varies + int height; // normally 80 + char **image; // dimension is width x height +}; +struct Buffer { + int size; + void *data; +}; +#define INITIAL_SIZE 32 +#define INTERFACE 0