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

83 lines
2.1 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"
#include "../types.h" // IRC_*
#include <assert.h> // assert()
#include <corelibs/llist.h> // llist_t remove_linked_list_elem()
#include <corelibs/stringext.h> // malloc_string()
#include <stdlib.h> // free()
#include <string.h> // strcpy()
// TODO: Indicate failure of this at any step and free previous if that happens
IRC_User*
uirc_malloc_user(const char* nick, const char* user, const char* real)
{
IRC_User* ret;
if ((ret = malloc(sizeof(IRC_User))) != NULL) {
ret->nick = (nick == NULL) ? NULL : malloc_string(nick, strlen(nick));
ret->user = (user == NULL) ? NULL : malloc_string(user, strlen(user));
ret->real = (real == NULL) ? NULL : malloc_string(real, strlen(real));
return ret;
} else
return NULL;
}
// TODO: uirc_malloc_tag_list uirc_malloc_tag
void
uirc_free_tag(IRC_Tag* t)
{
assert(t != NULL);
free(t->key);
free(t->value);
}
void
uirc_free_user(IRC_User* u)
{
assert(u != NULL);
free(u->user);
free(u->nick);
free(u->real);
}
void
uirc_free_message(IRC_Message* m)
{
assert(m != NULL);
free(m->command);
for (unsigned short i = 0; m->args[i] != NULL; i++) free(m->args[i]);
llist_t* t = m->tag_list;
for (; t != NULL; t = t->next) {
uirc_free_tag(t->content);
remove_linked_list_elem(t);
}
if (m->source != NULL) {
uirc_free_user(m->source);
free(m->source);
}
}