summaryrefslogtreecommitdiff
path: root/apps/misc.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/misc.c')
-rw-r--r--apps/misc.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/apps/misc.c b/apps/misc.c
index 4be9e43fd9..0936e85569 100644
--- a/apps/misc.c
+++ b/apps/misc.c
@@ -217,6 +217,53 @@ int read_line(int fd, char* buffer, int buffer_size)
217 return errno ? -1 : num_read; 217 return errno ? -1 : num_read;
218} 218}
219 219
220/* Performance optimized version of the previous function. */
221int fast_readline(int fd, char *buf, int buf_size, void *parameters,
222 int (*callback)(int n, const char *buf, void *parameters))
223{
224 char *p, *next;
225 int rc, pos = 0;
226 int count = 0;
227
228 while ( 1 )
229 {
230 next = NULL;
231
232 rc = read(fd, &buf[pos], buf_size - pos - 1);
233 if (rc >= 0)
234 buf[pos+rc] = '\0';
235
236 if ( (p = strchr(buf, '\r')) != NULL)
237 {
238 *p = '\0';
239 next = ++p;
240 }
241 else
242 p = buf;
243
244 if ( (p = strchr(p, '\n')) != NULL)
245 {
246 *p = '\0';
247 next = ++p;
248 }
249
250 rc = callback(count, buf, parameters);
251 if (rc < 0)
252 return rc;
253
254 count++;
255 if (next)
256 {
257 pos = buf_size - ((long)next - (long)buf) - 1;
258 memmove(buf, next, pos);
259 }
260 else
261 break ;
262 }
263
264 return 0;
265}
266
220#ifdef HAVE_LCD_BITMAP 267#ifdef HAVE_LCD_BITMAP
221 268
222#if LCD_DEPTH == 16 269#if LCD_DEPTH == 16