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

104 lines
2.3 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 "../types.h" // IRC_Tag
#include "assemblers.h" // uirc_assembler_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
return -1;
}
if (len < 1) return -1;
if ((res = snprintf(buf, len, "%s", t->key)) >= 0) {
buf += (size_t) res;
len -= (size_t) res;
} else
return -1;
if (t->value != NULL) {
if (len < 1) return -1;
if ((res = snprintf(buf, len, "=%s", t->value)) >= 0) {
buf += (size_t) res;
len -= (size_t) res;
} else
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->content != NULL && ((IRC_Tag*) tl->content)->key != NULL;) {
if (buf == sv) {
if (len > 1) {
*(buf++) = '@';
len--;
} else
return -1;
} else {
if (len > 1) {
*(buf++) = ';';
len--;
} else
return -1;
}
ssize_t ret;
if ((ret = uirc_assembler_tag(buf, tl->content, len)) >= 0) {
buf += (size_t) ret;
len -= (size_t) ret;
} else
return -1;
if ((tl = tl->next) == NULL) {
if (len > 1) {
*(buf++) = ' ';
len--;
} else
return -1;
break;
}
}
return buf - sv;
}