das2C
das core C utilities (v3)
Loading...
Searching...
No Matches
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 <das3/util.h>
27#include <das3/units.h>
28#include <das3/buffer.h>
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
35DAS_API double getDas2Fill(void);
36
38DAS_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 32
81
82
108typedef 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
143
180DAS_API DasEncoding* new_DasEncoding(int nCat, int nWidth, const char* sFmt);
181
182/* Das Encodings use value semantics */
183#define del_DasEncoding(pEnc) free(pEnc)
184
185#define DasEnc_isUtf8(pEnc) ((pEnc->nCat == DAS2DT_TIME)||(pEnc->nCat == DAS2DT_ASCII))
186
200DAS_API DasEncoding* new_DasEncoding_str(const char* sType);
201
202
205
206
214DAS_API bool DasEnc_equals(const DasEncoding* pOne, const DasEncoding* pTwo);
215
241DAS_API void DasEnc_setAsciiFormat(DasEncoding* pThis, const char* sValFmt,
242 int nFmtWidth);
243
244
279 DasEncoding* pThis, const char* sTimeFmt, int nFmtWidth
280);
281
282
283/* More explicit indication of a big-endian 8-byte number */
284#define DAS2DT_BE_REAL_8 0x0801
285
286/* little-endian (least significant byte first) 8-byte real */
287#define DAS2DT_LE_REAL_8 0x0802
288
289/* 8-byte real number, in host byte order */
290#ifdef HOST_IS_LSB_FIRST
291#define DAS2DT_DOUBLE 0x0802
292#else
293#define DAS2DT_DOUBLE 0x0801
294#endif
295
296/* More explicit indication of a big-endian 4-byte number */
297#define DAS2DT_BE_REAL_4 0x0401
298
299/* little-endian (least significant byte first) 4-byte real */
300#define DAS2DT_LE_REAL_4 0x0402
301
302
304#ifdef HOST_IS_LSB_FIRST
305#define DAS2DT_FLOAT 0x0402
306#else
307#define DAS2DT_FLOAT 0x0401
308#endif
309
310/* Legacy specific width encoding */
311#define DAS2DT_ASCII_10 0x0A03
312#define DAS2DT_ASCII_24 0x1804
313#define DAS2DT_ASCII_14 0x0E04
314
315#define DAS2DT_TIME_25 0x1904
316#define DAS2DT_TIME_28 0x1c04
317
318
349DAS_API unsigned int DasEnc_hash(const DasEncoding* pThis);
350
351
368DAS_API DasErrCode DasEnc_toStr(DasEncoding* pThis, char* sType, size_t nLen);
369
370
389DAS_API DasErrCode DasEnc_write(DasEncoding* pThis, DasBuf* pBuf, double value,
390 das_units units);
391
392
393/* (Not Implemented)
394 * Encode and write a value to a buffer.
395 *
396 * Similar to DasEnc_write except this version outputs to a DasBuf object.
397 *
398 * @param pThis the DasEncoding object to handle the translation
399 * @param pBuf the buffer to receive the encoded bytes
400 * @param value the numeric value to write
401 * @param units Handles scaling and offset of values if needed.
402 *
403 * @returns 0 on success, a positive error code on failure.
404 * @memberof DasEncoding
405 */
406/* ErrorCode DasEnc_encode(
407 DasEncoding* pThis, DasBuf* pBuf, double value, UnitType units
408); */
409
430 const DasEncoding* pThis, DasBuf* pBuf, das_units units, double* pOut
431);
432
433#ifdef __cplusplus
434}
435#endif
436
437#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:184
const char * das_units
Handle SI and other units, with accommodations for Epoch systems, from units.h.
Definition units.h:144
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 internal 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...