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/tags.c

88 lines
2.4 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/memory.h" // Free_IRC_Tag()
#include "../types.h" // IRC_Tag
#include "tokenizers.h" // uirc_tokenizer_tags()
#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, char* s);
// TODO: Only tokenizer IRC_Tag and put the looping logic in uirc_tokenizer_message()
llist_t*
uirc_tokenizer_tags(const char* s)
{
assert(s != NULL);
char* const ws = malloc_string(s, strlen(s));
char * p = ws, *tmp;
llist_t * pl = NULL, *l = NULL;
while ((tmp = strtok_mr(&p, ";")) != NULL) {
llist_t* cl;
if ((cl = allocate_linked_list_elem(sizeof(IRC_Tag))) == NULL) {
free_ctx(l, ws);
return NULL;
}
IRC_Tag* tag = (IRC_Tag*) cl->content;
char* ckey = tmp;
if (*tmp == '+') {
ckey++;
tag->clientbound = true;
}
char* cval = strchr(ckey, '=');
if (cval != NULL) {
*(cval++) = '\0';
if ((tag->value = malloc_string(cval, strlen(cval))) == NULL) {
free_ctx(l, ws);
return NULL;
}
}
if ((tag->key = malloc_string(ckey, strlen(ckey))) == NULL) {
free_ctx(l, ws);
return NULL;
}
if (l == NULL) l = cl;
else
connect_linked_list_elem(pl, cl);
pl = cl;
}
return l;
}
static void
free_ctx(llist_t* list, char* s)
{
for (llist_t* c = list; c != NULL;) {
llist_t* l = c;
c = c->next;
uirc_free_tag(l->content);
remove_linked_list_elem(l);
}
free(s);
}