/* * Returns the identifier in a given encoding to which a unit associated with * a unit-system maps. * * Arguments: * systemMap Pointer to the system-to-unit-to-id map. * unit Pointer to the unit whose identifier should be returned. * encoding The desired encoding of the identifier. * Returns: * NULL Failure. "ut_get_status()" will be * UT_BAD_ARG "unit" was NULL. * else Pointer to the identifier in the given encoding * associated with "unit". */ static const char* getId( SystemMap* const systemMap, const ut_unit* const unit, const ut_encoding encoding) { const char* id = NULL; /* failure */ if (unit == NULL) { ut_set_status(UT_BAD_ARG); ut_handle_error_message("NULL unit argument"); } else { UnitToIdMap** const unitToId = (UnitToIdMap**)smFind(systemMap, ut_get_system(unit)); if (unitToId != NULL) { UnitAndId* mapEntry = encoding == UT_LATIN1 ? utimFindLatin1ByUnit(*unitToId, unit) : encoding == UT_UTF8 ? utimFindUtf8ByUnit(*unitToId, unit) : utimFindAsciiByUnit(*unitToId, unit); if (mapEntry != NULL) id = mapEntry->id; } } return id; }
/* * Returns the unit to which an identifier maps in a particular unit-system. * * Arguments: * systemMap NULL or pointer to the system-map. If NULL, then * NULL will be returned. * system Pointer to the unit-system. * id Pointer to the identifier. * Returns: * NULL Failure. "ut_get_status()" will be: * UT_BAD_ARG "system" is NULL or "id" is NULL. * else Pointer to the unit in "system" with the identifier "id". * Should be passed to ut_free() when no longer needed. */ static ut_unit* getUnitById( const SystemMap* const systemMap, const ut_system* const system, const char* const id) { ut_unit* unit = NULL; /* failure */ if (system == NULL) { ut_set_status(UT_BAD_ARG); ut_handle_error_message("getUnitById(): NULL unit-system argument"); } else if (id == NULL) { ut_set_status(UT_BAD_ARG); ut_handle_error_message("getUnitById(): NULL identifier argument"); } else if (systemMap != NULL) { IdToUnitMap** const idToUnit = (IdToUnitMap**)smFind(systemMap, system); if (idToUnit != NULL) { const UnitAndId* uai = itumFind(*idToUnit, id); if (uai != NULL) unit = ut_clone(uai->unit); } } /* valid arguments */ return unit; }
/* * Removes an entry from the unit-to-identifier map associated with a * unit-system. * * Arguments: * sytemMap Pointer to the system-to-unit-to-identifier map. * unit The unit. May be freed upon return. * encoding The ostensible encoding of "id". * Returns: * UT_BAD_ARG "systemMap" is NULL. * UT_BAD_ARG "unit" is NULL. * UT_SUCCESS Success. */ static ut_status unmapUnitToId( SystemMap* const systemMap, const ut_unit* const unit, ut_encoding encoding) { ut_status status; if (systemMap == NULL || unit == NULL) { status = UT_BAD_ARG; } else { UnitToIdMap** const unitToIdMap = (UnitToIdMap**)smFind(systemMap, ut_get_system(unit)); status = (unitToIdMap == NULL || *unitToIdMap == NULL) ? UT_SUCCESS : utimRemove(*unitToIdMap, unit, encoding); } return status; }
/* * Removes the mapping from an identifier to a unit. * * Arguments: * systemMap Address of the pointer to the system-map. * id Pointer to the identifier. May be freed upon return. * system Pointer to the unit-system associated with the mapping. * Returns: * UT_BAD_ARG "id" is NULL, "system" is NULL, or "compare" is NULL. * UT_SUCCESS Success. */ static ut_status unmapId( SystemMap* const systemMap, const char* const id, ut_system* system) { ut_status status; if (systemMap == NULL || id == NULL || system == NULL) { status = UT_BAD_ARG; } else { IdToUnitMap** const idToUnit = (IdToUnitMap**)smFind(systemMap, system); status = (idToUnit == NULL || *idToUnit == NULL) ? UT_SUCCESS : itumRemove(*idToUnit, id); } /* valid arguments */ return status; }
/* * Frees resources associated with a unit-system. * * Arguments: * system Pointer to the unit-system to have its associated * resources freed. */ void utimFreeSystem( ut_system* system) { if (system != NULL) { SystemMap* systemMaps[2]; int i; systemMaps[0] = systemToUnitToName; systemMaps[1] = systemToUnitToSymbol; for (i = 0; i < 2; i++) { if (systemMaps[i] != NULL) { UnitToIdMap** const unitToId = (UnitToIdMap**)smFind(systemMaps[i], system); if (unitToId != NULL) utimFree(*unitToId); smRemove(systemMaps[i], system); } } } }
/* * Frees resources associated with a unit-system. * * Arguments: * system Pointer to the unit-system to have its associated * resources freed. */ void itumFreeSystem( ut_system* system) { if (system != NULL) { SystemMap* systemMaps[2]; int i; systemMaps[0] = systemToNameToUnit; systemMaps[1] = systemToSymbolToUnit; for (i = 0; i < 2; i++) { if (systemMaps[i] != NULL) { IdToUnitMap** const idToUnit = (IdToUnitMap**)smFind(systemMaps[i], system); if (idToUnit != NULL) itumFree(*idToUnit); smRemove(systemMaps[i], system); } } } /* valid arguments */ }