/* * 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; }
/* * Finds an entry with a UTF-8 identifier corresponding to a unit. * * Arguments: * map The unit-to-identifier map. * unit The unit to be used as the key in the search. * Returns: * NULL The map doesn't contain an entry corresponding to "unit" whose * identifier is in UTF-8. * else Pointer to the entry corresponding to "unit" whose identifier is * in UTF-8 (and might, actually, be in ASCII). */ static UnitAndId* utimFindUtf8ByUnit( UnitToIdMap* const map, const ut_unit* const unit) { UnitAndId targetEntry; UnitAndId** treeEntry = NULL; /* failure */ targetEntry.unit = (ut_unit*)unit; treeEntry = tfind(&targetEntry, &map->utf8, compareUnits); if (treeEntry == NULL) { treeEntry = tfind(&targetEntry, &map->latin1, compareUnits); if (treeEntry == NULL) { treeEntry = tfind(&targetEntry, &map->ascii, compareUnits); } else { /* * Create the UTF-8 version of the Latin-1 identifier and add it to * the UTF-8 unit-to-id map so that it will be found next time. */ char* const id = latin1ToUtf8((*treeEntry)->id); if (id == NULL) { ut_set_status(UT_OS); ut_handle_error_message(strerror(errno)); ut_handle_error_message( "Couldn't convert identifier from ISO-8859-1 to UTF-8"); treeEntry = NULL; } else { UnitAndId* newEntry = uaiNew(unit, id); if (newEntry != NULL) { treeEntry = tsearch(newEntry, &map->utf8, compareUnits); if (treeEntry == NULL) { uaiFree(newEntry); ut_set_status(UT_OS); ut_handle_error_message(strerror(errno)); ut_handle_error_message( "Couldn't add unit-and-identifier to search-tree"); } } free(id); } /* UTF-8 identifier created */ } /* found Latin-1 identifier */ } /* no UTF-8 identifier */ return treeEntry == NULL ? NULL : *treeEntry; }
/* * Adds an entry to a unit-to-identifier map. * * Arguments: * map Pointer to unit-to-identifier map. * unit The unit. May be freed upon return. * id The identifier. May be freed upon return. * encoding The ostensible encoding of "id". * Returns: * UT_BAD_ARG "id" is inconsistent with "encoding". * UT_OS Operating-system error. See "errno". * UT_EXISTS "unit" already maps to a different identifier. * UT_SUCCESS Success. */ static ut_status utimAdd( UnitToIdMap* const map, const ut_unit* unit, const char* const id, ut_encoding encoding) { ut_status status; assert(map != NULL); assert(unit != NULL); assert(id != NULL); if (adjustEncoding(&encoding, id)) { status = UT_BAD_ARG; ut_set_status(status); ut_handle_error_message("Identifier not in given encoding"); } else { UnitAndId* targetEntry = uaiNew(unit, id); if (targetEntry != NULL) { void** rootp = selectTree(map, encoding); UnitAndId** treeEntry = tsearch(targetEntry, rootp, compareUnits); if (treeEntry == NULL) { status = UT_OS; ut_set_status(status); ut_handle_error_message(strerror(errno)); ut_handle_error_message("Couldn't add search-tree entry"); uaiFree(targetEntry); } else { if (strcmp((*treeEntry)->id, id) != 0) { status = UT_EXISTS; ut_set_status(status); ut_handle_error_message("Unit already maps to \"%s\"", (*treeEntry)->id); } else { status = UT_SUCCESS; } if (targetEntry != *treeEntry) uaiFree(targetEntry); } } /* "targetEntry" allocated */ } /* valid arguments */ return status; }
/*! * Constructs a converter from \a from unit to \a to unit. */ UdUnitConverter::UdUnitConverter(const UdUnit &from, const UdUnit &to): m_from(from), m_to(to), m_converter(nullptr) { ut_set_status(UT_SUCCESS); m_converter = ut_get_converter(m_from.m_unit, m_to.m_unit); //m_status = ut_get_status(); }
/*! * Returns the inverse of this unit. This convenience function is equal to raisedBy(-1). */ UdUnit UdUnit::inverted() const { ut_set_status(UT_SUCCESS); ut_unit *unit = ut_invert(m_unit); int status = ut_get_status(); return UdUnit(unit, status); }
/*! * FIXME: get rid of time */ UdUnit UdUnit::offsetByTime(qreal origin) const { ut_set_status(UT_SUCCESS); ut_unit *unit = ut_offset_by_time(m_unit, origin); int status = ut_get_status(); return UdUnit(unit, status); }
/*! * Returns a unit equivalent to this unit relative to the origin defined by \a offset. * For example: * \code * UdUnit kelvin = ... * UdUnit celcius = kelvin.offsetBy(273.15); * \endcode */ UdUnit UdUnit::offsetBy(qreal offset) const { ut_set_status(UT_SUCCESS); ut_unit *unit = ut_offset(m_unit, offset); int status = ut_get_status(); return UdUnit(unit, status); }
/*! * Returns a unit equivalent to this unit scaled by \a factor. * For example: * \code * UdUnit meter = ... * UdUnit kilometer = meter.scaledBy(1000.0); * \endcode */ UdUnit UdUnit::scaledBy(qreal factor) const { ut_set_status(UT_SUCCESS); ut_unit *unit = ut_scale(factor, m_unit); int status = ut_get_status(); return UdUnit(unit, status); }
/*! * Returns true if this unit is dimensionless, false otherwise. * An invalid unit is considered dimensionfull. */ bool UdUnit::isDimensionless() const { ut_set_status(UT_SUCCESS); int result = ut_is_dimensionless(m_unit); //int status = ut_get_status(); return result != 0; }
/*! * TBD */ UdUnit operator /(const UdUnit &lhs, const UdUnit &rhs) { ut_set_status(UT_SUCCESS); ut_unit *unit = ut_divide(lhs.m_unit, rhs.m_unit); int status = ut_get_status(); return UdUnit(unit, status); }
/*! * Returns the dimensionless unit one of this unit-system or an Invalid unit if * this unit-system is invalid. * \sa UdUnitSystem::isValid(), UdUnit::isValid() */ UdUnit UdUnitSystem::dimensionLessUnitOne() const { ut_set_status(UT_SUCCESS); ut_unit *unit = ut_get_dimensionless_unit_one(m_system); int status = ut_get_status(); return UdUnit(unit, status); }
/*! * Returns the UdUnit to which \a symbol maps from this unit-system or an invalid * UdUnit if no such unit exists or if this unit-system is invalid. * Symbol comparisons are case-sensitive. * \sa UdUnitSystem::isValid(), UdUnit::isValid() */ UdUnit UdUnitSystem::unitBySymbol(const QString &symbol) const { ut_set_status(UT_SUCCESS); ut_unit *unit = ut_get_unit_by_symbol(m_system, symbol.toUtf8().constData()); int status = ut_get_status(); return UdUnit(unit, status); }
/*! * Returns the UdUnit to which \a name maps from this unit-system or an invalid * UdUnit if no such unit exists ot if this unit-system is invalid. * Name comparisons are case-sensitive. * \sa UdUnitSystem::isValid(), UdUnit::isValid() */ UdUnit UdUnitSystem::unitByName(const QString &name) const { ut_set_status(UT_SUCCESS); ut_unit *unit = ut_get_unit_by_name(m_system, name.toLocal8Bit().constData()); int status = ut_get_status(); return UdUnit(unit, status); }
/* * 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 equal to the \a root 'th root of this unit. */ UdUnit UdUnit::rootedBy(int root) const { ut_set_status(UT_SUCCESS); ut_unit *unit = ut_root(m_unit, root); int status = ut_get_status(); return UdUnit(unit, status); }
/*! * Returns the unit in this unit-system system corresponding to the \a text textual * unit representation or an invalid unit if \a text contains a syntax error or * an invalid identifier. * Unit names and symbols are case-insensitive. \a text must have no leading or * trailing whitespace. */ UdUnit UdUnitSystem::unitFromString(const QString &text) const { ut_set_status(UT_SUCCESS); ut_unit *unit = ut_parse(m_system, text.toUtf8().constData(), UT_UTF8); int status = ut_get_status(); return UdUnit(unit, status); }
/*! * Returns the unit corresponding to the logarithmic \a base base * using this unit as a reference level. * For example, the following creates a decibel unit with a one milliwatt reference level: * \code * UdUnit milliWatt = ... * UdUnit decibel_1_mW = milliWatt.toLogarithmic(10.0).scaledBy(0.1); * \endcode */ UdUnit UdUnit::toLogarithmic(qreal base) const { ut_set_status(UT_SUCCESS); ut_unit *unit = ut_log(base, m_unit); int status = ut_get_status(); return UdUnit(unit, status); }
/*! * Returns the unit equal to this unit raised by \a power. */ UdUnit UdUnit::raisedBy(int power) const { ut_set_status(UT_SUCCESS); ut_unit *unit = ut_raise(m_unit, power); int status = ut_get_status(); return UdUnit(unit, status); }
/* * 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; }
/* * Returns the symbol in a given encoding to which a unit maps. * * Arguments: * unit Pointer to the unit whose symbol should be returned. * encoding The desired encoding of the symbol. * Returns: * NULL Failure. "ut_get_status()" will be * UT_BAD_ARG "unit" is NULL. * UT_SUCCESS "unit" doesn't map to a symbol * in the given encoding. * else Pointer to the symbol in the given encoding to which * "unit" maps. */ const char* ut_get_symbol( const ut_unit* const unit, const ut_encoding encoding) { ut_set_status(UT_SUCCESS); return getId(systemToUnitToSymbol, unit, encoding); }
/* * Returns the unit with a given name from a unit-system. Name comparisons * are case-insensitive. * * Arguments: * system Pointer to the unit-system. * name Pointer to the name of the unit to be returned. * Returns: * NULL Failure. "ut_get_status()" will be * UT_SUCCESS "name" doesn't map to a unit of * "system". * UT_BAD_ARG "system" or "name" is NULL. * else Pointer to the unit of the unit-system with the given name. * The pointer should be passed to ut_free() when the unit is * no longer needed. */ ut_unit* ut_get_unit_by_name( const ut_system* const system, const char* const name) { ut_set_status(UT_SUCCESS); return getUnitById(systemToNameToUnit, system, name); }
/* * Removes a mapping from a unit to a symbol. * * Arguments: * unit Pointer to the unit to be unmapped to a symbol. May be * freed upon return. * encoding The encoding to be removed. The mappings for "unit" in * other encodings will not be removed. * Returns: * UT_SUCCESS Success. * UT_BAD_ARG "unit" is NULL. */ ut_status ut_unmap_unit_to_symbol( const ut_unit* const unit, ut_encoding encoding) { ut_set_status(unmapUnitToId(systemToUnitToSymbol, unit, encoding)); return ut_get_status(); }
/* * Removes a mapping from a unit to a name. * * Arguments: * unit Pointer to the unit. May be freed upon return. * encoding The encoding to be removed. No other encodings will be * removed. * Returns: * UT_BAD_ARG "unit" is NULL. * UT_SUCCESS Success. */ ut_status ut_unmap_unit_to_name( const ut_unit* const unit, ut_encoding encoding) { ut_set_status(unmapUnitToId(systemToUnitToName, unit, encoding)); return ut_get_status(); }
/* * Returns the unit with a given symbol from a unit-system. Symbol * comparisons are case-sensitive. * * Arguments: * system Pointer to the unit-system. * symbol Pointer to the symbol associated with the unit to be * returned. * Returns: * NULL Failure. "ut_get_status()" will be * UT_SUCCESS "symbol" doesn't map to a unit of * "system". * UT_BAD_ARG "system" or "symbol" is NULL. * else Pointer to the unit in the unit-system with the given symbol. * The pointer should be passed to ut_free() when the unit is no * longer needed. */ ut_unit* ut_get_unit_by_symbol( const ut_system* const system, const char* const symbol) { ut_set_status(UT_SUCCESS); return getUnitById(systemToSymbolToUnit, system, symbol); }
/*! * Returns a unit-system corresponding to the XML-formatted unit-database specified by \a pathname. * If \a pathname is an empty string (the default), then \UU will try to load * a database using the environment variable \e {UDUNITS2_XML_PATH}, if not set, * then \UU will load its default database. * \sa databaseOrigin(), databasePath() */ UdUnitSystem *UdUnitSystem::loadDatabase(const QString &pathname) { ut_set_status(UT_SUCCESS); const char *path = nullptr; if (!pathname.isEmpty()) path = pathname.toUtf8().constData(); ut_system *system = ut_read_xml(path); return new UdUnitSystem(system, ut_get_status()); }
/* * Removes a mapping from a name to a unit. After this function, * ut_get_unit_by_name(system,name) will no longer return a unit. * * Arguments: * system The unit-system to which the unit belongs. * name The name of the unit. * encoding The character encoding of "name". * Returns: * UT_SUCCESS Success. * UT_BAD_ARG "system" or "name" is NULL. */ ut_status ut_unmap_name_to_unit( ut_system* system, const char* const name, const ut_encoding encoding) { ut_set_status(unmapId(systemToNameToUnit, name, system)); return ut_get_status(); }
/* * Adds a mapping from a unit to a symbol. * * Arguments: * unit Pointer to the unit to be mapped to "symbol". May be * freed upon return. * symbol Pointer to the symbol to be mapped-to by "unit". May * be freed upon return. * encoding The encoding of "symbol". * Returns: * UT_SUCCESS Success. * UT_BAD_ARG "unit" or "symbol" is NULL. * UT_OS Operating-system error. See "errno". * UT_EXISTS "unit" already maps to a symbol. */ ut_status ut_map_unit_to_symbol( const ut_unit* unit, const char* const symbol, ut_encoding encoding) { ut_set_status(mapUnitToId(&systemToUnitToSymbol, unit, symbol, encoding)); return ut_get_status(); }
/* * Adds a mapping from a unit to a name. * * Arguments: * unit Pointer to the unit to be mapped to "name". May be * freed upon return. * name Pointer to the name to be mapped-to by "unit". May be * freed upon return. * encoding The encoding of "name". * Returns: * UT_SUCCESS Success. * UT_BAD_ARG "unit" or "name" is NULL, or "name" is not in the * specified encoding. * UT_OS Operating-system error. See "errno". * UT_EXISTS "unit" already maps to a name. */ ut_status ut_map_unit_to_name( const ut_unit* const unit, const char* const name, ut_encoding encoding) { ut_set_status(mapUnitToId(&systemToUnitToName, unit, name, encoding)); return ut_get_status(); }
/*! * Constructs an empty unit-system. * * An empty unit-system has only one unit defined: the dimensionless unit one. */ UdUnitSystem::UdUnitSystem() { m_errorMessage = QString(); ut_set_status(UT_SUCCESS); m_system = ut_new_system(); if (m_system == nullptr) { m_error = ut_get_status(); //m_errorMessage = ...; } }
/* * Removes a mapping from a symbol to a unit. After this function, * ut_get_unit_by_symbol(system,symbol) will no longer return a unit. * * Arguments: * system The unit-system to which the unit belongs. * symbol The symbol of the unit. * encoding The character encoding of "symbol". * Returns: * UT_SUCCESS Success. * UT_BAD_ARG "system" or "symbol" is NULL. */ ut_status ut_unmap_symbol_to_unit( ut_system* system, const char* const symbol, const ut_encoding encoding) { ut_set_status(unmapId(systemToSymbolToUnit, symbol, system)); return ut_get_status(); }