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

98 lines
2.3 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 "assembler.h" // uirc_assembler_tag_*
#include "error.h" // uirc_errno
#include "type.h" // IRC_Tag
#include <assert.h> // assert()
#include <corelibs/llist.h> // llist_t
#include <stdio.h> // NULL
#include <sys/types.h> // size_t ssize_t
ssize_t
uirc_assembler_tag(char* buf, const IRC_Tag* t, size_t len)
{
assert(t != NULL);
assert(buf != NULL);
assert(t->key != NULL);
const char* const sv = buf;
int res;
if (t->clientbound) {
if (len > 1) {
*(buf++) = '+';
len--;
} else {
uirc_errno = UIRC_ERR_BUFSIZ;
return -1;
}
}
if ((res = snprintf(buf, len, "%s", t->key)) >= 0) {
buf += (size_t) res;
len -= (size_t) res;
} else {
uirc_errno = UIRC_ERR_BUFSIZ;
return -1;
}
if (t->value != NULL) {
if ((res = snprintf(buf, len, "=%s", t->value)) >= 0) {
buf += (size_t) res;
len -= (size_t) res;
} else {
uirc_errno = UIRC_ERR_BUFSIZ;
return -1;
}
}
return buf - sv;
}
ssize_t
uirc_assembler_tag_list(char* buf, const llist_t* tl, size_t len)
{
assert(tl != NULL);
assert(buf != NULL);
const char* const sv = buf;
for (; tl != NULL && tl->content != NULL && ((IRC_Tag*) tl->content)->key != NULL; tl = tl->next) {
if (buf != sv) {
if (len > 1) {
*(buf++) = ';';
len--;
} else {
uirc_errno = UIRC_ERR_BUFSIZ;
return -1;
}
}
ssize_t ret;
if ((ret = uirc_assembler_tag(buf, tl->content, len)) >= 0) {
buf += (size_t) ret;
len -= (size_t) ret;
} else {
uirc_errno = UIRC_ERR_BUFSIZ;
return -1;
}
}
return buf - sv;
}