A bit less goofy
This commit is contained in:
parent
7ba0ec5e07
commit
c0b350038d
2 changed files with 30 additions and 108 deletions
|
@ -15,7 +15,7 @@ enum part_type {
|
|||
struct part {
|
||||
enum part_type type;
|
||||
enum skipstone_message_endianness endianness;
|
||||
uint16_t size;
|
||||
uint8_t size;
|
||||
};
|
||||
|
||||
struct _skipstone_message {
|
||||
|
@ -57,19 +57,19 @@ error_malloc_message:
|
|||
return NULL;
|
||||
}
|
||||
|
||||
int skipstone_message_append_string(skipstone_message *message,
|
||||
char *string, uint8_t size) {
|
||||
static int append_part(skipstone_message *message,
|
||||
void *buf, uint8_t size, enum part_type type) {
|
||||
struct part *part;
|
||||
|
||||
if ((part = malloc(sizeof(*part) + size)) == NULL) {
|
||||
goto error_malloc_part;
|
||||
}
|
||||
|
||||
part->type = SKIPSTONE_MESSAGE_PART_STRING;
|
||||
part->type = type;
|
||||
part->endianness = SKIPSTONE_MESSAGE_ENDIAN_LITTLE;
|
||||
part->size = size;
|
||||
|
||||
memcpy(part + 1, string, size);
|
||||
memcpy(part + 1, buf, size);
|
||||
|
||||
if (skipstone_queue_add(message->parts, part) < 0) {
|
||||
goto error_queue_add_parts;
|
||||
|
@ -84,58 +84,19 @@ error_malloc_part:
|
|||
return -1;
|
||||
}
|
||||
|
||||
int skipstone_message_append_uint8(skipstone_message *message,
|
||||
uint8_t value) {
|
||||
struct part *part;
|
||||
|
||||
if ((part = malloc(sizeof(*part) + sizeof(uint8_t))) == NULL) {
|
||||
goto error_malloc_part;
|
||||
}
|
||||
|
||||
part->type = SKIPSTONE_MESSAGE_PART_UINT8;
|
||||
part->endianness = SKIPSTONE_MESSAGE_ENDIAN_LITTLE;
|
||||
part->size = sizeof(uint8_t);
|
||||
|
||||
((uint8_t *)(part + 1))[0] = value;
|
||||
|
||||
if (skipstone_queue_add(message->parts, part) < 0) {
|
||||
goto error_queue_add_parts;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
error_queue_add_parts:
|
||||
free(part);
|
||||
|
||||
error_malloc_part:
|
||||
return -1;
|
||||
int skipstone_message_append_string(skipstone_message *message,
|
||||
char *string, uint8_t size) {
|
||||
return append_part(message, string, size, SKIPSTONE_MESSAGE_PART_STRING);
|
||||
}
|
||||
|
||||
int skipstone_message_append_uint16(skipstone_message *message,
|
||||
uint16_t value) {
|
||||
struct part *part;
|
||||
int skipstone_message_append_uint8(skipstone_message *message, uint8_t value) {
|
||||
return append_part(message, &value, sizeof(value),
|
||||
SKIPSTONE_MESSAGE_PART_UINT8);
|
||||
}
|
||||
|
||||
if ((part = malloc(sizeof(*part) + sizeof(uint16_t))) == NULL) {
|
||||
goto error_malloc_part;
|
||||
}
|
||||
|
||||
part->type = SKIPSTONE_MESSAGE_PART_UINT16;
|
||||
part->endianness = SKIPSTONE_MESSAGE_ENDIAN_LITTLE;
|
||||
part->size = sizeof(uint16_t);
|
||||
|
||||
((uint16_t *)(part + 1))[0] = value;
|
||||
|
||||
if (skipstone_queue_add(message->parts, part) < 0) {
|
||||
goto error_queue_add_parts;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
error_queue_add_parts:
|
||||
free(part);
|
||||
|
||||
error_malloc_part:
|
||||
return -1;
|
||||
int skipstone_message_append_uint16(skipstone_message *message, uint16_t value) {
|
||||
return append_part(message, &value, sizeof(value),
|
||||
SKIPSTONE_MESSAGE_PART_UINT16);
|
||||
}
|
||||
|
||||
int skipstone_message_pack(skipstone_message *message,
|
||||
|
|
|
@ -15,7 +15,7 @@ enum part_type {
|
|||
struct part {
|
||||
enum part_type type;
|
||||
enum skipstone_message_endianness endianness;
|
||||
uint16_t size;
|
||||
uint8_t size;
|
||||
};
|
||||
|
||||
struct _skipstone_message {
|
||||
|
@ -57,19 +57,19 @@ error_malloc_message:
|
|||
return NULL;
|
||||
}
|
||||
|
||||
int skipstone_message_append_string(skipstone_message *message,
|
||||
char *string, uint8_t size) {
|
||||
static int append_part(skipstone_message *message,
|
||||
void *buf, uint8_t size, enum part_type type) {
|
||||
struct part *part;
|
||||
|
||||
if ((part = malloc(sizeof(*part) + size)) == NULL) {
|
||||
goto error_malloc_part;
|
||||
}
|
||||
|
||||
part->type = SKIPSTONE_MESSAGE_PART_STRING;
|
||||
part->type = type;
|
||||
part->endianness = SKIPSTONE_MESSAGE_ENDIAN_LITTLE;
|
||||
part->size = size;
|
||||
|
||||
memcpy(part + 1, string, size);
|
||||
memcpy(part + 1, buf, size);
|
||||
|
||||
if (skipstone_queue_add(message->parts, part) < 0) {
|
||||
goto error_queue_add_parts;
|
||||
|
@ -84,58 +84,19 @@ error_malloc_part:
|
|||
return -1;
|
||||
}
|
||||
|
||||
int skipstone_message_append_uint8(skipstone_message *message,
|
||||
uint8_t value) {
|
||||
struct part *part;
|
||||
|
||||
if ((part = malloc(sizeof(*part) + sizeof(uint8_t))) == NULL) {
|
||||
goto error_malloc_part;
|
||||
}
|
||||
|
||||
part->type = SKIPSTONE_MESSAGE_PART_UINT8;
|
||||
part->endianness = SKIPSTONE_MESSAGE_ENDIAN_LITTLE;
|
||||
part->size = sizeof(uint8_t);
|
||||
|
||||
((uint8_t *)(part + 1))[0] = value;
|
||||
|
||||
if (skipstone_queue_add(message->parts, part) < 0) {
|
||||
goto error_queue_add_parts;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
error_queue_add_parts:
|
||||
free(part);
|
||||
|
||||
error_malloc_part:
|
||||
return -1;
|
||||
int skipstone_message_append_string(skipstone_message *message,
|
||||
char *string, uint8_t size) {
|
||||
return append_part(message, string, size, SKIPSTONE_MESSAGE_PART_STRING);
|
||||
}
|
||||
|
||||
int skipstone_message_append_uint16(skipstone_message *message,
|
||||
uint16_t value) {
|
||||
struct part *part;
|
||||
int skipstone_message_append_uint8(skipstone_message *message, uint8_t value) {
|
||||
return append_part(message, &value, sizeof(value),
|
||||
SKIPSTONE_MESSAGE_PART_UINT8);
|
||||
}
|
||||
|
||||
if ((part = malloc(sizeof(*part) + sizeof(uint16_t))) == NULL) {
|
||||
goto error_malloc_part;
|
||||
}
|
||||
|
||||
part->type = SKIPSTONE_MESSAGE_PART_UINT16;
|
||||
part->endianness = SKIPSTONE_MESSAGE_ENDIAN_LITTLE;
|
||||
part->size = sizeof(uint16_t);
|
||||
|
||||
((uint16_t *)(part + 1))[0] = value;
|
||||
|
||||
if (skipstone_queue_add(message->parts, part) < 0) {
|
||||
goto error_queue_add_parts;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
error_queue_add_parts:
|
||||
free(part);
|
||||
|
||||
error_malloc_part:
|
||||
return -1;
|
||||
int skipstone_message_append_uint16(skipstone_message *message, uint16_t value) {
|
||||
return append_part(message, &value, sizeof(value),
|
||||
SKIPSTONE_MESSAGE_PART_UINT16);
|
||||
}
|
||||
|
||||
int skipstone_message_pack(skipstone_message *message,
|
||||
|
|
Loading…
Add table
Reference in a new issue