/* * This file is part of uIRC. (https://git.redxen.eu/caskd/uIRC) * Copyright (c) 2019-2021 Alex-David Denes * * uIRC is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * any later version. * * uIRC is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with uIRC. If not, see . */ #include "error.h" // uirc_errno #include "memory.h" // Free_IRC_Tag() #include "tokenizer.h" // uirc_tokenizer_tags() #include "type.h" // IRC_Tag #include // assert() #include // llist_elem_alloc() llist_elem_conn() llist_elem_rm() #include // stringext_strmalloc() #include // true #include // free() #include // strchr() llist_t* uirc_tokenizer_tag_list(const char* str) { assert(str != NULL); llist_t* l = NULL; char* ws = NULL; if ((ws = stringext_strmalloc(str, strlen(str))) == NULL) { uirc_errno = UIRC_ERR_SYSERR; goto cleanup; } char * p = ws, *tmp; llist_t* pl = NULL; while ((tmp = stringext_strtok_mr(&p, ";")) != NULL) { llist_t* cl; if ((cl = llist_elem_alloc(0)) == NULL) { uirc_errno = UIRC_ERR_SYSERR; goto cleanup; } if ((cl->content = uirc_tokenizer_tag(tmp)) == NULL) goto cleanup; if (l == NULL) l = cl; else llist_elem_conn(pl, cl); pl = cl; } free(ws); return l; cleanup: if (l != NULL) uirc_list_free(l, IRC_STRUCT_TAG); return NULL; } IRC_Tag* uirc_tokenizer_tag(const char* str) { assert(str != NULL); char* ws = NULL; IRC_Tag* t = NULL; if ((ws = stringext_strmalloc(str, strlen(str))) == NULL) { uirc_errno = UIRC_ERR_SYSERR; goto cleanup; } if ((t = malloc(sizeof(*t))) == NULL) { uirc_errno = UIRC_ERR_SYSERR; goto cleanup; } memset(t, 0, sizeof(*t)); char* ckey = ws; if (*ws == '+') { ckey++; t->clientbound = true; } char* cval = strchr(ckey, '='); if (cval != NULL) { *(cval++) = '\0'; if ((t->value = stringext_strmalloc(cval, strlen(cval))) == NULL) { uirc_errno = UIRC_ERR_SYSERR; goto cleanup; } } if ((t->key = stringext_strmalloc(ckey, strlen(ckey))) == NULL) { uirc_errno = UIRC_ERR_SYSERR; goto cleanup; } free(ws); return t; cleanup: if (t != NULL) uirc_struct_free(t, IRC_STRUCT_TAG); free(t); free(ws); return NULL; }