gosl/msg.c

67 lines
1.1 KiB
C
Raw Normal View History

2013-11-02 14:44:17 +01:00
#include <stdlib.h>
#include <stdio.h>
2013-11-02 15:08:19 +01:00
#include <string.h>
2013-11-02 14:44:17 +01:00
#include <sys/types.h>
#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;
}
2013-11-02 15:08:19 +01:00
void append_space(Buffer * b, int n) {
b->size += n;
2013-11-02 14:44:17 +01:00
b->data = realloc(b->data, b->size);
}
void serialize_int(int x, Buffer * b) {
2013-11-02 15:08:19 +01:00
// htonl :: uint32_t -> uint32_t -- converts the parameter from host byte order to network byte order
2013-11-02 14:44:17 +01:00
x = htonl(x);
append_space(b, sizeof(int));
2013-11-02 15:08:19 +01:00
memcpy( ((char*)b->data) + b->size, &x, sizeof(int));
b->size += sizeof(int);
2013-11-02 14:44:17 +01:00
}
2013-11-02 15:08:19 +01:00
void serialize_string(char *str, Buffer *b) {
int newlen = strlen(str);
append_space(b, newlen);
for (int i=0; i < newlen; ++i) {
memcpy( ((char*)b->data) + b->size + i, str[i], sizeof(char));
}
b->size += newlen;
}
2013-11-02 15:01:40 +01:00
2013-11-02 14:44:17 +01:00
//serialize_message