gusimplewhiteboard
wb_vision_detection_feature.c
Go to the documentation of this file.
1/*
2 * file wb_vision_detection_feature.c
3 *
4 * This file was generated by classgenerator from vision_detection_feature.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_feature_description(const struct wb_vision_detection_feature* 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->featureType) {
135 {
136 len += snprintf(descString + len, bufferSize - len, "featureType=FeatureTypeCircle");
137 break;
138 }
139 case FeatureTypeL:
140 {
141 len += snprintf(descString + len, bufferSize - len, "featureType=FeatureTypeL");
142 break;
143 }
144 case FeatureTypeT:
145 {
146 len += snprintf(descString + len, bufferSize - len, "featureType=FeatureTypeT");
147 break;
148 }
149 case FeatureTypeX:
150 {
151 len += snprintf(descString + len, bufferSize - len, "featureType=FeatureTypeX");
152 break;
153 }
155 {
156 len += snprintf(descString + len, bufferSize - len, "featureType=NUMBER_OF_FEATURE_TYPES");
157 break;
158 }
159 }
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, "coordinate={", bufferSize);
168 if (len >= bufferSize) {
169 return descString;
170 }
171 char coordinate_buffer[POLAR_COORDINATE_DESC_BUFFER_SIZE];
172 char* coordinate_p = coordinate_buffer;
173 const char* coordinate_description = wb_polar_coordinate_description(&self->coordinate, coordinate_p, POLAR_COORDINATE_DESC_BUFFER_SIZE);
174 len = gu_strlcat(descString, coordinate_p, bufferSize);
175 if (len >= bufferSize) {
176 return descString;
177 }
178 len = gu_strlcat(descString, "}", bufferSize);
179 return descString;
180#pragma clang diagnostic pop
181}
182
186const char* wb_vision_detection_feature_to_string(const struct wb_vision_detection_feature* self, char* toString, size_t bufferSize)
187{
188#pragma clang diagnostic push
189#pragma clang diagnostic ignored "-Wunused-variable"
190 size_t len = 0;
191 if (len >= bufferSize) {
192 return toString;
193 }
194 switch (self->featureType) {
196 {
197 len += snprintf(toString + len, bufferSize - len, "FeatureTypeCircle");
198 break;
199 }
200 case FeatureTypeL:
201 {
202 len += snprintf(toString + len, bufferSize - len, "FeatureTypeL");
203 break;
204 }
205 case FeatureTypeT:
206 {
207 len += snprintf(toString + len, bufferSize - len, "FeatureTypeT");
208 break;
209 }
210 case FeatureTypeX:
211 {
212 len += snprintf(toString + len, bufferSize - len, "FeatureTypeX");
213 break;
214 }
216 {
217 len += snprintf(toString + len, bufferSize - len, "NUMBER_OF_FEATURE_TYPES");
218 break;
219 }
220 }
221 if (len >= bufferSize) {
222 return toString;
223 }
224 len = gu_strlcat(toString, ", ", bufferSize);
225 if (len >= bufferSize) {
226 return toString;
227 }
228 len = gu_strlcat(toString, "{", bufferSize);
229 if (len >= bufferSize) {
230 return toString;
231 }
232 char coordinate_buffer[POLAR_COORDINATE_TO_STRING_BUFFER_SIZE];
233 char* coordinate_p = coordinate_buffer;
234 const char* coordinate_to_string = wb_polar_coordinate_to_string(&self->coordinate, coordinate_p, POLAR_COORDINATE_TO_STRING_BUFFER_SIZE);
235 len = gu_strlcat(toString, coordinate_p, bufferSize);
236 if (len >= bufferSize) {
237 return toString;
238 }
239 len = gu_strlcat(toString, "}", bufferSize);
240 return toString;
241#pragma clang diagnostic pop
242}
243
248{
249 size_t temp_length = strlen(str);
250 int length = (temp_length <= INT_MAX) ? ((int)((ssize_t)temp_length)) : -1;
251 if (length < 1 || length > VISION_DETECTION_FEATURE_DESC_BUFFER_SIZE) {
252 return self;
253 }
254 char var_str_buffer[VISION_DETECTION_FEATURE_DESC_BUFFER_SIZE + 1];
255 char* var_str = &var_str_buffer[0];
256 char key_buffer[12];
257 char* key = &key_buffer[0];
258 int bracecount = 0;
259 int startVar = 0;
260 int index = 0;
261 int startKey = 0;
262 int endKey = -1;
263 int varIndex = 0;
264 if (index == 0 && str[0] == '{') {
265 index = 1;
266 }
267 startVar = index;
268 startKey = startVar;
269 do {
270 for (int i = index; i < length; i++) {
271 index = i + 1;
272 if (bracecount == 0 && str[i] == '=') {
273 endKey = i - 1;
274 startVar = index;
275 continue;
276 }
277 if (bracecount == 0 && isspace(str[i])) {
278 startVar = index;
279 if (endKey == -1) {
280 startKey = index;
281 }
282 continue;
283 }
284 if (bracecount == 0 && str[i] == ',') {
285 index = i - 1;
286 break;
287 }
288 if (str[i] == '{') {
289 bracecount++;
290 continue;
291 }
292 if (str[i] == '}') {
293 bracecount--;
294 if (bracecount < 0) {
295 index = i - 1;
296 break;
297 }
298 }
299 if (i == length - 1) {
300 index = i;
301 }
302 }
303 if (endKey >= startKey && endKey - startKey < length) {
304 strncpy(key, str + startKey, ((size_t)(endKey - startKey) + 1));
305 key[(endKey - startKey) + 1] = 0;
306 } else {
307 key[0] = 0;
308 }
309 strncpy(var_str, str + startVar, ((size_t)(index - startVar) + 1));
310 var_str[(index - startVar) + 1] = 0;
311 bracecount = 0;
312 index += 2;
313 startVar = index;
314 startKey = startVar;
315 endKey = -1;
316 if (strlen(key) > 0) {
317 if (0 == strcmp("featureType", key)) {
318 varIndex = 0;
319 } else if (0 == strcmp("coordinate", key)) {
320 varIndex = 1;
321 } else {
322 varIndex = -1;
323 }
324 }
325 switch (varIndex) {
326 case -1: { break; }
327 case 0:
328 {
329 if (strcmp("FeatureTypeCircle", var_str) == 0) {
330#pragma clang diagnostic push
331#pragma clang diagnostic ignored "-Wbad-function-cast"
332 self->featureType = FeatureTypeCircle;
333#pragma clang diagnostic pop
334 } else if (strcmp("FeatureTypeL", var_str) == 0) {
335#pragma clang diagnostic push
336#pragma clang diagnostic ignored "-Wbad-function-cast"
337 self->featureType = FeatureTypeL;
338#pragma clang diagnostic pop
339 } else if (strcmp("FeatureTypeT", var_str) == 0) {
340#pragma clang diagnostic push
341#pragma clang diagnostic ignored "-Wbad-function-cast"
342 self->featureType = FeatureTypeT;
343#pragma clang diagnostic pop
344 } else if (strcmp("FeatureTypeX", var_str) == 0) {
345#pragma clang diagnostic push
346#pragma clang diagnostic ignored "-Wbad-function-cast"
347 self->featureType = FeatureTypeX;
348#pragma clang diagnostic pop
349 } else if (strcmp("NUMBER_OF_FEATURE_TYPES", var_str) == 0) {
350#pragma clang diagnostic push
351#pragma clang diagnostic ignored "-Wbad-function-cast"
352 self->featureType = NUMBER_OF_FEATURE_TYPES;
353#pragma clang diagnostic pop
354 } else {
355#pragma clang diagnostic push
356#pragma clang diagnostic ignored "-Wbad-function-cast"
357 self->featureType = ((enum FeatureTypes)atoi(var_str));
358#pragma clang diagnostic pop
359 }
360 break;
361 }
362 case 1:
363 {
364 wb_polar_coordinate_from_string(&self->coordinate, var_str);
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
381{
382 uint16_t bit_offset = 0;
383 enum FeatureTypes featureType_nbo = htonl(self->featureType);
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 = !!((featureType_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->featureType ^= (-bitValue ^ dst->featureType) & (1UL << b);
417 bit_offset = bit_offset + 1;
418 } while(false);
419 }
420 } while(false);
421 dst->featureType = ntohl(dst->featureType);
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 FeatureTypes featureType
Type of the Feature.
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
size_t wb_vision_detection_feature_from_network_serialised(const char *src, struct wb_vision_detection_feature *dst)
Convert from a compressed, serialised, network byte order byte stream.
const char * wb_vision_detection_feature_to_string(const struct wb_vision_detection_feature *self, char *toString, size_t bufferSize)
Convert to a string.
struct wb_vision_detection_feature * wb_vision_detection_feature_from_string(struct wb_vision_detection_feature *self, const char *str)
Convert from a string.
size_t wb_vision_detection_feature_to_network_serialised(const struct wb_vision_detection_feature *self, char *dst)
Convert to a compressed, serialised, network byte order byte stream.
#define ntohl(x)
#define htonl(x)
const char * wb_vision_detection_feature_description(const struct wb_vision_detection_feature *self, char *descString, size_t bufferSize)
Convert to a description string.
#define VISION_DETECTION_FEATURE_DESC_BUFFER_SIZE
@ NUMBER_OF_FEATURE_TYPES