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 | ||
60 | #ifdef __STDC__ | |
61 | static int | |
62 | prinstpt (SCM exp, SCM port, int writing) | |
63 | #else | |
64 | static int | |
65 | prinstpt (exp, port, writing) | |
66 | SCM exp; | |
67 | SCM port; | |
68 | int writing; | |
69 | #endif | |
70 | { | |
71 | scm_prinport (exp, port, "string"); | |
72 | return !0; | |
73 | } | |
74 | ||
75 | #ifdef __STDC__ | |
76 | static int | |
77 | stputc (int c, SCM p) | |
78 | #else | |
79 | static int | |
80 | stputc (c, p) | |
81 | int c; | |
82 | SCM p; | |
83 | #endif | |
84 | { | |
85 | scm_sizet ind = SCM_INUM (SCM_CAR (p)); | |
86 | SCM_DEFER_INTS; | |
87 | if (ind >= SCM_LENGTH (SCM_CDR (p))) | |
88 | scm_vector_set_length_x (SCM_CDR (p), SCM_MAKINUM (ind + (ind >> 1))); | |
89 | SCM_ALLOW_INTS; | |
90 | SCM_CHARS (SCM_CDR (p))[ind] = c; | |
91 | SCM_CAR (p) = SCM_MAKINUM (ind + 1); | |
92 | return c; | |
93 | } | |
94 | ||
95 | #ifdef __STDC__ | |
96 | static scm_sizet | |
97 | stwrite (char *str, scm_sizet siz, scm_sizet num, SCM p) | |
98 | #else | |
99 | static scm_sizet | |
100 | stwrite (str, siz, num, p) | |
101 | char *str; | |
102 | scm_sizet siz; | |
103 | scm_sizet num; | |
104 | SCM p; | |
105 | #endif | |
106 | { | |
107 | scm_sizet ind = SCM_INUM (SCM_CAR (p)); | |
108 | scm_sizet len = siz * num; | |
109 | char *dst; | |
110 | SCM_DEFER_INTS; | |
111 | if (ind + len >= SCM_LENGTH (SCM_CDR (p))) | |
112 | scm_vector_set_length_x (SCM_CDR (p), SCM_MAKINUM (ind + len + ((ind + len) >> 1))); | |
113 | SCM_ALLOW_INTS; | |
114 | dst = &(SCM_CHARS (SCM_CDR (p))[ind]); | |
115 | while (len--) | |
116 | dst[len] = str[len]; | |
117 | SCM_CAR (p) = SCM_MAKINUM (ind + siz * num); | |
118 | return num; | |
119 | } | |
120 | ||
121 | #ifdef __STDC__ | |
122 | static int | |
123 | stputs (char *s, SCM p) | |
124 | #else | |
125 | static int | |
126 | stputs (s, p) | |
127 | char *s; | |
128 | SCM p; | |
129 | #endif | |
130 | { | |
131 | stwrite (s, 1, strlen (s), p); | |
132 | return 0; | |
133 | } | |
134 | ||
135 | #ifdef __STDC__ | |
136 | static int | |
137 | stgetc (SCM p) | |
138 | #else | |
139 | static int | |
140 | stgetc (p) | |
141 | SCM p; | |
142 | #endif | |
143 | { | |
144 | scm_sizet ind = SCM_INUM (SCM_CAR (p)); | |
145 | if (ind >= SCM_ROLENGTH (SCM_CDR (p))) | |
146 | return EOF; | |
147 | SCM_CAR (p) = SCM_MAKINUM (ind + 1); | |
148 | return SCM_ROUCHARS (SCM_CDR (p))[ind]; | |
149 | } | |
150 | ||
151 | #ifdef __STDC__ | |
152 | SCM | |
153 | scm_mkstrport (SCM pos, SCM str, long modes, char * caller) | |
154 | #else | |
155 | SCM | |
156 | scm_mkstrport (pos, str, modes, caller) | |
157 | SCM pos; | |
158 | SCM str; | |
159 | long modes; | |
160 | char * caller; | |
161 | #endif | |
162 | { | |
163 | SCM z; | |
164 | SCM stream; | |
165 | struct scm_port_table * pt; | |
166 | ||
167 | SCM_ASSERT(SCM_INUMP(pos) && SCM_INUM(pos) >= 0, pos, SCM_ARG1, caller); | |
168 | SCM_ASSERT(SCM_NIMP(str) && SCM_ROSTRINGP(str), str, SCM_ARG1, caller); | |
169 | stream = scm_cons(pos, str); | |
170 | SCM_NEWCELL (z); | |
171 | SCM_DEFER_INTS; | |
172 | pt = scm_add_to_port_table (z); | |
173 | SCM_CAR (z) = scm_tc16_strport | modes; | |
174 | SCM_SETPTAB_ENTRY (z, pt); | |
175 | SCM_SETSTREAM (z, stream); | |
176 | SCM_ALLOW_INTS; | |
177 | return z; | |
178 | } | |
179 | ||
180 | SCM_PROC(s_call_with_output_string, "call-with-output-string", 1, 0, 0, scm_call_with_output_string); | |
181 | #ifdef __STDC__ | |
182 | SCM | |
183 | scm_call_with_output_string (SCM proc) | |
184 | #else | |
185 | SCM | |
186 | scm_call_with_output_string (proc) | |
187 | SCM proc; | |
188 | #endif | |
189 | { | |
190 | SCM p; | |
191 | p = scm_mkstrport(SCM_INUM0, scm_make_string(SCM_MAKINUM(30), SCM_UNDEFINED), | |
192 | SCM_OPN | SCM_WRTNG, | |
193 | s_call_with_output_string); | |
194 | scm_apply (proc, p, scm_listofnull); | |
195 | { | |
196 | SCM answer; | |
197 | SCM_DEFER_INTS; | |
198 | answer = scm_makfromstr (SCM_CHARS (SCM_CDR (SCM_STREAM (p))), | |
199 | SCM_INUM (SCM_CAR (SCM_STREAM (p))), | |
200 | 0); | |
201 | SCM_ALLOW_INTS; | |
202 | return answer; | |
203 | } | |
204 | } | |
205 | ||
206 | ||
207 | ||
208 | /* Return a Scheme string obtained by printing a given object. | |
209 | */ | |
210 | ||
211 | #ifdef __STDC__ | |
212 | SCM | |
213 | scm_strprint_obj (SCM obj) | |
214 | #else | |
215 | SCM | |
216 | scm_strprint_obj (obj) | |
217 | SCM obj; | |
218 | #endif | |
219 | { | |
220 | SCM str; | |
221 | SCM port; | |
222 | ||
223 | str = scm_makstr (64, 0); | |
224 | port = scm_mkstrport (SCM_MAKINUM (0), str, SCM_OPN | SCM_WRTNG, "scm_strprint_obj"); | |
87818069 | 225 | scm_prin1 (obj, port, 1); |
0f2d19dd JB |
226 | { |
227 | SCM answer; | |
228 | SCM_DEFER_INTS; | |
229 | answer = scm_makfromstr (SCM_CHARS (SCM_CDR (SCM_STREAM (port))), | |
230 | SCM_INUM (SCM_CAR (SCM_STREAM (port))), | |
231 | 0); | |
232 | SCM_ALLOW_INTS; | |
233 | return answer; | |
234 | } | |
235 | } | |
236 | ||
237 | ||
238 | ||
239 | ||
240 | SCM_PROC(s_call_with_input_string, "call-with-input-string", 2, 0, 0, scm_call_with_input_string); | |
241 | #ifdef __STDC__ | |
242 | SCM | |
243 | scm_call_with_input_string (SCM str, SCM proc) | |
244 | #else | |
245 | SCM | |
246 | scm_call_with_input_string (str, proc) | |
247 | SCM str; | |
248 | SCM proc; | |
249 | #endif | |
250 | { | |
251 | SCM p = scm_mkstrport(SCM_INUM0, str, SCM_OPN | SCM_RDNG, s_call_with_input_string); | |
252 | return scm_apply (proc, p, scm_listofnull); | |
253 | } | |
254 | ||
255 | #ifdef __STDC__ | |
256 | static int | |
257 | noop0 (FILE *stream) | |
258 | #else | |
259 | static int | |
260 | noop0 (stream) | |
261 | FILE *stream; | |
262 | #endif | |
263 | { | |
264 | return 0; | |
265 | } | |
266 | ||
267 | ||
268 | scm_ptobfuns scm_stptob = | |
269 | { | |
270 | scm_markstream, | |
271 | noop0, | |
272 | prinstpt, | |
273 | 0, | |
274 | stputc, | |
275 | stputs, | |
276 | stwrite, | |
277 | noop0, | |
278 | stgetc, | |
279 | 0 | |
280 | }; | |
281 | ||
282 | ||
283 | #ifdef __STDC__ | |
284 | void | |
285 | scm_init_strports (void) | |
286 | #else | |
287 | void | |
288 | scm_init_strports () | |
289 | #endif | |
290 | { | |
291 | #include "strports.x" | |
292 | } | |
293 |