/* * data.c * * Copyright 2022 Jonathan Schöbel <jonathan@Ubermos-2019> * * This program 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 2 of the License, or * (at your option) any later version. * * This program 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 this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. * * */ #include <stdlib.h> #include <string.h> #include "macro.h" #include "data.h" static inline void init_pages (struct SH_Data * data); struct SH_Data * SH_Data_new () { struct SH_Data * data; data = malloc (sizeof (struct SH_Data)); if (data == NULL) { EXIT; } init_pages (data); return data; } static inline void free_pages (struct SH_Data * data); void SH_Data_free (struct SH_Data * data) { free_pages (data); free (data); return; } static inline void init_pages (struct SH_Data * data) { data->page_n = 0; data->pages = malloc (0); data->page_next = PAGE_MIN; return; } static inline void free_pages (struct SH_Data * data) { unsigned int index; for (index = 0; index < data->page_n; index++) { free (data->pages[index].name); } free (data->pages); return; } page_t SH_Data_register_page (struct SH_Data * data, const char * name) { data->pages = realloc (data->pages, (data->page_n + 1) * sizeof (struct SH_Page)); data->pages[data->page_n].id = data->page_next; data->pages[data->page_n].name = strdup (name); /* abort on overflow */ if (data->page_n == UINT_MAX || data->page_next == PAGE_MAX) { EXIT; } data->page_n++; return NEXT_PAGE(data->page_next); }