rdelim.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2006,
  2. * 2011 Free Software Foundation, Inc.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public License
  6. * as published by the Free Software Foundation; either version 3 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301 USA
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22. #include "libguile/_scm.h"
  23. #include <stdio.h>
  24. #ifdef HAVE_STRING_H
  25. #include <string.h>
  26. #endif
  27. #include "libguile/chars.h"
  28. #include "libguile/modules.h"
  29. #include "libguile/ports.h"
  30. #include "libguile/rdelim.h"
  31. #include "libguile/strings.h"
  32. #include "libguile/strports.h"
  33. #include "libguile/validate.h"
  34. SCM_DEFINE (scm_read_delimited_x, "%read-delimited!", 3, 3, 0,
  35. (SCM delims, SCM str, SCM gobble, SCM port, SCM start, SCM end),
  36. "Read characters from @var{port} into @var{str} until one of the\n"
  37. "characters in the @var{delims} string is encountered. If\n"
  38. "@var{gobble} is true, discard the delimiter character;\n"
  39. "otherwise, leave it in the input stream for the next read. If\n"
  40. "@var{port} is not specified, use the value of\n"
  41. "@code{(current-input-port)}. If @var{start} or @var{end} are\n"
  42. "specified, store data only into the substring of @var{str}\n"
  43. "bounded by @var{start} and @var{end} (which default to the\n"
  44. "beginning and end of the string, respectively).\n"
  45. "\n"
  46. " Return a pair consisting of the delimiter that terminated the\n"
  47. "string and the number of characters read. If reading stopped\n"
  48. "at the end of file, the delimiter returned is the\n"
  49. "@var{eof-object}; if the string was filled without encountering\n"
  50. "a delimiter, this value is @code{#f}.")
  51. #define FUNC_NAME s_scm_read_delimited_x
  52. {
  53. size_t j;
  54. size_t cstart;
  55. size_t cend;
  56. scm_t_wchar c;
  57. size_t num_delims;
  58. SCM_VALIDATE_STRING (1, delims);
  59. num_delims = scm_i_string_length (delims);
  60. SCM_VALIDATE_STRING (2, str);
  61. scm_i_get_substring_spec (scm_i_string_length (str),
  62. start, &cstart, end, &cend);
  63. if (SCM_UNBNDP (port))
  64. port = scm_current_input_port ();
  65. else
  66. SCM_VALIDATE_OPINPORT (4, port);
  67. for (j = cstart; j < cend; j++)
  68. {
  69. size_t k;
  70. c = scm_getc (port);
  71. for (k = 0; k < num_delims; k++)
  72. {
  73. if (scm_i_string_ref (delims, k) == c)
  74. {
  75. if (scm_is_false (gobble))
  76. scm_ungetc (c, port);
  77. return scm_cons (SCM_MAKE_CHAR (c),
  78. scm_from_size_t (j - cstart));
  79. }
  80. }
  81. if (c == EOF)
  82. return scm_cons (SCM_EOF_VAL,
  83. scm_from_size_t (j - cstart));
  84. scm_c_string_set_x (str, j, SCM_MAKE_CHAR (c));
  85. }
  86. return scm_cons (SCM_BOOL_F, scm_from_size_t (j - cstart));
  87. }
  88. #undef FUNC_NAME
  89. /*
  90. * %read-line
  91. * truncates any terminating newline from its input, and returns
  92. * a cons of the string read and its terminating character. Doing
  93. * so makes it easy to implement the hairy `read-line' options
  94. * efficiently in Scheme.
  95. */
  96. SCM_DEFINE (scm_read_line, "%read-line", 0, 1, 0,
  97. (SCM port),
  98. "Read a newline-terminated line from @var{port}, allocating storage as\n"
  99. "necessary. The newline terminator (if any) is removed from the string,\n"
  100. "and a pair consisting of the line and its delimiter is returned. The\n"
  101. "delimiter may be either a newline or the @var{eof-object}; if\n"
  102. "@code{%read-line} is called at the end of file, it returns the pair\n"
  103. "@code{(#<eof> . #<eof>)}.")
  104. #define FUNC_NAME s_scm_read_line
  105. {
  106. /* Threshold under which the only allocation performed is that of the
  107. resulting string and pair. */
  108. #define LINE_BUFFER_SIZE 256
  109. SCM line, strings, result;
  110. scm_t_wchar buf[LINE_BUFFER_SIZE], delim;
  111. size_t index;
  112. if (SCM_UNBNDP (port))
  113. port = scm_current_input_port ();
  114. SCM_VALIDATE_OPINPORT (1,port);
  115. index = 0;
  116. delim = 0;
  117. strings = SCM_BOOL_F;
  118. do
  119. {
  120. if (SCM_UNLIKELY (index >= LINE_BUFFER_SIZE))
  121. {
  122. /* The line is getting longer than BUF so store its current
  123. contents in STRINGS. */
  124. strings = scm_cons (scm_from_utf32_stringn (buf, index),
  125. scm_is_false (strings) ? SCM_EOL : strings);
  126. index = 0;
  127. }
  128. else
  129. {
  130. buf[index] = scm_getc (port);
  131. switch (buf[index])
  132. {
  133. case EOF:
  134. case '\n':
  135. delim = buf[index];
  136. break;
  137. default:
  138. index++;
  139. }
  140. }
  141. }
  142. while (delim == 0);
  143. if (SCM_LIKELY (scm_is_false (strings)))
  144. /* The fast path. */
  145. line = scm_from_utf32_stringn (buf, index);
  146. else
  147. {
  148. /* Aggregate the intermediary results. */
  149. strings = scm_cons (scm_from_utf32_stringn (buf, index), strings);
  150. line = scm_string_concatenate (scm_reverse (strings));
  151. }
  152. if (delim == EOF && scm_i_string_length (line) == 0)
  153. result = scm_cons (SCM_EOF_VAL, SCM_EOF_VAL);
  154. else
  155. result = scm_cons (line,
  156. delim == EOF ? SCM_EOF_VAL : SCM_MAKE_CHAR (delim));
  157. return result;
  158. #undef LINE_BUFFER_SIZE
  159. }
  160. #undef FUNC_NAME
  161. SCM_DEFINE (scm_write_line, "write-line", 1, 1, 0,
  162. (SCM obj, SCM port),
  163. "Display @var{obj} and a newline character to @var{port}. If\n"
  164. "@var{port} is not specified, @code{(current-output-port)} is\n"
  165. "used. This function is equivalent to:\n"
  166. "@lisp\n"
  167. "(display obj [port])\n"
  168. "(newline [port])\n"
  169. "@end lisp")
  170. #define FUNC_NAME s_scm_write_line
  171. {
  172. scm_display (obj, port);
  173. return scm_newline (port);
  174. }
  175. #undef FUNC_NAME
  176. SCM
  177. scm_init_rdelim_builtins (void)
  178. {
  179. #include "libguile/rdelim.x"
  180. return SCM_UNSPECIFIED;
  181. }
  182. void
  183. scm_init_rdelim (void)
  184. {
  185. scm_c_define_gsubr ("%init-rdelim-builtins", 0, 0, 0,
  186. scm_init_rdelim_builtins);
  187. }
  188. /*
  189. Local Variables:
  190. c-file-style: "gnu"
  191. End:
  192. */