1 /* Copyright (C) 1995,1996,1997 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
, effective_length
;
107 SCM_ASSERT (SCM_NIMP (dst
) && SCM_STRINGP (dst
), dst
, SCM_ARG3
,
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
);
118 /* Return the symbol named SYMBOL_STR. */
120 gh_symbol2scm (char *symbol_str
)
122 return SCM_CAR (scm_intern (symbol_str
, strlen (symbol_str
)));
126 /* data conversion scheme->C */
128 gh_scm2bool (SCM obj
)
130 return ((obj
) == SCM_BOOL_F
) ? 0 : 1;
133 gh_scm2ulong (SCM obj
)
135 return scm_num2ulong (obj
, (char *) SCM_ARG1
, "gh_scm2ulong");
138 gh_scm2long (SCM obj
)
140 return scm_num2long (obj
, (char *) SCM_ARG1
, "gh_scm2long");
145 /* NOTE: possible loss of precision here */
146 return (int) scm_num2long (obj
, (char *) SCM_ARG1
, "gh_scm2int");
149 gh_scm2double (SCM obj
)
151 return scm_num2dbl (obj
, "gh_scm2double");
154 gh_scm2char (SCM obj
)
156 return SCM_ICHR (obj
);
159 /* string conversions between C and Scheme */
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.
165 This function uses malloc to obtain storage for the copy; the
166 caller is responsible for freeing it.
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). */
174 gh_scm2newstr (SCM str
, int *lenp
)
179 SCM_ASSERT (SCM_NIMP (str
) && SCM_ROSTRINGP (str
), str
, SCM_ARG3
,
182 /* protect str from GC while we copy off its data */
183 scm_protect_object (str
);
185 len
= SCM_LENGTH (str
);
187 ret_str
= (char *) scm_must_malloc ((len
+ 1) * sizeof (char),
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 */
194 scm_unprotect_object (str
);
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.
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. */
213 gh_get_substr (SCM src
, char *dst
, int start
, int len
)
215 int src_len
, effective_length
;
216 SCM_ASSERT (SCM_NIMP (src
) && SCM_ROSTRINGP (src
), src
, SCM_ARG3
,
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
);
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
233 This function uses malloc to obtain storage for the copy; the
234 caller is responsible for freeing it. */
236 gh_symbol2newstr (SCM sym
, int *lenp
)
241 SCM_ASSERT (SCM_NIMP (sym
) && SCM_SYMBOLP (sym
), sym
, SCM_ARG3
,
244 /* protect str from GC while we copy off its data */
245 scm_protect_object (sym
);
247 len
= SCM_LENGTH (sym
);
249 ret_str
= (char *) scm_must_malloc ((len
+ 1) * sizeof (char),
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 */
256 scm_unprotect_object (sym
);
267 /* create a new vector of the given length, all initialized to the
270 gh_make_vector (SCM len
, SCM fill
)
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
);
278 /* set the given element of the given vector to the given value */
280 gh_vector_set_x (SCM vec
, SCM pos
, SCM val
)
282 return scm_vector_set_x (vec
, pos
, val
);
285 /* retrieve the given element of the given vector */
287 gh_vector_ref (SCM vec
, SCM pos
)
289 return scm_vector_ref (vec
, pos
);
292 /* returns the length of the given vector */
294 gh_vector_length (SCM v
)
296 return gh_scm2ulong (scm_vector_length (v
));
300 /* uniform vector support */
302 /* returns the length as a C unsigned long integer */
304 gh_uniform_vector_length (SCM v
)
306 return gh_scm2ulong (scm_uniform_vector_length (v
));
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 */
313 gh_uniform_vector_ref (SCM v
, SCM ilist
)
315 return scm_uniform_vector_ref (v
, ilist
);
318 /* sets an individual element in a uniform vector */
320 /* gh_list_to_uniform_array ( */
323 /* Data lookups between C and Scheme
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
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?
337 gh_lookup (char *sname
)
339 return gh_module_lookup (SCM_BOOL_F
, sname
);
343 gh_module_lookup (SCM vec
, char *sname
)
345 SCM sym
= gh_symbol2scm (sname
);
346 if ((scm_symbol_bound_p (vec
, sym
)) == SCM_BOOL_T
)
347 return scm_symbol_binding (vec
, sym
);
349 return SCM_UNDEFINED
;