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 _op_mod
}; };
#define MIN(a, b) a < b ? a : b
char _message_buffer[MESSAGE_BUFFER_SZ]; char _message_buffer[MESSAGE_BUFFER_SZ];
char *events_parse_string(EventHandler *handler, char *message) { char *events_parse_string(EventHandler *handler, char *message) {
char *message_ptr = _message_buffer; size_t message_pos = 0;
char in_op = message[0] == '`'; char in_token = 0;
char *token; 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; int *var;
unsigned int n, i; for(i=0;i<strlen(message);i++){
size_t len; c = message[i];
char *sign; if(c == '`'){
size_t left; in_token = !in_token;
size_t size; if(!in_token){
if(!strlen(message)) return message; if(tok_type == T_VAR_EDIT){
for(token=strtok(message,"`");token!=NULL;in_op=!in_op, /* Do the calculation */
token=strtok(NULL,"`")){ var_name[MIN(name_pos, TOKEN_MAX_SZ)] = '\0';
len = strlen(token); num[MIN(num_pos, TOKEN_MAX_SZ)] = '\0';
if(in_op && len){ for(n=0;n<handler->vars;n++){
sign = NULL; if(!strcmp(var_name, handler->var_names[n])){
for(n=0;sign==NULL&&n<OP_AMOUNT;n++,sign=strchr(token,op_chars[n])); var = handler->variables[n];
if(sign != NULL){ if(var_op){
var = NULL; *var = _operations[var_op](*var, atoi(num));
for(i=0;i<handler->vars;i++){ }
if(!strncmp(token+1, handler->var_names[i], sign-token-1)){ break;
var = handler->variables[i]; }
break;
} }
} }
if(var){ /* Reset everything */
*var = _operations[n](*var, atoi(sign+1)); 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{ if(c == '$'){
left = (_message_buffer+MESSAGE_BUFFER_SZ)-message_ptr; tok_type = T_VAR_EDIT;
size = len < left ? len : left; }
memcpy(message_ptr, token, size);
message_ptr += size;
} }
} }
*message_ptr = '\0'; _message_buffer[MIN(message_pos, MESSAGE_BUFFER_SZ)] = '\0';
return _message_buffer; return _message_buffer;
} }

View file

@ -2,8 +2,8 @@
#define EVENTS_H #define EVENTS_H
#define MAX_VARIABLES 32 #define MAX_VARIABLES 32
#define MESSAGE_BUFFER_SZ 256 #define MESSAGE_BUFFER_SZ 1024
#define TOKEN_MAX_SZ 32 #define TOKEN_MAX_SZ 1024
typedef struct { typedef struct {
int *variables[MAX_VARIABLES]; int *variables[MAX_VARIABLES];