Commit | Line | Data |
---|---|---|
ee2a8b9b JB |
1 | /* Copyright (C) 1995,1996,1987 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 | /* data initialization and C<->Scheme data conversion */ | |
44 | ||
45 | #include <stdio.h> | |
46 | ||
47 | #include <gh.h> | |
48 | ||
49 | /* data conversion C->scheme */ | |
50 | SCM | |
51 | gh_int2scmb (int x) | |
52 | { | |
53 | return (x ? SCM_BOOL_T : SCM_BOOL_F); | |
54 | } | |
55 | SCM | |
56 | gh_int2scm (int x) | |
57 | { | |
58 | return scm_long2num ((long) x); | |
59 | } | |
60 | SCM | |
61 | gh_ulong2scm (unsigned long x) | |
62 | { | |
63 | return scm_ulong2num (x); | |
64 | } | |
65 | SCM | |
66 | gh_long2scm (long x) | |
67 | { | |
68 | return scm_long2num (x); | |
69 | } | |
70 | SCM | |
71 | gh_double2scm (double x) | |
72 | { | |
73 | return scm_makdbl (x, 0.0); | |
74 | } | |
75 | SCM | |
76 | gh_char2scm (char c) | |
77 | { | |
78 | return SCM_MAKICHR (c); | |
79 | } | |
80 | SCM | |
81 | gh_str2scm (char *s, int len) | |
82 | { | |
83 | return scm_makfromstr (s, len, 0); | |
84 | } | |
85 | SCM | |
86 | gh_str02scm (char *s) | |
87 | { | |
88 | return scm_makfrom0str (s); | |
89 | } | |
90 | /* Copy LEN characters at SRC into the *existing* Scheme string DST, | |
91 | starting at START. START is an index into DST; zero means the | |
92 | beginning of the string. | |
93 | ||
94 | If START + LEN is off the end of DST, signal an out-of-range | |
95 | error. */ | |
96 | void | |
97 | gh_set_substr (char *src, SCM dst, int start, int len) | |
98 | { | |
99 | char *dst_ptr, dst_len, effective_length; | |
100 | ||
101 | SCM_ASSERT (SCM_NIMP (dst) && SCM_STRINGP (dst), dst, SCM_ARG3, | |
102 | "gh_set_substr"); | |
103 | scm_protect_object (dst); | |
104 | dst_ptr = SCM_CHARS (dst); | |
105 | dst_len = SCM_LENGTH (dst); | |
106 | effective_length = (len < dst_len) ? len : dst_len; | |
107 | memcpy (dst_ptr + start, src, effective_length); | |
108 | /* FIXME: must signal an error if len > dst_len */ | |
109 | scm_unprotect_object (dst); | |
110 | } | |
111 | ||
112 | /* Return the symbol named SYMBOL_STR. */ | |
113 | SCM | |
114 | gh_symbol2scm (char *symbol_str) | |
115 | { | |
116 | return SCM_CAR (scm_intern (symbol_str, strlen (symbol_str))); | |
117 | } | |
118 | ||
119 | ||
120 | /* data conversion scheme->C */ | |
121 | int | |
122 | gh_scm2bool (SCM obj) | |
123 | { | |
124 | return ((obj) == SCM_BOOL_F) ? 0 : 1; | |
125 | } | |
126 | unsigned long | |
127 | gh_scm2ulong (SCM obj) | |
128 | { | |
129 | return scm_num2ulong (obj, (char *) SCM_ARG1, "gh_scm2ulong"); | |
130 | } | |
131 | long | |
132 | gh_scm2long (SCM obj) | |
133 | { | |
134 | return scm_num2long (obj, (char *) SCM_ARG1, "gh_scm2long"); | |
135 | } | |
136 | int | |
137 | gh_scm2int (SCM obj) | |
138 | { | |
139 | /* NOTE: possible loss of precision here */ | |
140 | return (int) scm_num2long (obj, (char *) SCM_ARG1, "gh_scm2int"); | |
141 | } | |
142 | double | |
143 | gh_scm2double (SCM obj) | |
144 | { | |
145 | return scm_num2dbl (obj, "gh_scm2double"); | |
146 | } | |
147 | char | |
148 | gh_scm2char (SCM obj) | |
149 | { | |
150 | return SCM_ICHR (obj); | |
151 | } | |
152 | ||
153 | /* string conversions between C and Scheme */ | |
154 | ||
155 | /* gh_scm2newstr() -- Given a Scheme string STR, return a pointer to a | |
156 | new copy of its contents, followed by a null byte. If lenp is | |
157 | non-null, set *lenp to the string's length. | |
158 | ||
159 | This function uses malloc to obtain storage for the copy; the | |
160 | caller is responsible for freeing it. | |
161 | ||
162 | Note that Scheme strings may contain arbitrary data, including null | |
163 | characters. This means that null termination is not a reliable way | |
164 | to determine the length of the returned value. However, the | |
165 | function always copies the complete contents of STR, and sets | |
166 | *LEN_P to the true length of the string (when LEN_P is non-null). */ | |
167 | char * | |
168 | gh_scm2newstr (SCM str, int *lenp) | |
169 | { | |
170 | char *ret_str; | |
171 | int len; | |
172 | ||
173 | SCM_ASSERT (SCM_NIMP (str) && SCM_STRINGP (str), str, SCM_ARG3, | |
174 | "gh_scm2newstr"); | |
175 | ||
176 | /* protect str from GC while we copy off its data */ | |
177 | scm_protect_object (str); | |
178 | ||
179 | len = SCM_LENGTH (str); | |
180 | ||
181 | ret_str = (char *) malloc ((len + 1) * sizeof (char)); | |
182 | /* so we copy tmp_str to ret_str, which is what we will allocate */ | |
183 | memcpy (ret_str, SCM_CHARS (str), len); | |
184 | /* now make sure we null-terminate it */ | |
185 | ret_str[len] = '\0'; | |
186 | ||
187 | scm_unprotect_object (str); | |
188 | ||
189 | if (lenp != NULL) | |
190 | { | |
191 | *lenp = len; | |
192 | } | |
193 | ||
194 | return ret_str; | |
195 | } | |
196 | ||
197 | ||
198 | /* Copy LEN characters at START from the Scheme string SRC to memory | |
199 | at DST. START is an index into SRC; zero means the beginning of | |
200 | the string. DST has already been allocated by the caller. | |
201 | ||
202 | If START + LEN is off the end of SRC, silently truncate the source | |
203 | region to fit the string. If truncation occurs, the corresponding | |
204 | area of DST is left unchanged. */ | |
205 | void | |
206 | gh_get_substr (SCM src, char *dst, int start, int len) | |
207 | { | |
208 | int src_len, effective_length; | |
209 | SCM_ASSERT (SCM_NIMP (src) && SCM_STRINGP (src), src, SCM_ARG3, | |
210 | "gh_get_substr"); | |
211 | ||
212 | scm_protect_object (src); | |
213 | src_len = SCM_LENGTH (src); | |
214 | effective_length = (len < src_len) ? len : src_len; | |
215 | memcpy (dst + start, SCM_CHARS (src), effective_length * sizeof (char)); | |
216 | /* FIXME: must signal an error if len > src_len */ | |
217 | scm_unprotect_object (src); | |
218 | } | |
219 | ||
220 | ||
221 | /* gh_scm2newsymbol() -- Given a Scheme symbol 'identifier, return a | |
222 | pointer to a string with the symbol characters "identifier", | |
223 | followed by a null byte. If lenp is non-null, set *lenp to the | |
224 | string's length. | |
225 | ||
226 | This function uses malloc to obtain storage for the copy; the | |
227 | caller is responsible for freeing it. */ | |
228 | char * | |
229 | gh_symbol2newstr (SCM sym, int *lenp) | |
230 | { | |
231 | char *ret_str; | |
232 | int len; | |
233 | ||
234 | SCM_ASSERT (SCM_NIMP (sym) && SCM_SYMBOLP (sym), sym, SCM_ARG3, | |
235 | "gh_scm2newsymbol"); | |
236 | ||
237 | /* protect str from GC while we copy off its data */ | |
238 | scm_protect_object (sym); | |
239 | ||
240 | len = SCM_LENGTH (sym); | |
241 | ||
242 | ret_str = (char *) malloc ((len + 1) * sizeof (char)); | |
243 | /* so we copy tmp_str to ret_str, which is what we will allocate */ | |
244 | memcpy (ret_str, SCM_CHARS (sym), len); | |
245 | /* now make sure we null-terminate it */ | |
246 | ret_str[len] = '\0'; | |
247 | ||
248 | scm_unprotect_object (sym); | |
249 | ||
250 | if (lenp != NULL) | |
251 | { | |
252 | *lenp = len; | |
253 | } | |
254 | ||
255 | return ret_str; | |
256 | } | |
257 | ||
258 | ||
259 | /* create a new vector of the given length, all initialized to the | |
260 | given value */ | |
261 | SCM | |
262 | gh_vector (SCM length, SCM val) | |
263 | { | |
264 | return scm_make_vector (length, val, SCM_UNDEFINED); | |
265 | } | |
266 | ||
267 | /* set the given element of the given vector to the given value */ | |
268 | SCM | |
269 | gh_vset (SCM vec, SCM pos, SCM val) | |
270 | { | |
271 | return scm_vector_set_x (vec, pos, val); | |
272 | } | |
273 | ||
274 | /* retrieve the given element of the given vector */ | |
275 | SCM | |
276 | gh_vref (SCM vec, SCM pos) | |
277 | { | |
278 | return scm_vector_ref (vec, pos); | |
279 | } | |
280 | ||
281 | /* returns the length of the given vector */ | |
282 | unsigned long | |
283 | gh_vector_length (SCM v) | |
284 | { | |
285 | return gh_scm2ulong (scm_vector_length (v)); | |
286 | } |