gusimplewhiteboard
wb_line_sighting.c
Go to the documentation of this file.
1/*
2 * file wb_line_sighting.c
3 *
4 * This file was generated by classgenerator from line_sighting.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
64#include "wb_line_sighting.h"
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_line_sighting_description(const struct wb_line_sighting* 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 = gu_strlcat(descString, "leftPoint={", bufferSize);
134 if (len >= bufferSize) {
135 return descString;
136 }
137 char leftPoint_buffer[POLAR_COORDINATE_DESC_BUFFER_SIZE];
138 char* leftPoint_p = leftPoint_buffer;
139 const char* leftPoint_description = wb_polar_coordinate_description(&self->leftPoint, leftPoint_p, POLAR_COORDINATE_DESC_BUFFER_SIZE);
140 len = gu_strlcat(descString, leftPoint_p, bufferSize);
141 if (len >= bufferSize) {
142 return descString;
143 }
144 len = gu_strlcat(descString, "}", bufferSize);
145 if (len >= bufferSize) {
146 return descString;
147 }
148 len = gu_strlcat(descString, ", ", bufferSize);
149 if (len >= bufferSize) {
150 return descString;
151 }
152 len = gu_strlcat(descString, "rightPoint={", bufferSize);
153 if (len >= bufferSize) {
154 return descString;
155 }
156 char rightPoint_buffer[POLAR_COORDINATE_DESC_BUFFER_SIZE];
157 char* rightPoint_p = rightPoint_buffer;
158 const char* rightPoint_description = wb_polar_coordinate_description(&self->rightPoint, rightPoint_p, POLAR_COORDINATE_DESC_BUFFER_SIZE);
159 len = gu_strlcat(descString, rightPoint_p, bufferSize);
160 if (len >= bufferSize) {
161 return descString;
162 }
163 len = gu_strlcat(descString, "}", bufferSize);
164 if (len >= bufferSize) {
165 return descString;
166 }
167 len = gu_strlcat(descString, ", ", bufferSize);
168 if (len >= bufferSize) {
169 return descString;
170 }
171 switch (self->sightingType) {
173 {
174 len += snprintf(descString + len, bufferSize - len, "sightingType=FieldLineSightingType");
175 break;
176 }
178 {
179 len += snprintf(descString + len, bufferSize - len, "sightingType=HorizonLineSightingType");
180 break;
181 }
182 }
183 return descString;
184#pragma clang diagnostic pop
185}
186
190const char* wb_line_sighting_to_string(const struct wb_line_sighting* self, char* toString, size_t bufferSize)
191{
192#pragma clang diagnostic push
193#pragma clang diagnostic ignored "-Wunused-variable"
194 size_t len = 0;
195 if (len >= bufferSize) {
196 return toString;
197 }
198 len = gu_strlcat(toString, "{", bufferSize);
199 if (len >= bufferSize) {
200 return toString;
201 }
202 char leftPoint_buffer[POLAR_COORDINATE_TO_STRING_BUFFER_SIZE];
203 char* leftPoint_p = leftPoint_buffer;
204 const char* leftPoint_to_string = wb_polar_coordinate_to_string(&self->leftPoint, leftPoint_p, POLAR_COORDINATE_TO_STRING_BUFFER_SIZE);
205 len = gu_strlcat(toString, leftPoint_p, bufferSize);
206 if (len >= bufferSize) {
207 return toString;
208 }
209 len = gu_strlcat(toString, "}", bufferSize);
210 if (len >= bufferSize) {
211 return toString;
212 }
213 len = gu_strlcat(toString, ", ", bufferSize);
214 if (len >= bufferSize) {
215 return toString;
216 }
217 len = gu_strlcat(toString, "{", bufferSize);
218 if (len >= bufferSize) {
219 return toString;
220 }
221 char rightPoint_buffer[POLAR_COORDINATE_TO_STRING_BUFFER_SIZE];
222 char* rightPoint_p = rightPoint_buffer;
223 const char* rightPoint_to_string = wb_polar_coordinate_to_string(&self->rightPoint, rightPoint_p, POLAR_COORDINATE_TO_STRING_BUFFER_SIZE);
224 len = gu_strlcat(toString, rightPoint_p, bufferSize);
225 if (len >= bufferSize) {
226 return toString;
227 }
228 len = gu_strlcat(toString, "}", bufferSize);
229 if (len >= bufferSize) {
230 return toString;
231 }
232 len = gu_strlcat(toString, ", ", bufferSize);
233 if (len >= bufferSize) {
234 return toString;
235 }
236 switch (self->sightingType) {
238 {
239 len += snprintf(toString + len, bufferSize - len, "FieldLineSightingType");
240 break;
241 }
243 {
244 len += snprintf(toString + len, bufferSize - len, "HorizonLineSightingType");
245 break;
246 }
247 }
248 return toString;
249#pragma clang diagnostic pop
250}
251
256{
257 size_t temp_length = strlen(str);
258 int length = (temp_length <= INT_MAX) ? ((int)((ssize_t)temp_length)) : -1;
259 if (length < 1 || length > LINE_SIGHTING_DESC_BUFFER_SIZE) {
260 return self;
261 }
262 char var_str_buffer[LINE_SIGHTING_DESC_BUFFER_SIZE + 1];
263 char* var_str = &var_str_buffer[0];
264 char key_buffer[13];
265 char* key = &key_buffer[0];
266 int bracecount = 0;
267 int startVar = 0;
268 int index = 0;
269 int startKey = 0;
270 int endKey = -1;
271 int varIndex = 0;
272 if (index == 0 && str[0] == '{') {
273 index = 1;
274 }
275 startVar = index;
276 startKey = startVar;
277 do {
278 for (int i = index; i < length; i++) {
279 index = i + 1;
280 if (bracecount == 0 && str[i] == '=') {
281 endKey = i - 1;
282 startVar = index;
283 continue;
284 }
285 if (bracecount == 0 && isspace(str[i])) {
286 startVar = index;
287 if (endKey == -1) {
288 startKey = index;
289 }
290 continue;
291 }
292 if (bracecount == 0 && str[i] == ',') {
293 index = i - 1;
294 break;
295 }
296 if (str[i] == '{') {
297 bracecount++;
298 continue;
299 }
300 if (str[i] == '}') {
301 bracecount--;
302 if (bracecount < 0) {
303 index = i - 1;
304 break;
305 }
306 }
307 if (i == length - 1) {
308 index = i;
309 }
310 }
311 if (endKey >= startKey && endKey - startKey < length) {
312 strncpy(key, str + startKey, ((size_t)(endKey - startKey) + 1));
313 key[(endKey - startKey) + 1] = 0;
314 } else {
315 key[0] = 0;
316 }
317 strncpy(var_str, str + startVar, ((size_t)(index - startVar) + 1));
318 var_str[(index - startVar) + 1] = 0;
319 bracecount = 0;
320 index += 2;
321 startVar = index;
322 startKey = startVar;
323 endKey = -1;
324 if (strlen(key) > 0) {
325 if (0 == strcmp("leftPoint", key)) {
326 varIndex = 0;
327 } else if (0 == strcmp("rightPoint", key)) {
328 varIndex = 1;
329 } else if (0 == strcmp("sightingType", key)) {
330 varIndex = 2;
331 } else {
332 varIndex = -1;
333 }
334 }
335 switch (varIndex) {
336 case -1: { break; }
337 case 0:
338 {
339 wb_polar_coordinate_from_string(&self->leftPoint, var_str);
340 break;
341 }
342 case 1:
343 {
344 wb_polar_coordinate_from_string(&self->rightPoint, var_str);
345 break;
346 }
347 case 2:
348 {
349 if (strcmp("FieldLineSightingType", var_str) == 0) {
350#pragma clang diagnostic push
351#pragma clang diagnostic ignored "-Wbad-function-cast"
352 self->sightingType = FieldLineSightingType;
353#pragma clang diagnostic pop
354 } else if (strcmp("HorizonLineSightingType", var_str) == 0) {
355#pragma clang diagnostic push
356#pragma clang diagnostic ignored "-Wbad-function-cast"
357 self->sightingType = HorizonLineSightingType;
358#pragma clang diagnostic pop
359 } else {
360#pragma clang diagnostic push
361#pragma clang diagnostic ignored "-Wbad-function-cast"
362 self->sightingType = ((enum LineSightingType)atoi(var_str));
363#pragma clang diagnostic pop
364 }
365 break;
366 }
367 }
368 if (varIndex >= 0) {
369 varIndex++;
370 }
371 } while(index < length);
372 return self;
373}
374
375/*#ifdef WHITEBOARD_SERIALISATION*/
376
380size_t wb_line_sighting_to_network_serialised(const struct wb_line_sighting *self, char *dst)
381{
382 uint16_t bit_offset = 0;
383 enum LineSightingType sightingType_nbo = htonl(self->sightingType);
384 do {
385 int8_t b;
386 for (b = (32 - 1); b >= 0; b--) {
387 do {
388 uint16_t byte = bit_offset / 8;
389 uint16_t bit = 7 - (bit_offset % 8);
390 unsigned long newbit = !!((sightingType_nbo >> b) & 1U);
391 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
392 bit_offset = bit_offset + 1;
393 } while(false);
394 }
395 } while(false);
396 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
397 (void)self;
398 (void)dst;
399 return bit_offset;
400}
401
406{
407 uint16_t bit_offset = 0;
408 do {
409 int8_t b;
410 for (b = (32 - 1); b >= 0; b--) {
411 do {
412 uint16_t byte = bit_offset / 8;
413 uint16_t bit = 7 - (bit_offset % 8);
414 char dataByte = src[byte];
415 unsigned char bitValue = (dataByte >> bit) & 1U;
416 dst->sightingType ^= (-bitValue ^ dst->sightingType) & (1UL << b);
417 bit_offset = bit_offset + 1;
418 } while(false);
419 }
420 } while(false);
421 dst->sightingType = ntohl(dst->sightingType);
422 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
423 (void)src;
424 (void)dst;
425 return bit_offset;
426}
427
428/*#endif // WHITEBOARD_SERIALISATION*/
WHITEBOARD_POSTER_STRING_CONVERSION.
enum LineSightingType sightingType
The type of sighting.
const char * wb_line_sighting_description(const struct wb_line_sighting *self, char *descString, size_t bufferSize)
Convert to a description string.
size_t wb_line_sighting_to_network_serialised(const struct wb_line_sighting *self, char *dst)
Convert to a compressed, serialised, network byte order byte stream.
const char * wb_line_sighting_to_string(const struct wb_line_sighting *self, char *toString, size_t bufferSize)
Convert to a string.
#define ntohl(x)
#define htonl(x)
size_t wb_line_sighting_from_network_serialised(const char *src, struct wb_line_sighting *dst)
Convert from a compressed, serialised, network byte order byte stream.
struct wb_line_sighting * wb_line_sighting_from_string(struct wb_line_sighting *self, const char *str)
Convert from a string.
LineSightingType
@ HorizonLineSightingType
@ FieldLineSightingType
#define LINE_SIGHTING_DESC_BUFFER_SIZE
struct wb_polar_coordinate * wb_polar_coordinate_from_string(struct wb_polar_coordinate *self, const char *str)
Convert from a string.
const char * wb_polar_coordinate_to_string(const struct wb_polar_coordinate *self, char *toString, size_t bufferSize)
Convert to a string.
const char * wb_polar_coordinate_description(const struct wb_polar_coordinate *self, char *descString, size_t bufferSize)
Convert to a description string.
#define POLAR_COORDINATE_DESC_BUFFER_SIZE
#define POLAR_COORDINATE_TO_STRING_BUFFER_SIZE