[RELEASE] u22-v03

This version is submitted to U22 breau.
This commit is contained in:
2020-09-14 00:00:00 +00:00
parent 360595de37
commit 84c3a02b9a
357 changed files with 29223 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
add_library(mpkutil
file.c
get.c
pack.c
)
target_link_libraries(mpkutil
msgpackc
math
)

4
util/mpkutil/README.md Normal file
View File

@@ -0,0 +1,4 @@
mpkutil
====
utility library for msgpack-c

58
util/mpkutil/file.c Normal file
View File

@@ -0,0 +1,58 @@
#include "./file.h"
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <msgpack.h>
bool mpkutil_file_unpack_with_unpacker(
msgpack_unpacked* obj,
FILE* fp,
msgpack_unpacker* unpacker) {
assert(obj != NULL);
assert(fp != NULL);
assert(unpacker != NULL);
for (;;) {
if (feof(fp)) return false;
const size_t maxlen = msgpack_unpacker_buffer_capacity(unpacker);
if (maxlen == 0) {
fprintf(stderr, "unpacker buffer overflow\n");
abort();
}
const size_t len = fread(
msgpack_unpacker_buffer(unpacker), 1, maxlen, fp);
if (ferror(fp)) return false;
msgpack_unpacker_buffer_consumed(unpacker, len);
size_t parsed_len;
switch (msgpack_unpacker_next_with_size(unpacker, obj, &parsed_len)) {
case MSGPACK_UNPACK_SUCCESS:
return fseek(fp, (long) parsed_len - len, SEEK_CUR) == 0;
case MSGPACK_UNPACK_CONTINUE:
break;
case MSGPACK_UNPACK_PARSE_ERROR:
default:
return false;
}
}
}
bool mpkutil_file_unpack(msgpack_unpacked* obj, FILE* fp) {
assert(fp != NULL);
assert(obj != NULL);
msgpack_unpacker unpacker;
if (!msgpack_unpacker_init(&unpacker, 1024)) return false;
const bool result = mpkutil_file_unpack_with_unpacker(obj, fp, &unpacker);
msgpack_unpacker_destroy(&unpacker);
return result;
}

19
util/mpkutil/file.h Normal file
View File

@@ -0,0 +1,19 @@
#pragma once
#include <stdbool.h>
#include <stdio.h>
#include <msgpack.h>
bool
mpkutil_file_unpack_with_unpacker(
msgpack_unpacked* obj,
FILE* fp,
msgpack_unpacker* unpacker
);
bool
mpkutil_file_unpack(
msgpack_unpacked* obj,
FILE* fp
);

143
util/mpkutil/get.c Normal file
View File

