gusimplewhiteboard
wb_count.c
Go to the documentation of this file.
1/*
2 * file wb_count.c
3 *
4 * This file was generated by classgenerator from count.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_count.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_count_description(const struct wb_count* 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#ifdef __APPLE__
134 len += snprintf(descString + len, bufferSize - len, "count=%lld", self->count);
135#else
136 len += snprintf(descString + len, bufferSize - len, "count=%ld", self->count);
137#endif
138 return descString;
139#pragma clang diagnostic pop
140}
141
145const char* wb_count_to_string(const struct wb_count* self, char* toString, size_t bufferSize)
146{
147#pragma clang diagnostic push
148#pragma clang diagnostic ignored "-Wunused-variable"
149 size_t len = 0;
150 if (len >= bufferSize) {
151 return toString;
152 }
153#ifdef __APPLE__
154 len += snprintf(toString + len, bufferSize - len, "%lld", self->count);
155#else
156 len += snprintf(toString + len, bufferSize - len, "%ld", self->count);
157#endif
158 return toString;
159#pragma clang diagnostic pop
160}
161
165struct wb_count* wb_count_from_string(struct wb_count* self, const char* str)
166{
167 size_t temp_length = strlen(str);
168 int length = (temp_length <= INT_MAX) ? ((int)((ssize_t)temp_length)) : -1;
169 if (length < 1 || length > COUNT_DESC_BUFFER_SIZE) {
170 return self;
171 }
172 char var_str_buffer[COUNT_DESC_BUFFER_SIZE + 1];
173 char* var_str = &var_str_buffer[0];
174 char key_buffer[6];
175 char* key = &key_buffer[0];
176 int bracecount = 0;
177 int startVar = 0;
178 int index = 0;
179 int startKey = 0;
180 int endKey = -1;
181 int varIndex = 0;
182 if (index == 0 && str[0] == '{') {
183 index = 1;
184 }
185 startVar = index;
186 startKey = startVar;
187 do {
188 for (int i = index; i < length; i++) {
189 index = i + 1;
190 if (bracecount == 0 && str[i] == '=') {
191 endKey = i - 1;
192 startVar = index;
193 continue;
194 }
195 if (bracecount == 0 && isspace(str[i])) {
196 startVar = index;
197 if (endKey == -1) {
198 startKey = index;
199 }
200 continue;
201 }
202 if (bracecount == 0 && str[i] == ',') {
203 index = i - 1;
204 break;
205 }
206 if (str[i] == '{') {
207 bracecount++;
208 continue;
209 }
210 if (str[i] == '}') {
211 bracecount--;
212 if (bracecount < 0) {
213 index = i - 1;
214 break;
215 }
216 }
217 if (i == length - 1) {
218 index = i;
219 }
220 }
221 if (endKey >= startKey && endKey - startKey < length) {
222 strncpy(key, str + startKey, ((size_t)(endKey - startKey) + 1));
223 key[(endKey - startKey) + 1] = 0;
224 } else {
225 key[0] = 0;
226 }
227 strncpy(var_str, str + startVar, ((size_t)(index - startVar) + 1));
228 var_str[(index - startVar) + 1] = 0;
229 bracecount = 0;
230 index += 2;
231 startVar = index;
232 startKey = startVar;
233 endKey = -1;
234 if (strlen(key) > 0) {
235 if (0 == strcmp("count", key)) {
236 varIndex = 0;
237 } else {
238 varIndex = -1;
239 }
240 }
241 switch (varIndex) {
242 case -1: { break; }
243 case 0:
244 {
245#ifdef __APPLE__
246 self->count = ((int64_t)atoll(var_str));
247#else
248 self->count = ((int64_t)atol(var_str));
249#endif
250 break;
251 }
252 }
253 if (varIndex >= 0) {
254 varIndex++;
255 }
256 } while(index < length);
257 return self;
258}
259
260/*#ifdef WHITEBOARD_SERIALISATION*/
261
265size_t wb_count_to_network_serialised(const struct wb_count *self, char *dst)
266{
267 uint16_t bit_offset = 0;
268 int64_t count_nbo = htonll(self->count);
269 do {
270 int8_t b;
271 for (b = (64 - 1); b >= 0; b--) {
272 do {
273 uint16_t byte = bit_offset / 8;
274 uint16_t bit = 7 - (bit_offset % 8);
275 unsigned long newbit = !!((count_nbo >> b) & 1U);
276 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
277 bit_offset = bit_offset + 1;
278 } while(false);
279 }
280 } while(false);
281 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
282 (void)self;
283 (void)dst;
284 return bit_offset;
285}
286
290size_t wb_count_from_network_serialised(const char *src, struct wb_count *dst)
291{
292 uint16_t bit_offset = 0;
293 do {
294 int8_t b;
295 for (b = (64 - 1); b >= 0; b--) {
296 do {
297 uint16_t byte = bit_offset / 8;
298 uint16_t bit = 7 - (bit_offset % 8);
299 char dataByte = src[byte];
300 unsigned char bitValue = (dataByte >> bit) & 1U;
301 dst->count ^= (-bitValue ^ dst->count) & (1UL << b);
302 bit_offset = bit_offset + 1;
303 } while(false);
304 }
305 } while(false);
306 dst->count = ntohll(dst->count);
307 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
308 (void)src;
309 (void)dst;
310 return bit_offset;
311}
312
313/*#endif // WHITEBOARD_SERIALISATION*/
WHITEBOARD_POSTER_STRING_CONVERSION.
Definition: wb_count.h:105
int64_t count
The count.
Definition: wb_count.h:110
size_t wb_count_from_network_serialised(const char *src, struct wb_count *dst)
Convert from a compressed, serialised, network byte order byte stream.
Definition: wb_count.c:290
#define ntohll(x)
Definition: wb_count.c:94
const char * wb_count_to_string(const struct wb_count *self, char *toString, size_t bufferSize)
Convert to a string.
Definition: wb_count.c:145
#define htonll(x)
Definition: wb_count.c:93
const char * wb_count_description(const struct wb_count *self, char *descString, size_t bufferSize)
Convert to a description string.
Definition: wb_count.c:125
size_t wb_count_to_network_serialised(const struct wb_count *self, char *dst)
Convert to a compressed, serialised, network byte order byte stream.
Definition: wb_count.c:265
struct wb_count * wb_count_from_string(struct wb_count *self, const char *str)
Convert from a string.
Definition: wb_count.c:165
#define COUNT_DESC_BUFFER_SIZE
Definition: wb_count.h:93