diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-08-09 17:16:40 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-08-09 17:16:40 +0000 |
commit | 69f4f9a6f40e2825c93fad4d3adf453c9b33d9bb (patch) | |
tree | 5e35a08d3b3db69b6dc212cbdf681d78bf4425ff /libbb/get_line_from_file.c | |
parent | 3fd15e197e21aa313ce56126ee814f0ebc884dee (diff) | |
download | busybox-w32-69f4f9a6f40e2825c93fad4d3adf453c9b33d9bb.tar.gz busybox-w32-69f4f9a6f40e2825c93fad4d3adf453c9b33d9bb.tar.bz2 busybox-w32-69f4f9a6f40e2825c93fad4d3adf453c9b33d9bb.zip |
optimize config_read() (by Timo Teras <timo.teras AT iki.fi>)
function old new delta
bb_get_chunk_with_continuation - 176 +176
find_pair 169 187 +18
...
process_stdin 443 433 -10
config_read 549 456 -93
bb_get_chunk_from_file 139 7 -132
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 7/7 up/down: 215/-254) Total: -39 bytes
Diffstat (limited to 'libbb/get_line_from_file.c')
-rw-r--r-- | libbb/get_line_from_file.c | 30 |
1 files changed, 23 insertions, 7 deletions
diff --git a/libbb/get_line_from_file.c b/libbb/get_line_from_file.c index 968d7572d..3cb46d240 100644 --- a/libbb/get_line_from_file.c +++ b/libbb/get_line_from_file.c | |||
@@ -9,18 +9,22 @@ | |||
9 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | 9 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
10 | */ | 10 | */ |
11 | 11 | ||
12 | /* for getline() [GNUism] */ | 12 | /* for getline() [GNUism] |
13 | #ifndef _GNU_SOURCE | 13 | #ifndef _GNU_SOURCE |
14 | #define _GNU_SOURCE 1 | 14 | #define _GNU_SOURCE 1 |
15 | #endif | 15 | #endif |
16 | */ | ||
16 | #include "libbb.h" | 17 | #include "libbb.h" |
17 | 18 | ||
18 | /* This function reads an entire line from a text file, up to a newline | 19 | /* This function reads an entire line from a text file, up to a newline |
19 | * or NUL byte, inclusive. It returns a malloc'ed char * which | 20 | * or NUL byte, inclusive. It returns a malloc'ed char * which |
20 | * must be free'ed by the caller. If end is NULL '\n' isn't considered | 21 | * must be free'ed by the caller. If end is NULL '\n' isn't considered |
21 | * end of line. If end isn't NULL, length of the chunk read is stored in it. | 22 | * end of line. If end isn't NULL, length of the chunk is stored in it. |
22 | * Return NULL if EOF/error */ | 23 | * If lineno is not NULL, *lineno is incremented for each line, |
23 | char* FAST_FUNC bb_get_chunk_from_file(FILE *file, int *end) | 24 | * and also trailing '\' is recognized as line continuation. |
25 | * | ||
26 | * Returns NULL if EOF/error. */ | ||
27 | char* FAST_FUNC bb_get_chunk_with_continuation(FILE *file, int *end, int *lineno) | ||
24 | { | 28 | { |
25 | int ch; | 29 | int ch; |
26 | int idx = 0; | 30 | int idx = 0; |
@@ -30,12 +34,20 @@ char* FAST_FUNC bb_get_chunk_from_file(FILE *file, int *end) | |||
30 | while ((ch = getc(file)) != EOF) { | 34 | while ((ch = getc(file)) != EOF) { |
31 | /* grow the line buffer as necessary */ | 35 | /* grow the line buffer as necessary */ |
32 | if (idx >= linebufsz) { | 36 | if (idx >= linebufsz) { |
33 | linebufsz += 80; | 37 | linebufsz += 256; |
34 | linebuf = xrealloc(linebuf, linebufsz); | 38 | linebuf = xrealloc(linebuf, linebufsz); |
35 | } | 39 | } |
36 | linebuf[idx++] = (char) ch; | 40 | linebuf[idx++] = (char) ch; |
37 | if (!ch || (end && ch == '\n')) | 41 | if (!ch) |
38 | break; | 42 | break; |
43 | if (end && ch == '\n') { | ||
44 | if (lineno == NULL) | ||
45 | break; | ||
46 | (*lineno)++; | ||
47 | if (idx < 2 || linebuf[idx-2] != '\\') | ||
48 | break; | ||
49 | idx -= 2; | ||
50 | } | ||
39 | } | 51 | } |
40 | if (end) | 52 | if (end) |
41 | *end = idx; | 53 | *end = idx; |
@@ -52,6 +64,11 @@ char* FAST_FUNC bb_get_chunk_from_file(FILE *file, int *end) | |||
52 | return linebuf; | 64 | return linebuf; |
53 | } | 65 | } |
54 | 66 | ||
67 | char* FAST_FUNC bb_get_chunk_from_file(FILE *file, int *end) | ||
68 | { | ||
69 | return bb_get_chunk_with_continuation(file, end, NULL); | ||
70 | } | ||
71 | |||
55 | /* Get line, including trailing \n if any */ | 72 | /* Get line, including trailing \n if any */ |
56 | char* FAST_FUNC xmalloc_fgets(FILE *file) | 73 | char* FAST_FUNC xmalloc_fgets(FILE *file) |
57 | { | 74 | { |
@@ -72,7 +89,6 @@ char* FAST_FUNC xmalloc_fgetline(FILE *file) | |||
72 | } | 89 | } |
73 | 90 | ||
74 | #if 0 | 91 | #if 0 |
75 | |||
76 | /* GNUism getline() should be faster (not tested) than a loop with fgetc */ | 92 | /* GNUism getline() should be faster (not tested) than a loop with fgetc */ |
77 | 93 | ||
78 | /* Get line, including trailing \n if any */ | 94 | /* Get line, including trailing \n if any */ |