Unit-conversion
Overview
Unit-conversion functions
Copyright (C) 2004-2022 OKTET Labs Ltd. More…
// typedefs typedef enum te_unit_prefix te_unit_prefix; typedef struct te_unit te_unit; typedef struct te_unit_list te_unit_list; // enums enum te_unit_prefix; // structs struct te_unit; struct te_unit_list; // global functions const char* te_unit_prefix2str(te_unit_prefix unit); te_unit te_unit_pack(double value); double te_unit_unpack(te_unit value); te_unit te_unit_bin_pack(double value); double te_unit_bin_unpack(te_unit value); te_errno te_unit_from_string(const char* str, te_unit* value); te_errno te_unit_list_value_from_string(const char* str, const te_unit_list* type, double* value); // macros #define TE_UNITS_BIN_G2U(_val) #define TE_UNITS_BIN_K2U(_val) #define TE_UNITS_BIN_M2U(_val) #define TE_UNITS_BIN_U2G(_val) #define TE_UNITS_BIN_U2K(_val) #define TE_UNITS_BIN_U2M(_val) #define TE_UNITS_DEC_G2U(_val) #define TE_UNITS_DEC_K2U(_val) #define TE_UNITS_DEC_M2U(_val) #define TE_UNITS_DEC_U2G(_val) #define TE_UNITS_DEC_U2K(_val) #define TE_UNITS_DEC_U2M(_val)
Detailed Documentation
Unit-conversion functions
Copyright (C) 2004-2022 OKTET Labs Ltd. All rights reserved.
Typedefs
typedef enum te_unit_prefix te_unit_prefix
List of supported unit prefixes
typedef struct te_unit te_unit
Value-unit pair
typedef struct te_unit_list te_unit_list
Customizable unit type
Global Functions
const char* te_unit_prefix2str(te_unit_prefix unit)
Convert unit prefix to string.
Parameters:
unit |
Unit prefix. |
Returns:
String representation of unit prefix, or NULL
if prefix is unknown.
te_unit te_unit_pack(double value)
Convert plain value to value-unit.
Parameters:
value |
Value to convert. |
Returns:
Converted value with unit prefix.
double te_unit_unpack(te_unit value)
Convert value-unit to plain value.
Parameters:
value |
Value to convert. |
Returns:
Plain value.
te_unit te_unit_bin_pack(double value)
Convert binary plain value to value-unit.
Parameters:
value |
Value to convert. |
Returns:
Converted value with unit prefix.
double te_unit_bin_unpack(te_unit value)
Convert value-unit to binary plain value.
Parameters:
value |
Value to convert. |
Returns:
Plain value.
te_errno te_unit_from_string(const char* str, te_unit* value)
Read value-unit from the string and store it to unit
Parameters:
str |
String representation of value-unit |
value |
Value-unit to store result in |
Returns:
Status code.
te_errno te_unit_list_value_from_string(const char* str, const te_unit_list* type, double* value)
Read value from the string and convert it to double using given unit list
The API is independent and if the returned value is used in te_unit_*pack calls, the resulting te_unit
will not be consistent with type
.
Example:
te_unit_list units = { .scale = 1000, .start_pow = -1, .units = (const char * const[]){ "mHz", "Hz", "kHz", NULL }, };
"300mHz"
would be converted to 0.3
.
te_unit_list units = { .non_uniform_scale = { 1e-9, 1e-3, 1.0, 60.0, 3600.0 } .units = (const char * const[]){ "ns", "ms", "s", "m", "h", NULL }, };
"10h"
would be converted to 36000.0
and "10ms"
to 0.01
Parameters:
str |
String representation of value |
type |
Unit type |
value |
Value to store result in |
Returns:
Status code.