Commit | Line | Data |
---|---|---|
0f2d19dd JB |
1 | /* Copyright (C) 1995,1996 Free Software Foundation, Inc. |
2 | * | |
3 | * This program is free software; you can redistribute it and/or modify | |
4 | * it under the terms of the GNU General Public License as published by | |
5 | * the Free Software Foundation; either version 2, or (at your option) | |
6 | * any later version. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * along with this software; see the file COPYING. If not, write to | |
15 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
16 | * | |
17 | * As a special exception, the Free Software Foundation gives permission | |
18 | * for additional uses of the text contained in its release of GUILE. | |
19 | * | |
20 | * The exception is that, if you link the GUILE library with other files | |
21 | * to produce an executable, this does not by itself cause the | |
22 | * resulting executable to be covered by the GNU General Public License. | |
23 | * Your use of that executable is in no way restricted on account of | |
24 | * linking the GUILE library code into it. | |
25 | * | |
26 | * This exception does not however invalidate any other reasons why | |
27 | * the executable file might be covered by the GNU General Public License. | |
28 | * | |
29 | * This exception applies only to the code released by the | |
30 | * Free Software Foundation under the name GUILE. If you copy | |
31 | * code from other Free Software Foundation releases into a copy of | |
32 | * GUILE, as the General Public License permits, the exception does | |
33 | * not apply to the code that you add in this way. To avoid misleading | |
34 | * anyone as to the status of such modified files, you must delete | |
35 | * this exception notice from them. | |
36 | * | |
37 | * If you write modifications of your own for GUILE, it is your choice | |
38 | * whether to permit this exception to apply to your modifications. | |
39 | * If you do not wish that, delete this exception notice. | |
40 | */ | |
41 | \f | |
42 | ||
43 | #include <stdio.h> | |
44 | #include "_scm.h" | |
20e6290e JB |
45 | #include "unif.h" |
46 | #include "eval.h" | |
20e6290e JB |
47 | |
48 | #include "strports.h" | |
0f2d19dd | 49 | |
95b88819 GH |
50 | #ifdef HAVE_STRING_H |
51 | #include <string.h> | |
52 | #endif | |
53 | ||
0f2d19dd JB |
54 | \f |
55 | ||
56 | /* {Ports - string ports} | |
57 | * | |
58 | */ | |
59 | ||
1cc91f1b JB |
60 | |
61 | static int prinstpt SCM_P ((SCM exp, SCM port, scm_print_state *pstate)); | |
62 | ||
0f2d19dd | 63 | static int |
1cc91f1b | 64 | prinstpt (exp, port, pstate) |
0f2d19dd JB |
65 | SCM exp; |
66 | SCM port; | |
1cc91f1b | 67 | scm_print_state *pstate; |
0f2d19dd JB |
68 | { |
69 | scm_prinport (exp, port, "string"); | |
70 | return !0; | |
71 | } | |
72 | ||
1cc91f1b JB |
73 | |
74 | static int stputc SCM_P ((int c, SCM p)); | |
75 | ||
0f2d19dd JB |
76 | static int |
77 | stputc (c, p) | |
78 | int c; | |
79 | SCM p; | |
0f2d19dd JB |
80 | { |
81 | scm_sizet ind = SCM_INUM (SCM_CAR (p)); | |
82 | SCM_DEFER_INTS; | |
83 | if (ind >= SCM_LENGTH (SCM_CDR (p))) | |
84 | scm_vector_set_length_x (SCM_CDR (p), SCM_MAKINUM (ind + (ind >> 1))); | |
85 | SCM_ALLOW_INTS; | |
86 | SCM_CHARS (SCM_CDR (p))[ind] = c; | |
a6c64c3c | 87 | SCM_SETCAR (p, SCM_MAKINUM (ind + 1)); |
0f2d19dd JB |
88 | return c; |
89 | } | |
90 | ||
1cc91f1b JB |
91 | |
92 | static scm_sizet stwrite SCM_P ((char *str, scm_sizet siz, scm_sizet num, SCM p)); | |
93 | ||
0f2d19dd JB |
94 | static scm_sizet |
95 | stwrite (str, siz, num, p) | |
96 | char *str; | |
97 | scm_sizet siz; | |
98 | scm_sizet num; | |
99 | SCM p; | |
0f2d19dd JB |
100 | { |
101 | scm_sizet ind = SCM_INUM (SCM_CAR (p)); | |
102 | scm_sizet len = siz * num; | |
103 | char *dst; | |
104 | SCM_DEFER_INTS; | |
105 | if (ind + len >= SCM_LENGTH (SCM_CDR (p))) | |
106 | scm_vector_set_length_x (SCM_CDR (p), SCM_MAKINUM (ind + len + ((ind + len) >> 1))); | |
107 | SCM_ALLOW_INTS; | |
108 | dst = &(SCM_CHARS (SCM_CDR (p))[ind]); | |
109 | while (len--) | |
110 | dst[len] = str[len]; | |
a6c64c3c | 111 | SCM_SETCAR (p, SCM_MAKINUM (ind + siz * num)); |
0f2d19dd JB |
112 | return num; |
113 | } | |
114 | ||
1cc91f1b JB |
115 | |
116 | static int stputs SCM_P ((char *s, SCM p)); | |
117 | ||
0f2d19dd JB |
118 | static int |
119 | stputs (s, p) | |
120 | char *s; | |
121 | SCM p; | |
0f2d19dd JB |
122 | { |
123 | stwrite (s, 1, strlen (s), p); | |
124 | return 0; | |
125 | } | |
126 | ||
1cc91f1b JB |
127 | |
128 | static int stgetc SCM_P ((SCM p)); | |
129 | ||
0f2d19dd JB |
130 | static int |
131 | stgetc (p) | |
132 | SCM p; | |
0f2d19dd JB |
133 | { |
134 | scm_sizet ind = SCM_INUM (SCM_CAR (p)); | |
135 | if (ind >= SCM_ROLENGTH (SCM_CDR (p))) | |
136 | return EOF; | |
a6c64c3c | 137 | SCM_SETCAR (p, SCM_MAKINUM (ind + 1)); |
0f2d19dd JB |
138 | return SCM_ROUCHARS (SCM_CDR (p))[ind]; |
139 | } | |
140 | ||
1cc91f1b | 141 | |
0f2d19dd JB |
142 | SCM |
143 | scm_mkstrport (pos, str, modes, caller) | |
144 | SCM pos; | |
145 | SCM str; | |
146 | long modes; | |
147 | char * caller; | |
0f2d19dd JB |
148 | { |
149 | SCM z; | |
150 | SCM stream; | |
151 | struct scm_port_table * pt; | |
152 | ||
153 | SCM_ASSERT(SCM_INUMP(pos) && SCM_INUM(pos) >= 0, pos, SCM_ARG1, caller); | |
154 | SCM_ASSERT(SCM_NIMP(str) && SCM_ROSTRINGP(str), str, SCM_ARG1, caller); | |
155 | stream = scm_cons(pos, str); | |
156 | SCM_NEWCELL (z); | |
157 | SCM_DEFER_INTS; | |
158 | pt = scm_add_to_port_table (z); | |
a6c64c3c | 159 | SCM_SETCAR (z, scm_tc16_strport | modes); |
0f2d19dd JB |
160 | SCM_SETPTAB_ENTRY (z, pt); |
161 | SCM_SETSTREAM (z, stream); | |
162 | SCM_ALLOW_INTS; | |
163 | return z; | |
164 | } | |
165 | ||
166 | SCM_PROC(s_call_with_output_string, "call-with-output-string", 1, 0, 0, scm_call_with_output_string); | |
1cc91f1b | 167 | |
0f2d19dd JB |
168 | SCM |
169 | scm_call_with_output_string (proc) | |
170 | SCM proc; | |
0f2d19dd JB |
171 | { |
172 | SCM p; | |
173 | p = scm_mkstrport(SCM_INUM0, scm_make_string(SCM_MAKINUM(30), SCM_UNDEFINED), | |
174 | SCM_OPN | SCM_WRTNG, | |
175 | s_call_with_output_string); | |
176 | scm_apply (proc, p, scm_listofnull); | |
177 | { | |
178 | SCM answer; | |
179 | SCM_DEFER_INTS; | |
180 | answer = scm_makfromstr (SCM_CHARS (SCM_CDR (SCM_STREAM (p))), | |
181 | SCM_INUM (SCM_CAR (SCM_STREAM (p))), | |
182 | 0); | |
183 | SCM_ALLOW_INTS; | |
184 | return answer; | |
185 | } | |
186 | } | |
187 | ||
188 | ||
189 | ||
190 | /* Return a Scheme string obtained by printing a given object. | |
191 | */ | |
192 | ||
1cc91f1b | 193 | |
0f2d19dd JB |
194 | SCM |
195 | scm_strprint_obj (obj) | |
196 | SCM obj; | |
0f2d19dd JB |
197 | { |
198 | SCM str; | |
199 | SCM port; | |
200 | ||
201 | str = scm_makstr (64, 0); | |
202 | port = scm_mkstrport (SCM_MAKINUM (0), str, SCM_OPN | SCM_WRTNG, "scm_strprint_obj"); | |
87818069 | 203 | scm_prin1 (obj, port, 1); |
0f2d19dd JB |
204 | { |
205 | SCM answer; | |
206 | SCM_DEFER_INTS; | |
207 | answer = scm_makfromstr (SCM_CHARS (SCM_CDR (SCM_STREAM (port))), | |
208 | SCM_INUM (SCM_CAR (SCM_STREAM (port))), | |
209 | 0); | |
210 | SCM_ALLOW_INTS; | |
211 | return answer; | |
212 | } | |
213 | } | |
214 | ||
215 | ||
216 | ||
217 | ||
218 | SCM_PROC(s_call_with_input_string, "call-with-input-string", 2, 0, 0, scm_call_with_input_string); | |
1cc91f1b | 219 | |
0f2d19dd JB |
220 | SCM |
221 | scm_call_with_input_string (str, proc) | |
222 | SCM str; | |
223 | SCM proc; | |
0f2d19dd JB |
224 | { |
225 | SCM p = scm_mkstrport(SCM_INUM0, str, SCM_OPN | SCM_RDNG, s_call_with_input_string); | |
226 | return scm_apply (proc, p, scm_listofnull); | |
227 | } | |
228 | ||
1cc91f1b JB |
229 | |
230 | static int noop0 SCM_P ((SCM stream)); | |
231 | ||
0f2d19dd JB |
232 | static int |
233 | noop0 (stream) | |
1cc91f1b | 234 | SCM stream; |
0f2d19dd JB |
235 | { |
236 | return 0; | |
237 | } | |
238 | ||
239 | ||
240 | scm_ptobfuns scm_stptob = | |
241 | { | |
242 | scm_markstream, | |
243 | noop0, | |
244 | prinstpt, | |
245 | 0, | |
246 | stputc, | |
247 | stputs, | |
248 | stwrite, | |
249 | noop0, | |
250 | stgetc, | |
251 | 0 | |
252 | }; | |
253 | ||
254 | ||
1cc91f1b | 255 | |
0f2d19dd JB |
256 | void |
257 | scm_init_strports () | |
0f2d19dd JB |
258 | { |
259 | #include "strports.x" | |
260 | } | |
261 |