1
0
Fork 0
mirror of git://git.psyc.eu/libpsyc synced 2024-08-15 03:19:02 +00:00

PSYC_STRING, func params

This commit is contained in:
Gabor Adam Toth 2011-12-01 14:12:41 +01:00
parent 5dd68d87fc
commit c4d364a70b
9 changed files with 53 additions and 64 deletions

View file

@ -24,12 +24,14 @@
#define PSYC_VERSION 1 #define PSYC_VERSION 1
#define PSYC_EPOCH 1440444041 // 2015-08-24 21:20:41 CET (Monday) #define PSYC_EPOCH 1440444041 // 2015-08-24 21:20:41 CET (Monday)
#define PSYC_STRING(data, len) (PsycString) {len, data}
#define PSYC_C2STR(str) (PsycString) {sizeof(str)-1, str} #define PSYC_C2STR(str) (PsycString) {sizeof(str)-1, str}
#define PSYC_C2STRI(str) {sizeof(str)-1, str} #define PSYC_C2STRI(str) {sizeof(str)-1, str}
#define PSYC_C2ARG(str) str, sizeof(str)-1 #define PSYC_C2ARG(str) str, sizeof(str)-1
#define PSYC_C2ARG2(str) sizeof(str)-1, str #define PSYC_C2ARG2(str) sizeof(str)-1, str
#define PSYC_S2ARG(str) (str).data, (str).length #define PSYC_S2ARG(str) (str).data, (str).length
#define PSYC_S2ARG2(str) (str).length, (str).data #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))) #define PSYC_NUM_ELEM(a) (sizeof(a) / sizeof(*(a)))

View file