@@ -0,0 +1,143 @@
#include "./get.h"
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <msgpack.h>
#include "util/math/algorithm.h"
#include "util/math/vector.h"
bool mpkutil_get_bool(const msgpack_object* obj, bool* b) {
if (obj == NULL) return false;
if (obj->type != MSGPACK_OBJECT_BOOLEAN) return false;
if (b != NULL) *b = obj->via.boolean;
return true;
}
bool mpkutil_get_int(const msgpack_object* obj, intmax_t* i) {
if (obj == NULL) return false;
if (obj->type != MSGPACK_OBJECT_POSITIVE_INTEGER &&
obj->type != MSGPACK_OBJECT_NEGATIVE_INTEGER) {
return false;
}
if (i != NULL) *i = obj->via.i64;
return true;
}
#define define_mpkutil_get_intN_(N) \
bool mpkutil_get_int##N(const msgpack_object* obj, int##N##_t* i) { \
intmax_t temp; \
if (!mpkutil_get_int(obj, &temp) || \
temp < INT##N##_MIN || temp > INT##N##_MAX) { \
return false; \
} \
if (i != NULL) *i = temp; \
return true; \
}
define_mpkutil_get_intN_(8);
define_mpkutil_get_intN_(16);
define_mpkutil_get_intN_(32);
define_mpkutil_get_intN_(64);
#undef define_mpkutil_get_intN_
bool mpkutil_get_uint(const msgpack_object* obj, uintmax_t* i) {
if (obj == NULL || obj->type != MSGPACK_OBJECT_POSITIVE_INTEGER) {
return false;
}
if (i != NULL) *i = obj->via.u64;
return true;
}
#define define_mpkutil_get_uintN_(N) \
bool mpkutil_get_uint##N(const msgpack_object* obj, uint##N##_t* i) { \
uintmax_t temp; \
if (!mpkutil_get_uint(obj, &temp) || temp > UINT##N##_MAX) { \
return false; \
} \
if (i != NULL) *i = temp; \
return true; \
}
define_mpkutil_get_uintN_(8);
define_mpkutil_get_uintN_(16);
define_mpkutil_get_uintN_(32);
define_mpkutil_get_uintN_(64);
#undef define_mpkutil_get_uintN_
bool mpkutil_get_float(const msgpack_object* obj, float* f) {
if (obj == NULL || obj->type != MSGPACK_OBJECT_FLOAT64 ||
!MATH_FLOAT_VALID(obj->via.f64)) {
return false;
}
if (f != NULL) *f = obj->via.f64;
return true;
}
bool mpkutil_get_str(const msgpack_object* obj, const char** s, size_t* len) {
if (obj == NULL || obj->type != MSGPACK_OBJECT_STR) {
return false;
}
if (s != NULL) *s = obj->via.str.ptr;
if (len != NULL) *len = obj->via.str.size;
return true;
}
bool mpkutil_get_vec2(const msgpack_object* obj, vec2_t* v) {
const msgpack_object_array* array = mpkutil_get_array(obj);
if (array == NULL || array->size != 2) return false;
for (size_t i = 0; i < 2; ++i) {
if (array->ptr[i].type != MSGPACK_OBJECT_FLOAT64) return false;
}
if (v != NULL) {
v->x = array->ptr[0].via.f64;
v->y = array->ptr[1].via.f64;
}
return true;
}
bool mpkutil_get_vec4(const msgpack_object* obj, vec4_t* v) {
const msgpack_object_array* array = mpkutil_get_array(obj);
if (array == NULL || array->size != 4) return false;
for (size_t i = 0; i < 4; ++i) {
if (array->ptr[i].type != MSGPACK_OBJECT_FLOAT64) return false;
}
if (v != NULL) {
v->x = array->ptr[0].via.f64;
v->y = array->ptr[1].via.f64;
v->y = array->ptr[2].via.f64;
v->y = array->ptr[3].via.f64;
}
return true;
}
const msgpack_object_array* mpkutil_get_array(const msgpack_object* obj) {
if (obj == NULL || obj->type != MSGPACK_OBJECT_ARRAY) {
return NULL;
}
return &obj->via.array;
}
const msgpack_object_map* mpkutil_get_map(const msgpack_object* obj) {
if (obj == NULL || obj->type != MSGPACK_OBJECT_MAP) {
return NULL;
}
return &obj->via.map;
}
const msgpack_object* mpkutil_get_map_item_by_str(
const msgpack_object_map* map, const char* name) {
if (map == NULL) return NULL;
for (size_t i = 0; i < map->size; ++i) {
const msgpack_object* key_obj = &map->ptr[i].key;
if (key_obj->type != MSGPACK_OBJECT_STR) continue;
const msgpack_object_str* key = &key_obj->via.str;
if (strncmp(key->ptr, name, key->size) == 0 && name[key->size] == 0) {
return &map->ptr[i].val;
}
}
return NULL;
}

92
util/mpkutil/get.h Normal file
View File

