diff --git a/src/events.c b/src/events.c index df7f9e3..aee4472 100644 --- a/src/events.c +++ b/src/events.c @@ -58,43 +58,66 @@ int (*_operations[OP_AMOUNT])(int, int) = { _op_mod }; +#define MIN(a, b) a < b ? a : b + char _message_buffer[MESSAGE_BUFFER_SZ]; char *events_parse_string(EventHandler *handler, char *message) { - char *message_ptr = _message_buffer; - char in_op = message[0] == '`'; - char *token; + size_t message_pos = 0; + char in_token = 0; + char var_name[TOKEN_MAX_SZ]; + size_t name_pos = 0; + Operation var_op = OP_NULL; + char num[TOKEN_MAX_SZ]; + size_t num_pos = 0; + Token tok_type = T_NULL; + char c; + size_t i, n; int *var; - unsigned int n, i; - size_t len; - char *sign; - size_t left; - size_t size; - if(!strlen(message)) return message; - for(token=strtok(message,"`");token!=NULL;in_op=!in_op, - token=strtok(NULL,"`")){ - len = strlen(token); - if(in_op && len){ - sign = NULL; - for(n=0;sign==NULL&&nvars;i++){ - if(!strncmp(token+1, handler->var_names[i], sign-token-1)){ - var = handler->variables[i]; - break; + for(i=0;ivars;n++){ + if(!strcmp(var_name, handler->var_names[n])){ + var = handler->variables[n]; + if(var_op){ + *var = _operations[var_op](*var, atoi(num)); + } + break; + } } } - if(var){ - *var = _operations[n](*var, atoi(sign+1)); + /* Reset everything */ + tok_type = T_NULL; + name_pos = 0; + var_op = OP_NULL; + num_pos = 0; + } + }else if(!in_token){ + if(message_pos < TOKEN_MAX_SZ) _message_buffer[message_pos++] = c; + } + if(in_token && c != ' '){ + if(tok_type == T_VAR_EDIT){ + if(var_op != OP_NULL){ + if(num_pos < TOKEN_MAX_SZ) num[num_pos++] = c; + } + if(strchr(op_chars, c)){ + var_op = (Operation)(strchr(op_chars, c)-op_chars); + } + if(var_op == OP_NULL){ + if(name_pos < TOKEN_MAX_SZ) var_name[name_pos++] = c; } } - }else{ - left = (_message_buffer+MESSAGE_BUFFER_SZ)-message_ptr; - size = len < left ? len : left; - memcpy(message_ptr, token, size); - message_ptr += size; + if(c == '$'){ + tok_type = T_VAR_EDIT; + } } } - *message_ptr = '\0'; + _message_buffer[MIN(message_pos, MESSAGE_BUFFER_SZ)] = '\0'; return _message_buffer; } diff --git a/src/events.h b/src/events.h index 91f1923..f43fecf 100644 --- a/src/events.h +++ b/src/events.h @@ -2,8 +2,8 @@ #define EVENTS_H #define MAX_VARIABLES 32 -#define MESSAGE_BUFFER_SZ 256 -#define TOKEN_MAX_SZ 32 +#define MESSAGE_BUFFER_SZ 1024 +#define TOKEN_MAX_SZ 1024 typedef struct { int *variables[MAX_VARIABLES];