mirror of
git://git.psyced.org/git/psyclpc
synced 2024-08-15 03:20:16 +00:00
libpsyc refactoring
This commit is contained in:
parent
723775f1f9
commit
24abe02441
2 changed files with 52 additions and 52 deletions
|
@ -97,8 +97,8 @@ fill_header_from_mapping (svalue_t *key, svalue_t *val, void *extra) {
|
|||
uint8_t type;
|
||||
svalue_t vsp, *lval;
|
||||
|
||||
psycList list;
|
||||
psycString *elems = NULL;
|
||||
PsycList list;
|
||||
PsycString *elems = NULL;
|
||||
|
||||
if (key->type != T_STRING) {
|
||||
errorf("fill_header_from_mapping: key type %d not supported\n", key->type);
|
||||
|
@ -107,7 +107,7 @@ fill_header_from_mapping (svalue_t *key, svalue_t *val, void *extra) {
|
|||
|
||||
name = get_txt(key->u.str);
|
||||
namelen = mstrsize(key->u.str);
|
||||
type = psyc_getVarType2(name, namelen);
|
||||
type = psyc_var_type(name, namelen);
|
||||
|
||||
if (m->num_values > 1)
|
||||
oper = val[1].u.number;
|
||||
|
@ -142,7 +142,7 @@ fill_header_from_mapping (svalue_t *key, svalue_t *val, void *extra) {
|
|||
|
||||
case T_POINTER:
|
||||
if (VEC_SIZE(val->u.vec)) {
|
||||
elems = pxalloc(sizeof(psycString) * VEC_SIZE(val->u.vec));
|
||||
elems = pxalloc(sizeof(PsycString) * VEC_SIZE(val->u.vec));
|
||||
if (!elems) {
|
||||
errorf("Out of memory in fill_header_from_mapping for elems\n");
|
||||
return; // not reached
|
||||
|
@ -152,7 +152,7 @@ fill_header_from_mapping (svalue_t *key, svalue_t *val, void *extra) {
|
|||
lval = &(val->u.vec->item[i]);
|
||||
switch (lval->type) {
|
||||
case T_STRING:
|
||||
elems[i] = (psycString){mstrsize(lval->u.str), get_txt(lval->u.str)};
|
||||
elems[i] = (PsycString){mstrsize(lval->u.str), get_txt(lval->u.str)};
|
||||
break;
|
||||
case T_NUMBER:
|
||||
case T_OBJECT:
|
||||
|
@ -167,7 +167,7 @@ fill_header_from_mapping (svalue_t *key, svalue_t *val, void *extra) {
|
|||
}
|
||||
|
||||
f_to_string(&vsp);
|
||||
elems[i] = (psycString){mstrsize(vsp.u.str), get_txt(vsp.u.str)};
|
||||
elems[i] = (PsycString){mstrsize(vsp.u.str), get_txt(vsp.u.str)};
|
||||
break;
|
||||
default:
|
||||
errorf("fill_header_from_mapping: list value type %d not supported\n", lval->type);
|
||||
|
@ -176,7 +176,7 @@ fill_header_from_mapping (svalue_t *key, svalue_t *val, void *extra) {
|
|||
}
|
||||
}
|
||||
|
||||
list = psyc_newList(elems, VEC_SIZE(val->u.vec), PSYC_LIST_CHECK_LENGTH);
|
||||
list = psyc_list_new(elems, VEC_SIZE(val->u.vec), PSYC_LIST_CHECK_LENGTH);
|
||||
valuelen = list.length;
|
||||
value = pxalloc(valuelen);
|
||||
if (!value) {
|
||||
|
@ -184,7 +184,7 @@ fill_header_from_mapping (svalue_t *key, svalue_t *val, void *extra) {
|
|||
return; // not reached
|
||||
}
|
||||
|
||||
psyc_renderList(&list, value, valuelen);
|
||||
psyc_render_list(&list, value, valuelen);
|
||||
break;
|
||||
|
||||
default:
|
||||
|
@ -193,7 +193,7 @@ fill_header_from_mapping (svalue_t *key, svalue_t *val, void *extra) {
|
|||
}
|
||||
|
||||
m->header->modifiers[m->header->lines++] =
|
||||
psyc_newModifier2(oper, name, namelen, value, valuelen, m->flag);
|
||||
psyc_modifier_new(oper, name, namelen, value, valuelen, m->flag);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*/
|
||||
|
@ -209,8 +209,8 @@ f_psyc_render(svalue_t *sp) {
|
|||
size_t mlen, blen;
|
||||
mapping_t *map;
|
||||
|
||||
psycPacket packet;
|
||||
psycHeader headers[2];
|
||||
PsycPacket packet;
|
||||
PsycHeader headers[2];
|
||||
|
||||
// unless (sp->type == T_POINTER) return sp;
|
||||
v = sp->u.vec;
|
||||
|
@ -221,7 +221,7 @@ f_psyc_render(svalue_t *sp) {
|
|||
map = v->item[i].u.map;
|
||||
if (!MAP_SIZE(map)) continue;
|
||||
|
||||
headers[i].modifiers = malloc(sizeof(psycModifier) * MAP_SIZE(v->item[i].u.map));
|
||||
headers[i].modifiers = malloc(sizeof(PsycModifier) * MAP_SIZE(v->item[i].u.map));
|
||||
if (!headers[i].modifiers) {
|
||||
errorf("Out of memory in psyc_render for modifier table.\n");
|
||||
return sp; // not reached
|
||||
|
@ -259,7 +259,7 @@ f_psyc_render(svalue_t *sp) {
|
|||
blen = 0;
|
||||
}
|
||||
|
||||
packet = psyc_newPacket2(headers[PACKET_ROUTING].modifiers,
|
||||
packet = psyc_packet_new(headers[PACKET_ROUTING].modifiers,
|
||||
headers[PACKET_ROUTING].lines,
|
||||
headers[PACKET_ENTITY].modifiers,
|
||||
headers[PACKET_ENTITY].lines,
|
||||
|
@ -293,8 +293,8 @@ f_psyc_parse (svalue_t *sp) {
|
|||
vector_t *v, *list;
|
||||
mapping_t *map;
|
||||
char oper = 0;
|
||||
psycString name = {0,0}, value = {0,0}, elems[MAX_LIST_SIZE], elem;
|
||||
psycParseListState listState;
|
||||
PsycString name = {0,0}, value = {0,0}, elems[MAX_LIST_SIZE], elem;
|
||||
PsycParseListState listState;
|
||||
int ret, retl, type = -1, error = 0;
|
||||
size_t size, i;
|
||||
ssize_t n;
|
||||
|
@ -317,12 +317,12 @@ f_psyc_parse (svalue_t *sp) {
|
|||
O_GET_PSYC_STATE(current_object) = state;
|
||||
memset(state, 0, sizeof(psyc_state_t));
|
||||
|
||||
state->parser = pxalloc(sizeof(psycParseState));
|
||||
state->parser = pxalloc(sizeof(PsycParseState));
|
||||
if (!state->parser) {
|
||||
errorf("Out of memory for psyc parse state struct.\n");
|
||||
return sp; // not reached
|
||||
}
|
||||
psyc_initParseState(state->parser);
|
||||
psyc_parse_state_init(state->parser, PSYC_PARSE_ALL);
|
||||
}
|
||||
v = state->packet;
|
||||
|
||||
|
@ -345,13 +345,13 @@ f_psyc_parse (svalue_t *sp) {
|
|||
memcpy(buffer, state->remaining, state->remaining_len);
|
||||
memcpy(buffer + state->remaining_len, get_txt(sp->u.str),
|
||||
mstrsize(sp->u.str));
|
||||
psyc_setParseBuffer2(state->parser, buffer,
|
||||
psyc_parse_buffer_set(state->parser, buffer,
|
||||
state->remaining_len + mstrsize(sp->u.str));
|
||||
pfree(state->remaining);
|
||||
state->remaining = NULL;
|
||||
state->remaining_len = 0;
|
||||
} else {
|
||||
psyc_setParseBuffer2(state->parser, get_txt(sp->u.str),
|
||||
psyc_parse_buffer_set(state->parser, get_txt(sp->u.str),
|
||||
mstrsize(sp->u.str));
|
||||
}
|
||||
} else {
|
||||
|
@ -363,7 +363,7 @@ f_psyc_parse (svalue_t *sp) {
|
|||
ret = psyc_parse(state->parser, &oper, &name, &value);
|
||||
#ifdef DEBUG
|
||||
printf("#%2d %c%.*s = %.*s\n", ret, oper ? oper : ' ',
|
||||
(int)name.length, name.ptr, (int)value.length, value.ptr);
|
||||
(int)name.length, name.data, (int)value.length, value.data);
|
||||
#endif
|
||||
if (!state->packet) {
|
||||
state->packet = allocate_array(4);
|
||||
|
@ -393,7 +393,7 @@ f_psyc_parse (svalue_t *sp) {
|
|||
// incomplete entity or body
|
||||
state->oper = oper;
|
||||
state->name = mstring_alloc_string(name.length);
|
||||
memcpy(get_txt(state->name), name.ptr, name.length);
|
||||
memcpy(get_txt(state->name), name.data, name.length);
|
||||
if (!state->name) {
|
||||
errorf("Out of memory for name.\n");
|
||||
return sp; // not reached
|
||||
|
@ -401,7 +401,7 @@ f_psyc_parse (svalue_t *sp) {
|
|||
|
||||
// allocate memory for the total length of the value
|
||||
state->value_len = 0;
|
||||
state->value = mstring_alloc_string(psyc_getParseValueLength(state->parser));
|
||||
state->value = mstring_alloc_string(psyc_parse_value_length(state->parser));
|
||||
if (!state->value) {
|
||||
errorf("Out of memory for value.\n");
|
||||
return sp; // not reached
|
||||
|
@ -411,7 +411,7 @@ f_psyc_parse (svalue_t *sp) {
|
|||
case PSYC_PARSE_ENTITY_CONT: case PSYC_PARSE_BODY_CONT:
|
||||
case PSYC_PARSE_ENTITY_END: case PSYC_PARSE_BODY_END:
|
||||
// append value to tmp buffer in state
|
||||
memcpy(get_txt(state->value) + state->value_len, value.ptr, value.length);
|
||||
memcpy(get_txt(state->value) + state->value_len, value.data, value.length);
|
||||
state->value_len += value.length;
|
||||
}
|
||||
|
||||
|
@ -419,9 +419,9 @@ f_psyc_parse (svalue_t *sp) {
|
|||
// incomplete entity or body parsing done,
|
||||
// set oper/name/value to the ones saved in state
|
||||
oper = state->oper;
|
||||
name.ptr = get_txt(state->name);
|
||||
name.data = get_txt(state->name);
|
||||
name.length = mstrsize(state->name);
|
||||
value.ptr = get_txt(state->value);
|
||||
value.data = get_txt(state->value);
|
||||
value.length = mstrsize(state->value);
|
||||
}
|
||||
|
||||
|
@ -431,13 +431,13 @@ f_psyc_parse (svalue_t *sp) {
|
|||
|
||||
// new_n_tabled fetches a reference of a probably existing
|
||||
// shared string
|
||||
put_string(sv, new_n_tabled(name.ptr, name.length));
|
||||
put_string(sv, new_n_tabled(name.data, name.length));
|
||||
sv = get_map_lvalue(v->item[PACKET_ROUTING].u.map, sv);
|
||||
put_number(&sv[1], oper);
|
||||
// strings are capable of containing 0 so we can do this
|
||||
// for binary data too. let's use a tabled string even
|
||||
// for values of routing variables as they repeat a lot
|
||||
put_string(sv, new_n_tabled(value.ptr, value.length));
|
||||
put_string(sv, new_n_tabled(value.data, value.length));
|
||||
break;
|
||||
|
||||
case PSYC_PARSE_ENTITY_START:
|
||||
|
@ -449,54 +449,54 @@ f_psyc_parse (svalue_t *sp) {
|
|||
sv = pxalloc(sizeof(svalue_t));
|
||||
|
||||
if (ret == PSYC_PARSE_ENTITY)
|
||||
put_string(sv, new_n_tabled(name.ptr, name.length));
|
||||
put_string(sv, new_n_tabled(name.data, name.length));
|
||||
else // PSYC_PARSE_ENTITY_END
|
||||
put_string(sv, make_tabled(state->name));
|
||||
|
||||
sv = get_map_lvalue(v->item[PACKET_ENTITY].u.map, sv);
|
||||
put_number(&sv[1], oper);
|
||||
|
||||
type = psyc_getVarType(&name);
|
||||
type = psyc_var_type(PSYC_S2ARG(name));
|
||||
|
||||
switch (type) {
|
||||
case PSYC_TYPE_DATE: // number + PSYC_EPOCH
|
||||
if (psyc_parseDate(&value, &timmy))
|
||||
if (psyc_parse_date(PSYC_S2ARG(value), &timmy))
|
||||
put_number(sv, timmy);
|
||||
else
|
||||
error = PSYC_PARSE_ERROR_DATE;
|
||||
break;
|
||||
case PSYC_TYPE_TIME: // number
|
||||
if (psyc_parseTime(&value, &timmy))
|
||||
if (psyc_parse_time(PSYC_S2ARG(value), &timmy))
|
||||
put_number(sv, timmy);
|
||||
else
|
||||
error = PSYC_PARSE_ERROR_TIME;
|
||||
break;
|
||||
case PSYC_TYPE_AMOUNT: // number
|
||||
if (psyc_parseNumber(&value, &n))
|
||||
if (psyc_parse_number(PSYC_S2ARG(value), &n))
|
||||
put_number(sv, n);
|
||||
else
|
||||
error = PSYC_PARSE_ERROR_AMOUNT;
|
||||
break;
|
||||
case PSYC_TYPE_DEGREE: // first digit
|
||||
if (value.length && value.ptr[0] >= '0' && value.ptr[0] <= '9')
|
||||
put_number(sv, value.ptr[0] - '0');
|
||||
if (value.length && value.data[0] >= '0' && value.data[0] <= '9')
|
||||
put_number(sv, value.data[0] - '0');
|
||||
else
|
||||
error = PSYC_PARSE_ERROR_DEGREE;
|
||||
break;
|
||||
case PSYC_TYPE_FLAG: // 0 or 1
|
||||
if (value.length && value.ptr[0] >= '0' && value.ptr[0] <= '1')
|
||||
put_number(sv, value.ptr[0] - '0');
|
||||
if (value.length && value.data[0] >= '0' && value.data[0] <= '1')
|
||||
put_number(sv, value.data[0] - '0');
|
||||
else
|
||||
error = PSYC_PARSE_ERROR_FLAG;
|
||||
break;
|
||||
case PSYC_TYPE_LIST: // array
|
||||
size = 0;
|
||||
if (value.length) {
|
||||
psyc_initParseListState(&listState);
|
||||
psyc_setParseListBuffer(&listState, value);
|
||||
elem = (psycString){0, 0};
|
||||
psyc_parse_list_state_init(&listState);
|
||||
psyc_parse_list_buffer_set(&listState, PSYC_S2ARG(value));
|
||||
elem = (PsycString){0, 0};
|
||||
do {
|
||||
retl = psyc_parseList(&listState, &elem);
|
||||
retl = psyc_parse_list(&listState, &elem);
|
||||
switch (retl) {
|
||||
case PSYC_PARSE_LIST_END:
|
||||
retl = 0;
|
||||
|
@ -516,7 +516,7 @@ f_psyc_parse (svalue_t *sp) {
|
|||
|
||||
list = allocate_array(size);
|
||||
for (i = 0; i < size; i++)
|
||||
put_string(&list->item[i], new_n_tabled(elems[i].ptr,
|
||||
put_string(&list->item[i], new_n_tabled(elems[i].data,
|
||||
elems[i].length));
|
||||
|
||||
put_array(sv, list);
|
||||
|
@ -525,8 +525,8 @@ f_psyc_parse (svalue_t *sp) {
|
|||
if (ret == PSYC_PARSE_ENTITY)
|
||||
// is it good to put entity variable values into the
|
||||
// shared string table? probably yes.. but it's a guess
|
||||
//t_string(sv, new_n_mstring(value.ptr, value.length));
|
||||
put_string(sv, new_n_tabled(value.ptr, value.length));
|
||||
//t_string(sv, new_n_mstring(value.data, value.length));
|
||||
put_string(sv, new_n_tabled(value.data, value.length));
|
||||
else // PSYC_PARSE_ENTITY_END
|
||||
put_string(sv, state->value);
|
||||
}
|
||||
|
@ -544,11 +544,11 @@ f_psyc_parse (svalue_t *sp) {
|
|||
case PSYC_PARSE_BODY:
|
||||
// new_n_tabled gets the shared string for the method
|
||||
put_string(&v->item[PACKET_METHOD],
|
||||
new_n_tabled(name.ptr, name.length));
|
||||
new_n_tabled(name.data, name.length));
|
||||
|
||||
// allocate an untabled string for the packet body
|
||||
put_string(&v->item[PACKET_BODY],
|
||||
new_n_mstring(value.ptr, value.length));
|
||||
new_n_mstring(value.data, value.length));
|
||||
break;
|
||||
|
||||
case PSYC_PARSE_COMPLETE:
|
||||
|
@ -559,11 +559,11 @@ f_psyc_parse (svalue_t *sp) {
|
|||
|
||||
case PSYC_PARSE_INSUFFICIENT:
|
||||
// insufficient data, save remaining bytes
|
||||
state->remaining_len = psyc_getParseRemainingLength(state->parser);
|
||||
state->remaining_len = psyc_parse_remaining_length(state->parser);
|
||||
if (state->remaining_len) {
|
||||
state->remaining = pxalloc(state->remaining_len);
|
||||
memcpy(state->remaining,
|
||||
psyc_getParseRemainingBuffer(state->parser),
|
||||
psyc_parse_remaining_buffer(state->parser),
|
||||
state->remaining_len);
|
||||
} else
|
||||
state->remaining = NULL;
|
||||
|
|
|
@ -28,7 +28,7 @@
|
|||
# define PSYC_PARSE_ERROR_LIST_TOO_LARGE 7
|
||||
|
||||
typedef struct psyc_state_s {
|
||||
psycParseState *parser;
|
||||
PsycParseState *parser;
|
||||
vector_t *packet;
|
||||
// tmp storage for incomplete modifier/body
|
||||
char oper;
|
||||
|
@ -41,9 +41,9 @@ typedef struct psyc_state_s {
|
|||
} psyc_state_t;
|
||||
|
||||
typedef struct psyc_modifier_s {
|
||||
psycHeader *header;
|
||||
PsycHeader *header;
|
||||
p_int num_values;
|
||||
psycModifierFlag flag;
|
||||
PsycModifierFlag flag;
|
||||
} psyc_modifier_t;
|
||||
|
||||
static inline void
|
||||
|
|
Loading…
Reference in a new issue