gusimplewhiteboard
wb_vision_line.c
Go to the documentation of this file.
1/*
2 * file wb_vision_line.c
3 *
4 * This file was generated by classgenerator from vision_line.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
64#include "wb_vision_line.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_vision_line_description(const struct wb_vision_line* 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, "lineEdgeTop={", bufferSize);
134 if (len >= bufferSize) {
135 return descString;
136 }
137 char lineEdgeTop_buffer[PIXEL_COORDINATE_LINE_DESC_BUFFER_SIZE];
138 char* lineEdgeTop_p = lineEdgeTop_buffer;
139 const char* lineEdgeTop_description = wb_pixel_coordinate_line_description(&self->lineEdgeTop, lineEdgeTop_p, PIXEL_COORDINATE_LINE_DESC_BUFFER_SIZE);
140 len = gu_strlcat(descString, lineEdgeTop_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, "lineEdgeBottom={", bufferSize);
153 if (len >= bufferSize) {
154 return descString;
155 }
156 char lineEdgeBottom_buffer[PIXEL_COORDINATE_LINE_DESC_BUFFER_SIZE];
157 char* lineEdgeBottom_p = lineEdgeBottom_buffer;
158 const char* lineEdgeBottom_description = wb_pixel_coordinate_line_description(&self->lineEdgeBottom, lineEdgeBottom_p, PIXEL_COORDINATE_LINE_DESC_BUFFER_SIZE);
159 len = gu_strlcat(descString, lineEdgeBottom_p, bufferSize);
160 if (len >= bufferSize) {
161 return descString;
162 }
163 len = gu_strlcat(descString, "}", bufferSize);
164 return descString;
165#pragma clang diagnostic pop
166}
167
171const char* wb_vision_line_to_string(const struct wb_vision_line* self, char* toString, size_t bufferSize)
172{
173#pragma clang diagnostic push
174#pragma clang diagnostic ignored "-Wunused-variable"
175 size_t len = 0;
176 if (len >= bufferSize) {
177 return toString;
178 }
179 len = gu_strlcat(toString, "{", bufferSize);
180 if (len >= bufferSize) {
181 return toString;
182 }
183 char lineEdgeTop_buffer[PIXEL_COORDINATE_LINE_TO_STRING_BUFFER_SIZE];
184 char* lineEdgeTop_p = lineEdgeTop_buffer;
185 const char* lineEdgeTop_to_string = wb_pixel_coordinate_line_to_string(&self->lineEdgeTop, lineEdgeTop_p, PIXEL_COORDINATE_LINE_TO_STRING_BUFFER_SIZE);
186 len = gu_strlcat(toString, lineEdgeTop_p, bufferSize);
187 if (len >= bufferSize) {
188 return toString;
189 }
190 len = gu_strlcat(toString, "}", bufferSize);
191 if (len >= bufferSize) {
192 return toString;
193 }
194 len = gu_strlcat(toString, ", ", bufferSize);
195 if (len >= bufferSize) {
196 return toString;
197 }
198 len = gu_strlcat(toString, "{", bufferSize);
199 if (len >= bufferSize) {
200 return toString;
201 }
202 char lineEdgeBottom_buffer[PIXEL_COORDINATE_LINE_TO_STRING_BUFFER_SIZE];
203 char* lineEdgeBottom_p = lineEdgeBottom_buffer;
204 const char* lineEdgeBottom_to_string = wb_pixel_coordinate_line_to_string(&self->lineEdgeBottom, lineEdgeBottom_p, PIXEL_COORDINATE_LINE_TO_STRING_BUFFER_SIZE);
205 len = gu_strlcat(toString, lineEdgeBottom_p, bufferSize);
206 if (len >= bufferSize) {
207 return toString;
208 }
209 len = gu_strlcat(toString, "}", bufferSize);
210 return toString;
211#pragma clang diagnostic pop
212}
213
217struct wb_vision_line* wb_vision_line_from_string(struct wb_vision_line* self, const char* str)
218{
219 size_t temp_length = strlen(str);
220 int length = (temp_length <= INT_MAX) ? ((int)((ssize_t)temp_length)) : -1;
221 if (length < 1 || length > VISION_LINE_DESC_BUFFER_SIZE) {
222 return self;
223 }
224 char var_str_buffer[VISION_LINE_DESC_BUFFER_SIZE + 1];
225 char* var_str = &var_str_buffer[0];
226 char key_buffer[15];
227 char* key = &key_buffer[0];
228 int bracecount = 0;
229 int startVar = 0;
230 int index = 0;
231 int startKey = 0;
232 int endKey = -1;
233 int varIndex = 0;
234 if (index == 0 && str[0] == '{') {
235 index = 1;
236 }
237 startVar = index;
238 startKey = startVar;
239 do {
240 for (int i = index; i < length; i++) {
241 index = i + 1;
242 if (bracecount == 0 && str[i] == '=') {
243 endKey = i - 1;
244 startVar = index;
245 continue;
246 }
247 if (bracecount == 0 && isspace(str[i])) {
248 startVar = index;
249 if (endKey == -1) {
250 startKey = index;
251 }
252 continue;
253 }
254 if (bracecount == 0 && str[i] == ',') {
255 index = i - 1;
256 break;
257 }
258 if (str[i] == '{') {
259 bracecount++;
260 continue;
261 }
262 if (str[i] == '}') {
263 bracecount--;
264 if (bracecount < 0) {
265 index = i - 1;
266 break;
267 }
268 }
269 if (i == length - 1) {
270 index = i;
271 }
272 }
273 if (endKey >= startKey && endKey - startKey < length) {
274 strncpy(key, str + startKey, ((size_t)(endKey - startKey) + 1));
275 key[(endKey - startKey) + 1] = 0;
276 } else {
277 key[0] = 0;
278 }
279 strncpy(var_str, str + startVar, ((size_t)(index - startVar) + 1));
280 var_str[(index - startVar) + 1] = 0;
281 bracecount = 0;
282 index += 2;
283 startVar = index;
284 startKey = startVar;
285 endKey = -1;
286 if (strlen(key) > 0) {
287 if (0 == strcmp("lineEdgeTop", key)) {
288 varIndex = 0;
289 } else if (0 == strcmp("lineEdgeBottom", key)) {
290 varIndex = 1;
291 } else {
292 varIndex = -1;
293 }
294 }
295 switch (varIndex) {
296 case -1: { break; }
297 case 0:
298 {
299 wb_pixel_coordinate_line_from_string(&self->lineEdgeTop, var_str);
300 break;
301 }
302 case 1:
303 {
304 wb_pixel_coordinate_line_from_string(&self->lineEdgeBottom, var_str);
305 break;
306 }
307 }
308 if (varIndex >= 0) {
309 varIndex++;
310 }
311 } while(index < length);
312 return self;
313}
314
315/*#ifdef WHITEBOARD_SERIALISATION*/
316
320size_t wb_vision_line_to_network_serialised(const struct wb_vision_line *self, char *dst)
321{
322 uint16_t bit_offset = 0;
323
324 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
325 (void)self;
326 (void)dst;
327 return bit_offset;
328}
329
333size_t wb_vision_line_from_network_serialised(const char *src, struct wb_vision_line *dst)
334{
335 uint16_t bit_offset = 0;
336
337 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
338 (void)src;
339 (void)dst;
340 return bit_offset;
341}
342
343/*#endif // WHITEBOARD_SERIALISATION*/
WHITEBOARD_POSTER_STRING_CONVERSION.
struct wb_pixel_coordinate_line * wb_pixel_coordinate_line_from_string(struct wb_pixel_coordinate_line *self, const char *str)
Convert from a string.
const char * wb_pixel_coordinate_line_to_string(const struct wb_pixel_coordinate_line *self, char *toString, size_t bufferSize)
Convert to a string.
const char * wb_pixel_coordinate_line_description(const struct wb_pixel_coordinate_line *self, char *descString, size_t bufferSize)
Convert to a description string.
#define PIXEL_COORDINATE_LINE_TO_STRING_BUFFER_SIZE
#define PIXEL_COORDINATE_LINE_DESC_BUFFER_SIZE
size_t wb_vision_line_from_network_serialised(const char *src, struct wb_vision_line *dst)
Convert from a compressed, serialised, network byte order byte stream.
const char * wb_vision_line_description(const struct wb_vision_line *self, char *descString, size_t bufferSize)
Convert to a description string.
const char * wb_vision_line_to_string(const struct wb_vision_line *self, char *toString, size_t bufferSize)
Convert to a string.
size_t wb_vision_line_to_network_serialised(const struct wb_vision_line *self, char *dst)
Convert to a compressed, serialised, network byte order byte stream.
struct wb_vision_line * wb_vision_line_from_string(struct wb_vision_line *self, const char *str)
Convert from a string.
#define VISION_LINE_DESC_BUFFER_SIZE