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#define DASENC_FMT_LEN 64
68#define DASENC_TYPE_LEN 32
69
70
96typedef struct das_encoding{
107 unsigned int nCat;
108
114 unsigned int nWidth;
115
121 char sFmt[DASENC_FMT_LEN];
122
128 char sType[DASENC_TYPE_LEN];
129
131
161DAS_API DasEncoding* new_DasEncoding(int nCat, int nWidth, const char* sFmt);
162
163/* Das Encodings use value semantics */
164#define del_DasEncoding(pEnc) free(pEnc)
165
166#define DasEnc_isUtf8(pEnc) ((pEnc->nCat == DAS2DT_TIME)||(pEnc->nCat == DAS2DT_ASCII))
167
181DAS_API DasEncoding* new_DasEncoding_str(const char* sType);
182
183
186
187
195DAS_API bool DasEnc_equals(const DasEncoding* pOne, const DasEncoding* pTwo);
196
222DAS_API void DasEnc_setAsciiFormat(DasEncoding* pThis, const char* sValFmt,
223 int nFmtWidth);
224
225
260 DasEncoding* pThis, const char* sTimeFmt, int nFmtWidth
261);
262
263
264/* More explicit indication of a big-endian 8-byte number */
265#define DAS2DT_BE_REAL_8 0x0801
266
267/* little-endian (least significant byte first) 8-byte real */
268#define DAS2DT_LE_REAL_8 0x0802
269
270/* 8-byte real number, in host byte order */
271#ifdef HOST_IS_LSB_FIRST
272#define DAS2DT_DOUBLE 0x0802
273#else
274#define DAS2DT_DOUBLE 0x0801
275#endif
276
277/* More explicit indication of a big-endian 4-byte number */
278#define DAS2DT_BE_REAL_4 0x0401
279
280/* little-endian (least significant byte first) 4-byte real */
281#define DAS2DT_LE_REAL_4 0x0402
282
283
285#ifdef HOST_IS_LSB_FIRST
286#define DAS2DT_FLOAT 0x0402
287#else
288#define DAS2DT_FLOAT 0x0401
289#endif
290
291/* Legacy specific width encoding */
292#define DAS2DT_ASCII_10 0x0A03
293#define DAS2DT_ASCII_24 0x1804
294#define DAS2DT_ASCII_14 0x0E04
295
296#define DAS2DT_TIME_25 0x1904
297#define DAS2DT_TIME_28 0x1c04
298
299
330DAS_API unsigned int DasEnc_hash(const DasEncoding* pThis);
331
332
350DAS_API char* DasEnc_toStr(const DasEncoding* pThis, char* sType, size_t uLen);
351
352
371DAS_API DasErrCode DasEnc_write(DasEncoding* pThis, DasBuf* pBuf, double value,
372 das_units units);
373
374
375/* (Not Implemented)
376 * Encode and write a value to a buffer.
377 *
378 * Similar to DasEnc_write except this version outputs to a DasBuf object.
379 *
380 * @param pThis the DasEncoding object to handle the translation
381 * @param pBuf the buffer to receive the encoded bytes
382 * @param value the numeric value to write
383 * @param units Handles scaling and offset of values if needed.
384 *
385 * @returns 0 on success, a positive error code on failure.
386 * @memberof DasEncoding
387 */
388/* ErrorCode DasEnc_encode(
389 DasEncoding* pThis, DasBuf* pBuf, double value, UnitType units
390); */
391
412 const DasEncoding* pThis, DasBuf* pBuf, das_units units, double* pOut
413);
414
415#ifdef __cplusplus
416}
417#endif
418
419#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:189
const char * das_units
Handle SI and other units, with accommodations for Epoch systems, from units.h.
Definition units.h:141
Buffer class to handle accumulating byte streams.
Definition buffer.h:47
Reading and writing values to buffers.
Definition encoding.h:96
DAS_API DasEncoding * new_DasEncoding(int nCat, int nWidth, const char *sFmt)
Make a new data encoder/decoder.
#define DasEnc_toStr(P, B, N)
Get a string representation of the data type.
unsigned int nCat
The basic encoding category.
Definition encoding.h:107
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:114
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...