libmpm

morphux C package management library
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/libmpm

commit 8bdcb8855ffd9a4a5170d9f0884f02be473150d6
parent 0827c555d6be539bc0c92be896ca5909e25299cd
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Mon, 30 Jan 2017 19:03:04 +0100

Add(Package & Files): Init and free functions

Diffstat:
Minc/category.h | 3++-
Minc/files.h | 4++++
Msrc/category.c | 12+++++++++++-
Asrc/files.c | 44++++++++++++++++++++++++++++++++++++++++++++
Msrc/package.c | 2+-
Mtests/test_database.c | 2++
6 files changed, 64 insertions(+), 3 deletions(-)

diff --git a/inc/category.h b/inc/category.h @@ -23,6 +23,7 @@ typedef struct s_category { char *name; } category_t; -int mpm_free_category(void *tmp); +int mpm_category_free(void *tmp); +void mpm_category_init(category_t *ptr); #endif /* CATEGORY_H */ diff --git a/inc/files.h b/inc/files.h @@ -36,4 +36,8 @@ typedef struct s_file { char *hash; } file_t; + +void mpm_file_init(file_t *ptr); +int mpm_file_free(void *tmp); + #endif /* FILES_H */ diff --git a/src/category.c b/src/category.c @@ -20,7 +20,7 @@ * \brief Free a category * \note This function can be used as a list_free callback */ -int mpm_free_category(void *tmp) { +int mpm_category_free(void *tmp) { category_t *ptr = tmp; if (ptr) { @@ -28,3 +28,13 @@ int mpm_free_category(void *tmp) { } return 1; } + +/*! + * \brief Initialize a category_t + * \param ptr Structure to initialize + */ +void mpm_category_init(category_t *ptr) { + if (ptr) { + ptr->name = NULL; + } +} diff --git a/src/files.c b/src/files.c @@ -0,0 +1,44 @@ +/*********************************** LICENSE **********************************\ +* Copyright 2017 Morphux * +* * +* Licensed under the Apache License, Version 2.0 (the "License"); * +* you may not use this file except in compliance with the License. * +* You may obtain a copy of the License at * +* * +* http://www.apache.org/licenses/LICENSE-2.0 * +* * +* Unless required by applicable law or agreed to in writing, software * +* distributed under the License is distributed on an "AS IS" BASIS, * +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * +* See the License for the specific language governing permissions and * +* limitations under the License. * +\******************************************************************************/ + +#include <files.h> + +/*! + * \brief Initialize a file_t structure + * \param ptr Structure to initialize + */ +void mpm_file_init(file_t *ptr) { + if (ptr) { + ptr->path = NULL; + ptr->parent = NULL; + ptr->hash = NULL; + } +} + +/*! + * \brief Free a file_t entry + * \note Can be called as a callback of list_free + */ +int mpm_file_free(void *tmp) { + file_t *ptr = tmp; + + if (ptr) { + free(ptr->path); + free(ptr->hash); + mpm_package_free(ptr->parent); + } + return 1; +} diff --git a/src/package.c b/src/package.c @@ -27,7 +27,7 @@ int mpm_package_free(void *tmp) { free(ptr->name); free(ptr->version); free(ptr->desc); - mpm_free_category(ptr->categ); + mpm_category_free(ptr->categ); free(ptr->categ); /*list_free(ptr->deps, &package_free);*/ /* TODO: Files free */ diff --git a/tests/test_database.c b/tests/test_database.c @@ -174,6 +174,8 @@ TEST(database_add_pkg_1) { cat = malloc(sizeof(category_t)); assert(pkg != NULL && cat != NULL); + mpm_package_init(pkg); + mpm_category_init(pkg->categ); pkg->name = strdup("test"); pkg->version = strdup("1.0"); cat->name = strdup("test");