* gc.c, tags.h: Doc fixes.
[bpt/guile.git] / libguile / gh_data.c
1 /* Copyright (C) 1995,1996,1997 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, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
37 *
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
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) /* this is being phased out */
52 {
53 return (x ? SCM_BOOL_T : SCM_BOOL_F);
54 }
55 SCM
56 gh_bool2scm (int x)
57 {
58 return (x ? SCM_BOOL_T : SCM_BOOL_F);
59 }
60 SCM
61 gh_int2scm (int x)
62 {
63 return scm_long2num ((long) x);
64 }
65 SCM
66 gh_ulong2scm (unsigned long x)
67 {
68 return scm_ulong2num (x);
69 }
70 SCM
71 gh_long2scm (long x)
72 {
73 return scm_long2num (x);
74 }
75 SCM
76 gh_double2scm (double x)
77 {
78 return scm_makdbl (x, 0.0);
79 }
80 SCM
81 gh_char2scm (char c)
82 {
83 return SCM_MAKICHR (c);
84 }
85 SCM
86 gh_str2scm (char *s, int len)
87 {
88 return scm_makfromstr (s, len, 0);
89 }
90 SCM
91 gh_str02scm (char *s)
92 {
93 return scm_makfrom0str (s);
94 }
95 /* Copy LEN characters at SRC into the *existing* Scheme string DST,
96 starting at START. START is an index into DST; zero means the
97 beginning of the string.
98
99 If START + LEN is off the end of DST, signal an out-of-range
100 error. */
101 void
102 gh_set_substr (char *src, SCM dst, int start, int len)
103 {
104 char *dst_ptr;
105 unsigned long dst_len, effective_length;
106
107 SCM_ASSERT (SCM_NIMP (dst) && SCM_STRINGP (dst), dst, SCM_ARG3,
108 "gh_set_substr");
109 scm_protect_object (dst);
110 dst_ptr = SCM_CHARS (dst);
111 dst_len = SCM_LENGTH (dst);
112 effective_length = (len < dst_len) ? len : dst_len;
113 memcpy (dst_ptr + start, src, effective_length);
114 /* FIXME: must signal an error if len > dst_len */
115 scm_unprotect_object (dst);
116 }
117
118 /* Return the symbol named SYMBOL_STR. */
119 SCM
120 gh_symbol2scm (char *symbol_str)
121 {
122 return SCM_CAR (scm_intern (symbol_str, strlen (symbol_str)));
123 }
124
125
126 /* data conversion scheme->C */
127 int
128 gh_scm2bool (SCM obj)
129 {
130 return ((obj) == SCM_BOOL_F) ? 0 : 1;
131 }
132 unsigned long
133 gh_scm2ulong (SCM obj)
134 {
135 return scm_num2ulong (obj, (char *) SCM_ARG1, "gh_scm2ulong");
136 }
137 long
138 gh_scm2long (SCM obj)
139 {
140 return scm_num2long (obj, (char *) SCM_ARG1, "gh_scm2long");
141 }
142 int
143 gh_scm2int (SCM obj)
144 {
145 /* NOTE: possible loss of precision here */
146 return (int) scm_num2long (obj, (char *) SCM_ARG1, "gh_scm2int");
147 }
148 double
149 gh_scm2double (SCM obj)
150 {
151 return scm_num2dbl (obj, "gh_scm2double");
152 }
153 char
154 gh_scm2char (SCM obj)
155 {
156 return SCM_ICHR (obj);
157 }
158
159 /* string conversions between C and Scheme */
160
161 /* gh_scm2newstr() -- Given a Scheme string STR, return a pointer to a
162 new copy of its contents, followed by a null byte. If lenp is
163 non-null, set *lenp to the string's length.
164
165 This function uses malloc to obtain storage for the copy; the
166 caller is responsible for freeing it.
167
168 Note that Scheme strings may contain arbitrary data, including null
169 characters. This means that null termination is not a reliable way
170 to determine the length of the returned value. However, the
171 function always copies the complete contents of STR, and sets
172 *LEN_P to the true length of the string (when LEN_P is non-null). */
173 char *
174 gh_scm2newstr (SCM str, int *lenp)
175 {
176 char *ret_str;
177 int len;
178
179 SCM_ASSERT (SCM_NIMP (str) && SCM_ROSTRINGP (str), str, SCM_ARG3,
180 "gh_scm2newstr");
181
182 /* protect str from GC while we copy off its data */
183 scm_protect_object (str);
184
185 len = SCM_LENGTH (str);
186
187 ret_str = (char *) scm_must_malloc ((len + 1) * sizeof (char),
188 "gh_scm2newstr");
189 /* so we copy tmp_str to ret_str, which is what we will allocate */
190 memcpy (ret_str, SCM_ROCHARS (str), len); /* test ROCHARS here -twp */
191 /* now make sure we null-terminate it */
192 ret_str[len] = '\0';
193
194 scm_unprotect_object (str);
195
196 if (lenp != NULL)
197 {
198 *lenp = len;
199 }
200
201 return ret_str;
202 }
203
204
205 /* Copy LEN characters at START from the Scheme string SRC to memory
206 at DST. START is an index into SRC; zero means the beginning of
207 the string. DST has already been allocated by the caller.
208
209 If START + LEN is off the end of SRC, silently truncate the source
210 region to fit the string. If truncation occurs, the corresponding
211 area of DST is left unchanged. */
212 void
213 gh_get_substr (SCM src, char *dst, int start, int len)
214 {
215 int src_len, effective_length;
216 SCM_ASSERT (SCM_NIMP (src) && SCM_ROSTRINGP (src), src, SCM_ARG3,
217 "gh_get_substr");
218
219 scm_protect_object (src);
220 src_len = SCM_LENGTH (src);
221 effective_length = (len < src_len) ? len : src_len;
222 memcpy (dst + start, SCM_ROCHARS (src), effective_length * sizeof (char));
223 /* FIXME: must signal an error if len > src_len */
224 scm_unprotect_object (src);
225 }
226
227
228 /* gh_scm2newsymbol() -- Given a Scheme symbol 'identifier, return a
229 pointer to a string with the symbol characters "identifier",
230 followed by a null byte. If lenp is non-null, set *lenp to the
231 string's length.
232
233 This function uses malloc to obtain storage for the copy; the
234 caller is responsible for freeing it. */
235 char *
236 gh_symbol2newstr (SCM sym, int *lenp)
237 {
238 char *ret_str;
239 int len;
240
241 SCM_ASSERT (SCM_NIMP (sym) && SCM_SYMBOLP (sym), sym, SCM_ARG3,
242 "gh_scm2newsymbol");
243
244 /* protect str from GC while we copy off its data */
245 scm_protect_object (sym);
246
247 len = SCM_LENGTH (sym);
248
249 ret_str = (char *) scm_must_malloc ((len + 1) * sizeof (char),
250 "gh_symbol2newstr");
251 /* so we copy tmp_str to ret_str, which is what we will allocate */
252 memcpy (ret_str, SCM_CHARS (sym), len);
253 /* now make sure we null-terminate it */
254 ret_str[len] = '\0';
255
256 scm_unprotect_object (sym);
257
258 if (lenp != NULL)
259 {
260 *lenp = len;
261 }
262
263 return ret_str;
264 }
265
266
267 /* create a new vector of the given length, all initialized to the
268 given value */
269 SCM
270 gh_make_vector (SCM len, SCM fill)
271 {
272 /* scm_make_vector() takes a third boolean argument which should be
273 set to SCM_BOOL_T when you are dealing with multi-dimensional
274 arrays; gh_make_vector() does not do multi-dimensional arrays */
275 return scm_make_vector(len, fill, SCM_BOOL_F);
276 }
277
278 /* set the given element of the given vector to the given value */
279 SCM
280 gh_vector_set_x (SCM vec, SCM pos, SCM val)
281 {
282 return scm_vector_set_x (vec, pos, val);
283 }
284
285 /* retrieve the given element of the given vector */
286 SCM
287 gh_vector_ref (SCM vec, SCM pos)
288 {
289 return scm_vector_ref (vec, pos);
290 }
291
292 /* returns the length of the given vector */
293 unsigned long
294 gh_vector_length (SCM v)
295 {
296 return gh_scm2ulong (scm_vector_length (v));
297 }
298
299
300 /* uniform vector support */
301
302 /* returns the length as a C unsigned long integer */
303 unsigned long
304 gh_uniform_vector_length (SCM v)
305 {
306 return gh_scm2ulong (scm_uniform_vector_length (v));
307 }
308
309 /* gets the given element from a uniform vector; ilist is a list (or
310 possibly a single integer) of indices, and its length is the
311 dimension of the uniform vector */
312 SCM
313 gh_uniform_vector_ref (SCM v, SCM ilist)
314 {
315 return scm_uniform_vector_ref (v, ilist);
316 }
317
318 /* sets an individual element in a uniform vector */
319 /* SCM */
320 /* gh_list_to_uniform_array ( */
321
322
323 /* Data lookups between C and Scheme
324
325 Look up a symbol with a given name, and return the object to which
326 it is bound. gh_lookup examines the Guile top level, and
327 gh_module_lookup checks the module namespace specified by the
328 `vec' argument.
329
330 The return value is the Scheme object to which SNAME is bound, or
331 SCM_UNDEFINED if SNAME is not bound in the given context. [FIXME:
332 should this be SCM_UNSPECIFIED? Can a symbol ever legitimately be
333 bound to SCM_UNDEFINED or SCM_UNSPECIFIED? What is the difference?
334 -twp] */
335
336 SCM
337 gh_lookup (char *sname)
338 {
339 return gh_module_lookup (SCM_BOOL_F, sname);
340 }
341
342 SCM
343 gh_module_lookup (SCM vec, char *sname)
344 {
345 SCM sym = gh_symbol2scm (sname);
346 if ((scm_symbol_bound_p (vec, sym)) == SCM_BOOL_T)
347 return scm_symbol_binding (vec, sym);
348 else
349 return SCM_UNDEFINED;
350 }