diff options
author | William Wilgus <me.theuser@yahoo.com> | 2018-10-29 02:54:35 -0400 |
---|---|---|
committer | William Wilgus <me.theuser@yahoo.com> | 2018-10-29 23:22:35 -0400 |
commit | eab73b3deead4054ba8a1d9165b577ad935b9a05 (patch) | |
tree | adf00c7efe610bb268a26109aa7cd9de3b26ca09 /apps/plugins/lua/rockaux.c | |
parent | cc0a4c632aeda82ab4517355b4b4489190da013e (diff) | |
download | rockbox-eab73b3deead4054ba8a1d9165b577ad935b9a05.tar.gz rockbox-eab73b3deead4054ba8a1d9165b577ad935b9a05.zip |
Lua replace fscanf
Rocklua was using the full fscanf implementation
to simply read %ld for the file:read("*n") function
wasting 1k on unneeded/unused functionality
Instead, I've implemented a filetol function to duplicate it
without the extra overhead using strtol which as an added bonus
ERANGE errors now resolve to LONG_MIN and LONGMAX instead of
integer overflow
filetol()
reads long int from an open file, skips preceding
whitespaces returns -1 if error, 1 on success.
*num set to LONG_MAX or LONG_MIN on overflow.
If number of digits is > than LUAI_MAXNUMBER2STR
filepointer will continue till the next non digit
but buffer will stop being filled with characters.
Preceding zero is ignored.
Change-Id: Ia42d0f73c63a894625bca4581e9b7e1cc7387fd2
Diffstat (limited to 'apps/plugins/lua/rockaux.c')
-rw-r--r-- | apps/plugins/lua/rockaux.c | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/apps/plugins/lua/rockaux.c b/apps/plugins/lua/rockaux.c index ba3a37343b..562d1654a7 100644 --- a/apps/plugins/lua/rockaux.c +++ b/apps/plugins/lua/rockaux.c | |||
@@ -8,6 +8,7 @@ | |||
8 | * $Id$ | 8 | * $Id$ |
9 | * | 9 | * |
10 | * Copyright (C) 2008 Dan Everton (safetydan) | 10 | * Copyright (C) 2008 Dan Everton (safetydan) |
11 | * Copyright (C) 2018 William Wilgus | ||
11 | * String function implementations taken from dietlibc 0.31 (GPLv2 License) | 12 | * String function implementations taken from dietlibc 0.31 (GPLv2 License) |
12 | * | 13 | * |
13 | * This program is free software; you can redistribute it and/or | 14 | * This program is free software; you can redistribute it and/or |
@@ -24,6 +25,8 @@ | |||
24 | #define _ROCKCONF_H_ /* Protect against unwanted include */ | 25 | #define _ROCKCONF_H_ /* Protect against unwanted include */ |
25 | #include "lua.h" | 26 | #include "lua.h" |
26 | 27 | ||
28 | extern long strtol(const char *nptr, char **endptr, int base); | ||
29 | |||
27 | #if (CONFIG_PLATFORM & PLATFORM_NATIVE) | 30 | #if (CONFIG_PLATFORM & PLATFORM_NATIVE) |
28 | int errno = 0; | 31 | int errno = 0; |
29 | #endif | 32 | #endif |
@@ -101,3 +104,63 @@ int get_current_path(lua_State *L, int level) | |||
101 | lua_pushnil(L); | 104 | lua_pushnil(L); |
102 | return 1; | 105 | return 1; |
103 | } | 106 | } |
107 | |||
108 | /* filetol() | ||
109 | reads long int from an open file, skips preceding | ||
110 | whitespaces returns -1 if error, 1 on success. | ||
111 | *num set to LONG_MAX or LONG_MIN on overflow. | ||
112 | If number of digits is > than LUAI_MAXNUMBER2STR | ||
113 | filepointer will continue till the next non digit | ||
114 | but buffer will stop being filled with characters. | ||
115 | Preceding zero is ignored | ||
116 | */ | ||
117 | int filetol(int fd, long *num) | ||
118 | { | ||
119 | static char buffer[LUAI_MAXNUMBER2STR]; | ||
120 | int retn = -1; | ||
121 | char chbuf = 0; | ||
122 | size_t count = 0; | ||
123 | bool neg = false; | ||
124 | long val; | ||
125 | |||
126 | while (rb->read(fd, &chbuf, 1) == 1) | ||
127 | { | ||
128 | if(!isspace(chbuf) || retn == 1) | ||
129 | { | ||
130 | if(chbuf == '0') /* strip preceeding zeros */ | ||
131 | { | ||
132 | *num = 0; | ||
133 | retn = 1; | ||
134 | } | ||
135 | else if(chbuf == '-' && retn != 1) | ||
136 | neg = true; | ||
137 | else | ||
138 | { | ||
139 | rb->lseek(fd, -1, SEEK_CUR); | ||
140 | break; | ||
141 | } | ||
142 | } | ||
143 | } | ||
144 | |||
145 | while (rb->read(fd, &chbuf, 1) == 1) | ||
146 | { | ||
147 | if(!isdigit(chbuf)) | ||
148 | { | ||
149 | rb->lseek(fd, -1, SEEK_CUR); | ||
150 | break; | ||
151 | } | ||
152 | else if (count < LUAI_MAXNUMBER2STR - 2) | ||
153 | buffer[count++] = chbuf; | ||
154 | } | ||
155 | |||
156 | if(count) | ||
157 | { | ||
158 | buffer[count] = '\0'; | ||
159 | val = strtol(buffer, NULL, 10); | ||
160 | *num = (neg)? -val:val; | ||
161 | retn = 1; | ||
162 | } | ||
163 | |||
164 | return retn; | ||
165 | } | ||
166 | |||