This repository has been archived on 2021-04-17. You can view files and clone it, but cannot push or open issues or pull requests.
uIRC/src/tokenizer/tag.c

110 lines
2.8 KiB
C

/*
* 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 <https://www.gnu.org/licenses/>.
*/
#include "uirc/error.h" // uirc_errno
#include "uirc/memory.h" // Free_IRC_Tag()
#include "uirc/tokenizer.h" // uirc_tokenizer_tags()
#include "uirc/type.h" // IRC_Tag
#include <assert.h> // assert()
#include <corelibs/llist.h> // llist_elem_alloc() llist_elem_conn() llist_elem_rm()
#include <corelibs/stringext.h> // stringext_strmalloc()
#include <stdbool.h> // true
#include <stdlib.h> // free()
#include <string.h> // 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) {
IRC_Tag* t;
if ((t = uirc_tokenizer_tag(tmp)) == NULL) goto cleanup;
if (l == NULL) {
l = pl = uirc_list_append(NULL, t);
} else {
// Append tag to previous list element and then set previous list element to current to use it in next loop
pl = uirc_list_append(pl, t);
}
}
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;
}