1 /* 2 Copyright (C) 2016-2021, Dirk Krause 3 SPDX-License-Identifier: BSD-3-Clause 4 */ 5 6 /* 7 WARNING: This file was generated by the dkct program (see 8 http://dktools.sourceforge.net/ for details). 9 Changes you make here will be lost if dkct is run again! 10 You should modify the original source and run dkct on it. 11 Original source: dk4strms.ctr 12 */ 13 14 #ifndef DK4STRMS_H_INCLUDED 15 /** Avoid multiple inclusions. */ 16 #define DK4STRMS_H_INCLUDED 1 17 18 19 /** @file dk4strms.h Serialize data to a stream. 20 21 The functions to serialize integers write the least significant byte 22 first. 23 24 When serializing a string, the string length is serialized first as 25 16 bit unsigned number, string contents follows. 26 The finalizing 0x00 byte is both counted in the string length and written 27 to the stream. 28 So a reader application can retrieve the length first, allocate a memory 29 block of exact size and read the string. Reader applications should 30 check/ensure the finalizing byte after reading string contents. 31 32 When serializing a double, the double is converted to a string and the 33 string is serialized to the stream. For 0.0 and values nearby 34 (absolute value is below a given limit) a string length 0 is serialized 35 to the stream instead, no string is written. 36 37 */ 38 39 40 #ifndef DK4CONF_H_INCLUDED 41 #if DK4_BUILDING_DKTOOLS4 42 #include "dk4conf.h" 43 #else 44 #include <dktools-4/dk4conf.h> 45 #endif 46 #endif 47 48 #ifndef DK4TYPES_H_INCLUDED 49 #if DK4_BUILDING_DKTOOLS4 50 #include <libdk4base/dk4types.h> 51 #else 52 #include <dktools-4/dk4types.h> 53 #endif 54 #endif 55 56 #ifndef DK4ERROR_H_INCLUDED 57 #if DK4_BUILDING_DKTOOLS4 58 #include <libdk4base/dk4error.h> 59 #else 60 #include <dktools-4/dk4error.h> 61 #endif 62 #endif 63 64 #ifndef DK4STRM_H_INCLUDED 65 #if DK4_BUILDING_DKTOOLS4 66 #include <libdk4c/dk4strm.h> 67 #else 68 #include <dktools-4/dk4strm.h> 69 #endif 70 #endif 71 72 73 #ifdef __cplusplus 74 extern "C" { 75 #endif 76 77 78 79 /** Serialize unsigned 32 bit integer to stream. 80 @param strm Destination stream. 81 @param u32 Value to serialize. 82 @param erp Error report, may be NULL. 83 @return 1 on success, 0 on error. 84 85 Error codes: 86 - DK4_E_INVALID_ARGUMENTS<br> 87 if strm is NULL or not opened for writing, 88 - DK4_E_WRITE_FAILED<br> 89 if writing one ore multiple bytes to the stream failed, 90 - DK4_E_FLUSH_FAILED<br> 91 if flusing data downwards failed. 92 */ 93 94 int 95 dk4stream_serialize_u32(dk4_stream_t *strm, uint32_t u32, dk4_er_t *erp); 96 97 98 /** Serialize signed 32 bit integer to stream. 99 @param strm Destination stream. 100 @param i32 Value to serialize. 101 @param erp Error report, may be NULL. 102 @return 1 on success, 0 on error. 103 104 Error codes: 105 - DK4_E_INVALID_ARGUMENTS<br> 106 if strm is NULL or not opened for writing, 107 - DK4_E_WRITE_FAILED<br> 108 if writing one ore multiple bytes to the stream failed, 109 - DK4_E_FLUSH_FAILED<br> 110 if flusing data downwards failed. 111 */ 112 113 int 114 dk4stream_serialize_i32(dk4_stream_t *strm, int32_t i32, dk4_er_t *erp); 115 116 117 /** Serialize unsigned 16 bit integer to stream. 118 @param strm Destination stream. 119 @param u16 Value to serialize. 120 @param erp Error report, may be NULL. 121 @return 1 on success, 0 on error. 122 123 Error codes: 124 - DK4_E_INVALID_ARGUMENTS<br> 125 if strm is NULL or not opened for writing, 126 - DK4_E_WRITE_FAILED<br> 127 if writing one ore multiple bytes to the stream failed, 128 - DK4_E_FLUSH_FAILED<br> 129 if flusing data downwards failed. 130 */ 131 132 int 133 dk4stream_serialize_u16(dk4_stream_t *strm, uint16_t u16, dk4_er_t *erp); 134 135 136 /** Serialize signed 16 bit integer to stream. 137 @param strm Destination stream. 138 @param i16 Value to serialize. 139 @param erp Error report, may be NULL. 140 @return 1 on success, 0 on error. 141 142 Error codes: 143 - DK4_E_INVALID_ARGUMENTS<br> 144 if strm is NULL or not opened for writing, 145 - DK4_E_WRITE_FAILED<br> 146 if writing one ore multiple bytes to the stream failed, 147 - DK4_E_FLUSH_FAILED<br> 148 if flusing data downwards failed. 149 */ 150 151 int 152 dk4stream_serialize_i16(dk4_stream_t *strm, int16_t i16, dk4_er_t *erp); 153 154 155 /** Serialize unsigned 8 bit integer to stream. 156 @param strm Destination stream. 157 @param u8 Value to serialize. 158 @param erp Error report, may be NULL. 159 @return 1 on success, 0 on error. 160 161 Error codes: 162 - DK4_E_INVALID_ARGUMENTS<br> 163 if strm is NULL or not opened for writing, 164 - DK4_E_WRITE_FAILED<br> 165 if writing one ore multiple bytes to the stream failed, 166 - DK4_E_FLUSH_FAILED<br> 167 if flusing data downwards failed. 168 */ 169 170 int 171 dk4stream_serialize_u8(dk4_stream_t *strm, uint8_t u8, dk4_er_t *erp); 172 173 174 /** Serialize signed 8 bit integer to stream. 175 @param strm Destination stream. 176 @param i8 Value to serialize. 177 @param erp Error report, may be NULL. 178 @return 1 on success, 0 on error. 179 180 Error codes: 181 - DK4_E_INVALID_ARGUMENTS<br> 182 if strm is NULL or not opened for writing, 183 - DK4_E_WRITE_FAILED<br> 184 if writing one ore multiple bytes to the stream failed, 185 - DK4_E_FLUSH_FAILED<br> 186 if flusing data downwards failed. 187 */ 188 189 int 190 dk4stream_serialize_i8(dk4_stream_t *strm, int8_t i8, dk4_er_t *erp); 191 192 193 /** Serialize string to stream. 194 First the string length including the 0x00 finalizer byte 195 is written as 16 bit unsigned integer, the string including the 196 finalizer byte follows. 197 By reading the length first, a reader application can allocate 198 a memory block of exact size before reading the string contents. 199 @param strm Destination stream. 200 @param str String to serialize, max 65534 characters long. 201 @param erp Error report, may be NULL. 202 @return 1 on success, 0 on error. 203 204 Error codes: 205 - DK4_E_INVALID_ARGUMENTS<br> 206 if strm is NULL or not opened for writing, 207 - DK4_E_BUFFER_TOO_SMALL<br> 208 if the str string is too long, 209 - DK4_E_WRITE_FAILED<br> 210 if writing one ore multiple bytes to the stream failed, 211 - DK4_E_FLUSH_FAILED<br> 212 if flusing data downwards failed. 213 */ 214 215 int 216 dk4stream_serialize_string(dk4_stream_t *strm, const char *str, dk4_er_t *erp); 217 218 219 /** Serialize floating point value to stream. 220 For non-0.0 values the value is converted to a string, the string 221 is serialized to the stream. For 0.0 and values near enough 222 (absolute value is below a given limit) a string length 0 is written 223 indicating the 0.0 value to reader applications. 224 @param strm Destination stream. 225 @param d Value to serialize. 226 @param e Epsilon for check whether d is 0. 227 @param erp Error report, may be NULL. 228 @return 1 on success, 0 on error. 229 */ 230 231 int 232 dk4stream_serialize_double(dk4_stream_t *strm,double d,double e,dk4_er_t *erp); 233 234 235 /** Serialize floating point value 0 in short form. 236 @param strm Stream to write to. 237 @param erp Error report, may be NULL. 238 */ 239 240 int 241 dk4stream_serialize_double_0(dk4_stream_t *strm, dk4_er_t *erp); 242 243 244 #ifdef __cplusplus 245 } 246 #endif 247 248 249 /* vim: set ai sw=4 ts=4 : */ 250 251 252 #endif 253