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/tokenizers/tag.c

115 lines
2.7 KiB
C

/*
* This file is part of uIRC. (https://git.redxen.eu/caskd/uIRC)
* Copyright (c) 2019, 2020 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 "memory.h" // Free_IRC_Tag()
#include "tokenizers.h" // uirc_tokenizer_tags()
#include "types.h" // IRC_Tag
#include <assert.h> // assert()
#include <corelibs/llist.h> // allocate_linked_list_elem() connect_linked_list_elem() remove_linked_list_elem()
#include <corelibs/stringext.h> // malloc_string()
#include <stdbool.h> // true
#include <stdlib.h> // free()
#include <string.h> // strchr()
static void free_ctx(llist_t* list, IRC_Tag* t, char* s);
llist_t*
uirc_tokenizer_tag_list(const char* str)
{
assert(str != NULL);
char* const ws = malloc_string(str, strlen(str));
char * p = ws, *tmp;
llist_t * pl = NULL, *l = NULL;
if (ws == NULL) return NULL;
while ((tmp = strtok_mr(&p, ";")) != NULL) {
llist_t* cl;
if ((cl = allocate_linked_list_elem(0)) == NULL) {
free_ctx(l, NULL, ws);
return NULL;
}
if ((cl->content = uirc_tokenizer_tag(tmp)) == NULL) {
free_ctx(l, NULL, ws);
return NULL;
}
if (l == NULL) l = cl;
else
connect_linked_list_elem(pl, cl);
pl = cl;
}
free(ws);
return l;
}
IRC_Tag*
uirc_tokenizer_tag(const char* str)
{
assert(str != NULL);
char* const ws = malloc_string(str, strlen(str));
IRC_Tag* t = malloc(sizeof(IRC_Tag));
char* ckey = ws;
if (t == NULL || ws == NULL) {
free(t);
free(ws);
return NULL;
}
memset(t, 0, sizeof(IRC_Tag));
if (*ws == '+') {
ckey++;
t->clientbound = true;
}
char* cval = strchr(ckey, '=');
if (cval != NULL) {
*(cval++) = '\0';
if ((t->value = malloc_string(cval, strlen(cval))) == NULL) {
free_ctx(NULL, t, ws);
return NULL;
}
}
if ((t->key = malloc_string(ckey, strlen(ckey))) == NULL) {
free_ctx(NULL, t, ws);
return NULL;
}
free(ws);
return t;
}
static void
free_ctx(llist_t* list, IRC_Tag* t, char* s)
{
for (llist_t* c = list; c != NULL;) {
llist_t* l = c;
c = c->next;
if (l->content != NULL) uirc_free_tag(l->content);
remove_linked_list_elem(l);
}
uirc_free_tag(t);
free(t);
free(s);
}