Beispiel #1
0
/*!
 * 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);
}
Beispiel #2
0
value ml_ut_format( value u, value encoding, value names, value basic, value max_length ) {
    CAMLparam5( u, encoding, names, basic, max_length );
    CAMLlocal1( ml_buf );

    int opts =
        Int_val( encoding ) |
        (Int_val( basic ) ? UT_DEFINITION : 0) |
        (Int_val( names ) ? UT_NAMES : 0);

    int result;
    char *buf;
    buf = (char *)malloc( sizeof(char) * Int_val( max_length ) );
    if ( buf == NULL ) {
        caml_failwith( "Unable to allocate buffer" );
    }

    result = ut_format( UD_ut_unit_val( u ), buf, Int_val( max_length ), opts );
    if ( result == -1 ) {
        caml_raise_with_arg( *caml_named_value( "ut status exception" ), Val_int( ut_get_status() ) );
    }

    ml_buf = caml_copy_string( buf );

    free( buf );

    CAMLreturn( ml_buf );
}
Beispiel #3
0
/*!
 * 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);
}
Beispiel #4
0
/*!
 * 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);
}
Beispiel #5
0
/*!
 * 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);
}
Beispiel #6
0
/*!
 * 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);
}
Beispiel #7
0
/*!
 * 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);
}
Beispiel #8
0
/*!
 * 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);
}
Beispiel #9
0
/*!
 * 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);
}
Beispiel #10
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);
}
Beispiel #11
0
/*!
 * 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);
}
Beispiel #12
0
/*
 * Initialize the units(3) package.
 */
int
utInit(
    const char	*path)
{
    int status;

    (void)ut_set_error_message_handler(ut_ignore);
    if (unitSystem != NULL) {
	ut_free_system(unitSystem);
	unitSystem = NULL;
    }
    unitSystem = ut_read_xml(NULL);
    if (unitSystem == NULL) {
	status = ut_get_status() == UT_PARSE
	    ? UT_ESYNTAX
	    : UT_EIO;
    }
    else {
	second = ut_get_unit_by_name(unitSystem, "second");
	encodedTimeUnit =
	    ut_offset_by_time(second, ut_encode_time(2001, 1, 1, 0, 0, 0.0));
	buffer = malloc(buflen);
	if (buffer == NULL) {
	    buflen = 0;
	    status = UT_EALLOC;
	}
	else {
	    status = 0;
	}
    }
    return status;
}
Beispiel #13
0
/*
 * Compute the conversion factor between two unit-structures.
 */
int
utConvert(
    const utUnit	*from,
    const utUnit	*to,
    double		*slope,
    double		*intercept)
{
    int			status;
    cv_converter*	converter = ut_get_converter(from->unit2, to->unit2);

    if (converter == NULL) {
	status = ut_get_status();

	if (status == UT_BAD_ARG) {
	    status = UT_EINVALID;
	}
	else if (status == UT_NOT_SAME_SYSTEM) {
	    status = UT_ENOINIT;
	}
	else if (status == UT_MEANINGLESS) {
	    status = UT_ECONVERT;
	}
	else {
	    status = UT_EALLOC;
	}
    }
    else {
	*intercept = cv_convert_double(converter, 0.0);
	*slope = cv_convert_double(converter, 1.0) - *intercept;
	status = 0;
    }
    return status;
}
Beispiel #14
0
/*
 * Return the unit corresponding to a unit-specification.
 *
 */
