1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001 Free Software Foundation, Inc.
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2, or (at your option)
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this software; see the file COPYING. If not, write to
14 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15 * Boston, MA 02111-1307 USA
17 * As a special exception, the Free Software Foundation gives permission
18 * for additional uses of the text contained in its release of GUILE.
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.
26 * This exception does not however invalidate any other reasons why
27 * the executable file might be covered by the GNU General Public License.
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.
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. */
42 /* data initialization and C<->Scheme data conversion */
44 #include "libguile/gh.h"
49 /* data conversion C->scheme */
51 #if (SCM_DEBUG_DEPRECATED == 0)
54 gh_int2scmb (int x
) /* this is being phased out */
59 #endif /* SCM_DEBUG_DEPRECATED == 0 */
69 return scm_long2num ((long) x
);
72 gh_ulong2scm (unsigned long x
)
74 return scm_ulong2num (x
);
79 return scm_long2num (x
);
82 gh_double2scm (double x
)
84 return scm_make_real (x
);
89 return SCM_MAKE_CHAR (c
);
92 gh_str2scm (const char *s
, int len
)
94 return scm_makfromstr (s
, len
, 0);
97 gh_str02scm (const char *s
)
99 return scm_makfrom0str (s
);
101 /* Copy LEN characters at SRC into the *existing* Scheme string DST,
102 starting at START. START is an index into DST; zero means the
103 beginning of the string.
105 If START + LEN is off the end of DST, signal an out-of-range
108 gh_set_substr (char *src
, SCM dst
, int start
, int len
)
111 unsigned long dst_len
;
112 unsigned long effective_length
;
114 SCM_ASSERT (SCM_STRINGP (dst
), dst
, SCM_ARG3
, "gh_set_substr");
116 dst_ptr
= SCM_STRING_CHARS (dst
);
117 dst_len
= SCM_STRING_LENGTH (dst
);
118 SCM_ASSERT (len
>= 0 && (unsigned) len
<= dst_len
,
119 dst
, SCM_ARG4
, "gh_set_substr");
121 effective_length
= ((unsigned) len
< dst_len
) ? len
: dst_len
;
122 memmove (dst_ptr
+ start
, src
, effective_length
);
123 scm_remember_upto_here_1 (dst
);
126 /* Return the symbol named SYMBOL_STR. */
128 gh_symbol2scm (const char *symbol_str
)
130 return scm_str2symbol(symbol_str
);
134 gh_ints2scm (const int *d
, int n
)
137 SCM v
= scm_c_make_vector (n
, SCM_UNSPECIFIED
);
138 SCM
*velts
= SCM_VELTS(v
);
140 for (i
= 0; i
< n
; ++i
)
141 velts
[i
] = (SCM_FIXABLE (d
[i
]) ? SCM_MAKINUM (d
[i
]) : scm_long2big (d
[i
]));
147 gh_doubles2scm (const double *d
, int n
)
150 SCM v
= scm_c_make_vector (n
, SCM_UNSPECIFIED
);
151 SCM
*velts
= SCM_VELTS(v
);
153 for(i
= 0; i
< n
; i
++)
154 velts
[i
] = scm_make_real (d
[i
]);
159 /* Do not use this function for building normal Scheme vectors, unless
160 you arrange for the elements to be protected from GC while you
161 initialize the vector. */
163 makvect (char* m
, int len
, int type
)
168 SCM_SET_UVECTOR_BASE (ans
, m
);
169 SCM_SET_UVECTOR_LENGTH (ans
, len
, type
);
175 gh_chars2byvect (const char *d
, int n
)
177 char *m
= scm_must_malloc (n
* sizeof (char), "vector");
178 memcpy (m
, d
, n
* sizeof (char));
179 return makvect (m
, n
, scm_tc7_byvect
);
183 gh_shorts2svect (const short *d
, int n
)
185 char *m
= scm_must_malloc (n
* sizeof (short), "vector");
186 memcpy (m
, d
, n
* sizeof (short));
187 return makvect (m
, n
, scm_tc7_svect
);
191 gh_longs2ivect (const long *d
, int n
)
193 char *m
= scm_must_malloc (n
* sizeof (long), "vector");
194 memcpy (m
, d
, n
* sizeof (long));
195 return makvect (m
, n
, scm_tc7_ivect
);
199 gh_ulongs2uvect (const unsigned long *d
, int n
)
201 char *m
= scm_must_malloc (n
* sizeof (unsigned long), "vector");
202 memcpy (m
, d
, n
* sizeof (unsigned long));
203 return makvect (m
, n
, scm_tc7_uvect
);
207 gh_floats2fvect (const float *d
, int n
)
209 char *m
= scm_must_malloc (n
* sizeof (float), "vector");
210 memcpy (m
, d
, n
* sizeof (float));
211 return makvect (m
, n
, scm_tc7_fvect
);
215 gh_doubles2dvect (const double *d
, int n
)
217 char *m
= scm_must_malloc (n
* sizeof (double), "vector");
218 memcpy (m
, d
, n
* sizeof (double));
219 return makvect (m
, n
, scm_tc7_dvect
);
223 /* data conversion scheme->C */
225 gh_scm2bool (SCM obj
)
227 return (SCM_FALSEP (obj
)) ? 0 : 1;
230 gh_scm2ulong (SCM obj
)
232 return scm_num2ulong (obj
, (char *) SCM_ARG1
, "gh_scm2ulong");
235 gh_scm2long (SCM obj
)
237 return scm_num2long (obj
, (char *) SCM_ARG1
, "gh_scm2long");
242 /* NOTE: possible loss of precision here */
243 return (int) scm_num2long (obj
, (char *) SCM_ARG1
, "gh_scm2int");
246 gh_scm2double (SCM obj
)
248 return scm_num2dbl (obj
, "gh_scm2double");
251 gh_scm2char (SCM obj
)
252 #define FUNC_NAME "gh_scm2char"
254 SCM_VALIDATE_CHAR (SCM_ARG1
, obj
);
255 return SCM_CHAR (obj
);
259 /* Convert a vector, weak vector, string, substring or uniform vector
260 into an array of chars. If result array in arg 2 is NULL, malloc a
261 new one. If out of memory, return NULL. */
263 gh_scm2chars (SCM obj
, char *m
)
269 scm_wrong_type_arg (0, 0, obj
);
270 switch (SCM_TYP7 (obj
))
274 n
= SCM_VECTOR_LENGTH (obj
);
275 for (i
= 0; i
< n
; ++i
)
277 val
= SCM_VELTS (obj
)[i
];
281 if (v
< -128 || v
> 255)
282 scm_out_of_range (0, obj
);
285 scm_wrong_type_arg (0, 0, obj
);
288 m
= (char *) malloc (n
* sizeof (char));
291 for (i
= 0; i
< n
; ++i
)
292 m
[i
] = SCM_INUM (SCM_VELTS (obj
)[i
]);
296 n
= SCM_UVECTOR_LENGTH (obj
);
298 m
= (char *) malloc (n
* sizeof (char));
301 memcpy (m
, SCM_VELTS (obj
), n
* sizeof (char));
305 case scm_tc7_substring
:
306 n
= SCM_STRING_LENGTH (obj
);
308 m
= (char *) malloc (n
* sizeof (char));
311 memcpy (m
, SCM_VELTS (obj
), n
* sizeof (char));
314 scm_wrong_type_arg (0, 0, obj
);
319 /* Convert a vector, weak vector or uniform vector into an array of
320 shorts. If result array in arg 2 is NULL, malloc a new one. If
321 out of memory, return NULL. */
323 gh_scm2shorts (SCM obj
, short *m
)
329 scm_wrong_type_arg (0, 0, obj
);
330 switch (SCM_TYP7 (obj
))
334 n
= SCM_VECTOR_LENGTH (obj
);
335 for (i
= 0; i
< n
; ++i
)
337 val
= SCM_VELTS (obj
)[i
];
341 if (v
< -32768 || v
> 65535)
342 scm_out_of_range (0, obj
);
345 scm_wrong_type_arg (0, 0, obj
);
348 m
= (short *) malloc (n
* sizeof (short));
351 for (i
= 0; i
< n
; ++i
)
352 m
[i
] = SCM_INUM (SCM_VELTS (obj
)[i
]);
356 n
= SCM_UVECTOR_LENGTH (obj
);
358 m
= (short *) malloc (n
* sizeof (short));
361 memcpy (m
, SCM_VELTS (obj
), n
* sizeof (short));
365 scm_wrong_type_arg (0, 0, obj
);
370 /* Convert a vector, weak vector or uniform vector into an array of
371 longs. If result array in arg 2 is NULL, malloc a new one. If out
372 of memory, return NULL. */
374 gh_scm2longs (SCM obj
, long *m
)
379 scm_wrong_type_arg (0, 0, obj
);
380 switch (SCM_TYP7 (obj
))
384 n
= SCM_VECTOR_LENGTH (obj
);
385 for (i
= 0; i
< n
; ++i
)
387 val
= SCM_VELTS (obj
)[i
];
388 if (!SCM_INUMP (val
) && !SCM_BIGP (val
))
389 scm_wrong_type_arg (0, 0, obj
);
392 m
= (long *) malloc (n
* sizeof (long));
395 for (i
= 0; i
< n
; ++i
)
397 val
= SCM_VELTS (obj
)[i
];
398 m
[i
] = SCM_INUMP (val
) ? SCM_INUM (val
) : scm_num2long (val
, 0, 0);
404 n
= SCM_UVECTOR_LENGTH (obj
);
406 m
= (long *) malloc (n
* sizeof (long));
409 memcpy (m
, SCM_VELTS (obj
), n
* sizeof (long));
413 scm_wrong_type_arg (0, 0, obj
);
418 /* Convert a vector, weak vector or uniform vector into an array of
419 floats. If result array in arg 2 is NULL, malloc a new one. If
420 out of memory, return NULL. */
422 gh_scm2floats (SCM obj
, float *m
)
427 scm_wrong_type_arg (0, 0, obj
);
428 switch (SCM_TYP7 (obj
))
432 n
= SCM_VECTOR_LENGTH (obj
);
433 for (i
= 0; i
< n
; ++i
)
435 val
= SCM_VELTS (obj
)[i
];
437 && !(SCM_BIGP (val
) || SCM_REALP (val
)))
438 scm_wrong_type_arg (0, 0, val
);
441 m
= (float *) malloc (n
* sizeof (float));
444 for (i
= 0; i
< n
; ++i
)
446 val
= SCM_VELTS (obj
)[i
];
448 m
[i
] = SCM_INUM (val
);
449 else if (SCM_BIGP (val
))
450 m
[i
] = scm_num2long (val
, 0, 0);
452 m
[i
] = SCM_REAL_VALUE (val
);
457 n
= SCM_UVECTOR_LENGTH (obj
);
459 m
= (float *) malloc (n
* sizeof (float));
462 memcpy (m
, (float *) SCM_VELTS (obj
), n
* sizeof (float));
466 n
= SCM_UVECTOR_LENGTH (obj
);
468 m
= (float*) malloc (n
* sizeof (float));
471 for (i
= 0; i
< n
; ++i
)
472 m
[i
] = ((double *) SCM_VELTS (obj
))[i
];
476 scm_wrong_type_arg (0, 0, obj
);
481 /* Convert a vector, weak vector or uniform vector into an array of
482 doubles. If result array in arg 2 is NULL, malloc a new one. If
483 out of memory, return NULL. */
485 gh_scm2doubles (SCM obj
, double *m
)
490 scm_wrong_type_arg (0, 0, obj
);
491 switch (SCM_TYP7 (obj
))
495 n
= SCM_VECTOR_LENGTH (obj
);
496 for (i
= 0; i
< n
; ++i
)
498 val
= SCM_VELTS (obj
)[i
];
500 && !(SCM_BIGP (val
) || SCM_REALP (val
)))
501 scm_wrong_type_arg (0, 0, val
);
504 m
= (double *) malloc (n
* sizeof (double));
507 for (i
= 0; i
< n
; ++i
)
509 val
= SCM_VELTS (obj
)[i
];
511 m
[i
] = SCM_INUM (val
);
512 else if (SCM_BIGP (val
))
513 m
[i
] = scm_num2long (val
, 0, 0);
515 m
[i
] = SCM_REAL_VALUE (val
);
520 n
= SCM_UVECTOR_LENGTH (obj
);
522 m
= (double *) malloc (n
* sizeof (double));
525 for (i
= 0; i
< n
; ++i
)
526 m
[i
] = ((float *) SCM_VELTS (obj
))[i
];
530 n
= SCM_UVECTOR_LENGTH (obj
);
532 m
= (double*) malloc (n
* sizeof (double));
535 memcpy (m
, SCM_VELTS (obj
), n
* sizeof (double));
539 scm_wrong_type_arg (0, 0, obj
);
544 /* string conversions between C and Scheme */
546 /* gh_scm2newstr() -- Given a Scheme string STR, return a pointer to a
547 new copy of its contents, followed by a null byte. If lenp is
548 non-null, set *lenp to the string's length.
550 This function uses malloc to obtain storage for the copy; the
551 caller is responsible for freeing it. If out of memory, NULL is
554 Note that Scheme strings may contain arbitrary data, including null
555 characters. This means that null termination is not a reliable way
556 to determine the length of the returned value. However, the
557 function always copies the complete contents of STR, and sets
558 *LEN_P to the true length of the string (when LEN_P is non-null). */
560 gh_scm2newstr (SCM str
, int *lenp
)
565 SCM_ASSERT (SCM_STRINGP (str
), str
, SCM_ARG3
, "gh_scm2newstr");
567 len
= SCM_STRING_LENGTH (str
);
569 ret_str
= (char *) malloc ((len
+ 1) * sizeof (char));
572 /* so we copy tmp_str to ret_str, which is what we will allocate */
573 memcpy (ret_str
, SCM_STRING_CHARS (str
), len
);
574 scm_remember_upto_here_1 (str
);
575 /* now make sure we null-terminate it */
587 /* Copy LEN characters at START from the Scheme string SRC to memory
588 at DST. START is an index into SRC; zero means the beginning of
589 the string. DST has already been allocated by the caller.
591 If START + LEN is off the end of SRC, silently truncate the source
592 region to fit the string. If truncation occurs, the corresponding
593 area of DST is left unchanged. */
595 gh_get_substr (SCM src
, char *dst
, int start
, int len
)
597 int src_len
, effective_length
;
598 SCM_ASSERT (SCM_STRINGP (src
), src
, SCM_ARG3
, "gh_get_substr");
600 src_len
= SCM_STRING_LENGTH (src
);
601 effective_length
= (len
< src_len
) ? len
: src_len
;
602 memcpy (dst
+ start
, SCM_STRING_CHARS (src
), effective_length
* sizeof (char));
603 /* FIXME: must signal an error if len > src_len */
604 scm_remember_upto_here_1 (src
);
608 /* gh_scm2newsymbol() -- Given a Scheme symbol 'identifier, return a
609 pointer to a string with the symbol characters "identifier",
610 followed by a null byte. If lenp is non-null, set *lenp to the
613 This function uses malloc to obtain storage for the copy; the
614 caller is responsible for freeing it. If out of memory, NULL is
617 gh_symbol2newstr (SCM sym
, int *lenp
)
622 SCM_ASSERT (SCM_SYMBOLP (sym
), sym
, SCM_ARG3
, "gh_scm2newsymbol");
624 len
= SCM_SYMBOL_LENGTH (sym
);
626 ret_str
= (char *) malloc ((len
+ 1) * sizeof (char));
629 /* so we copy sym to ret_str, which is what we will allocate */
630 memcpy (ret_str
, SCM_SYMBOL_CHARS (sym
), len
);
631 scm_remember_upto_here_1 (sym
);
632 /* now make sure we null-terminate it */
644 /* create a new vector of the given length, all initialized to the
647 gh_make_vector (SCM len
, SCM fill
)
649 return scm_make_vector (len
, fill
);
652 /* set the given element of the given vector to the given value */
654 gh_vector_set_x (SCM vec
, SCM pos
, SCM val
)
656 return scm_vector_set_x (vec
, pos
, val
);
659 /* retrieve the given element of the given vector */
661 gh_vector_ref (SCM vec
, SCM pos
)
663 return scm_vector_ref (vec
, pos
);
666 /* returns the length of the given vector */
668 gh_vector_length (SCM v
)
670 return gh_scm2ulong (scm_vector_length (v
));
674 /* uniform vector support */
676 /* returns the length as a C unsigned long integer */
678 gh_uniform_vector_length (SCM v
)
680 return gh_scm2ulong (scm_uniform_vector_length (v
));
683 /* gets the given element from a uniform vector; ilist is a list (or
684 possibly a single integer) of indices, and its length is the
685 dimension of the uniform vector */
687 gh_uniform_vector_ref (SCM v
, SCM ilist
)
689 return scm_uniform_vector_ref (v
, ilist
);
692 /* sets an individual element in a uniform vector */
694 /* gh_list_to_uniform_array ( */
697 /* Data lookups between C and Scheme
699 Look up a symbol with a given name, and return the object to which
700 it is bound. gh_lookup examines the Guile top level, and
701 gh_module_lookup checks the module namespace specified by the
704 The return value is the Scheme object to which SNAME is bound, or
705 SCM_UNDEFINED if SNAME is not bound in the given context. [FIXME:
706 should this be SCM_UNSPECIFIED? Can a symbol ever legitimately be
707 bound to SCM_UNDEFINED or SCM_UNSPECIFIED? What is the difference?
711 gh_lookup (const char *sname
)
713 return gh_module_lookup (SCM_BOOL_F
, sname
);
717 gh_module_lookup (SCM vec
, const char *sname
)
719 SCM sym
= gh_symbol2scm (sname
);
720 if (SCM_EQ_P (scm_symbol_bound_p (vec
, sym
), SCM_BOOL_T
))
721 return scm_symbol_binding (vec
, sym
);
723 return SCM_UNDEFINED
;