"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "lib/unitcore.c" between
udunits-2.2.26.tar.gz and udunits-2.2.28.tar.gz

About: UDUNITS supports units of physical quantities. Its C library provides for arithmetic manipulation of units and for conversion of numeric values between compatible units.

unitcore.c  (udunits-2.2.26):unitcore.c  (udunits-2.2.28)
/* /*
* Copyright 2013 University Corporation for Atmospheric Research * Copyright 2020 University Corporation for Atmospheric Research
* *
* This file is part of the UDUNITS-2 package. See the file COPYRIGHT * 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 * in the top-level source-directory of the package for copying and
* redistribution conditions. * redistribution conditions.
*/ */
/* /*
* Unit creation and manipulation routines for the udunits(3) library. * Unit creation and manipulation routines for the udunits(3) library.
* *
* The following data-structures exist in this module: * The following data-structures exist in this module:
* BasicUnit Like an ISO "base unit" but also for dimensionless * BasicUnit Like an ISO "base unit" but also for dimensionless
* units (e.g., "radian"). * units (e.g., "radian").
* ProductUnit A unit that, when it is created, contains all the * ProductUnit A unit that, when it is created, contains all the
* BasicUnit-s that exist at the time, each raised * BasicUnit-s that exist at the time, each raised
* to an integral power (that can be zero). * to an integral power (that can be zero).
* GalileanUnit A unit whose value is related to another unit by a * GalileanUnit A unit whose value is related to another unit by a
* Galilean transformation (y = ax + b). Examples include * Galilean transformation (y = ax + b). Examples include
* "yard" and "degrees Fahrenheit". * "yard" and "degrees Fahrenheit".
* LogUnit A unit that is related to another unit by a logarithmic * LogUnit A unit that is related to another unit by a logarithmic
* transformation (y = a*log(x)). The "Bel" is an example. * transformation (y = a*log(x)). The "Bel" is an example.
* TimestampUnit A wrong-headed unit that shouldn't exist but does for * TimestampUnit A wrong-headed unit that shouldn't exist but does for
* backward compatibility. It was intended to provide * backward compatibility. It was intended to provide
* similar functionality as the GalileanUnit, but for time * similar functionality as the GalileanUnit, but for time
* units (e.g., "seconds since the epoch"). Unfortunately, * units (e.g., "seconds since the epoch"). Unfortunately,
* people try to use it for more than it is capable (e.g., * people try to use it for more than it is capable (e.g.,
* days since some time on an imaginary world with only 360 * days since some time on an imaginary world with only 360
* days per year). * days per year).
* ut_unit A data-structure that encapsulates ProductUnit, * ut_unit A data-structure that encapsulates ProductUnit,
* GalileanUnit, LogUnit, and TimestampUnit. * GalileanUnit, LogUnit, and TimestampUnit.
* *
* This module is thread-compatible but not thread-safe: multi-thread access to * This module is thread-compatible but not thread-safe: multi-thread access to
* this module must be externally synchronized. * this module must be externally synchronized.
*/ */
/*LINTLIBRARY*/ /*LINTLIBRARY*/
#ifndef _XOPEN_SOURCE #include "config.h"
# define _XOPEN_SOURCE 500
#endif #include "udunits2.h" /* this module's API */
#include "converter.h"
#include <assert.h> #include <assert.h>
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include <float.h> #include <float.h>
#ifndef _MSC_VER #ifndef _MSC_VER
#include <inttypes.h> #include <inttypes.h>
#else #else
#define int32_t __int32 #define int32_t __int32
#endif #endif
skipping to change at line 75 skipping to change at line 76
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#ifndef _MSC_VER #ifndef _MSC_VER
#include <strings.h> #include <strings.h>
#endif #endif
#include "udunits2.h" /* this module's API */
#include "converter.h"
typedef enum { typedef enum {
PRODUCT_EQUAL = 0, /* The units are equal -- ignoring dimensionless PRODUCT_EQUAL = 0, /* The units are equal -- ignoring dimensionless
* basic-units */ * basic-units */
PRODUCT_INVERSE, /* The units are reciprocals of each other */ PRODUCT_INVERSE, /* The units are reciprocals of each other */
PRODUCT_UNCONVERTIBLE, /* The units have incompatible dimensionality */ PRODUCT_UNCONVERTIBLE, /* The units have incompatible dimensionality */
PRODUCT_UNKNOWN /* The relationship is unknown */ PRODUCT_UNKNOWN /* The relationship is unknown */
} ProductRelationship; } ProductRelationship;
typedef struct BasicUnit BasicUnit; typedef struct BasicUnit BasicUnit;
typedef struct ProductUnit ProductUnit; typedef struct ProductUnit ProductUnit;
skipping to change at line 403 skipping to change at line 401
* seconds The number of seconds. * seconds The number of seconds.
* Returns: * Returns:
* The clock-time encoded as a scalar value. * The clock-time encoded as a scalar value.
*/ */
double double
ut_encode_clock( ut_encode_clock(
int hours, int hours,
int minutes, int minutes,
double seconds) double seconds)
{ {
if (abs(hours) >= 24 || abs(minutes) >= 60 || fabs(seconds) > 62) {
ut_set_status(UT_BAD_ARG);
return 0;
}
return (hours*60 + minutes)*60 + seconds; return (hours*60 + minutes)*60 + seconds;
} }
static int inline static int
mydiv( const double numer, mydiv( const double numer,
const unsigned denom, const unsigned denom,
double* const rem) double* const rem)
{ {
int n = (int)(abs(numer)/denom); int n = abs((int)numer)/denom;
if (numer < 0) if (numer < 0)
n = -n; n = -n;
*rem = numer - (long)n * (long)denom; *rem = numer - (long)n * (long)denom;
return n; return n;
} }
static void static void
decomp( double value, decomp( double value,
int* const days, int* const days,
int* const hours, int* const hours,
skipping to change at line 626 skipping to change at line 629
ut_handle_error_message(strerror(errno)); ut_handle_error_message(strerror(errno));
ut_handle_error_message( ut_handle_error_message(
"basicNew(): Couldn't allocate %lu-byte basic-unit", "basicNew(): Couldn't allocate %lu-byte basic-unit",
sizeof(BasicUnit)); sizeof(BasicUnit));
} }
else if (commonInit(&basicUnit->common, &basicOps, system, else if (commonInit(&basicUnit->common, &basicOps, system,
BASIC) == 0) { BASIC) == 0) {
basicUnit->index = index; basicUnit->index = index;
basicUnit->isDimensionless = isDimensionless; basicUnit->isDimensionless = isDimensionless;
basicUnit->product = product; basicUnit->product = product;
error = 0; error = 0; // Success
} /* "basicUnit" allocated */ } /* "basicUnit" allocated */
if (error) if (error)
productFree((ut_unit*)product); productFree((ut_unit*)product);
} /* "product" allocated */ } /* "product" allocated */
return basicUnit; return basicUnit;
} }
static ProductUnit* static ProductUnit*
skipping to change at line 1533 skipping to change at line 1536
return isDimensionless; return isDimensionless;
} }
/****************************************************************************** /******************************************************************************
* Galilean Unit: * Galilean Unit:
******************************************************************************/ ******************************************************************************/
static UnitOps galileanOps; static UnitOps galileanOps;
/* /**
* Returns a new unit instance. The returned instance is not necessarily a * Returns a new unit instance. The returned instance is not necessarily a
* Galilean unit. * Galilean unit.
* *
* Arguments: * @param[in] scale The scale-factor for the new unit.
* scale The scale-factor for the new unit. * @param[in] unit The underlying unit. May be freed upon return.
* unit The underlying unit. May be freed upon return. * @param[in] offset The offset for the new unit.
* offset The offset for the new unit. * @retval NULL Failure. `ut_get_status()` will be:
* Returns: * UT_OS Operating-system error. See `errno`.
* NULL Failure. "ut_get_status()" will be: * UT_BAD_ARG `scale == 0 || unit == NULL`
* UT_OS Operating-system error. See "errno". * @return The newly-allocated, galilean-unit.
* else The newly-allocated, galilean-unit.
*/ */
static ut_unit* static ut_unit*
galileanNew( galileanNew(
double scale, double scale,
const ut_unit* unit, const ut_unit* unit,
double offset) double offset)
{ {
ut_unit* newUnit = NULL; /* failure */ ut_unit* newUnit;
assert(scale != 0);
assert(unit != NULL);
if (IS_GALILEAN(unit)) { if (scale == 0 || unit == NULL) {
scale *= unit->galilean.scale; ut_set_status(UT_BAD_ARG);
offset += (unit->galilean.scale * unit->galilean.offset) / scale; newUnit = NULL;
unit = unit->galilean.unit;
}
if (areAlmostEqual(scale, 1) && areAlmostEqual(offset, 0)) {
newUnit = CLONE(unit);
} }
else { else {
GalileanUnit* galileanUnit = malloc(sizeof(GalileanUnit)); if (IS_GALILEAN(unit)) {
scale *= unit->galilean.scale;
offset += (unit->galilean.scale * unit->galilean.offset) / scale;
unit = unit->galilean.unit;
}
if (galileanUnit == NULL) { if (areAlmostEqual(scale, 1) && areAlmostEqual(offset, 0)) {
ut_set_status(UT_OS); newUnit = CLONE(unit);
ut_handle_error_message("galileanNew(): " }
"Couldn't allocate %lu-byte Galilean unit", else {
sizeof(GalileanUnit)); GalileanUnit* galileanUnit = malloc(sizeof(GalileanUnit));
}
else {
int error = 1;
if (commonInit(&galileanUnit->common, &galileanOps, if (galileanUnit == NULL) {
unit->common.system, GALILEAN) == 0) { ut_set_status(UT_OS);
galileanUnit->scale = scale; ut_handle_error_message("galileanNew(): "
galileanUnit->offset = offset; "Couldn't allocate %lu-byte Galilean unit",
galileanUnit->unit = CLONE(unit); sizeof(GalileanUnit));
error = 0; }
} else {
int error = 1;
if (error) { if (commonInit(&galileanUnit->common, &galileanOps,
free(galileanUnit); unit->common.system, GALILEAN) == 0) {
galileanUnit = NULL; galileanUnit->scale = scale;
} galileanUnit->offset = offset;
} /* "galileanUnit" allocated */ galileanUnit->unit = CLONE(unit);
error = 0;
}
if (error) {
free(galileanUnit);
galileanUnit = NULL;
}
} /* "galileanUnit" allocated */
newUnit = (ut_unit*)galileanUnit; newUnit = (ut_unit*)galileanUnit;
} /* Galilean unit necessary */ } /* Galilean unit necessary */
} // `scale != 0 && unit != NULL`
return newUnit; return newUnit;
} }
static ProductUnit* static ProductUnit*
galileanGetProduct( galileanGetProduct(
const ut_unit* const unit) const ut_unit* const unit)
{ {
assert(unit != NULL); assert(unit != NULL);
assert(IS_GALILEAN(unit)); assert(IS_GALILEAN(unit));
 End of changes. 17 change blocks. 
55 lines changed or deleted 60 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)