serialize_string

This commit is contained in:
Lennart Buhl 2013-11-02 15:08:19 +01:00
parent 7856a47f69
commit bdb20e8de1

27
msg.c
View File

@ -1,5 +1,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <sys/types.h> #include <sys/types.h>
#include "msg.h" #include "msg.h"
@ -31,25 +32,35 @@ struct Buffer *new_buffer() {
return b; return b;
} }
void append_space(Buffer * b, int bytes) { void append_space(Buffer * b, int n) {
b->size += bytes; b->size += n;
b->data = realloc(b->data, b->size); b->data = realloc(b->data, b->size);
} }
void serialize_int(int x, Buffer * b) { void serialize_int(int x, Buffer * b) {
// htonl :: uint32_t -> uint32_t // htonl :: uint32_t -> uint32_t -- converts the parameter from host byte order to network byte order
x = htonl(x); x = htonl(x);
append_space(b, sizeof(int)); append_space(b, sizeof(int));
memcpy(((char *)b->data) + b->next, &x, sizeof(int)); memcpy( ((char*)b->data) + b->size, &x, sizeof(int));
b->next += sizeof(int); b->size += sizeof(int);
}
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;
} }
void struct
//serialize_string
//serialize_message //serialize_message