@ -140,7 +140,7 @@ psyc_modifier_length_check (PsycModifier *m)
/** Initialize modifier */ /** Initialize modifier */
static inline void static inline void
psyc_modifier_init (PsycModifier *m, char oper, psyc_modifier_init (PsycModifier *m, PsycOperator oper,
char *name, size_t namelen, char *name, size_t namelen,
char *value, size_t valuelen, PsycModifierFlag flag) char *value, size_t valuelen, PsycModifierFlag flag)
{ {

View file

@ -327,9 +327,9 @@ psyc_parse_state_init (PsycParseState *state, uint8_t flags)
* @see PsycString * @see PsycString
*/ */
static inline void 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; state->cursor = 0;
if (state->flags & PSYC_PARSE_START_AT_CONTENT) { 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. * Sets a new buffer in the list parser state struct with data to be parsed.
*/ */
static inline void 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; 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. * Sets a new buffer in the list parser state struct with data to be parsed.
*/ */
static inline void 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; state->cursor = 0;
} }
@ -526,7 +528,7 @@ psyc_parse_index (const char *value, size_t len, int64_t *n)
* Glyphs are: : = + - ? ! * Glyphs are: : = + - ? !
*/ */
static inline PsycBool static inline PsycBool
psyc_is_glyph (char g) psyc_is_oper (char g)
{ {
switch (g) { switch (g) {
case ':': case ':':

View file

@ -79,6 +79,6 @@ typedef enum {
} PsycEntityType; } PsycEntityType;
int int
psyc_uniform_parse (PsycUniform * uni, char *str, size_t length); psyc_uniform_parse (PsycUniform *uni, const char *buffer, size_t length);
#endif #endif

View file

@ -193,7 +193,7 @@ psyc_parse (PsycParseState *state, char *oper,
// Each line of the header starts with a glyph, // Each line of the header starts with a glyph,
// i.e. :_name, -_name +_name etc, // i.e. :_name, -_name +_name etc,
// so just test if the first char is a glyph. // 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 // it is a glyph, so a variable starts here
ret = psyc_parse_modifier(state, oper, name, value); ret = psyc_parse_modifier(state, oper, name, value);
state->routinglen += state->cursor - pos; 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. // So just test if the first char is a glyph.
// In the body, the same applies, only that the // In the body, the same applies, only that the
// method does not start with a glyph. // 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) { if (state->content_parsed == 0) {
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT); ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
if (state->buffer.data[state->cursor] == '\n') { if (state->buffer.data[state->cursor] == '\n') {

View file

@ -1,3 +1,5 @@
#include <stdio.h>
#include "lib.h" #include "lib.h"
#include <psyc/packet.h> #include <psyc/packet.h>
#include <psyc/render.h> #include <psyc/render.h>

View file

@ -4,21 +4,20 @@
#include "psyc/parse.h" #include "psyc/parse.h"
int int
psyc_uniform_parse (PsycUniform *uni, char *str, size_t length) psyc_uniform_parse (PsycUniform *uni, const char *buffer, size_t length)
{ {
char c; char c;
PsycString *p; PsycString *p;
char *data = (char*)buffer;
size_t pos = 0, part = PSYC_UNIFORM_SCHEME; size_t pos = 0, part = PSYC_UNIFORM_SCHEME;
uni->valid = 0; uni->valid = 0;
uni->full.data = str; uni->full = PSYC_STRING(data, length);
uni->full.length = length;
while (pos < length) { while (pos < length) {
c = str[pos]; c = data[pos];
if (c == ':') { if (c == ':') {
uni->scheme.data = str; uni->scheme = PSYC_STRING(data, pos++);
uni->scheme.length = pos++;
break; break;
} else if (!psyc_is_host_char(c)) } else if (!psyc_is_host_char(c))
return PSYC_PARSE_UNIFORM_INVALID_SCHEME; 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')) { tolower(p->data[3]) == 'c')) {
uni->type = PSYC_SCHEME_PSYC; uni->type = PSYC_SCHEME_PSYC;
part = PSYC_UNIFORM_SLASHES; part = PSYC_UNIFORM_SLASHES;
uni->slashes.data = str + pos; uni->slashes = PSYC_STRING(data + pos, 0);
uni->slashes.length = 0;
while (pos < length) { while (pos < length) {
c = str[pos]; c = data[pos];
switch (part) { switch (part) {
case PSYC_UNIFORM_SLASHES: case PSYC_UNIFORM_SLASHES:
if (c == '/') if (c == '/')
@ -46,8 +44,7 @@ psyc_uniform_parse (PsycUniform *uni, char *str, size_t length)
if (uni->slashes.length == 2) { if (uni->slashes.length == 2) {
part = PSYC_UNIFORM_HOST; part = PSYC_UNIFORM_HOST;
uni->host.data = str + pos + 1; uni->host = PSYC_STRING(data + pos + 1, 0);
uni->host.length = 0;
} }
break; break;
@ -64,16 +61,14 @@ psyc_uniform_parse (PsycUniform *uni, char *str, size_t length)
part = PSYC_UNIFORM_PORT; part = PSYC_UNIFORM_PORT;
p = &uni->port; p = &uni->port;
} else if (c == '/') { } else if (c == '/') {
uni->slash.data = str + pos; uni->slash = PSYC_STRING(data + pos, 1);
uni->slash.length = 1;
part = PSYC_UNIFORM_RESOURCE; part = PSYC_UNIFORM_RESOURCE;
p = &uni->resource; p = &uni->resource;
} else } else
return PSYC_PARSE_UNIFORM_INVALID_HOST; return PSYC_PARSE_UNIFORM_INVALID_HOST;
p->data = str + pos + 1; *p = PSYC_STRING(data + pos + 1, 0);
p->length = 0;
break; break;
case PSYC_UNIFORM_PORT: case PSYC_UNIFORM_PORT:
@ -86,17 +81,14 @@ psyc_uniform_parse (PsycUniform *uni, char *str, size_t length)
return PSYC_PARSE_UNIFORM_INVALID_PORT; return PSYC_PARSE_UNIFORM_INVALID_PORT;
if (c == '/') { if (c == '/') {
uni->slash.data = str + pos; uni->slash = PSYC_STRING(data + pos, 1);
uni->slash.length = 1;
part = PSYC_UNIFORM_RESOURCE; part = PSYC_UNIFORM_RESOURCE;
uni->resource.data = str + pos + 1; uni->resource = PSYC_STRING(data + pos + 1, 0);
uni->resource.length = 0;
break; break;
} else { } else {
part = PSYC_UNIFORM_TRANSPORT; part = PSYC_UNIFORM_TRANSPORT;
uni->transport.data = str + pos; uni->transport = PSYC_STRING(data + pos, 0);
uni->transport.length = 0;
} }
// fall thru // fall thru
@ -113,12 +105,10 @@ psyc_uniform_parse (PsycUniform *uni, char *str, size_t length)
uni->transport.length++; uni->transport.length++;
break; break;
case '/': case '/':
uni->slash.data = str + pos; uni->slash = PSYC_STRING(data + pos, 1);
uni->slash.length = 1;
part = PSYC_UNIFORM_RESOURCE; part = PSYC_UNIFORM_RESOURCE;
uni->resource.data = str + pos + 1; uni->resource = PSYC_STRING(data + pos + 1, 0);
uni->resource.length = 0;
break; break;
default: default:
return PSYC_PARSE_UNIFORM_INVALID_TRANSPORT; return PSYC_PARSE_UNIFORM_INVALID_TRANSPORT;
@ -131,8 +121,7 @@ psyc_uniform_parse (PsycUniform *uni, char *str, size_t length)
break; break;
} else if (c == '#') { } else if (c == '#') {
part = PSYC_UNIFORM_CHANNEL; part = PSYC_UNIFORM_CHANNEL;
uni->channel.data = str + pos + 1; uni->channel = PSYC_STRING(data + pos + 1, 0);
uni->channel.length = 0;
break; break;
} else } else
return PSYC_PARSE_UNIFORM_INVALID_RESOURCE; 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) if (uni->host.length == 0)
return PSYC_PARSE_UNIFORM_INVALID_HOST; return PSYC_PARSE_UNIFORM_INVALID_HOST;
uni->host_port.data = uni->host.data; uni->host_port = PSYC_STRING(uni->host.data, uni->host.length
uni->host_port.length = uni->host.length + uni->port.length + uni->port.length + uni->transport.length);
+ uni->transport.length;
if (uni->port.length > 0 || uni->transport.length > 0) if (uni->port.length > 0 || uni->transport.length > 0)
uni->host_port.length++; uni->host_port.length++;
uni->root.data = str; uni->root = PSYC_STRING(data, uni->scheme.length + 1
uni->root.length = uni->scheme.length + 1 + uni->slashes.length + uni->host_port.length);
+ uni->slashes.length + uni->host_port.length;
uni->entity.data = str; uni->entity = PSYC_STRING(data, uni->root.length + uni->slash.length
uni->entity.length = uni->root.length + uni->slash.length + uni->resource.length);
+ uni->resource.length;
uni->body.data = uni->host.data; uni->body = PSYC_STRING(uni->host.data,
uni->body.length = length - uni->scheme.length - 1 - uni->slashes.length; length - uni->scheme.length - 1 - uni->slashes.length);
if (uni->resource.length) { if (uni->resource.length)
uni->nick.data = uni->resource.data + 1; uni->nick = PSYC_STRING(uni->resource.data + 1, uni->resource.length);
uni->nick.length = uni->resource.length;
}
} else } else
return PSYC_PARSE_UNIFORM_INVALID_SCHEME; return PSYC_PARSE_UNIFORM_INVALID_SCHEME;

View file

@ -8,9 +8,9 @@
/* example renderer generating a presence packet */ /* example renderer generating a presence packet */
int int
testPresence (const char *avail, int availlen, testPresence (char *avail, int availlen,
const char *desc, int desclen, char *desc, int desclen,
const char *rendered, uint8_t verbose) char *rendered, uint8_t verbose)
{ {
PsycModifier routing[1]; PsycModifier routing[1];
psyc_modifier_init(&routing[0], PSYC_OPERATOR_SET, psyc_modifier_init(&routing[0], PSYC_OPERATOR_SET,

View file

@ -100,7 +100,7 @@ main (int argc, char **argv)
} }
size_t tlen = 0; 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("_notice_context_enter = %s, %ld\n", t, tlen);
printf("psyc_text passed all tests.\n"); printf("psyc_text passed all tests.\n");