@@ -0,0 +1,92 @@
#pragma once
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <msgpack.h>
#include "util/math/vector.h"
bool
mpkutil_get_bool(
const msgpack_object* obj, /* NULLABLE */
bool* b /* NULLABLE */
);
bool
mpkutil_get_int(
const msgpack_object* obj, /* NULLABLE */
intmax_t* i /* NULLABLE */
);
#define decl_mpkutil_get_intN_(N) \
bool \
mpkutil_get_int##N( \
const msgpack_object* obj, \
int##N##_t* i \
);
decl_mpkutil_get_intN_(8);
decl_mpkutil_get_intN_(16);
decl_mpkutil_get_intN_(32);
decl_mpkutil_get_intN_(64);
#undef decl_mpkutil_get_intN_
bool
mpkutil_get_uint(
const msgpack_object* obj, /* NULLABLE */
uintmax_t* i /* NULLABLE */
);
#define decl_mpkutil_get_uintN_(N) \
bool \
mpkutil_get_uint##N( \
const msgpack_object* obj, \
uint##N##_t* i \
);
decl_mpkutil_get_uintN_(8);
decl_mpkutil_get_uintN_(16);
decl_mpkutil_get_uintN_(32);
decl_mpkutil_get_uintN_(64);
#undef decl_mpkutil_get_uintN_
bool
mpkutil_get_float(
const msgpack_object* obj, /* NULLABLE */
float* f /* NULLABLE */
);
bool
mpkutil_get_str(
const msgpack_object* obj, /* NULLABLE */
const char** s, /* NULLABLE */
size_t* len /* NULLABLE */
);
bool
mpkutil_get_vec2(
const msgpack_object* obj, /* NULLABLE */
vec2_t* v /* NULLABLE */
);
bool
mpkutil_get_vec4(
const msgpack_object* obj, /* NULLABLE */
vec4_t* v /* NULLABLE */
);
const msgpack_object_array* /* NULLABLE */
mpkutil_get_array(
const msgpack_object* obj /* NULLABLE */
);
const msgpack_object_map* /* NULLABLE */
mpkutil_get_map(
const msgpack_object* obj /* NULLABLE */
);
const msgpack_object* /* NULLABLE */
mpkutil_get_map_item_by_str(
const msgpack_object_map* map, /* NULLABLE */
const char* name /* NULL-terminated */
);

49
util/mpkutil/pack.c Normal file
View File

@@ -0,0 +1,49 @@
#include "./pack.h"
#include <assert.h>
#include <stddef.h>
#include <msgpack.h>
#include "util/math/vector.h"
void mpkutil_pack_bool(msgpack_packer* packer, bool b) {
assert(packer != NULL);
(b? msgpack_pack_true: msgpack_pack_false)(packer);
}
void mpkutil_pack_str(msgpack_packer* packer, const char* str) {
assert(packer != NULL);
assert(str != NULL);
mpkutil_pack_strn(packer, str, strlen(str));
}
void mpkutil_pack_strn(msgpack_packer* packer, const char* str, size_t len) {
assert(packer != NULL);
assert(str != NULL);
msgpack_pack_str(packer, len);
msgpack_pack_str_body(packer, str, len);
}
void mpkutil_pack_vec2(msgpack_packer* packer, const vec2_t* v) {
assert(packer != NULL);
assert(v != NULL);
msgpack_pack_array(packer, 2);
msgpack_pack_double(packer, v->x);
msgpack_pack_double(packer, v->y);
}
void mpkutil_pack_vec4(msgpack_packer* packer, const vec4_t* v) {
assert(packer != NULL);
assert(v != NULL);
msgpack_pack_array(packer, 4);
msgpack_pack_double(packer, v->x);
msgpack_pack_double(packer, v->y);
msgpack_pack_double(packer, v->z);
msgpack_pack_double(packer, v->w);
}

39
util/mpkutil/pack.h Normal file
View File

@@ -0,0 +1,39 @@
#pragma once
#include <stdbool.h>
#include <stddef.h>
#include <msgpack.h>
#include "util/math/vector.h"
void
mpkutil_pack_bool(
msgpack_packer* packer,
bool v
);
void
mpkutil_pack_str(
msgpack_packer* packer,
const char* str /* NULL-terminated */
);
void
mpkutil_pack_strn(
msgpack_packer* packer,
const char* str,
size_t len
);
void
mpkutil_pack_vec2(
msgpack_packer* packer,
const vec2_t* v
);
void
mpkutil_pack_vec4(
msgpack_packer* packer,
const vec4_t* v
);