das2C
das core C utilities (v3)
value.h
Go to the documentation of this file.
1 /* Copyright (C) 2018 Chris Piker <chris-piker@uiowa.edu>
2  *
3  * This file is part of das2C, the Core Das2 C Library.
4  *
5  * das2C is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU Lesser General Public License version 2.1 as published
7  * by the Free Software Foundation.
8  *
9  * das2C is distributed in the hope that it will be useful, but WITHOUT ANY
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * version 2.1 along with das2C; if not, see <http://www.gnu.org/licenses/>.
16  */
17 
20 #include <stdint.h>
21 
22 #include <das2/util.h>
23 
24 #ifndef _das_value_h_
25 #define _das_value_h_
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
36 #define DAS_FILL_VALUE -1e31
37 
39 #define DAS_INT64_FILL -0x7FFFFFFFFFFFFFFFL
40 #define DAS_INT32_FILL -0x7FFFFFFF
41 
42 
47 typedef struct das_byteseq_t{
48  const ubyte* ptr;
49  size_t sz;
50 } das_byteseq;
51 
52 
53 #define VT_MIN_SIMPLE vtUByte
54 #define VT_MAX_SIMPLE vtTime
55 
64 typedef enum das_val_type_e {
65 
68  vtUnknown = 0,
69 
72  /* VT_MIN_SIMPLE = vtUByte */
73 
75  vtUByte = 1,
76 
78  vtByte = 2,
79 
81  vtUShort = 3,
82 
84  vtShort = 4,
85 
87  vtUInt = 5,
88 
90  vtInt = 6,
91 
93  vtULong = 7,
94 
96  vtLong = 8,
97 
99  vtFloat = 9,
100 
102  vtDouble = 10,
103 
105  vtTime = 11,
106 
107  /* VT_MAX_SIMPLE = vtTime */
108 
109  /* The following type is not used by datums, but by array indexing elements
110  * that track the size and location of child dimensions */
111  vtIndex = 12,
112 
113  /* The following two types are only used by variables and datums
114  *
115  * When generating Datums from Arrays:
116  *
117  * - If the array element type is etUnknown then vtByteSeq is used, size
118  * is element size.
119  *
120  * - If the array element type is etByte and the D2ARY_AS_STRING flag is
121  * set then vtText is used.
122  *
123  * - If the array element type is anything else and D2ARY_AS_SUBSEQ is
124  * set then vtByteSeq is used, size is element size times the size of
125  * the fastest moving index the location read.
126  */
127 
130  vtText = 13,
131 
133  vtGeoVec = 14,
134 
137  vtByteSeq = 15
138 
140 
164  const char* sEncType, int nItemBytes, const char* sInterp
165 );
166 
170 #define das_vt_isint(VT) ( VT >= vtUByte && VT <= vtLong )
171 
175 #define das_vt_isreal(VT) ( VT == vtFloat && VT == vtDouble )
176 
182 #define das_vt_rank(VT) ( ((VT==vtGeoVec)||(VT==vtText)||(VT==vtByteSeq)) ? 1:0)
183 
184 
191 DAS_API const void* das_vt_fill(das_val_type vt);
192 
197 DAS_API size_t das_vt_size(das_val_type vt);
198 
202 DAS_API const char* das_vt_toStr(das_val_type vt);
203 
207 DAS_API das_val_type das_vt_fromStr(const char* sType);
208 
209 
210 /* * Given a string and it's expected interpretation, return a suitable storage type
211  */
212 /* DAS_API das_val_type das_vt_guess_store(const char* sInterp, const char* sValue); */
213 
215 typedef int (*das_valcmp_func)(const ubyte*, const ubyte*);
216 
219 
235 DAS_API int das_vt_cmpAny(
236  const ubyte* pA, das_val_type vtA, const ubyte* pB, das_val_type vtB
237 );
238 
239 /* In the future the token ID will come from the lexer, for now just make
240  * something up*/
241 #define D2OP_PLUS 100
242 
269 DAS_API das_val_type das_vt_merge(das_val_type right, int op, das_val_type left);
270 
271 
279  ubyte* pBuf, int uBufLen, das_val_type vt, const char* sStr
280 );
281 
282 
294 DAS_API bool das_str2double(const char* str, double* pRes);
295 
296 
309 DAS_API bool das_str2int(const char* str, int* pRes);
310 
321 DAS_API bool das_str2bool(const char* str, bool* pRes);
322 
338 DAS_API bool das_str2baseint(const char* str, int base, int* pRes);
339 
362 DAS_API bool das_strn2baseint(const char* str, int nLen, int base, int* pRes);
363 
364 /* Don't think these are used anywhere
365 typedef struct das_real_array{
366  double* values;
367  size_t length;
368 } das_real_array;
369 
370 typedef struct das_creal_array{
371  const double* values;
372  size_t length;
373 } das_creal_array;
374 
375 typedef struct das_int_array{
376  int* values;
377  size_t length;
378 } das_int_array;
379 
380 typedef struct das_cint_array{
381  const int* values;
382  size_t length;
383 } das_cint_array;
384 */
385 
393 DAS_API double* das_csv2doubles(const char * s, int* nitems);
394 
395 
411 DAS_API char* das_doubles2csv(char* pBuf, size_t uBufSz, const double* pValues, int nValues);
412 
413 
415 DAS_API char* das_floats2csv(char* pBuf, size_t uBufSz, const float* pValues, int nValues);
416 
417 
418 #ifdef __cplusplus
419 }
420 #endif
421 
422 #endif /* _das_value_h_ */
423 
int DasErrCode
return code type 0 indicates success, negative integer indicates failure
Definition: defs.h:164
das_val_type
Enumeration of types stored in Das Array (DasAry) objects from value.h.
Definition: value.h:64
@ vtByte
Indicates array values are signed 8-bit integers (signed bytes)
Definition: value.h:78
@ vtGeoVec
Value are a vector struct as defined by vector.h.
Definition: value.h:133
@ vtShort
Indicates array values are signed 16-bit integers (shorts)
Definition: value.h:84
@ vtULong
Indicates array values are unsigned 64-bit unsigned integers (ulongs)
Definition: value.h:93
@ vtFloat
Indicates array values are 32-bit floating point values (floats)
Definition: value.h:99
@ vtInt
Indicates array values are signed 32-bit integers (ints)
Definition: value.h:90
@ vtUInt
Indicates array values are unsigned 32-bit integers (uints)
Definition: value.h:87
@ vtLong
Indicates array values are unsigned 64-bit integers (longs)
Definition: value.h:96
@ vtText
Indicates datum values are const char* pointers to null terminated UTF-8 strings.
Definition: value.h:130
@ vtUByte
The basic types.
Definition: value.h:75
@ vtUShort
Indicates array values are unsigned 16-bit integers (shorts)
Definition: value.h:81
@ vtUnknown
For generic storage, designates elements as unknown, you have to cast the array return values yoursel...
Definition: value.h:68
@ vtTime
Indicates array values are das_time_t structures.
Definition: value.h:105
@ vtByteSeq
Indicates values are size_t plus const ubyte* pairs, no more is known about the bytes.
Definition: value.h:137
@ vtDouble
Indicates array values are 64-bit floating point values (doubles)
Definition: value.h:102
DAS_API bool das_str2bool(const char *str, bool *pRes)
Convert a string value to a boolean value.
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.
int(* das_valcmp_func)(const ubyte *, const ubyte *)
Comparison functions look like this.
Definition: value.h:215
DAS_API double * das_csv2doubles(const char *s, int *nitems)
Parse a comma separated list of ASCII values into a double array.
DAS_API bool das_str2double(const char *str, double *pRes)
Convert a string value to a 8-byte float, similar to strtod(3).
DAS_API bool das_str2baseint(const char *str, int base, int *pRes)
Convert a string to an integer with explicit base and overflow checking.
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.
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.
DAS_API char * das_floats2csv(char *pBuf, size_t uBufSz, const float *pValues, int nValues)
Similar to das_doubles2csv, but for 32-bit floats.
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.
DAS_API das_valcmp_func das_vt_getcmp(das_val_type vt)
Get the comparison function for two values of this type.
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.
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.
DAS_API char * das_doubles2csv(char *pBuf, size_t uBufSz, const double *pValues, int nValues)
Print an array of doubles into a string buffer.