#include "irc_parse.h" /*mvar things*/ //shitty macro #define MVAR_ALLOC_STRC(VNAME,VTYPE,VRET)\ VTYPE *VNAME;\ VNAME=malloc(sizeof(VTYPE));\ if ((VNAME)==NULL){\ VRET\ }\ memset(VNAME,0,sizeof(VTYPE)); #define MVAR_ALLOC_ARR(VNAME,VTYPE,VSZ,VRET)\ VTYPE *VNAME;\ VNAME=malloc(sizeof(VTYPE)*(VSZ));\ if ((VNAME)==NULL){\ VRET\ }\ memset(VNAME,0,sizeof(VTYPE)*(VSZ)); /**/ irc_token* token_create() { MVAR_ALLOC_STRC(ret,irc_token,{ return NULL; }); ret->size = 0; ret->max_size = IRC_TK_MAX_SIZE; //should be enought MVAR_ALLOC_ARR(reg_tk_list,irc_token_el*,ret->max_size,{ free(ret); return NULL; }); ret->tk_list = reg_tk_list; return ret; } //? maybe return number of index? int token_add(irc_token *tk, char *s, char *e) { int ret = -1; //as there irc usually have no more then 512 bytes if ((e-s)>512) { return -1; } MVAR_ALLOC_STRC(token,irc_token_el,{ return -1; }); int sz=e-s; token->type = IRC_TKT_NONE; token->size = sz; token->token = malloc(sz+1); //blow memcpy(token->token,s,token->size); token->token[token->size] = 0x0; //dont support more tokens then original size, for now if (tk->size == IRC_TK_MAX_SIZE) { printf("Cant add token, no space\n"); return -1; } tk->tk_list[tk->size] = token; tk->size += 1; ret = 0; return ret; } int token_cmp(irc_token *tk, int idx, char *str) { int ret = -1; if ((idx>=tk->size) || (idx<0)) { return -1; } if (strncmp(tk->tk_list[idx]->token,str,strlen(tk->tk_list[idx]->token)) != 0) { return 0; } ret = 1; return ret; } char* token_new_str(irc_token *tk, int idx) { char *ret = NULL; if (tk->size < idx ) { return NULL; } if (idx < 0) { return NULL; } ret = alloc_new_str_s(tk->tk_list[idx]->token,tk->tk_list[idx]->size); return ret; } int token_len(irc_token *tk) { int ret = -1; ret = tk->size; return ret; } irc_token* token_cpy_new(irc_token *tk) { int i = 0; irc_token *ret=NULL; if (tk == NULL) { return NULL; } ret = token_create(); if (ret == NULL) { return NULL; } ret->size = tk->size; ret->max_size = tk->max_size; for (i=0;isize;i++) { irc_token_el *te = NULL; te = malloc(sizeof(irc_token_el)); te->type = tk->tk_list[i]->type; te->size = tk->tk_list[i]->size; te->token = alloc_new_str_s(tk->tk_list[i]->token, tk->tk_list[i]->size); ret->tk_list[i] = te; te = NULL; } return ret; } void token_destroy(irc_token *tk) { if (tk != NULL) { int i; for (i=0;isize;i++) { free(tk->tk_list[i]->token); free(tk->tk_list[i]); } free(tk->tk_list); free(tk); } } #define IRC_P_START 1 #define IRC_P_START_WORD 2 #define IRC_P_CONTINUE 3 #define IRC_P_MESSAGE 4 #define IRC_P_END 5 irc_token* irc_parse(char *str, int sz) { irc_token *ret=NULL; int j=-1; int state=IRC_P_START; char *iter = str; char *st=NULL,*en=NULL; int tok_cnt=0; if (str[0] != ':') { return NULL; } ret = token_create(); if (ret == NULL) { printf("Cant allocate token array\n"); return NULL; } for (j=0;j