gusimplewhiteboard
wb_vision_detection_goal.c
Go to the documentation of this file.
1/*
2 * file wb_vision_detection_goal.c
3 *
4 * This file was generated by classgenerator from vision_detection_goal.gen.
5 * DO NOT CHANGE MANUALLY!
6 *
7 * Copyright © 2021 Carl Lusty. 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 Carl Lusty.
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_vision_detection_goal_description(const struct wb_vision_detection_goal* 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 switch (self->sightingType) {
134 case DoublePostGoal:
135 {
136 len += snprintf(descString + len, bufferSize - len, "sightingType=DoublePostGoal");
137 break;
138 }
139 case NoGoalDetected:
140 {
141 len += snprintf(descString + len, bufferSize - len, "sightingType=NoGoalDetected");
142 break;
143 }
144 case SinglePostGoal:
145 {
146 len += snprintf(descString + len, bufferSize - len, "sightingType=SinglePostGoal");
147 break;
148 }
149 }
150 if (len >= bufferSize) {
151 return descString;
152 }
153 len = gu_strlcat(descString, ", ", bufferSize);
154 if (len >= bufferSize) {
155 return descString;
156 }
157 len = gu_strlcat(descString, "post1={", bufferSize);
158 if (len >= bufferSize) {
159 return descString;
160 }
162 char* post1_p = post1_buffer;
163 const char* post1_description = wb_vision_detection_goal_post_description(&self->post1, post1_p, VISION_DETECTION_GOAL_POST_DESC_BUFFER_SIZE);
164 len = gu_strlcat(descString, post1_p, bufferSize);
165 if (len >= bufferSize) {
166 return descString;
167 }
168 len = gu_strlcat(descString, "}", bufferSize);
169 if (len >= bufferSize) {
170 return descString;
171 }
172 len = gu_strlcat(descString, ", ", bufferSize);
173 if (len >= bufferSize) {
174 return descString;
175 }
176 len = gu_strlcat(descString, "post2={", bufferSize);
177 if (len >= bufferSize) {
178 return descString;
179 }
181 char* post2_p = post2_buffer;
182 const char* post2_description = wb_vision_detection_goal_post_description(&self->post2, post2_p, VISION_DETECTION_GOAL_POST_DESC_BUFFER_SIZE);
183 len = gu_strlcat(descString, post2_p, bufferSize);
184 if (len >= bufferSize) {
185 return descString;
186 }
187 len = gu_strlcat(descString, "}", bufferSize);
188 return descString;
189#pragma clang diagnostic pop
190}
191
195const char* wb_vision_detection_goal_to_string(const struct wb_vision_detection_goal* self, char* toString, size_t bufferSize)
196{
197#pragma clang diagnostic push
198#pragma clang diagnostic ignored "-Wunused-variable"
199 size_t len = 0;
200 if (len >= bufferSize) {
201 return toString;
202 }
203 switch (self->sightingType) {
204 case DoublePostGoal:
205 {
206 len += snprintf(toString + len, bufferSize - len, "DoublePostGoal");
207 break;
208 }
209 case NoGoalDetected:
210 {
211 len += snprintf(toString + len, bufferSize - len, "NoGoalDetected");
212 break;
213 }
214 case SinglePostGoal:
215 {
216 len += snprintf(toString + len, bufferSize - len, "SinglePostGoal");
217 break;
218 }
219 }
220 if (len >= bufferSize) {
221 return toString;
222 }
223 len = gu_strlcat(toString, ", ", bufferSize);
224 if (len >= bufferSize) {
225 return toString;
226 }
227 len = gu_strlcat(toString, "{", bufferSize);
228 if (len >= bufferSize) {
229 return toString;
230 }
232 char* post1_p = post1_buffer;
233 const char* post1_to_string = wb_vision_detection_goal_post_to_string(&self->post1, post1_p, VISION_DETECTION_GOAL_POST_TO_STRING_BUFFER_SIZE);
234 len = gu_strlcat(toString, post1_p, bufferSize);
235 if (len >= bufferSize) {
236 return toString;
237 }
238 len = gu_strlcat(toString, "}", bufferSize);
239 if (len >= bufferSize) {
240 return toString;
241 }
242 len = gu_strlcat(toString, ", ", bufferSize);
243 if (len >= bufferSize) {
244 return toString;
245 }
246 len = gu_strlcat(toString, "{", bufferSize);
247 if (len >= bufferSize) {
248 return toString;
249 }
251 char* post2_p = post2_buffer;
252 const char* post2_to_string = wb_vision_detection_goal_post_to_string(&self->post2, post2_p, VISION_DETECTION_GOAL_POST_TO_STRING_BUFFER_SIZE);
253 len = gu_strlcat(toString, post2_p, bufferSize);
254 if (len >= bufferSize) {
255 return toString;
256 }
257 len = gu_strlcat(toString, "}", bufferSize);
258 return toString;
259#pragma clang diagnostic pop
260}
261
266{
267 size_t temp_length = strlen(str);
268 int length = (temp_length <= INT_MAX) ? ((int)((ssize_t)temp_length)) : -1;
269 if (length < 1 || length > VISION_DETECTION_GOAL_DESC_BUFFER_SIZE) {
270 return self;
271 }
272 char var_str_buffer[VISION_DETECTION_GOAL_DESC_BUFFER_SIZE + 1];
273 char* var_str = &var_str_buffer[0];
274 char key_buffer[13];
275 char* key = &key_buffer[0];
276 int bracecount = 0;
277 int startVar = 0;
278 int index = 0;
279 int startKey = 0;
280 int endKey = -1;
281 int varIndex = 0;
282 if (index == 0 && str[0] == '{') {
283 index = 1;
284 }
285 startVar = index;
286 startKey = startVar;
287 do {
288 for (int i = index; i < length; i++) {
289 index = i + 1;
290 if (bracecount == 0 && str[i] == '=') {
291 endKey = i - 1;
292 startVar = index;
293 continue;
294 }
295 if (bracecount == 0 && isspace(str[i])) {
296 startVar = index;
297 if (endKey == -1) {
298 startKey = index;
299 }
300 continue;
301 }
302 if (bracecount == 0 && str[i] == ',') {
303 index = i - 1;
304 break;
305 }
306 if (str[i] == '{') {
307 bracecount++;
308 continue;
309 }
310 if (str[i] == '}') {
311 bracecount--;
312 if (bracecount < 0) {
313 index = i - 1;
314 break;
315 }
316 }
317 if (i == length - 1) {
318 index = i;
319 }
320 }
321 if (endKey >= startKey && endKey - startKey < length) {
322 strncpy(key, str + startKey, ((size_t)(endKey - startKey) + 1));
323 key[(endKey - startKey) + 1] = 0;
324 } else {
325 key[0] = 0;
326 }
327 strncpy(var_str, str + startVar, ((size_t)(index - startVar) + 1));
328 var_str[(index - startVar) + 1] = 0;
329 bracecount = 0;
330 index += 2;
331 startVar = index;
332 startKey = startVar;
333 endKey = -1;
334 if (strlen(key) > 0) {
335 if (0 == strcmp("sightingType", key)) {
336 varIndex = 0;
337 } else if (0 == strcmp("post1", key)) {
338 varIndex = 1;
339 } else if (0 == strcmp("post2", key)) {
340 varIndex = 2;
341 } else {
342 varIndex = -1;
343 }
344 }
345 switch (varIndex) {
346 case -1: { break; }
347 case 0:
348 {
349 if (strcmp("DoublePostGoal", var_str) == 0) {
350#pragma clang diagnostic push
351#pragma clang diagnostic ignored "-Wbad-function-cast"
352 self->sightingType = DoublePostGoal;
353#pragma clang diagnostic pop
354 } else if (strcmp("NoGoalDetected", var_str) == 0) {
355#pragma clang diagnostic push
356#pragma clang diagnostic ignored "-Wbad-function-cast"
357 self->sightingType = NoGoalDetected;
358#pragma clang diagnostic pop
359 } else if (strcmp("SinglePostGoal", var_str) == 0) {
360#pragma clang diagnostic push
361#pragma clang diagnostic ignored "-Wbad-function-cast"
362 self->sightingType = SinglePostGoal;
363#pragma clang diagnostic pop
364 } else {
365#pragma clang diagnostic push
366#pragma clang diagnostic ignored "-Wbad-function-cast"
367 self->sightingType = ((enum GoalOptions)atoi(var_str));
368#pragma clang diagnostic pop
369 }
370 break;
371 }
372 case 1:
373 {
374 wb_vision_detection_goal_post_from_string(&self->post1, var_str);
375 break;
376 }
377 case 2:
378 {
379 wb_vision_detection_goal_post_from_string(&self->post2, var_str);
380 break;
381 }
382 }
383 if (varIndex >= 0) {
384 varIndex++;
385 }
386 } while(index < length);
387 return self;
388}
389
390/*#ifdef WHITEBOARD_SERIALISATION*/
391
396{
397 uint16_t bit_offset = 0;
398 enum GoalOptions sightingType_nbo = htonl(self->sightingType);
399 do {
400 int8_t b;
401 for (b = (32 - 1); b >= 0; b--) {
402 do {
403 uint16_t byte = bit_offset / 8;
404 uint16_t bit = 7 - (bit_offset % 8);
405 unsigned long newbit = !!((sightingType_nbo >> b) & 1U);
406 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
407 bit_offset = bit_offset + 1;
408 } while(false);
409 }
410 } while(false);
411 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
412 (void)self;
413 (void)dst;
414 return bit_offset;
415}
416
421{
422 uint16_t bit_offset = 0;
423 do {
424 int8_t b;
425 for (b = (32 - 1); b >= 0; b--) {
426 do {
427 uint16_t byte = bit_offset / 8;
428 uint16_t bit = 7 - (bit_offset % 8);
429 char dataByte = src[byte];
430 unsigned char bitValue = (dataByte >> bit) & 1U;
431 dst->sightingType ^= (-bitValue ^ dst->sightingType) & (1UL << b);
432 bit_offset = bit_offset + 1;
433 } while(false);
434 }
435 } while(false);
436 dst->sightingType = ntohl(dst->sightingType);
437 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
438 (void)src;
439 (void)dst;
440 return bit_offset;
441}
442
443/*#endif // WHITEBOARD_SERIALISATION*/
WHITEBOARD_POSTER_STRING_CONVERSION.
enum GoalOptions sightingType
Goal detection details.
const char * wb_vision_detection_goal_description(const struct wb_vision_detection_goal *self, char *descString, size_t bufferSize)
Convert to a description string.
size_t wb_vision_detection_goal_to_network_serialised(const struct wb_vision_detection_goal *self, char *dst)
Convert to a compressed, serialised, network byte order byte stream.
size_t wb_vision_detection_goal_from_network_serialised(const char *src, struct wb_vision_detection_goal *dst)
Convert from a compressed, serialised, network byte order byte stream.
#define ntohl(x)
#define htonl(x)
const char * wb_vision_detection_goal_to_string(const struct wb_vision_detection_goal *self, char *toString, size_t bufferSize)
Convert to a string.
struct wb_vision_detection_goal * wb_vision_detection_goal_from_string(struct wb_vision_detection_goal *self, const char *str)
Convert from a string.
#define VISION_DETECTION_GOAL_DESC_BUFFER_SIZE
const char * wb_vision_detection_goal_post_to_string(const struct wb_vision_detection_goal_post *self, char *toString, size_t bufferSize)
Convert to a string.
struct wb_vision_detection_goal_post * wb_vision_detection_goal_post_from_string(struct wb_vision_detection_goal_post *self, const char *str)
Convert from a string.
const char * wb_vision_detection_goal_post_description(const struct wb_vision_detection_goal_post *self, char *descString, size_t bufferSize)
Convert to a description string.
#define VISION_DETECTION_GOAL_POST_TO_STRING_BUFFER_SIZE
#define VISION_DETECTION_GOAL_POST_DESC_BUFFER_SIZE