1 /* Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
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)
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.
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
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
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.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
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.
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. */
43 /* data initialization and C<->Scheme data conversion */
49 /* data conversion C->scheme */
51 gh_int2scmb (int x
) /* this is being phased out */
53 return (x
? SCM_BOOL_T
: SCM_BOOL_F
);
58 return (x
? SCM_BOOL_T
: SCM_BOOL_F
);
63 return scm_long2num ((long) x
);
66 gh_ulong2scm (unsigned long x
)
68 return scm_ulong2num (x
);
73 return scm_long2num (x
);
76 gh_double2scm (double x
)
78 return scm_makdbl (x
, 0.0);
83 return SCM_MAKICHR (c
);
86 gh_str2scm (char *s
, int len
)
88 return scm_makfromstr (s
, len
, 0);
93 return scm_makfrom0str (s
);
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.
99 If START + LEN is off the end of DST, signal an out-of-range
102 gh_set_substr (char *src
, SCM dst
, int start
, int len
)
105 unsigned long dst_len
;
106 unsigned long effective_length
;
108 SCM_ASSERT (SCM_NIMP (dst
) && SCM_STRINGP (dst
), dst
, SCM_ARG3
,
111 dst_ptr
= SCM_CHARS (dst
);
112 dst_len
= SCM_LENGTH (dst
);
113 SCM_ASSERT (len
>= 0 && (unsigned) len
<= dst_len
,
114 dst
, SCM_ARG4
, "gh_set_substr");
116 scm_protect_object (dst
);
117 effective_length
= ((unsigned) len
< dst_len
) ? len
: dst_len
;
118 memmove (dst_ptr
+ start
, src
, effective_length
);
119 scm_unprotect_object (dst
);
122 /* Return the symbol named SYMBOL_STR. */
124 gh_symbol2scm (char *symbol_str
)
126 return SCM_CAR (scm_intern (symbol_str
, strlen (symbol_str
)));
130 makvect (char* m
, int len
, int type
)
135 SCM_SETCHARS (ans
, m
);
136 SCM_SETLENGTH (ans
, len
, type
);
142 gh_ints2scm (int *d
, int n
)
146 for (i
= 0; i
< n
; ++i
)
147 SCM_ASSERT (d
[i
] >= SCM_INUM (LONG_MIN
) && d
[i
] <= SCM_INUM (LONG_MAX
),
151 m
= (SCM
*) scm_must_malloc (n
* sizeof (SCM
), "vector");
152 for (i
= 0; i
< n
; ++i
)
153 m
[i
] = SCM_MAKINUM (d
[i
]);
154 return makvect ((char *) m
, n
, scm_tc7_vector
);
158 gh_longs2ivect (long *d
, int n
)
160 char *m
= scm_must_malloc (n
* sizeof (long), "vector");
161 memcpy (m
, d
, n
* sizeof (long));
162 return makvect (m
, n
, scm_tc7_ivect
);
166 gh_ulongs2uvect (unsigned long *d
, int n
)
168 char *m
= scm_must_malloc (n
* sizeof (unsigned long), "vector");
169 memcpy (m
, d
, n
* sizeof (unsigned long));
170 return makvect (m
, n
, scm_tc7_uvect
);
174 gh_doubles2scm (double *d
, int n
)
176 SCM
*m
= (SCM
*) scm_must_malloc (n
* sizeof (SCM
), "vector");
178 for (i
= 0; i
< n
; ++i
)
179 m
[i
] = scm_makdbl (d
[i
], 0.0);
180 return makvect ((char *) m
, n
, scm_tc7_vector
);
185 gh_doubles2dvect (double *d
, int n
)
187 char *m
= scm_must_malloc (n
* sizeof (double), "vector");
188 memcpy (m
, d
, n
* sizeof (double));
189 return makvect (m
, n
, scm_tc7_dvect
);
193 /* data conversion scheme->C */
195 gh_scm2bool (SCM obj
)
197 return ((obj
) == SCM_BOOL_F
) ? 0 : 1;
200 gh_scm2ulong (SCM obj
)
202 return scm_num2ulong (obj
, (char *) SCM_ARG1
, "gh_scm2ulong");
205 gh_scm2long (SCM obj
)
207 return scm_num2long (obj
, (char *) SCM_ARG1
, "gh_scm2long");
212 /* NOTE: possible loss of precision here */
213 return (int) scm_num2long (obj
, (char *) SCM_ARG1
, "gh_scm2int");
216 gh_scm2double (SCM obj
)
218 return scm_num2dbl (obj
, "gh_scm2double");
221 gh_scm2char (SCM obj
)
223 return SCM_ICHR (obj
);
226 /* Convert a vector, weak vector or uniform vector into a malloced
229 gh_scm2doubles (SCM obj
)
235 scm_wrong_type_arg (0, 0, obj
);
236 switch (SCM_TYP7 (obj
))
240 n
= SCM_LENGTH (obj
);
241 m
= (double*) malloc (n
* sizeof (double));
242 for (i
= 0; i
< n
; ++i
)
244 val
= SCM_VELTS (obj
)[i
];
246 m
[i
] = SCM_INUM (val
);
247 else if (SCM_NIMP (val
) && SCM_REALP (val
))
248 m
[i
] = SCM_REALPART (val
);
252 scm_wrong_type_arg (0, 0, val
);
259 n
= SCM_LENGTH (obj
);
260 m
= (double*) malloc (n
* sizeof (double));
261 for (i
= 0; i
< n
; ++i
)
262 m
[i
] = ((float*) SCM_VELTS (obj
))[i
];
266 n
= SCM_LENGTH (obj
);
267 m
= (double*) malloc (n
* sizeof (double));
268 for (i
= 0; i
< n
; ++i
)
269 m
[i
] = ((double*) SCM_VELTS (obj
))[i
];
273 scm_wrong_type_arg (0, 0, obj
);
278 /* string conversions between C and Scheme */
280 /* gh_scm2newstr() -- Given a Scheme string STR, return a pointer to a
281 new copy of its contents, followed by a null byte. If lenp is
282 non-null, set *lenp to the string's length.
284 This function uses malloc to obtain storage for the copy; the
285 caller is responsible for freeing it.
287 Note that Scheme strings may contain arbitrary data, including null
288 characters. This means that null termination is not a reliable way
289 to determine the length of the returned value. However, the
290 function always copies the complete contents of STR, and sets
291 *LEN_P to the true length of the string (when LEN_P is non-null). */
293 gh_scm2newstr (SCM str
, int *lenp
)
298 SCM_ASSERT (SCM_NIMP (str
) && SCM_ROSTRINGP (str
), str
, SCM_ARG3
,
301 /* protect str from GC while we copy off its data */
302 scm_protect_object (str
);
304 len
= SCM_LENGTH (str
);
306 ret_str
= (char *) scm_must_malloc ((len
+ 1) * sizeof (char),
308 /* so we copy tmp_str to ret_str, which is what we will allocate */
309 memcpy (ret_str
, SCM_ROCHARS (str
), len
); /* test ROCHARS here -twp */
310 /* now make sure we null-terminate it */
313 scm_unprotect_object (str
);
324 /* Copy LEN characters at START from the Scheme string SRC to memory
325 at DST. START is an index into SRC; zero means the beginning of
326 the string. DST has already been allocated by the caller.
328 If START + LEN is off the end of SRC, silently truncate the source
329 region to fit the string. If truncation occurs, the corresponding
330 area of DST is left unchanged. */
332 gh_get_substr (SCM src
, char *dst
, int start
, int len
)
334 int src_len
, effective_length
;
335 SCM_ASSERT (SCM_NIMP (src
) && SCM_ROSTRINGP (src
), src
, SCM_ARG3
,
338 scm_protect_object (src
);
339 src_len
= SCM_LENGTH (src
);
340 effective_length
= (len
< src_len
) ? len
: src_len
;
341 memcpy (dst
+ start
, SCM_ROCHARS (src
), effective_length
* sizeof (char));
342 /* FIXME: must signal an error if len > src_len */
343 scm_unprotect_object (src
);
347 /* gh_scm2newsymbol() -- Given a Scheme symbol 'identifier, return a
348 pointer to a string with the symbol characters "identifier",
349 followed by a null byte. If lenp is non-null, set *lenp to the
352 This function uses malloc to obtain storage for the copy; the
353 caller is responsible for freeing it. */
355 gh_symbol2newstr (SCM sym
, int *lenp
)
360 SCM_ASSERT (SCM_NIMP (sym
) && SCM_SYMBOLP (sym
), sym
, SCM_ARG3
,
363 /* protect str from GC while we copy off its data */
364 scm_protect_object (sym
);
366 len
= SCM_LENGTH (sym
);
368 ret_str
= (char *) scm_must_malloc ((len
+ 1) * sizeof (char),
370 /* so we copy tmp_str to ret_str, which is what we will allocate */
371 memcpy (ret_str
, SCM_CHARS (sym
), len
);
372 /* now make sure we null-terminate it */
375 scm_unprotect_object (sym
);
386 /* create a new vector of the given length, all initialized to the
389 gh_make_vector (SCM len
, SCM fill
)
391 return scm_make_vector (len
, fill
);
394 /* set the given element of the given vector to the given value */
396 gh_vector_set_x (SCM vec
, SCM pos
, SCM val
)
398 return scm_vector_set_x (vec
, pos
, val
);
401 /* retrieve the given element of the given vector */
403 gh_vector_ref (SCM vec
, SCM pos
)
405 return scm_vector_ref (vec
, pos
);
408 /* returns the length of the given vector */
410 gh_vector_length (SCM v
)
412 return gh_scm2ulong (scm_vector_length (v
));
416 /* uniform vector support */
418 /* returns the length as a C unsigned long integer */
420 gh_uniform_vector_length (SCM v
)
422 return gh_scm2ulong (scm_uniform_vector_length (v
));
425 /* gets the given element from a uniform vector; ilist is a list (or
426 possibly a single integer) of indices, and its length is the
427 dimension of the uniform vector */
429 gh_uniform_vector_ref (SCM v
, SCM ilist
)
431 return scm_uniform_vector_ref (v
, ilist
);
434 /* sets an individual element in a uniform vector */
436 /* gh_list_to_uniform_array ( */
439 /* Data lookups between C and Scheme
441 Look up a symbol with a given name, and return the object to which
442 it is bound. gh_lookup examines the Guile top level, and
443 gh_module_lookup checks the module namespace specified by the
446 The return value is the Scheme object to which SNAME is bound, or
447 SCM_UNDEFINED if SNAME is not bound in the given context. [FIXME:
448 should this be SCM_UNSPECIFIED? Can a symbol ever legitimately be
449 bound to SCM_UNDEFINED or SCM_UNSPECIFIED? What is the difference?
453 gh_lookup (char *sname
)
455 return gh_module_lookup (SCM_BOOL_F
, sname
);
459 gh_module_lookup (SCM vec
, char *sname
)
461 SCM sym
= gh_symbol2scm (sname
);
462 if ((scm_symbol_bound_p (vec
, sym
)) == SCM_BOOL_T
)
463 return scm_symbol_binding (vec
, sym
);
465 return SCM_UNDEFINED
;