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

112 lines
2.7 KiB
C
Raw Normal View History

/*
* This file is part of uIRC. (https://git.redxen.eu/caskd/uIRC)
2021-03-11 22:52:30 +00:00
* 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 "error.h" // uirc_errno
#include "memory.h" // Free_IRC_Tag()
#include "tokenizer.h" // uirc_tokenizer_tags()
#include "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);
2021-03-30 13:24:16 +00:00
llist_t* l = NULL;
char* ws = NULL;
if ((ws = stringext_strmalloc(str, strlen(str))) == NULL) {
uirc_errno = UIRC_ERR_SYSERR;
goto cleanup;
}
2021-03-30 13:24:16 +00:00
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) {
2021-03-30 13:24:16 +00:00
uirc_errno = UIRC_ERR_SYSERR;
goto cleanup;
}
2021-03-30 13:24:16 +00:00
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;
2021-03-30 13:24:16 +00:00
cleanup:
if (l != NULL) uirc_list_free(l, IRC_STRUCT_TAG);
return NULL;
}
IRC_Tag*
uirc_tokenizer_tag(const char* str)
{
assert(str != NULL);
2021-03-30 13:24:16 +00:00
char* ws = NULL;
IRC_Tag* t = NULL;
2021-03-30 13:24:16 +00:00
if ((ws = stringext_strmalloc(str, strlen(str))) == NULL) {
uirc_errno = UIRC_ERR_SYSERR;
goto cleanup;
}
2021-03-30 13:24:16 +00:00
if ((t = malloc(sizeof(*t))) == NULL) {
uirc_errno = UIRC_ERR_SYSERR;
goto cleanup;
}
memset(t, 0, sizeof(*t));
2021-03-30 13:24:16 +00:00
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) {
2021-03-30 13:24:16 +00:00
uirc_errno = UIRC_ERR_SYSERR;
goto cleanup;
}
}
if ((t->key = stringext_strmalloc(ckey, strlen(ckey))) == NULL) {
2021-03-30 13:24:16 +00:00
uirc_errno = UIRC_ERR_SYSERR;
goto cleanup;
}
free(ws);
return t;
2021-03-30 13:24:16 +00:00
cleanup:
if (t != NULL) uirc_struct_free(t, IRC_STRUCT_TAG);
free(t);
2021-03-30 13:24:16 +00:00
free(ws);
return NULL;
}