diff --git a/include/psyc.h b/include/psyc.h index 5c3076d..2ba0d56 100644 --- a/include/psyc.h +++ b/include/psyc.h @@ -24,12 +24,14 @@ #define PSYC_VERSION 1 #define PSYC_EPOCH 1440444041 // 2015-08-24 21:20:41 CET (Monday) -#define PSYC_C2STR(str) (PsycString) {sizeof(str)-1, str} -#define PSYC_C2STRI(str) {sizeof(str)-1, str} -#define PSYC_C2ARG(str) str, sizeof(str)-1 -#define PSYC_C2ARG2(str) sizeof(str)-1, str -#define PSYC_S2ARG(str) (str).data, (str).length -#define PSYC_S2ARG2(str) (str).length, (str).data +#define PSYC_STRING(data, len) (PsycString) {len, data} +#define PSYC_C2STR(str) (PsycString) {sizeof(str)-1, str} +#define PSYC_C2STRI(str) {sizeof(str)-1, str} +#define PSYC_C2ARG(str) str, sizeof(str)-1 +#define PSYC_C2ARG2(str) sizeof(str)-1, str +#define PSYC_S2ARG(str) (str).data, (str).length +#define PSYC_S2ARG2(str) (str).length, (str).data +#define PSYC_S2ARGP(str) (int)(str).length, (str).data #define PSYC_NUM_ELEM(a) (sizeof(a) / sizeof(*(a))) diff --git a/include/psyc/packet.h b/include/psyc/packet.h index 3e5a979..7eecd0c 100644 --- a/include/psyc/packet.h +++ b/include/psyc/packet.h @@ -140,7 +140,7 @@ psyc_modifier_length_check (PsycModifier *m) /** Initialize modifier */ static inline void -psyc_modifier_init (PsycModifier *m, char oper, +psyc_modifier_init (PsycModifier *m, PsycOperator oper, char *name, size_t namelen, char *value, size_t valuelen, PsycModifierFlag flag) { diff --git a/include/psyc/parse.h b/include/psyc/parse.h index afa6757..36b0991 100644 --- a/include/psyc/parse.h +++ b/include/psyc/parse.h @@ -327,9 +327,9 @@ psyc_parse_state_init (PsycParseState *state, uint8_t flags) * @see PsycString */ static inline void -psyc_parse_buffer_set (PsycParseState *state, char *buffer, size_t length) +psyc_parse_buffer_set (PsycParseState *state, const char *buffer, size_t length) { - state->buffer = (PsycString) {length, buffer}; + state->buffer = (PsycString) {length, (char*)buffer}; state->cursor = 0; if (state->flags & PSYC_PARSE_START_AT_CONTENT) { @@ -351,9 +351,10 @@ psyc_parse_list_state_init (PsycParseListState *state) * Sets a new buffer in the list parser state struct with data to be parsed. */ static inline void -psyc_parse_list_buffer_set (PsycParseListState *state, char *buffer, size_t length) +psyc_parse_list_buffer_set (PsycParseListState *state, + const char *buffer, size_t length) { - state->buffer = (PsycString) {length, buffer}; + state->buffer = (PsycString) {length, (char*)buffer}; state->cursor = 0; } @@ -377,9 +378,10 @@ psyc_parse_table_state_init (PsycParseTableState *state) * Sets a new buffer in the list parser state struct with data to be parsed. */ static inline void -psyc_parse_table_buffer_set (PsycParseTableState *state, char *buffer, size_t length) +psyc_parse_table_buffer_set (PsycParseTableState *state, + const char *buffer, size_t length) { - state->buffer = (PsycString) {length, buffer}; + state->buffer = (PsycString) {length, (char*)buffer}; state->cursor = 0; } @@ -526,7 +528,7 @@ psyc_parse_index (const char *value, size_t len, int64_t *n) * Glyphs are: : = + - ? ! */ static inline PsycBool -psyc_is_glyph (char g) +psyc_is_oper (char g) { switch (g) { case ':': diff --git a/include/psyc/uniform.h b/include/psyc/uniform.h index e69599c..0200519 100644 --- a/include/psyc/uniform.h +++ b/include/psyc/uniform.h @@ -79,6 +79,6 @@ typedef enum { } PsycEntityType; int -psyc_uniform_parse (PsycUniform * uni, char *str, size_t length); +psyc_uniform_parse (PsycUniform *uni, const char *buffer, size_t length); #endif diff --git a/src/parse.c b/src/parse.c index f4701fe..f621491 100644 --- a/src/parse.c +++ b/src/parse.c @@ -193,7 +193,7 @@ psyc_parse (PsycParseState *state, char *oper, // Each line of the header starts with a glyph, // i.e. :_name, -_name +_name etc, // so just test if the first char is a glyph. - if (psyc_is_glyph(state->buffer.data[state->cursor])) { + if (psyc_is_oper(state->buffer.data[state->cursor])) { // it is a glyph, so a variable starts here ret = psyc_parse_modifier(state, oper, name, value); state->routinglen += state->cursor - pos; @@ -266,7 +266,7 @@ psyc_parse (PsycParseState *state, char *oper, // So just test if the first char is a glyph. // In the body, the same applies, only that the // method does not start with a glyph. - if (psyc_is_glyph(state->buffer.data[state->cursor])) { + if (psyc_is_oper(state->buffer.data[state->cursor])) { if (state->content_parsed == 0) { ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT); if (state->buffer.data[state->cursor] == '\n') { diff --git a/src/render.c b/src/render.c index 9ba51ff..0db289d 100644 --- a/src/render.c +++ b/src/render.c @@ -1,3 +1,5 @@ +#include + #include "lib.h" #include #include diff --git a/src/uniform.c b/src/uniform.c index 5a68ca3..e7a9fbf 100644 --- a/src/uniform.c +++ b/src/uniform.c @@ -4,21 +4,20 @@ #include "psyc/parse.h" int -psyc_uniform_parse (PsycUniform *uni, char *str, size_t length) +psyc_uniform_parse (PsycUniform *uni, const char *buffer, size_t length) { char c; PsycString *p; + char *data = (char*)buffer; size_t pos = 0, part = PSYC_UNIFORM_SCHEME; uni->valid = 0; - uni->full.data = str; - uni->full.length = length; + uni->full = PSYC_STRING(data, length); while (pos < length) { - c = str[pos]; + c = data[pos]; if (c == ':') { - uni->scheme.data = str; - uni->scheme.length = pos++; + uni->scheme = PSYC_STRING(data, pos++); break; } else if (!psyc_is_host_char(c)) return PSYC_PARSE_UNIFORM_INVALID_SCHEME; @@ -32,11 +31,10 @@ psyc_uniform_parse (PsycUniform *uni, char *str, size_t length) tolower(p->data[3]) == 'c')) { uni->type = PSYC_SCHEME_PSYC; part = PSYC_UNIFORM_SLASHES; - uni->slashes.data = str + pos; - uni->slashes.length = 0; + uni->slashes = PSYC_STRING(data + pos, 0); while (pos < length) { - c = str[pos]; + c = data[pos]; switch (part) { case PSYC_UNIFORM_SLASHES: if (c == '/') @@ -46,8 +44,7 @@ psyc_uniform_parse (PsycUniform *uni, char *str, size_t length) if (uni->slashes.length == 2) { part = PSYC_UNIFORM_HOST; - uni->host.data = str + pos + 1; - uni->host.length = 0; + uni->host = PSYC_STRING(data + pos + 1, 0); } break; @@ -64,16 +61,14 @@ psyc_uniform_parse (PsycUniform *uni, char *str, size_t length) part = PSYC_UNIFORM_PORT; p = &uni->port; } else if (c == '/') { - uni->slash.data = str + pos; - uni->slash.length = 1; + uni->slash = PSYC_STRING(data + pos, 1); part = PSYC_UNIFORM_RESOURCE; p = &uni->resource; } else return PSYC_PARSE_UNIFORM_INVALID_HOST; - p->data = str + pos + 1; - p->length = 0; + *p = PSYC_STRING(data + pos + 1, 0); break; case PSYC_UNIFORM_PORT: @@ -86,17 +81,14 @@ psyc_uniform_parse (PsycUniform *uni, char *str, size_t length) return PSYC_PARSE_UNIFORM_INVALID_PORT; if (c == '/') { - uni->slash.data = str + pos; - uni->slash.length = 1; + uni->slash = PSYC_STRING(data + pos, 1); part = PSYC_UNIFORM_RESOURCE; - uni->resource.data = str + pos + 1; - uni->resource.length = 0; + uni->resource = PSYC_STRING(data + pos + 1, 0); break; } else { part = PSYC_UNIFORM_TRANSPORT; - uni->transport.data = str + pos; - uni->transport.length = 0; + uni->transport = PSYC_STRING(data + pos, 0); } // fall thru @@ -113,12 +105,10 @@ psyc_uniform_parse (PsycUniform *uni, char *str, size_t length) uni->transport.length++; break; case '/': - uni->slash.data = str + pos; - uni->slash.length = 1; + uni->slash = PSYC_STRING(data + pos, 1); part = PSYC_UNIFORM_RESOURCE; - uni->resource.data = str + pos + 1; - uni->resource.length = 0; + uni->resource = PSYC_STRING(data + pos + 1, 0); break; default: return PSYC_PARSE_UNIFORM_INVALID_TRANSPORT; @@ -131,8 +121,7 @@ psyc_uniform_parse (PsycUniform *uni, char *str, size_t length) break; } else if (c == '#') { part = PSYC_UNIFORM_CHANNEL; - uni->channel.data = str + pos + 1; - uni->channel.length = 0; + uni->channel = PSYC_STRING(data + pos + 1, 0); break; } else return PSYC_PARSE_UNIFORM_INVALID_RESOURCE; @@ -150,28 +139,22 @@ psyc_uniform_parse (PsycUniform *uni, char *str, size_t length) if (uni->host.length == 0) return PSYC_PARSE_UNIFORM_INVALID_HOST; - uni->host_port.data = uni->host.data; - uni->host_port.length = uni->host.length + uni->port.length - + uni->transport.length; - + uni->host_port = PSYC_STRING(uni->host.data, uni->host.length + + uni->port.length + uni->transport.length); if (uni->port.length > 0 || uni->transport.length > 0) uni->host_port.length++; - uni->root.data = str; - uni->root.length = uni->scheme.length + 1 - + uni->slashes.length + uni->host_port.length; + uni->root = PSYC_STRING(data, uni->scheme.length + 1 + + uni->slashes.length + uni->host_port.length); - uni->entity.data = str; - uni->entity.length = uni->root.length + uni->slash.length - + uni->resource.length; + uni->entity = PSYC_STRING(data, uni->root.length + uni->slash.length + + uni->resource.length); - uni->body.data = uni->host.data; - uni->body.length = length - uni->scheme.length - 1 - uni->slashes.length; + uni->body = PSYC_STRING(uni->host.data, + length - uni->scheme.length - 1 - uni->slashes.length); - if (uni->resource.length) { - uni->nick.data = uni->resource.data + 1; - uni->nick.length = uni->resource.length; - } + if (uni->resource.length) + uni->nick = PSYC_STRING(uni->resource.data + 1, uni->resource.length); } else return PSYC_PARSE_UNIFORM_INVALID_SCHEME; diff --git a/test/test_render.c b/test/test_render.c index 506b384..be57804 100644 --- a/test/test_render.c +++ b/test/test_render.c @@ -8,9 +8,9 @@ /* example renderer generating a presence packet */ int -testPresence (const char *avail, int availlen, - const char *desc, int desclen, - const char *rendered, uint8_t verbose) +testPresence (char *avail, int availlen, + char *desc, int desclen, + char *rendered, uint8_t verbose) { PsycModifier routing[1]; psyc_modifier_init(&routing[0], PSYC_OPERATOR_SET, diff --git a/test/test_text.c b/test/test_text.c index 3d69ea4..977a66e 100644 --- a/test/test_text.c +++ b/test/test_text.c @@ -100,7 +100,7 @@ main (int argc, char **argv) } size_t tlen = 0; - char *t = psyc_template(PSYC_MC_NOTICE_CONTEXT_ENTER, &tlen); + const char *t = psyc_template(PSYC_MC_NOTICE_CONTEXT_ENTER, &tlen); printf("_notice_context_enter = %s, %ld\n", t, tlen); printf("psyc_text passed all tests.\n");