Fully working dialogs

This commit is contained in:
mibi88 2024-07-20 17:44:44 +02:00
parent 7a34fefad5
commit 15570a8d91
2 changed files with 54 additions and 31 deletions

View file

@ -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&&n<OP_AMOUNT;n++,sign=strchr(token,op_chars[n]));
if(sign != NULL){
var = NULL;
for(i=0;i<handler->vars;i++){
if(!strncmp(token+1, handler->var_names[i], sign-token-1)){
var = handler->variables[i];
break;
for(i=0;i<strlen(message);i++){
c = message[i];
if(c == '`'){
in_token = !in_token;
if(!in_token){
if(tok_type == T_VAR_EDIT){
/* Do the calculation */
var_name[MIN(name_pos, TOKEN_MAX_SZ)] = '\0';
num[MIN(num_pos, TOKEN_MAX_SZ)] = '\0';
for(n=0;n<handler->vars;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;
}

View file

@ -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];