das2C
das core C utilities (v3)
Macros | Typedefs | Enumerations | Functions
value.h File Reference

A generic value type for use in arrays, datums and variables. More...

#include <stdint.h>
#include <das2/util.h>
Include dependency graph for value.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define DAS_FILL_VALUE   -1e31
 Canonical fill value (.
 
#define DAS_INT64_FILL   -0x7FFFFFFFFFFFFFFFL
 Conversion fill value for integer time intervals (-9.223e+18)
 
#define das_vt_isint(VT)   ( VT >= vtUByte && VT <= vtLong )
 Is this value type an integer of some sort.
 
#define das_vt_isreal(VT)   ( VT == vtFloat && VT == vtDouble )
 Is this value type an real value of some sort.
 
#define das_vt_rank(VT)   ( ((VT==vtGeoVec)||(VT==vtText)||(VT==vtByteSeq)) ? 1:0)
 Get the rank of a value type. More...
 

Typedefs

typedef int(* das_valcmp_func) (const ubyte *, const ubyte *)
 Comparison functions look like this.
 

Enumerations

enum  das_val_type {
  vtUnknown = 0 , vtUByte = 1 , vtByte = 2 , vtUShort = 3 ,
  vtShort = 4 , vtUInt = 5 , vtInt = 6 , vtULong = 7 ,
  vtLong = 8 , vtFloat = 9 , vtDouble = 10 , vtTime = 11 ,
  vtIndex = 12 , vtText = 13 , vtGeoVec = 14 , vtByteSeq = 15
}
 Enumeration of types stored in Das Array (DasAry) objects from value.h. More...
 

Functions

DAS_API das_val_type das_vt_store_type (const char *sEncType, int nItemBytes, const char *sInterp)
 Get a storage value type given the common packet encodings. More...
 
DAS_API das_valcmp_func das_vt_getcmp (das_val_type vt)
 Get the comparison function for two values of this type.
 
DAS_API int das_vt_cmpAny (const ubyte *pA, das_val_type vtA, const ubyte *pB, das_val_type vtB)
 Compare any two value types for equality. More...
 
DAS_API das_val_type das_vt_merge (das_val_type right, int op, das_val_type left)
 What would be the resulting type given an operation on the given value type. More...
 
DAS_API DasErrCode das_value_fromStr (ubyte *pBuf, int uBufLen, das_val_type vt, const char *sStr)
 Get a das value from a null terminated string. More...
 
DAS_API bool das_str2double (const char *str, double *pRes)
 Convert a string value to a 8-byte float, similar to strtod(3). More...
 
DAS_API bool das_str2int (const char *str, int *pRes)
 Convert the initial portion of a string to an integer with explicit over/underflow checks. More...
 
DAS_API bool das_str2bool (const char *str, bool *pRes)
 Convert a string value to a boolean value. More...
 
DAS_API bool das_str2baseint (const char *str, int base, int *pRes)
 Convert a string to an integer with explicit base and overflow checking. More...
 
DAS_API bool das_strn2baseint (const char *str, int nLen, int base, int *pRes)
 Convert an explicit length string to an integer with explicit base with over/underflow checks. More...
 
DAS_API double * das_csv2doubles (const char *s, int *nitems)
 Parse a comma separated list of ASCII values into a double array. More...
 
DAS_API char * das_doubles2csv (char *pBuf, size_t uBufSz, const double *pValues, int nValues)
 Print an array of doubles into a string buffer. More...
 
DAS_API char * das_floats2csv (char *pBuf, size_t uBufSz, const float *pValues, int nValues)
 Similar to das_doubles2csv, but for 32-bit floats.
 

Detailed Description

A generic value type for use in arrays, datums and variables.

Macro Definition Documentation

◆ das_vt_rank

#define das_vt_rank (   VT)    ( ((VT==vtGeoVec)||(VT==vtText)||(VT==vtByteSeq)) ? 1:0)

Get the rank of a value type.

Most items are scalars (rank 0), but strings and vectors are rank 1

Function Documentation

◆ das_vt_store_type()

DAS_API das_val_type das_vt_store_type ( const char *  sEncType,
int  nItemBytes,
const char *  sInterp 
)

Get a storage value type given the common packet encodings.

Storage types are values you can do calculations on. For binary encodings, these just represent the type minus any endian considerations

For text types that have an intended use, this returns a suitable binary storage type.

Parameters
sEncTypeThe storage type one of: byte, ubyte, BEint, BEuint, BEreal, LEint, LEuint, LEreal or: utf8, none
nItemsBytesThe number of bytes for each stored item
sInterp- Ignored unless the encoding is utf8, otherwise one of the values: bool, datetime, int, real, string

◆ das_vt_cmpAny()

DAS_API int das_vt_cmpAny ( const ubyte *  pA,
das_val_type  vtA,
const ubyte *  pB,
das_val_type  vtB 
)

Compare any two value types for equality.

If two types (vtA, vtB) are the same, memcmp is used. If two types are different the following promotion rules are applied.

  1. Strings are never equal to non strings.
  2. Since values have no units, times are never equal to non-times

If either side is a vtByte, vtUShort, vtShort, vtInt, or vtFloat, vtDouble, both sides are promoted to double and compared.

Returns
-1 if A is less than B, 0 if equal, +1 if A is greater than B or -2 if A is not comparable to B.

◆ das_vt_merge()

DAS_API das_val_type das_vt_merge ( das_val_type  right,
int  op,
das_val_type  left 
)

What would be the resulting type given an operation on the given value type.

Currently the binary type combining rules are:

  1. Unknown combined with anything is unknown.
  2. Index combined with anything is unknown.
  3. ByteSeq combined with anything is unknown.
  4. Text combined with anything is unknown.
  5. Byte, UShort and Short math results in floats.
  6. Int, Long, Float and Double math results in doubles.
  7. If time in involved the following rules apply:

    Time - Time = Double Time +/- (Byte, UShort, Short, Int, Float Double) => Time

    All other operations invalving times are unknown

Parameters
right
opAn operation ID.
left
Returns
The resulting type or vtUnknown if the types cannot be combinded via any known operations

◆ das_value_fromStr()

DAS_API DasErrCode das_value_fromStr ( ubyte *  pBuf,
int  uBufLen,
das_val_type  vt,
const char *  sStr 
)

Get a das value from a null terminated string.

This function should not exit, instead erroreous parsing triggers log messages

Returns
DAS_OKAY if parsing was successful, an error return code otherwise.

◆ das_str2double()

DAS_API bool das_str2double ( const char *  str,
double *  pRes 
)

Convert a string value to a 8-byte float, similar to strtod(3).

Parameters
strthe string to convert. Conversion stops at the first improper character. Whitespace and leading 0's are ignored in the input.
pResThe location to store the resulting 8-byte float.
Returns
true if the conversion succeeded, false otherwise. Among other reason, conversion will fail if the resulting value won't fit in a 8 byte float.

◆ das_str2int()

DAS_API bool das_str2int ( const char *  str,
int *  pRes 
)

Convert the initial portion of a string to an integer with explicit over/underflow checks.

Parameters
strthe string to convert. Conversion stops at the first improper character. Whitespace and leading 0's are ignored in the input. The number is assumed to be in base 10, unless the first non-whitespace characters after the optional '+' or '-' sign are '0x'.
pResThe location to store the resulting integer.
Returns
true if the conversion succeeded, false otherwise.

◆ das_str2bool()

DAS_API bool das_str2bool ( const char *  str,
bool *  pRes 
)

Convert a string value to a boolean value.

Parameters
strthe string to convert. The following values are accepted as representing true: 'true' (any case), 'yes' (any case), 'T', 'Y', '1'. The following values are accepted as representing false: 'false' (any case), 'no', (any case), 'F', 'N', '0'. Anything else results in no conversion.
pResthe location to store the resulting boolean value
Returns
true if the string could be converted to a boolean, false otherwise.

◆ das_str2baseint()

DAS_API bool das_str2baseint ( const char *  str,
int  base,
int *  pRes 
)

Convert a string to an integer with explicit base and overflow checking.

Parameters
strthe string to convert. Conversion stops at the first improper character. Whitespace and leading 0's are ignored in the input. No assumptions are made about the base of the string. So anything that is not a proper character is the given base is causes an error return.
basean integer from 1 to 60 inclusive.
pResThe location to store the resulting integer.
Returns
true if the conversion succeeded, false otherwise.

◆ das_strn2baseint()

DAS_API bool das_strn2baseint ( const char *  str,
int  nLen,
int  base,
int *  pRes 
)

Convert an explicit length string to an integer with explicit base with over/underflow checks.

Parameters
strthe string to convert. Conversion stops at the first improper character. Whitespace and leading 0's are ignored in the input. No assumptions are made about the base of the string. So anything that is not a proper character is the given base is causes an error return.
basean integer from 1 to 60 inclusive.
nLenonly look at up to this many characters of input. Encountering whitespace or a '\0' characater will still halt character accumlation.
pResThe location to store the resulting integer.
Returns
true if the conversion succeeded, false otherwise.

Will only inspect up to 64 non-whitespace characters when converting a value.

◆ das_csv2doubles()

DAS_API double* das_csv2doubles ( const char *  s,
int *  nitems 
)

Parse a comma separated list of ASCII values into a double array.

Parameters
[in]sThe string of comma separated values
[out]nitemsa pointer to an integer which will be set to the length of the newly allocated array.
Returns
a new double array allocated on the heap.

◆ das_doubles2csv()

DAS_API char* das_doubles2csv ( char *  pBuf,
size_t  uBufSz,
const double *  pValues,
int  nValues 
)

Print an array of doubles into a string buffer.

Prints an array of doubles into a string buffer with commas and spaces between each entry. Note there is no precision limit for the printing so the space needed to hold the array may 24 bytes times the number number of values, or more.

Todo:
this function is a potential source of buffer overruns, fix it.
Parameters
[out]pBufa pointer to the buffer to receive the printed values
[in]uBufSzThe length of the buffer to received the converted values
[in]pValuesan array of doubles
[in]nValuesthe number of items to print to the array
Returns
A pointer to the supplied buffer.