cake
/
libg1m
Archived
1
0
Fork 0
This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
libg1m/src/core/free.c

176 lines
3.9 KiB
C

/* *****************************************************************************
* core/free.c -- free elements of a handle.
* Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
*
* This file is part of libg1m.
* libg1m is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3.0 of the License,
* or (at your option) any later version.
*
* libg1m 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libg1m; if not, see <http://www.gnu.org/licenses/>.
* ************************************************************************** */
#include <libg1m/internals.h>
#include <stdlib.h>
/**
* g1m_free_line_content:
* Free an e-activity line content.
*
* @arg handle the line handle.
*/
void g1m_free_line_content(g1m_line_t *line)
{
if (line->type & (g1m_linetype_title | g1m_linetype_text
| g1m_linetype_picture))
free(line->content);
if (line->type & g1m_linetype_eact) {
for (int i = 0; i < line->count; i++) {
g1m_free_line_content(line->lines[i]);
free(line->lines[i]);
}
free(line->lines);
}
}
/**
* g1m_free_mcsfile_content:
* Free an MCS file handle content.
*
* @arg handle the handle which has the content to free.
*/
void g1m_free_mcsfile_content(g1m_mcsfile_t *handle)
{
unsigned int type = handle->head.type;
/* free content for unknown files */
if (!type)
free(handle->content);
/* free cells */
if (type & (g1m_mcstype_mat | g1m_mcstype_vct | g1m_mcstype_list
| g1m_mcstype_spreadsheet)
&& handle->head.width && handle->head.height) {
free(handle->cells[0]);
free(handle->cells);
}
/* free the images */
if (type & g1m_mcstype_pict) {
for (int i = 0; i < handle->head.count; i++)
free(handle->pics[i]);
if (handle->pics != &handle->pic)
free(handle->pics);
}
/* free the vars */
if ((type & g1m_mcstype_alphamem)
&& handle->vars != &handle->var)
free(handle->vars);
/* free the content */
if (type & g1m_mcstype_program)
free(handle->content);
}
/**
* g1m_free_mcsfile:
* Free an MCS file handle.
*
* @arg handle the handle to free.
*/
void g1m_free_mcsfile(g1m_mcsfile_t *handle)
{
g1m_free_mcsfile_content(handle);
free(handle);
}
/**
* g1m_free_mcs:
* Free all of the MCS.
*
* @arg handle the handle to close.
*/
void g1m_free_mcs(g1m_t *handle)
{
/* check if mcs */
if (!handle->files)
return ;
/* foreach file in mcs */
g1m_mcsfile_t **files = handle->files;
int file_count = handle->count;
for (int i = 0; i < file_count; i++) {
/* free the file if exists */
if (files[i]) g1m_free_mcsfile(files[i]);
}
free(handle->files); handle->files = NULL;
}
/**
* g1m_free_content:
* Free handle data.
*
* @arg handle the handle to close.
*/
void g1m_free_content(g1m_t *handle)
{
/* addin time! */
if (handle->type & g1m_type_addin)
free(handle->pixels);
/* mcs time! */
if (handle->type & g1m_type_mcs)
g1m_free_mcs(handle);
/* messages time! */
if (handle->type & g1m_type_lang
&& handle->messages) {
for (int i = 0; i < handle->count; i++)
free(handle->messages[i]);
free(handle->messages);
}
/* function keys time! */
if (handle->type & g1m_type_fkey
&& handle->fkeys) {
for (int i = 0; i < handle->count; i++)
free(handle->fkeys[i]);
free(handle->fkeys);
}
/* picture time! */
if (handle->type & g1m_type_picture)
free(handle->pixels);
/* e-activities time! */
if (handle->type & g1m_type_eact)
g1m_free_line_content(handle->line);
}
/**
* g1m_free:
* Free a handle and its data.
*
* @arg handle the handle.
*/
void g1m_free(g1m_t *handle)
{
if (!handle) return ;
g1m_free_content(handle);
free(handle);
}