1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
/*
* Copyright 2020 University Corporation for Atmospheric Research
*
* This file is part of the UDUNITS-2 package. See the file COPYRIGHT
* in the top-level source-directory of the package for copying and
* redistribution conditions.
*/
/*
* Searchable unit-and-identifier tree.
*/
/*LINTLIBRARY*/
#include "config.h"
#include "unitAndId.h"
#include "udunits2.h"
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
/*
* Arguments:
* unit The unit. May be freed upon return.
* id The identifier (name or symbol). May be freed upon return.
* Returns:
* NULL Failure. "ut_get_status()" will be
* UT_BAD_ARG "unit" or "id" is NULL.
* UT_OS Operating-system failure. See "errno".
* else Pointer to the new unit-and-identifier.
*/
UnitAndId*
uaiNew(
const ut_unit* const unit,
const char* const id)
{
UnitAndId* entry = NULL; /* failure */
if (id == NULL || unit == NULL) {
ut_set_status(UT_BAD_ARG);
ut_handle_error_message("uaiNew(): NULL argument");
}
else {
entry = malloc(sizeof(UnitAndId));
if (entry == NULL) {
ut_set_status(UT_OS);
ut_handle_error_message(strerror(errno));
ut_handle_error_message("Couldn't allocate %lu-byte data-structure",
sizeof(UnitAndId));
}
else {
entry->id = strdup(id);
if (entry->id == NULL) {
ut_set_status(UT_OS);
ut_handle_error_message(strerror(errno));
ut_handle_error_message("Couldn't duplicate identifier");
}
else {
entry->unit = ut_clone(unit);
if (entry->unit == NULL) {
assert(ut_get_status() != UT_SUCCESS);
free(entry->id);
}
}
if (ut_get_status() != UT_SUCCESS) {
free(entry);
entry = NULL;
}
}
}
return entry;
}
/*
* Frees memory of a unit-and-identifier.
*
* Arguments:
* entry Pointer to the unit-and-identifier or NULL.
*/
void
uaiFree(
UnitAndId* const entry)
{
if (entry != NULL) {
free(entry->id);
ut_free(entry->unit);
free(entry);
}
}
|