gusimplewhiteboard
wb_pixel_coordinate.c
Go to the documentation of this file.
1/*
2 * file wb_pixel_coordinate.c
3 *
4 * This file was generated by classgenerator from pixel_coordinate.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_pixel_coordinate.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_pixel_coordinate_description(const struct wb_pixel_coordinate* 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, "x=%d", self->x);
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, "y=%d", self->y);
142 return descString;
143#pragma clang diagnostic pop
144}
145
149const char* wb_pixel_coordinate_to_string(const struct wb_pixel_coordinate* 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, "%d", self->x);
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, "%d", self->y);
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 > PIXEL_COORDINATE_DESC_BUFFER_SIZE) {
178 return self;
179 }
180 char var_str_buffer[PIXEL_COORDINATE_DESC_BUFFER_SIZE + 1];
181 char* var_str = &var_str_buffer[0];
182 char key_buffer[2];
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("x", key)) {
244 varIndex = 0;
245 } else if (0 == strcmp("y", key)) {
246 varIndex = 1;
247 } else {
248 varIndex = -1;
249 }
250 }
251 switch (varIndex) {
252 case -1: { break; }
253 case 0:
254 {
255 self->x = ((int16_t)atoi(var_str));
256 break;
257 }
258 case 1:
259 {
260 self->y = ((int16_t)atoi(var_str));
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 int16_t x_nbo = htons(self->x);
280 do {
281 int8_t b;
282 for (b = (16 - 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 = !!((x_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 int16_t y_nbo = htons(self->y);
294 do {
295 int8_t b;
296 for (b = (16 - 1); b >= 0; b--) {
297 do {
298 uint16_t byte = bit_offset / 8;
299 uint16_t bit = 7 - (bit_offset % 8);
300 unsigned long newbit = !!((y_nbo >> b) & 1U);
301 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
302 bit_offset = bit_offset + 1;
303 } while(false);
304 }
305 } while(false);
306 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
307 (void)self;
308 (void)dst;
309 return bit_offset;
310}
311
316{
317 uint16_t bit_offset = 0;
318 do {
319 int8_t b;
320 for (b = (16 - 1); b >= 0; b--) {
321 do {
322 uint16_t byte = bit_offset / 8;
323 uint16_t bit = 7 - (bit_offset % 8);
324 char dataByte = src[byte];
325 unsigned char bitValue = (dataByte >> bit) & 1U;
326 dst->x ^= (-bitValue ^ dst->x) & (1UL << b);
327 bit_offset = bit_offset + 1;
328 } while(false);
329 }
330 } while(false);
331 dst->x = ntohs(dst->x);
332
333 do {
334 int8_t b;
335 for (b = (16 - 1); b >= 0; b--) {
336 do {
337 uint16_t byte = bit_offset / 8;
338 uint16_t bit = 7 - (bit_offset % 8);
339 char dataByte = src[byte];
340 unsigned char bitValue = (dataByte >> bit) & 1U;
341 dst->y ^= (-bitValue ^ dst->y) & (1UL << b);
342 bit_offset = bit_offset + 1;
343 } while(false);
344 }
345 } while(false);
346 dst->y = ntohs(dst->y);
347 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
348 (void)src;
349 (void)dst;
350 return bit_offset;
351}
352
353/*#endif // WHITEBOARD_SERIALISATION*/
354
355struct wb_pixel_coordinate px_coord_to_wb_px_coord(const gu_pixel_coordinate coord)
356{
357 const struct wb_pixel_coordinate temp = { px_t_to_i16(coord.x), px_t_to_i16(coord.y) };
358 return temp;
359}
360
361gu_pixel_coordinate wb_px_coord_to_px_coord(const struct wb_pixel_coordinate coord, const uint16_t resWidth, const uint16_t resHeight)
362{
363 const gu_pixel_coordinate temp = { i16_to_px_t(coord.x), i16_to_px_t(coord.y), u16_to_px_u(resWidth), u16_to_px_u(resHeight) };
364 return temp;
365}
WHITEBOARD_POSTER_STRING_CONVERSION.
int16_t y
The y coordinate.
int16_t x
The x coordinate.
size_t wb_pixel_coordinate_to_network_serialised(const struct wb_pixel_coordinate *self, char *dst)
Convert to a compressed, serialised, network byte order byte stream.
#define htons(x)
struct wb_pixel_coordinate px_coord_to_wb_px_coord(const gu_pixel_coordinate coord)
WHITEBOARD_POSTER_STRING_CONVERSION.
const char * wb_pixel_coordinate_description(const struct wb_pixel_coordinate *self, char *descString, size_t bufferSize)
Convert to a description string.
size_t wb_pixel_coordinate_from_network_serialised(const char *src, struct wb_pixel_coordinate *dst)
Convert from a compressed, serialised, network byte order byte stream.
struct wb_pixel_coordinate * wb_pixel_coordinate_from_string(struct wb_pixel_coordinate *self, const char *str)
Convert from a string.
const char * wb_pixel_coordinate_to_string(const struct wb_pixel_coordinate *self, char *toString, size_t bufferSize)
Convert to a string.
#define ntohs(x)
gu_pixel_coordinate wb_px_coord_to_px_coord(const struct wb_pixel_coordinate coord, const uint16_t resWidth, const uint16_t resHeight)
#define PIXEL_COORDINATE_DESC_BUFFER_SIZE