3
0

get_line_from_file.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) 2005, 2006 Rob Landley <rob@landley.net>
  6. * Copyright (C) 2004 Erik Andersen <andersen@codepoet.org>
  7. * Copyright (C) 2001 Matt Krai
  8. *
  9. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  10. */
  11. /* for getline() [GNUism]
  12. #ifndef _GNU_SOURCE
  13. #define _GNU_SOURCE 1
  14. #endif
  15. */
  16. #include "libbb.h"
  17. /* This function reads an entire line from a text file, up to a newline
  18. * or NUL byte, inclusive. It returns a malloc'ed char * which
  19. * must be free'ed by the caller. If end is NULL '\n' isn't considered
  20. * end of line. If end isn't NULL, length of the chunk is stored in it.
  21. * If lineno is not NULL, *lineno is incremented for each line,
  22. * and also trailing '\' is recognized as line continuation.
  23. *
  24. * Returns NULL if EOF/error. */
  25. char* FAST_FUNC bb_get_chunk_with_continuation(FILE *file, int *end, int *lineno)
  26. {
  27. int ch;
  28. int idx = 0;
  29. char *linebuf = NULL;
  30. int linebufsz = 0;
  31. while ((ch = getc(file)) != EOF) {
  32. /* grow the line buffer as necessary */
  33. if (idx >= linebufsz) {
  34. linebufsz += 256;
  35. linebuf = xrealloc(linebuf, linebufsz);
  36. }
  37. linebuf[idx++] = (char) ch;
  38. if (!ch)
  39. break;
  40. if (end && ch == '\n') {
  41. if (lineno == NULL)
  42. break;
  43. (*lineno)++;
  44. if (idx < 2 || linebuf[idx-2] != '\\')
  45. break;
  46. idx -= 2;
  47. }
  48. }
  49. if (end)
  50. *end = idx;
  51. if (linebuf) {
  52. // huh, does fgets discard prior data on error like this?
  53. // I don't think so....
  54. //if (ferror(file)) {
  55. // free(linebuf);
  56. // return NULL;
  57. //}
  58. linebuf = xrealloc(linebuf, idx + 1);
  59. linebuf[idx] = '\0';
  60. }
  61. return linebuf;
  62. }
  63. char* FAST_FUNC bb_get_chunk_from_file(FILE *file, int *end)
  64. {
  65. return bb_get_chunk_with_continuation(file, end, NULL);
  66. }
  67. /* Get line, including trailing \n if any */
  68. char* FAST_FUNC xmalloc_fgets(FILE *file)
  69. {
  70. int i;
  71. return bb_get_chunk_from_file(file, &i);
  72. }
  73. /* Get line. Remove trailing \n */
  74. char* FAST_FUNC xmalloc_fgetline(FILE *file)
  75. {
  76. int i;
  77. char *c = bb_get_chunk_from_file(file, &i);
  78. if (i && c[--i] == '\n')
  79. c[i] = '\0';
  80. return c;
  81. }
  82. #if 0
  83. /* GNUism getline() should be faster (not tested) than a loop with fgetc */
  84. /* Get line, including trailing \n if any */
  85. char* FAST_FUNC xmalloc_fgets(FILE *file)
  86. {
  87. char *res_buf = NULL;
  88. size_t res_sz;
  89. if (getline(&res_buf, &res_sz, file) == -1) {
  90. free(res_buf); /* uclibc allocates a buffer even on EOF. WTF? */
  91. res_buf = NULL;
  92. }
  93. //TODO: trimming to res_sz?
  94. return res_buf;
  95. }
  96. /* Get line. Remove trailing \n */
  97. char* FAST_FUNC xmalloc_fgetline(FILE *file)
  98. {
  99. char *res_buf = NULL;
  100. size_t res_sz;
  101. res_sz = getline(&res_buf, &res_sz, file);
  102. if ((ssize_t)res_sz != -1) {
  103. if (res_buf[res_sz - 1] == '\n')
  104. res_buf[--res_sz] = '\0';
  105. //TODO: trimming to res_sz?
  106. } else {
  107. free(res_buf); /* uclibc allocates a buffer even on EOF. WTF? */
  108. res_buf = NULL;
  109. }
  110. return res_buf;
  111. }
  112. #endif
  113. #if 0
  114. /* Faster routines (~twice as fast). +170 bytes. Unused as of 2008-07.
  115. *
  116. * NB: they stop at NUL byte too.
  117. * Performance is important here. Think "grep 50gigabyte_file"...
  118. * Ironically, grep can't use it because of NUL issue.
  119. * We sorely need C lib to provide fgets which reports size!
  120. *
  121. * Update:
  122. * Actually, uclibc and glibc have it. man getline. It's GNUism,
  123. * but very useful one (if it's as fast as this code).
  124. * TODO:
  125. * - currently, sed and sort use bb_get_chunk_from_file and heavily
  126. * depend on its "stop on \n or \0" behavior, and STILL they fail
  127. * to handle all cases with embedded NULs correctly. So:
  128. * - audit sed and sort; convert them to getline FIRST.
  129. * - THEN ditch bb_get_chunk_from_file, replace it with getline.
  130. * - provide getline implementation for non-GNU systems.
  131. */
  132. static char* xmalloc_fgets_internal(FILE *file, int *sizep)
  133. {
  134. int len;
  135. int idx = 0;
  136. char *linebuf = NULL;
  137. while (1) {
  138. char *r;
  139. linebuf = xrealloc(linebuf, idx + 0x100);
  140. r = fgets(&linebuf[idx], 0x100, file);
  141. if (!r) {
  142. /* need to terminate in case this is error
  143. * (EOF puts NUL itself) */
  144. linebuf[idx] = '\0';
  145. break;
  146. }
  147. /* stupid. fgets knows the len, it should report it somehow */
  148. len = strlen(&linebuf[idx]);
  149. idx += len;
  150. if (len != 0xff || linebuf[idx - 1] == '\n')
  151. break;
  152. }
  153. *sizep = idx;
  154. if (idx) {
  155. /* xrealloc(linebuf, idx + 1) is up to caller */
  156. return linebuf;
  157. }
  158. free(linebuf);
  159. return NULL;
  160. }
  161. /* Get line, remove trailing \n */
  162. char* FAST_FUNC xmalloc_fgetline_fast(FILE *file)
  163. {
  164. int sz;
  165. char *r = xmalloc_fgets_internal(file, &sz);
  166. if (r && r[sz - 1] == '\n')
  167. r[--sz] = '\0';
  168. return r; /* not xrealloc(r, sz + 1)! */
  169. }
  170. char* FAST_FUNC xmalloc_fgets(FILE *file)
  171. {
  172. int sz;
  173. return xmalloc_fgets_internal(file, &sz);
  174. }
  175. /* Get line, remove trailing \n */
  176. char* FAST_FUNC xmalloc_fgetline(FILE *file)
  177. {
  178. int sz;
  179. char *r = xmalloc_fgets_internal(file, &sz);
  180. if (!r)
  181. return r;
  182. if (r[sz - 1] == '\n')
  183. r[--sz] = '\0';
  184. return xrealloc(r, sz + 1);
  185. }
  186. #endif