int
utFind(
    char	*spec,
    utUnit	*up)
{
    int		status;
    ut_unit*	unit = ut_parse(unitSystem, spec, UT_ASCII);

    if (unit == NULL) {
	status = ut_get_status();

	if (status == UT_BAD_ARG) {
	    status = UT_EINVALID;
	}
	else if (status == UT_SYNTAX) {
	    status = UT_ESYNTAX;
	}
	else if (status == UT_UNKNOWN) {
	    status = UT_EUNKNOWN;
	}
	else if (status == UT_OS) {
	    status = UT_EALLOC;
	}
    }
    else {
	status = setUnit(up, unit);
    }
    return status;
}
Beispiel #15
0
/*!
 * 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);
}
Beispiel #16
0
/*!
 * 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);
}
Beispiel #17
0
void cmor_convert_value( char *units, char *ctmp, double *tmp ) {
/* -------------------------------------------------------------------- */
/*      Local variables                                                 */
/* -------------------------------------------------------------------- */
    ut_unit *user_units = NULL, *cmor_units = NULL;
    cv_converter *ut_cmor_converter = NULL;
    double value;
    char msg[CMOR_MAX_STRING];


    cmor_add_traceback( "cmor_convert_value" );

    value = *tmp;
    if( units[0] != '\0' ) {
	cmor_prep_units( ctmp, units, &cmor_units, &user_units,
			 &ut_cmor_converter );
	*tmp = cv_convert_double( ut_cmor_converter, value );
	if( ut_get_status(  ) != UT_SUCCESS ) {
	    snprintf( msg, CMOR_MAX_STRING,
		      "Udunits: Error converting units from %s to %s",
		      units, ctmp );
	    cmor_handle_error( msg, CMOR_CRITICAL );
	}

	cv_free( ut_cmor_converter );
	if( ut_get_status(  ) != UT_SUCCESS ) {
	    snprintf( msg, CMOR_MAX_STRING,
		      "Udunits: Error freeing converter" );
	    cmor_handle_error( msg, CMOR_CRITICAL );
	}
	ut_free( cmor_units );
	if( ut_get_status(  ) != UT_SUCCESS ) {
	    snprintf( msg, CMOR_MAX_STRING,
		      "Udunits: Error freeing units" );
	    cmor_handle_error( msg, CMOR_CRITICAL );
	}
	ut_free( user_units );
	if( ut_get_status(  ) != UT_SUCCESS ) {
	    snprintf( msg, CMOR_MAX_STRING,
		      "Udunits: Error freeing units" );
	    cmor_handle_error( msg, CMOR_CRITICAL );
	}
    } else
	*tmp = value;
    cmor_pop_traceback(  );
    return;
}
Beispiel #18
0
/*
 * 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;
}
Beispiel #19
0
/*!
 * 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());
}
Beispiel #20
0
/*
 * 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();
}
Beispiel #21
0
/*
 * 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();
}
Beispiel #22
0
/*
 * 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();
}
Beispiel #23
0
/*
 * 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();
}
Beispiel #24
0
/*
 * 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();
}
Beispiel #25
0
  udunits::udunits(std::string const& filename) 
  {
    unit_system_ = ut_read_xml(filename.c_str());
    ut_status stat = ut_get_status();
    if(stat == UT_OPEN_ARG)   throw udunits_exception("Could not open unit database!");
    else if(stat == UT_OS)    throw udunits_exception("Unit database encountered operating system error!");
    else if(stat == UT_PARSE) throw udunits_exception("Unit database could not process unit XML file!");

    ut_set_error_message_handler(&ut_ignore);
  }
Beispiel #26
0
/*!
 * 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 = ...;
    }
}
Beispiel #27
0
/*
 * 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();
}
Beispiel #28
0
/*
 * Adds a mapping from a name to a unit.
 *
 * Arguments:
 *	name		Pointer to the name to be mapped to "unit".  May be
 *			freed upon return.
 *      encoding        The character encoding of "name".
 *	unit		Pointer to the unit to be mapped-to by "name".  May be
 *			freed upon return.
 * Returns:
 *	UT_BAD_ARG	"name" or "unit" is NULL.
 *	UT_OS		Operating-system error.  See "errno".
 *	UT_EXISTS	"name" already maps to a different unit.
 *	UT_SUCCESS	Success.
 */
ut_status
ut_map_name_to_unit(
    const char* const		name,
    const ut_encoding		encoding,
    const ut_unit* const	unit)
{
    ut_set_status(
	mapIdToUnit(&systemToNameToUnit, name, unit, insensitiveCompare));

    return ut_get_status();
}
Beispiel #29
0
/*
 * Adds a mapping from a symbol to a unit.
 *
 * Arguments:
 *	symbol		Pointer to the symbol to be mapped to "unit".  May be
 *			freed upon return.
 *      encoding        The character encoding of "symbol".
 *	unit		Pointer to the unit to be mapped-to by "symbol".  May
 *			be freed upon return.
 * Returns:
 *	UT_BAD_ARG	"symbol" or "unit" is NULL.
 *	UT_OS		Operating-system error.  See "errno".
 *	UT_EXISTS	"symbol" already maps to a different unit.
 *	UT_SUCCESS	Success.
 */
ut_status
ut_map_symbol_to_unit(
    const char* const		symbol,
    const ut_encoding		encoding,
    const ut_unit* const	unit)
{
    ut_set_status(
	mapIdToUnit(&systemToSymbolToUnit, symbol, unit, sensitiveCompare));

    return ut_get_status();
}
Beispiel #30
0
 void udunits::investigate_parse_error(std::string const& unit)
 {
   std::string err_msg = "Error parsing unit \""+unit+"\": ";
   ut_status status = ut_get_status();
   if(status == UT_BAD_ARG) throw udunits_exception(err_msg+"Unit or system is NULL!");
   else
   if(status == UT_SYNTAX)  throw udunits_exception(err_msg+"Unit string contains a syntax error!");
   else
   if(status == UT_UNKNOWN) throw udunits_exception(err_msg+"Unit string contains an unknown identifier!");
   else                     throw udunits_exception(err_msg+"An unknown error occured!");
 }