das2C
das core C utilities (v3)
Loading...
Searching...
No Matches
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 <das3/util.h>
23#include <das3/units.h> /* das_sem_default needs das_units to read calendar reps */
24
25#ifndef _das_value_h_
26#define _das_value_h_
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
37#define DAS_FILL_VALUE -1e31
38
40#define DAS_INT64_FILL -0x7FFFFFFFFFFFFFFFL
41#define DAS_INT32_FILL -0x7FFFFFFF
42
43
48typedef struct das_byte_seq_t{
49 ubyte* ptr;
50 size_t sz;
51} das_byte_seq;
52
53typedef struct das_const_byte_seq_t{
54 const ubyte* ptr;
55 size_t sz;
56} das_cbyte_seq;
57
59#define DAS_BS_NULL ((das_byte_seq){NULL, 0})
60#define DAS_CBS_NULL ((das_cbyte_seq){NULL, 0})
61
62
63#define VT_MIN_SIMPLE vtUByte
64#define VT_MAX_SIMPLE vtTime
65
66/* The types a das_datum stores in its own bytes, as against the ones it can
67 only box a pointer to. A contiguous range because the enum is ordered that
68 way on purpose: everything from vtText up reaches outside itself.
69 See das_datum_islocal() in datum.h. */
70#define VT_MIN_LOCAL vtUByte
71#define VT_MAX_LOCAL vtIndex
72
81typedef enum das_val_type_e {
82
86
89 /* VT_MIN_SIMPLE = vtUByte */
90
93
95 vtByte = 2,
96
99
102
105
107 vtInt = 6,
108
111
114
117
120
122 vtTime = 11,
123
124 /* VT_MAX_SIMPLE = vtTime */
125
126 /* The following type is not used by datums, but by array indexing elements
127 * that track the size and location of child dimensions */
128 vtIndex = 12,
129
130 /* The following two types are only used by variables and datums
131 *
132 * When generating Datums from Arrays:
133 *
134 * - If the array element type is etUnknown then vtByteSeq is used, size
135 * is element size.
136 *
137 * - If the array element type is etByte and the D2ARY_AS_STRING flag is
138 * set then vtText is used.
139 *
140 * - If the array element type is anything else and D2ARY_AS_SUBSEQ is
141 * set then vtByteSeq is used, size is element size times the size of
142 * the fastest moving index the location read.
143 */
144
147 vtText = 13,
148
150 /* Include later: vtPixel = 14, */
151
155
161 vtComposite = 16
162
164
165/* What one cell of a composite item holds.
166 *
167 * This enum is a subset of what das_val_type can describe.
168 *
169 * Note there is no element for affine time. A TT2000 point-time is stored as a
170 * plain etLong, and the affine rule rides as a formalism in form.h, not as an
171 * element type. The bits of a time and a count are identical; only the
172 * composition rules differ, and rules are not storage. The one struct time
173 * that IS an element is etTime, the das_time broken-down calendar wart. */
174typedef enum das_elem_type_e {
175 etUnknown = 0, /* == vtUnknown */
176 etUByte = 1, /* == vtUByte, also the byte-run cell of a <bytes> */
177 etByte = 2, /* == vtByte */
178 etUShort = 3, /* == vtUShort */
179 etShort = 4, /* == vtShort */
180 etUInt = 5, /* == vtUInt */
181 etInt = 6, /* == vtInt */
182 etULong = 7, /* == vtULong */
183 etLong = 8, /* == vtLong. a TT2000 point-time is stored here */
184 etFloat = 9, /* == vtFloat */
185 etDouble = 10, /* == vtDouble */
186 etTime = 11 /* == vtTime. das_time struct, broken-down-time wart */
187} das_elem_type;
188
191DAS_API bool das_elem_asDouble(das_elem_type et, const ubyte* p, double* pOut);
192
193
202#ifndef _das_value_c
203extern const char* DAS_ENC_BYTE;
204extern const char* DAS_ENC_UBYTE;
205extern const char* DAS_ENC_BEINT;
206extern const char* DAS_ENC_BEUINT;
207extern const char* DAS_ENC_BEREAL;
208extern const char* DAS_ENC_LEINT;
209extern const char* DAS_ENC_LEUINT;
210extern const char* DAS_ENC_LEREAL;
211extern const char* DAS_ENC_UTF8;
212extern const char* DAS_ENC_BLOB;
213extern const char* DAS_ENC_BASE64;
214#endif
215
228const char* das_enc_fromStr(const char* sEncType);
229
230
237#ifndef _das_value_c
238extern const char* DAS_SEM_BLOB;
239extern const char* DAS_SEM_BOOL;
240extern const char* DAS_SEM_DATE;
241extern const char* DAS_SEM_INT;
242extern const char* DAS_SEM_REAL;
243extern const char* DAS_SEM_TEXT;
244#endif
245
261
263das_val_type das_vt_default(const char* sSemantic);
264
285const char* das_sem_fromStr(const char* sSemantic);
286
311DAS_API das_val_type das_vt_store_type(
312 const char* sEncType, int nItemBytes, const char* sInterp
313);
314
318DAS_API const char* das_vt_serial_type(das_val_type et);
319
323#define das_vt_isint(VT) ( VT >= vtUByte && VT <= vtLong )
324
328#define das_vt_isreal(VT) ( VT == vtFloat || VT == vtDouble )
329
335#define das_vt_rank(VT) ( ((VT==vtText)||(VT==vtByteSeq)) ? 1:0)
336
337
344DAS_API const void* das_vt_fill(das_val_type vt);
345
350DAS_API size_t das_vt_size(das_val_type vt);
351
355DAS_API const char* das_vt_toStr(das_val_type vt);
356
360DAS_API das_val_type das_vt_fromStr(const char* sType);
361
362
363/* * Given a string and it's expected interpretation, return a suitable storage type
364 */
365/* DAS_API das_val_type das_vt_guess_store(const char* sInterp, const char* sValue); */
366
368typedef int (*das_valcmp_func)(const ubyte*, const ubyte*);
369
372
389 const ubyte* pA, das_val_type vtA, const ubyte* pB, das_val_type vtB
390);
391
392
393#define das_vt_cmpAny das_value_cmpAny
394
395/* In the future the token ID will come from the lexer, for now just make
396 * something up*/
397#define D2OP_PLUS 100
398
426
427#define DAS_VAL_NOERR_RNG 0x1
428#define DAS_VAL_ERR_RESLOSS 0x2
452 das_val_type vtIn, const ubyte* pValIn, const ubyte* pFillIn,
453 das_val_type vtOut, ubyte* pValOut, const ubyte* pFillOut,
454 uint32_t uFlags
455);
456
517DAS_API DasErrCode das_value_binop(
518 int nOp, das_val_type vtL, const ubyte* pL, das_val_type vtR,
519 const ubyte* pR, das_val_type vtOut, ubyte* pOut
520);
521
522DAS_API DasErrCode das_value_accum(
523 das_val_type vt, ubyte* pAccum, const ubyte* pStep, ptrdiff_t nCount
524);
525
544 char* sBuf, int nBufLen, das_val_type vt, const char* sSemantic, int nFitTo
545);
546
554DAS_API void das_value_trimReal(char* sVal);
555
563 ubyte* pBuf, int uBufLen, das_val_type vt, const char* sStr
564);
565
566
578DAS_API bool das_str2double(const char* str, double* pRes);
579
580
593DAS_API bool das_str2int(const char* str, int* pRes);
594
605DAS_API bool das_str2bool(const char* str, bool* pRes);
606
622DAS_API bool das_str2baseint(const char* str, int base, int* pRes);
623
646DAS_API bool das_strn2baseint(const char* str, int nLen, int base, int* pRes);
647
648/* Don't think these are used anywhere
649typedef struct das_real_array{
650 double* values;
651 size_t length;
652} das_real_array;
653
654typedef struct das_creal_array{
655 const double* values;
656 size_t length;
657} das_creal_array;
658
659typedef struct das_int_array{
660 int* values;
661 size_t length;
662} das_int_array;
663
664typedef struct das_cint_array{
665 const int* values;
666 size_t length;
667} das_cint_array;
668*/
669
677DAS_API double* das_csv2doubles(const char * s, int* nitems);
678
679
695DAS_API char* das_doubles2csv(char* pBuf, size_t uBufSz, const double* pValues, int nValues);
696
697
699DAS_API char* das_floats2csv(char* pBuf, size_t uBufSz, const float* pValues, int nValues);
700
701
702#ifdef __cplusplus
703}
704#endif
705
706#endif /* _das_value_h_ */
707
int DasErrCode
return code type 0 indicates success, negative integer indicates failure
Definition defs.h:184
const char * das_sem_default(das_val_type vt, das_units units)
Suggest a default semantic for a (value type, units) pair.
const char * das_units
Handle SI and other units, with accommodations for Epoch systems, from units.h.
Definition units.h:144
const char * das_sem_fromStr(const char *sSemantic)
Resolve a semantic string to its canonical schema value.
const char * DAS_ENC_BYTE
The canonical wire-encoding vocabulary: the closed set that IS the schema's EncodingType pattern (das...
das_val_type das_vt_default(const char *sSemantic)
Given a semantic meaning, suggest a default value type.
const char * das_enc_fromStr(const char *sEncType)
Resolve a wire-encoding string to its canonical schema value.
das_val_type
Enumeration of types stored in Das Array (DasAry) objects from value.h.
Definition value.h:81
const char * DAS_SEM_BLOB
The canonical value-semantic vocabulary: the closed set that IS the schema's Semantic pattern (das-ba...
@ vtByte
Indicates array values are signed 8-bit integers (signed bytes)
Definition value.h:95
@ vtShort
Indicates array values are signed 16-bit integers (shorts)
Definition value.h:101
@ vtULong
Indicates array values are unsigned 64-bit unsigned integers (ulongs)
Definition value.h:110
@ vtFloat
Indicates array values are 32-bit floating point values (floats)
Definition value.h:116
@ vtInt
Indicates array values are signed 32-bit integers (ints)
Definition value.h:107
@ vtComposite
These values are run of simple elements plus the DasForm that says what they mean: a rotation,...
Definition value.h:161
@ vtUInt
Indicates array values are unsigned 32-bit integers (uints)
Definition value.h:104
@ vtLong
Indicates array values are unsigned 64-bit integers (longs)
Definition value.h:113
@ vtText
Indicates datum values are const char* pointers to null terminated UTF-8 strings.
Definition value.h:147
@ vtUByte
The basic types.
Definition value.h:92
@ vtUShort
Indicates array values are unsigned 16-bit integers (shorts)
Definition value.h:98
@ vtUnknown
For generic storage, designates elements as unknown, you have to cast the array return values yoursel...
Definition value.h:85
@ vtTime
Indicates array values are das_time_t structures.
Definition value.h:122
@ vtByteSeq
Values are a picture element, possibly in multiple planes.
Definition value.h:154
@ vtDouble
Indicates array values are 64-bit floating point values (doubles)
Definition value.h:119
Defines units used for items in the stream, most notably time units that reference an epoch and a ste...
DAS_API bool das_str2bool(const char *str, bool *pRes)
Convert a string value to a boolean value.
DAS_API DasErrCode das_value_fmt(char *sBuf, int nBufLen, das_val_type vt, const char *sSemantic, int nFitTo)
Generate a printf style format code for a value type, usage and buffer size.
DAS_API char * das_doubles2csv(char *pBuf, size_t uBufSz, const double *pValues, int nValues)
Print an array of doubles into a string buffer.
int(* das_valcmp_func)(const ubyte *, const ubyte *)
Comparison functions look like this.
Definition value.h:368
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 void das_value_trimReal(char *sVal)
Small helper for printing reals in less space.
DAS_API das_valcmp_func das_vt_getcmp(das_val_type vt)
Get the comparison function for two values of this type.
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_value_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_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 DasErrCode das_value_binXform(das_val_type vtIn, const ubyte *pValIn, const ubyte *pFillIn, das_val_type vtOut, ubyte *pValOut, const ubyte *pFillOut, uint32_t uFlags)
Convert any integral type to any other with range checking and swapping.
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_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.