gusimplewhiteboard
wb_particle_output_map.c
Go to the documentation of this file.
1/*
2 * file wb_particle_output_map.c
3 *
4 * This file was generated by classgenerator from particle_output_map.gen.
5 * DO NOT CHANGE MANUALLY!
6 *
7 * Copyright © 2021 Callum McColl. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer in the documentation and/or other materials
19 * provided with the distribution.
20 *
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgement:
23 *
24 * This product includes software developed by Callum McColl.
25 *
26 * 4. Neither the name of the author nor the names of contributors
27 * may be used to endorse or promote products derived from this
28 * software without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
34 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
35 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
36 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
37 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
38 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
39 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
40 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 *
42 * -----------------------------------------------------------------------
43 * This program is free software; you can redistribute it and/or
44 * modify it under the above terms or under the terms of the GNU
45 * General Public License as published by the Free Software Foundation;
46 * either version 2 of the License, or (at your option) any later version.
47 *
48 * This program is distributed in the hope that it will be useful,
49 * but WITHOUT ANY WARRANTY; without even the implied warranty of
50 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
51 * GNU General Public License for more details.
52 *
53 * You should have received a copy of the GNU General Public License
54 * along with this program; if not, see http://www.gnu.org/licenses/
55 * or write to the Free Software Foundation, Inc., 51 Franklin Street,
56 * Fifth Floor, Boston, MA 02110-1301, USA.
57 *
58 */
59
60#ifndef WHITEBOARD_POSTER_STRING_CONVERSION
61#define WHITEBOARD_POSTER_STRING_CONVERSION
62#endif // WHITEBOARD_POSTER_STRING_CONVERSION
63
65#include <stdio.h>
66#include <string.h>
67#include <stdlib.h>
68#include <ctype.h>
69#include <limits.h>
70
71/* Network byte order functions */
72#pragma clang diagnostic push
73#pragma clang diagnostic ignored "-Wunused-macros"
74#if defined(__linux)
75# include <endian.h>
76# include <byteswap.h>
77#elif defined(__APPLE__)
78# include <machine/endian.h> //Needed for __BYTE_ORDER
79# include <architecture/byte_order.h> //Needed for byte swap functions
80# define bswap_16(x) NXSwapShort(x)
81# define bswap_32(x) NXSwapInt(x)
82# define bswap_64(x) NXSwapLongLong(x)
83#elif defined(ESP8266)
84# define bswap_16(x) __builtin_bswap16(x)
85# define bswap_32(x) __builtin_bswap32(x)
86# define bswap_64(x) __builtin_bswap64(x)
87#else
88 //Manually define swap macros?
89#endif
90
91#if (!defined(__BYTE_ORDER) && !defined(__LITTLE_ENDIAN)) || (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && __BYTE_ORDER == __LITTLE_ENDIAN)
92# if !defined(htonll) && !defined(ntohll)
93# define htonll(x) bswap_64(x)
94# define ntohll(x) bswap_64(x)
95# endif
96# if !defined(htonl) && !defined(ntohl)
97# define htonl(x) bswap_32(x)
98# define ntohl(x) bswap_32(x)
99# endif
100# if !defined(htons) && !defined(ntohs)
101# define htons(x) bswap_16(x)
102# define ntohs(x) bswap_16(x)
103# endif
104#else
105# if !defined(htonll) && !defined(ntohll)
106# define htonll(x) (x)
107# define ntohll(x) (x)
108# endif
109# if !defined(htonl) && !defined(ntohl)
110# define htonl(x) (x)
111# define ntohl(x) (x)
112# endif
113# if !defined(htons) && !defined(ntohs)
114# define htons(x) (x)
115# define ntohs(x) (x)
116# endif
117#endif
118#pragma clang diagnostic pop
119
120
121
125const char* wb_particle_output_map_description(const struct wb_particle_output_map* self, char* descString, size_t bufferSize)
126{
127#pragma clang diagnostic push
128#pragma clang diagnostic ignored "-Wunused-variable"
129 size_t len = 0;
130 if (len >= bufferSize) {
131 return descString;
132 }
133 len += snprintf(descString + len, bufferSize - len, "fileId=%u", self->fileId);
134 if (len >= bufferSize) {
135 return descString;
136 }
137 len = gu_strlcat(descString, ", ", bufferSize);
138 if (len >= bufferSize) {
139 return descString;
140 }
141 len += snprintf(descString + len, bufferSize - len, "filePath=%s", self->filePath);
142 return descString;
143#pragma clang diagnostic pop
144}
145
149const char* wb_particle_output_map_to_string(const struct wb_particle_output_map* self, char* toString, size_t bufferSize)
150{
151#pragma clang diagnostic push
152#pragma clang diagnostic ignored "-Wunused-variable"
153 size_t len = 0;
154 if (len >= bufferSize) {
155 return toString;
156 }
157 len += snprintf(toString + len, bufferSize - len, "%u", self->fileId);
158 if (len >= bufferSize) {
159 return toString;
160 }
161 len = gu_strlcat(toString, ", ", bufferSize);
162 if (len >= bufferSize) {
163 return toString;
164 }
165 len += snprintf(toString + len, bufferSize - len, "%s", self->filePath);
166 return toString;
167#pragma clang diagnostic pop
168}
169
174{
175 size_t temp_length = strlen(str);
176 int length = (temp_length <= INT_MAX) ? ((int)((ssize_t)temp_length)) : -1;
177 if (length < 1 || length > PARTICLE_OUTPUT_MAP_DESC_BUFFER_SIZE) {
178 return self;
179 }
180 char var_str_buffer[PARTICLE_OUTPUT_MAP_DESC_BUFFER_SIZE + 1];
181 char* var_str = &var_str_buffer[0];
182 char key_buffer[9];
183 char* key = &key_buffer[0];
184 int bracecount = 0;
185 int startVar = 0;
186 int index = 0;
187 int startKey = 0;
188 int endKey = -1;
189 int varIndex = 0;
190 if (index == 0 && str[0] == '{') {
191 index = 1;
192 }
193 startVar = index;
194 startKey = startVar;
195 do {
196 for (int i = index; i < length; i++) {
197 index = i + 1;
198 if (bracecount == 0 && str[i] == '=') {
199 endKey = i - 1;
200 startVar = index;
201 continue;
202 }
203 if (bracecount == 0 && isspace(str[i])) {
204 startVar = index;
205 if (endKey == -1) {
206 startKey = index;
207 }
208 continue;
209 }
210 if (bracecount == 0 && str[i] == ',') {
211 index = i - 1;
212 break;
213 }
214 if (str[i] == '{') {
215 bracecount++;
216 continue;
217 }
218 if (str[i] == '}') {
219 bracecount--;
220 if (bracecount < 0) {
221 index = i - 1;
222 break;
223 }
224 }
225 if (i == length - 1) {
226 index = i;
227 }
228 }
229 if (endKey >= startKey && endKey - startKey < length) {
230 strncpy(key, str + startKey, ((size_t)(endKey - startKey) + 1));
231 key[(endKey - startKey) + 1] = 0;
232 } else {
233 key[0] = 0;
234 }
235 strncpy(var_str, str + startVar, ((size_t)(index - startVar) + 1));
236 var_str[(index - startVar) + 1] = 0;
237 bracecount = 0;
238 index += 2;
239 startVar = index;
240 startKey = startVar;
241 endKey = -1;
242 if (strlen(key) > 0) {
243 if (0 == strcmp("fileId", key)) {
244 varIndex = 0;
245 } else if (0 == strcmp("filePath", key)) {
246 varIndex = 1;
247 } else {
248 varIndex = -1;
249 }
250 }
251 switch (varIndex) {
252 case -1: { break; }
253 case 0:
254 {
255 self->fileId = ((uint8_t)atoi(var_str));
256 break;
257 }
258 case 1:
259 {
260 strncpy(self->filePath, var_str, 64);
261 break;
262 }
263 }
264 if (varIndex >= 0) {
265 varIndex++;
266 }
267 } while(index < length);
268 return self;
269}
270
271/*#ifdef WHITEBOARD_SERIALISATION*/
272
277{
278 uint16_t bit_offset = 0;
279 uint8_t fileId_nbo = (self->fileId);
280 do {
281 int8_t b;
282 for (b = (8 - 1); b >= 0; b--) {
283 do {
284 uint16_t byte = bit_offset / 8;
285 uint16_t bit = 7 - (bit_offset % 8);
286 unsigned long newbit = !!((fileId_nbo >> b) & 1U);
287 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
288 bit_offset = bit_offset + 1;
289 } while(false);
290 }
291 } while(false);
292
293 do { //limit declaration scope
294 uint8_t len = (uint8_t) strlen(self->filePath);
295 int8_t b;
296 for (b = 7; b >= 0; b--) {
297 do {
298 uint16_t byte = bit_offset / 8;
299 uint16_t bit = 7 - (bit_offset % 8);
300 unsigned long newbit = !!((len >> b) & 1U);
301 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
302 bit_offset = bit_offset + 1;
303 } while(false);
304 }
305 uint8_t c;
306 for (c = 0; c < len; c++) {
307 for (b = 7; b >= 0; b--) {
308 do {
309 uint16_t byte = bit_offset / 8;
310 uint16_t bit = 7 - (bit_offset % 8);
311 unsigned long newbit = !!((self->filePath[c] >> b) & 1U);
312 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
313 bit_offset = bit_offset + 1;
314 } while(false);
315 }
316 }
317 } while(false);
318 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
319 (void)self;
320 (void)dst;
321 return bit_offset;
322}
323
328{
329 uint16_t bit_offset = 0;
330 do {
331 int8_t b;
332 for (b = (8 - 1); b >= 0; b--) {
333 do {
334 uint16_t byte = bit_offset / 8;
335 uint16_t bit = 7 - (bit_offset % 8);
336 char dataByte = src[byte];
337 unsigned char bitValue = (dataByte >> bit) & 1U;
338 dst->fileId ^= (-bitValue ^ dst->fileId) & (1UL << b);
339 bit_offset = bit_offset + 1;
340 } while(false);
341 }
342 } while(false);
343 dst->fileId = (dst->fileId);
344
345 do {
346 uint8_t len = 0;
347 int8_t b;
348 for (b = 7; b >= 0; b--) {
349 do {
350 uint16_t byte = bit_offset / 8;
351 uint16_t bit = 7 - (bit_offset % 8);
352 char dataByte = src[byte];
353 unsigned char bitValue = (dataByte >> bit) & 1U;
354 len ^= (-bitValue ^ len) & (1UL << b);
355 bit_offset = bit_offset + 1;
356 } while(false);
357 }
358 uint8_t c;
359 for (c = 0; c < len; c++) {
360 for (b = 7; b >= 0; b--) {
361 do {
362 uint16_t byte = bit_offset / 8;
363 uint16_t bit = 7 - (bit_offset % 8);
364 char dataByte = src[byte];
365 unsigned char bitValue = (dataByte >> bit) & 1U;
366 dst->filePath[c] ^= (-bitValue ^ dst->filePath[c]) & (1UL << b);
367 bit_offset = bit_offset + 1;
368 } while(false);
369 }
370 }
371 } while (false);
372 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
373 (void)src;
374 (void)dst;
375 return bit_offset;
376}
377
378/*#endif // WHITEBOARD_SERIALISATION*/
WHITEBOARD_POSTER_STRING_CONVERSION.
uint8_t fileId
The id of the file.
char filePath[64]
The path to the file containing the list of particles.
size_t wb_particle_output_map_from_network_serialised(const char *src, struct wb_particle_output_map *dst)
Convert from a compressed, serialised, network byte order byte stream.
const char * wb_particle_output_map_description(const struct wb_particle_output_map *self, char *descString, size_t bufferSize)
Convert to a description string.
size_t wb_particle_output_map_to_network_serialised(const struct wb_particle_output_map *self, char *dst)
Convert to a compressed, serialised, network byte order byte stream.
struct wb_particle_output_map * wb_particle_output_map_from_string(struct wb_particle_output_map *self, const char *str)
Convert from a string.
const char * wb_particle_output_map_to_string(const struct wb_particle_output_map *self, char *toString, size_t bufferSize)
Convert to a string.
#define PARTICLE_OUTPUT_MAP_DESC_BUFFER_SIZE