/* ***************************************************************************** * manage/handle.c -- create, free a handle. * Copyright (C) 2017 Thomas "Cakeisalie5" Touhey * * 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 . * ************************************************************************** */ #include /* ************************************************************************** */ /* Make a handle */ /* ************************************************************************** */ /** * g1m_make_picture: * Make a picture handle. * * @arg handle the handle to make. * @arg width the picture width. * @arg height the picture height. * @return the error code (0 if ok). */ int g1m_make_picture(g1m_t **h, unsigned int width, unsigned int height) { /* allocate the handle */ *h = malloc(sizeof(g1m_t)); if (!*h) return (g1m_error_alloc); g1m_t *handle = *h; /* allocate the pixels */ handle->width = width; handle->height = height; handle->pixels = alloc_pixels(width, height); if (!handle->pixels) { free(*h); *h = NULL; return (g1m_error_alloc); } prepare_pixels(handle->pixels, width, height) /* everything went well! */ return (0); } /** * g1m_make_mcs: * Make an MCS file. * * @arg h pointer to the handle to create. * @arg count the number of slots in the index. * @return the error code (0 if ok). */ int g1m_make_mcs(g1m_t **h, int count) { /* allocate the handle */ *h = malloc(sizeof(g1m_t)); if (!*h) return (g1m_error_alloc); /* initialize it */ g1m_t *handle = *h; handle->type = g1m_type_mcs; handle->platform = g1m_platform_fx; /* allocate space */ handle->count = count; handle->_size = count; handle->files = NULL; if (count) { handle->files = malloc(sizeof(g1m_mcsfile_t*) * count); if (!handle->files) { free(*h); *h = NULL; return (g1m_error_alloc); } memset(handle->files, 0, sizeof(g1m_mcsfile_t*) * count); } /* no error */ return (0); } /* ************************************************************************** */ /* Free a 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); }