das2C
das core C utilities (v3)
encoding.h
Go to the documentation of this file.
1 /* Copyright (C) 2015-2017 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 
18 
23 #ifndef _das_encoding_h_
24 #define _das_encoding_h_
25 
26 #include <das2/util.h>
27 #include <das2/units.h>
28 #include <das2/buffer.h>
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
35 DAS_API double getDas2Fill(void);
36 
38 DAS_API int isDas2Fill( double value );
39 
40 
41 /* Most Significant byte First (big-endian) IEEE-754 reals */
42 #define DAS2DT_BE_REAL 0x0001
43 
44 /* Most Significant byte last (little-endian) IEEE-754 reals */
45 #define DAS2DT_LE_REAL 0x0002
46 
47 #ifdef HOST_IS_LSB_FIRST
48 #define DAS2DT_HOST_REAL 0x0002
49 #else
50 #define DAS2DT_HOST_REAL 0x0001
51 #endif
52 
53 /* A real number formatted in some number of characters.
54  * Conventionally there are a whitespace characters to improve readability,
55  * but this is not required. The formatted number should be parsable by
56  * scanf in C, readf in IDL, or Double.parseDouble in java.
57  */
58 #define DAS2DT_ASCII 0x0003
59 
60 /* A date-time formatted as an ASCII ISO-8601 string.
61  * Actually any time that is parseable by the parsetime routine will work
62  * Generally if a human can read it, it's parseable. For example,
63  * YYYY-MM-DDThh:mm:ss.mmmZ.
64  */
65 #define DAS2DT_TIME 0x0004
66 
67 /* Most Significant byte First (big-endian) signed integers */
68 #define DAS2DT_BE_INT 0x0005
69 
70 /* Most Significant byte last (little-endian) signed integers */
71 #define DAS2DT_LE_INT 0x0006
72 
73 /* Most Significant byte First (big-endian) un-signed integers */
74 #define DAS2DT_BE_UINT 0x0007
75 
76 /* Most Significant byte last (little-endian) un-signed integers */
77 #define DAS2DT_LE_UINT 0x0008
78 
79 #define DASENC_FMT_LEN 64
80 #define DASENC_TYPE_LEN 48
81 
82 
108 typedef struct das_encoding{
119  unsigned int nCat;
120 
126  unsigned int nWidth;
127 
133  char sFmt[DASENC_FMT_LEN];
134 
140  char sType[DASENC_TYPE_LEN];
141 
142 } DasEncoding;
143 
180 DAS_API DasEncoding* new_DasEncoding(int nCat, int nWidth, const char* sFmt);
181 
182 /* Das Encondings use value semantics */
183 #define del_DasEncoding(pEnc) free(pEnc)
184 
198 DAS_API DasEncoding* new_DasEncoding_str(const char* sType);
199 
200 
203 
204 
212 DAS_API bool DasEnc_equals(const DasEncoding* pOne, const DasEncoding* pTwo);
213 
239 DAS_API void DasEnc_setAsciiFormat(DasEncoding* pThis, const char* sValFmt,
240  int nFmtWidth);
241 
242 
276 DAS_API void DasEnc_setTimeFormat(
277  DasEncoding* pThis, const char* sTimeFmt, int nFmtWidth
278 );
279 
280 
281 /* More explicit indication of a big-endian 8-byte number */
282 #define DAS2DT_BE_REAL_8 0x0801
283 
284 /* little-endian (least significant byte first) 8-byte real */
285 #define DAS2DT_LE_REAL_8 0x0802
286 
287 /* 8-byte real number, in host byte order */
288 #ifdef HOST_IS_LSB_FIRST
289 #define DAS2DT_DOUBLE 0x0802
290 #else
291 #define DAS2DT_DOUBLE 0x0801
292 #endif
293 
294 /* More explicit indication of a big-endian 4-byte number */
295 #define DAS2DT_BE_REAL_4 0x0401
296 
297 /* little-endian (least significant byte first) 4-byte real */
298 #define DAS2DT_LE_REAL_4 0x0402
299 
300 
302 #ifdef HOST_IS_LSB_FIRST
303 #define DAS2DT_FLOAT 0x0402
304 #else
305 #define DAS2DT_FLOAT 0x0401
306 #endif
307 
308 /* Legacy specific width encoding */
309 #define DAS2DT_ASCII_10 0x0A03
310 #define DAS2DT_ASCII_24 0x1804
311 #define DAS2DT_ASCII_14 0x0E04
312 
313 #define DAS2DT_TIME_25 0x1904
314 #define DAS2DT_TIME_28 0x1c04
315 
316 
347 DAS_API unsigned int DasEnc_hash(const DasEncoding* pThis);
348 
349 
366 DAS_API DasErrCode DasEnc_toStr(DasEncoding* pThis, char* sType, size_t nLen);
367 
368 
387 DAS_API DasErrCode DasEnc_write(DasEncoding* pThis, DasBuf* pBuf, double value,
388  das_units units);
389 
390 
391 /* (Not Implemented)
392  * Encode and write a value to a buffer.
393  *
394  * Similar to DasEnc_write except this version outputs to a DasBuf object.
395  *
396  * @param pThis the DasEncoding object to handle the translation
397  * @param pBuf the buffer to receive the encoded bytes
398  * @param value the numeric value to write
399  * @param units Handles scaling and offset of values if needed.
400  *
401  * @returns 0 on success, a positive error code on failure.
402  * @memberof DasEncoding
403  */
404 /* ErrorCode DasEnc_encode(
405  DasEncoding* pThis, DasBuf* pBuf, double value, UnitType units
406 ); */
407 
428  const DasEncoding* pThis, DasBuf* pBuf, das_units units, double* pOut
429 );
430 
431 #ifdef __cplusplus
432 }
433 #endif
434 
435 #endif /* _das_encoding_h_ */
Utility to assist with encode and decode operations.
DAS_API DasEncoding * DasEnc_copy(DasEncoding *pThis)
Deepcopy a DasEncoding pointer.
DAS_API bool DasEnc_equals(const DasEncoding *pOne, const DasEncoding *pTwo)
Check for equality between two encodings.
DAS_API int isDas2Fill(double value)
An inconvenient way to check for the canonical fill value, -1e31.
DAS_API double getDas2Fill(void)
An inconvenient way to get canonical fill value, -1e31.
int DasErrCode
return code type 0 indicates success, negative integer indicates failure
Definition: defs.h:164
const char * das_units
Handle SI and other units, with accommodations for Epoch systems, from units.h.
Definition: units.h:139
Buffer class to handle accumulating byte streams.
Definition: buffer.h:47
Reading and writing values to buffers.
Definition: encoding.h:108
DAS_API DasErrCode DasEnc_toStr(DasEncoding *pThis, char *sType, size_t nLen)
Get a string representation of the data type.
DAS_API DasEncoding * new_DasEncoding(int nCat, int nWidth, const char *sFmt)
Make a new data encoder/decoder.
unsigned int nCat
The basic encoding category.
Definition: encoding.h:119
DAS_API unsigned int DasEnc_hash(const DasEncoding *pThis)
Get a hash value suitable for use in switch statements.
DAS_API void DasEnc_setTimeFormat(DasEncoding *pThis, const char *sTimeFmt, int nFmtWidth)
Set the output format to be used when converting binary time values to to ASCII strings.
DAS_API DasEncoding * new_DasEncoding_str(const char *sType)
Create a new encoding based on the encoding type string.
unsigned int nWidth
The width in bytes of the encoded values.
Definition: encoding.h:126
DAS_API void DasEnc_setAsciiFormat(DasEncoding *pThis, const char *sValFmt, int nFmtWidth)
Set the output format to be used when converting interal binary values to ASCII strings.
DAS_API DasErrCode DasEnc_write(DasEncoding *pThis, DasBuf *pBuf, double value, das_units units)
Encode and write a value onto a string.
DAS_API DasErrCode DasEnc_read(const DasEncoding *pThis, DasBuf *pBuf, das_units units, double *pOut)
Read and Decode a value from a string.
Defines units used for items in the stream, most notably time units that reference an epoch and a ste...