summaryrefslogtreecommitdiff
path: root/firmware
diff options
context:
space:
mode:
Diffstat (limited to 'firmware')
-rw-r--r--firmware/SOURCES64
-rw-r--r--firmware/common/strptokspn.c88
-rw-r--r--firmware/export/font.h2
-rw-r--r--firmware/font.c24
-rw-r--r--firmware/include/strptokspn_r.h26
5 files changed, 166 insertions, 38 deletions
diff --git a/firmware/SOURCES b/firmware/SOURCES
index bbd67631a9..4aa7c38daf 100644
--- a/firmware/SOURCES
+++ b/firmware/SOURCES
@@ -205,39 +205,10 @@ target/hosted/samsungypr/ypr1/wmcodec-ypr1.c
205target/hosted/maemo/maemo-thread.c 205target/hosted/maemo/maemo-thread.c
206#endif 206#endif
207 207
208/* Standard library */
209#if (CONFIG_PLATFORM & PLATFORM_NATIVE) || defined(__MINGW32__) || defined(__CYGWIN__)
210libc/strtok.c
211#endif /* PLATFORM_NATIVE || __MINGW32__ || __CYGWIN__ */
212#if (CONFIG_PLATFORM & PLATFORM_NATIVE) || defined(HAVE_ROCKBOX_C_LIBRARY)
213libc/atoi.c
214libc/errno.c
215#if (CONFIG_PLATFORM & PLATFORM_NATIVE)
216/* our ctype.[ch] comes from newlib and is incompitble with most desktop's ctype */
217libc/ctype.c
218/* alsa on linux requires a more advanced sprintf, i.e. not ours */
219libc/sprintf.c
220#endif
221
222libc/memchr.c
223libc/memcmp.c
224
225libc/qsort.c
226libc/random.c
227libc/strcat.c
228libc/strchr.c
229libc/strcmp.c
230libc/strcpy.c
231
232libc/strncmp.c
233libc/strrchr.c
234libc/strstr.c
235libc/mktime.c
236libc/gmtime.c
237#endif /* CONFIG_PLATFORM || HAVE_ROCKBOX_C_LIBRARY */
238
239/* Common */ 208/* Common */
240#ifndef BOOTLOADER 209#ifndef BOOTLOADER
210common/strptokspn.c
211#define HAVE_STRTOK_R
241common/ap_int.c 212common/ap_int.c
242#endif 213#endif
243common/version.c 214common/version.c
@@ -277,6 +248,37 @@ common/zip.c
277common/adler32.c 248common/adler32.c
278common/inflate.c 249common/inflate.c
279 250
251/* Standard library */
252#if (CONFIG_PLATFORM & PLATFORM_NATIVE) || defined(__MINGW32__) || defined(__CYGWIN__)
253libc/strtok.c
254#endif /* PLATFORM_NATIVE || __MINGW32__ || __CYGWIN__ */
255#if (CONFIG_PLATFORM & PLATFORM_NATIVE) || defined(HAVE_ROCKBOX_C_LIBRARY)
256libc/atoi.c
257libc/errno.c
258#if (CONFIG_PLATFORM & PLATFORM_NATIVE)
259/* our ctype.[ch] comes from newlib and is incompitble with most desktop's ctype */
260libc/ctype.c
261/* alsa on linux requires a more advanced sprintf, i.e. not ours */
262libc/sprintf.c
263#endif
264
265libc/memchr.c
266libc/memcmp.c
267
268libc/qsort.c
269libc/random.c
270libc/strcat.c
271libc/strchr.c
272libc/strcmp.c
273libc/strcpy.c
274
275libc/strncmp.c
276libc/strrchr.c
277libc/strstr.c
278libc/mktime.c
279libc/gmtime.c
280#endif /* CONFIG_PLATFORM || HAVE_ROCKBOX_C_LIBRARY */
281
280/* Display */ 282/* Display */
281scroll_engine.c 283scroll_engine.c
282 284
diff --git a/firmware/common/strptokspn.c b/firmware/common/strptokspn.c
new file mode 100644
index 0000000000..f4b92c0712
--- /dev/null
+++ b/firmware/common/strptokspn.c
@@ -0,0 +1,88 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2022 by William WIlgus
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied. *
19 ****************************************************************************/
20
21#include "config.h"
22
23#include <stddef.h>
24#include <string.h>
25#include "strtok_r.h"
26/* strptokspn_r is a custom implementation of strtok_r that does NOT modify
27 * the source string.
28 *
29 * strptokspn_r reads ptr as a series of zero or more tokens,
30 * and sep as delimiters of the tokens
31 * The tokens can be separated by one or more of the delimiters
32 * first call searches for the first token skipping over any leading delimiters.
33 * Returns pointer to first token
34 * Pointer *len contains the span to the first delimeter
35 * (this would be the resulting strlen had token actually been NULL terminated)
36 * Pointer **end contains pointer to first character after the last delimeter
37 *
38 * When strptokspn_r is called with a ptr == NULL, the next token is read from
39 * Pointer **end
40 *
41 * Note the returned token is NOT NULL terminated by the function as in strtok_r
42 * However the caller can use ret[len+1] = '\0'; to emulate a call to strtok_r
43*/
44
45const char *strptokspn_r(const char *ptr, const char *sep, size_t *len, const char **end)
46{
47 *len = 0;
48 if (!ptr)
49 /* we got NULL input so then we get our last position instead */
50 ptr = *end;
51
52 /* pass all letters that are including in the separator string */
53 while (*ptr && strchr(sep, *ptr))
54 ++ptr;
55
56 if (*ptr) {
57 /* so this is where the next piece of string starts */
58 const char *start = ptr;
59
60 /* set the end pointer to the first byte after the start */
61 *end = start + 1;
62
63 /* scan through the string to find where it ends, it ends on a
64 null byte or a character that exists in the separator string */
65 while (**end && !strchr(sep, **end))
66 ++*end;
67 *len = (*end - start) - 1; /* this would be the string len if there actually was a NULL */
68 if (**end) { /* the end is not a null byte */
69 ++*end; /* advance last pointer to beyond the match */
70 }
71
72 return start; /* return the position where the string starts */
73 }
74
75 /* we ended up on a null byte, there are no more strings to find! */
76 return NULL;
77}
78
79#if !defined(HAVE_STRTOK_R)
80char * strtok_r(char *ptr, const char *sep, char **end)
81{
82 size_t len;
83 char * ret = (char*) strptokspn_r((const char*)ptr, sep, &len, (const char**) end);
84 if (ret)
85 ret[len + 1] = '\0';
86 return ret;
87}
88#endif
diff --git a/firmware/export/font.h b/firmware/export/font.h
index 067c67e43d..38e30187b3 100644
--- a/firmware/export/font.h
+++ b/firmware/export/font.h
@@ -132,7 +132,7 @@ void font_disable_all(void);
132void font_enable_all(void); 132void font_enable_all(void);
133 133
134struct font* font_get(int font); 134struct font* font_get(int font);
135 135int font_getstringnsize(const unsigned char *str, size_t maxbytes, int *w, int *h, int fontnumber);
136int font_getstringsize(const unsigned char *str, int *w, int *h, int fontnumber); 136int font_getstringsize(const unsigned char *str, int *w, int *h, int fontnumber);
137int font_get_width(struct font* ft, unsigned short ch); 137int font_get_width(struct font* ft, unsigned short ch);
138const unsigned char * font_get_bits(struct font* ft, unsigned short ch); 138const unsigned char * font_get_bits(struct font* ft, unsigned short ch);
diff --git a/firmware/font.c b/firmware/font.c
index 7ce64ed47d..97a15221fc 100644
--- a/firmware/font.c
+++ b/firmware/font.c
@@ -1046,16 +1046,20 @@ const unsigned char* font_get_bits(struct font* pf, unsigned short char_code)
1046#endif /* BOOTLOADER */ 1046#endif /* BOOTLOADER */
1047 1047
1048/* 1048/*
1049 * Returns the stringsize of a given string. 1049 * Returns the stringsize of a given NULL terminated string
1050 * stops after maxbytes or NULL (\0) whichever occurs first.
1051 * maxbytes = -1 ignores maxbytes and relies on NULL terminator (\0)
1052 * to terminate the string
1050 */ 1053 */
1051int font_getstringsize(const unsigned char *str, int *w, int *h, int fontnumber) 1054int font_getstringnsize(const unsigned char *str, size_t maxbytes, int *w, int *h, int fontnum)
1052{ 1055{
1053 struct font* pf = font_get(fontnumber); 1056 struct font* pf = font_get(fontnum);
1057 font_lock( fontnum, true );
1054 unsigned short ch; 1058 unsigned short ch;
1055 int width = 0; 1059 int width = 0;
1060 size_t b = maxbytes - 1;
1056 1061
1057 font_lock( fontnumber, true ); 1062 for (str = utf8decode(str, &ch); ch != 0 && b < maxbytes; str = utf8decode(str, &ch), b--)
1058 for (str = utf8decode(str, &ch); ch != 0 ; str = utf8decode(str, &ch))
1059 { 1063 {
1060 if (is_diacritic(ch, NULL)) 1064 if (is_diacritic(ch, NULL))
1061 continue; 1065 continue;
@@ -1067,10 +1071,18 @@ int font_getstringsize(const unsigned char *str, int *w, int *h, int fontnumber)
1067 *w = width; 1071 *w = width;
1068 if ( h ) 1072 if ( h )
1069 *h = pf->height; 1073 *h = pf->height;
1070 font_lock( fontnumber, false ); 1074 font_lock( fontnum, false );
1071 return width; 1075 return width;
1072} 1076}
1073 1077
1078/*
1079 * Returns the stringsize of a given NULL terminated string.
1080 */
1081int font_getstringsize(const unsigned char *str, int *w, int *h, int fontnumber)
1082{
1083 return font_getstringnsize(str, -1, w, h, fontnumber);
1084}
1085
1074/* ----------------------------------------------------------------- 1086/* -----------------------------------------------------------------
1075 * vim: et sw=4 ts=8 sts=4 tw=78 1087 * vim: et sw=4 ts=8 sts=4 tw=78
1076 */ 1088 */
diff --git a/firmware/include/strptokspn_r.h b/firmware/include/strptokspn_r.h
new file mode 100644
index 0000000000..d565118190
--- /dev/null
+++ b/firmware/include/strptokspn_r.h
@@ -0,0 +1,26 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2022 William Wilgus
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22
23#ifndef __STRPTOKSPN_R_H__
24#define __STRPTOKSPN_R_H__
25const char *strptokspn_r(const char *ptr, const char *sep, size_t *len, const char **end);
26#endif