/* * fragment.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 <errno.h> #include <stdlib.h> #include <string.h> #include "macro.h" #include "error.h" #include "log.h" #include "data.h" #include "validator.h" #include "fragment.h" struct SH_Fragment * SH_Fragment_new (const char * tag, struct SH_Data * data, struct SH_Error * error) { struct SH_Fragment * fragment; if (!SH_Validator_check_tag (data->validator, tag)) { ERROR2 ("Tag %s is'nt valid.\n", tag); if (error != NULL) { error->type = VALUE_ERROR; } return NULL; } fragment = malloc (sizeof (struct SH_Fragment)); if (fragment == NULL) { ERROR1 ("Memory allocation for SH_Fragment failed.\n"); if (error != NULL) { error->type = ALLOCATION_FAILED; } return NULL; } fragment->data = data; fragment->tag = strdup (tag); if (errno == ENOMEM) { ERROR1 ("Memory allocation for fragment tag failed.\n"); if (error != NULL) { error->type = ALLOCATION_FAILED; } return NULL; } if (error != NULL) { error->type = SUCCESS; } return fragment; } void SH_Fragment_free (struct SH_Fragment * fragment, struct SH_Error * error) { free (fragment->tag); free (fragment); if (error != NULL) { error->type = SUCCESS; } return; } char * SH_Fragment_get_tag (struct SH_Fragment * fragment, struct SH_Error * error) { char * tag; tag = strdup (fragment->tag); if (errno == ENOMEM) { ERROR1 ("Memory allocation for fragment tag failed.\n"); if (error != NULL) { error->type = ALLOCATION_FAILED; } return NULL; } if (error != NULL) { error->type = SUCCESS; } return tag; }