ceef34db07a3f7694291db924f22b562e36db598
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 */
59 return scm_long2num ((long) x
);
62 gh_ulong2scm (unsigned long x
)
64 return scm_ulong2num (x
);
69 return scm_long2num (x
);
72 gh_double2scm (double x
)
74 return scm_make_real (x
);
79 return SCM_MAKE_CHAR (c
);
82 gh_str2scm (const char *s
, size_t len
)
84 return scm_mem2string (s
, len
);
87 gh_str02scm (const char *s
)
89 return scm_makfrom0str (s
);
91 /* Copy LEN characters at SRC into the *existing* Scheme string DST,
92 starting at START. START is an index into DST; zero means the
93 beginning of the string.
95 If START + LEN is off the end of DST, signal an out-of-range
98 gh_set_substr (char *src
, SCM dst
, long start
, size_t len
)
103 SCM_ASSERT (SCM_STRINGP (dst
), dst
, SCM_ARG3
, "gh_set_substr");
105 dst_ptr
= SCM_STRING_CHARS (dst
);
106 dst_len
= SCM_STRING_LENGTH (dst
);
107 SCM_ASSERT (start
+ len
<= dst_len
, dst
, SCM_ARG4
, "gh_set_substr");
109 memmove (dst_ptr
+ start
, src
, len
);
110 scm_remember_upto_here_1 (dst
);
113 /* Return the symbol named SYMBOL_STR. */
115 gh_symbol2scm (const char *symbol_str
)
117 return scm_str2symbol(symbol_str
);
121 gh_ints2scm (const int *d
, long n
)
124 SCM v
= scm_c_make_vector (n
, SCM_UNSPECIFIED
);
125 SCM
*velts
= SCM_VELTS(v
);
127 for (i
= 0; i
< n
; ++i
)
128 velts
[i
] = (SCM_FIXABLE (d
[i
]) ? SCM_MAKINUM (d
[i
]) : scm_i_long2big (d
[i
]));
134 gh_doubles2scm (const double *d
, long 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_make_real (d
[i
]);
146 /* Do not use this function for building normal Scheme vectors, unless
147 you arrange for the elements to be protected from GC while you
148 initialize the vector. */
150 makvect (char *m
, size_t len
, int type
)
152 return scm_alloc_cell (SCM_MAKE_UVECTOR_TAG (len
, type
), (scm_t_bits
) m
);
156 gh_chars2byvect (const char *d
, long n
)
158 char *m
= scm_gc_malloc (n
* sizeof (char), "vector");
159 memcpy (m
, d
, n
* sizeof (char));
160 return makvect (m
, n
, scm_tc7_byvect
);
164 gh_shorts2svect (const short *d
, long n
)
166 char *m
= scm_gc_malloc (n
* sizeof (short), "vector");
167 memcpy (m
, d
, n
* sizeof (short));
168 return makvect (m
, n
, scm_tc7_svect
);
172 gh_longs2ivect (const long *d
, long n
)
174 char *m
= scm_gc_malloc (n
* sizeof (long), "vector");
175 memcpy (m
, d
, n
* sizeof (long));
176 return makvect (m
, n
, scm_tc7_ivect
);
180 gh_ulongs2uvect (const unsigned long *d
, long n
)
182 char *m
= scm_gc_malloc (n
* sizeof (unsigned long), "vector");
183 memcpy (m
, d
, n
* sizeof (unsigned long));
184 return makvect (m
, n
, scm_tc7_uvect
);
188 gh_floats2fvect (const float *d
, long n
)
190 char *m
= scm_gc_malloc (n
* sizeof (float), "vector");
191 memcpy (m
, d
, n
* sizeof (float));
192 return makvect (m
, n
, scm_tc7_fvect
);
196 gh_doubles2dvect (const double *d
, long n
)
198 char *m
= scm_gc_malloc (n
* sizeof (double), "vector");
199 memcpy (m
, d
, n
* sizeof (double));
200 return makvect (m
, n
, scm_tc7_dvect
);
204 /* data conversion scheme->C */
206 gh_scm2bool (SCM obj
)
208 return (SCM_FALSEP (obj
)) ? 0 : 1;
211 gh_scm2ulong (SCM obj
)
213 return scm_num2ulong (obj
, SCM_ARG1
, "gh_scm2ulong");
216 gh_scm2long (SCM obj
)
218 return scm_num2long (obj
, SCM_ARG1
, "gh_scm2long");
223 return (int) scm_num2int (obj
, SCM_ARG1
, "gh_scm2int");
226 gh_scm2double (SCM obj
)
228 return scm_num2dbl (obj
, "gh_scm2double");
231 gh_scm2char (SCM obj
)
232 #define FUNC_NAME "gh_scm2char"
234 SCM_VALIDATE_CHAR (SCM_ARG1
, obj
);
235 return SCM_CHAR (obj
);
239 /* Convert a vector, weak vector, string, substring or uniform vector
240 into an array of chars. If result array in arg 2 is NULL, malloc a
241 new one. If out of memory, return NULL. */
243 gh_scm2chars (SCM obj
, char *m
)
249 scm_wrong_type_arg (0, 0, obj
);
250 switch (SCM_TYP7 (obj
))
254 n
= SCM_VECTOR_LENGTH (obj
);
255 for (i
= 0; i
< n
; ++i
)
257 val
= SCM_VELTS (obj
)[i
];
261 if (v
< -128 || v
> 255)
262 scm_out_of_range (0, obj
);
265 scm_wrong_type_arg (0, 0, obj
);
268 m
= (char *) malloc (n
* sizeof (char));
271 for (i
= 0; i
< n
; ++i
)
272 m
[i
] = SCM_INUM (SCM_VELTS (obj
)[i
]);
276 n
= SCM_UVECTOR_LENGTH (obj
);
278 m
= (char *) malloc (n
* sizeof (char));
281 memcpy (m
, SCM_VELTS (obj
), n
* sizeof (char));
285 n
= SCM_STRING_LENGTH (obj
);
287 m
= (char *) malloc (n
* sizeof (char));
290 memcpy (m
, SCM_VELTS (obj
), n
* sizeof (char));
293 scm_wrong_type_arg (0, 0, obj
);
298 /* Convert a vector, weak vector or uniform vector into an array of
299 shorts. If result array in arg 2 is NULL, malloc a new one. If
300 out of memory, return NULL. */
302 gh_scm2shorts (SCM obj
, short *m
)
308 scm_wrong_type_arg (0, 0, obj
);
309 switch (SCM_TYP7 (obj
))
313 n
= SCM_VECTOR_LENGTH (obj
);
314 for (i
= 0; i
< n
; ++i
)
316 val
= SCM_VELTS (obj
)[i
];
320 if (v
< -32768 || v
> 65535)
321 scm_out_of_range (0, obj
);
324 scm_wrong_type_arg (0, 0, obj
);
327 m
= (short *) malloc (n
* sizeof (short));
330 for (i
= 0; i
< n
; ++i
)
331 m
[i
] = SCM_INUM (SCM_VELTS (obj
)[i
]);
335 n
= SCM_UVECTOR_LENGTH (obj
);
337 m
= (short *) malloc (n
* sizeof (short));
340 memcpy (m
, SCM_VELTS (obj
), n
* sizeof (short));
344 scm_wrong_type_arg (0, 0, obj
);
349 /* Convert a vector, weak vector or uniform vector into an array of
350 longs. If result array in arg 2 is NULL, malloc a new one. If out
351 of memory, return NULL. */
353 gh_scm2longs (SCM obj
, long *m
)
358 scm_wrong_type_arg (0, 0, obj
);
359 switch (SCM_TYP7 (obj
))
363 n
= SCM_VECTOR_LENGTH (obj
);
364 for (i
= 0; i
< n
; ++i
)
366 val
= SCM_VELTS (obj
)[i
];
367 if (!SCM_INUMP (val
) && !SCM_BIGP (val
))
368 scm_wrong_type_arg (0, 0, obj
);
371 m
= (long *) malloc (n
* sizeof (long));
374 for (i
= 0; i
< n
; ++i
)
376 val
= SCM_VELTS (obj
)[i
];
377 m
[i
] = SCM_INUMP (val
)
379 : scm_num2long (val
, 0, NULL
);
385 n
= SCM_UVECTOR_LENGTH (obj
);
387 m
= (long *) malloc (n
* sizeof (long));
390 memcpy (m
, SCM_VELTS (obj
), n
* sizeof (long));
394 scm_wrong_type_arg (0, 0, obj
);
399 /* Convert a vector, weak vector or uniform vector into an array of
400 floats. If result array in arg 2 is NULL, malloc a new one. If
401 out of memory, return NULL. */
403 gh_scm2floats (SCM obj
, float *m
)
408 scm_wrong_type_arg (0, 0, obj
);
409 switch (SCM_TYP7 (obj
))
413 n
= SCM_VECTOR_LENGTH (obj
);
414 for (i
= 0; i
< n
; ++i
)
416 val
= SCM_VELTS (obj
)[i
];
418 && !(SCM_BIGP (val
) || SCM_REALP (val
)))
419 scm_wrong_type_arg (0, 0, val
);
422 m
= (float *) malloc (n
* sizeof (float));
425 for (i
= 0; i
< n
; ++i
)
427 val
= SCM_VELTS (obj
)[i
];
429 m
[i
] = SCM_INUM (val
);
430 else if (SCM_BIGP (val
))
431 m
[i
] = scm_num2long (val
, 0, NULL
);
433 m
[i
] = SCM_REAL_VALUE (val
);
438 n
= SCM_UVECTOR_LENGTH (obj
);
440 m
= (float *) malloc (n
* sizeof (float));
443 memcpy (m
, (float *) SCM_VELTS (obj
), n
* sizeof (float));
447 n
= SCM_UVECTOR_LENGTH (obj
);
449 m
= (float*) malloc (n
* sizeof (float));
452 for (i
= 0; i
< n
; ++i
)
453 m
[i
] = ((double *) SCM_VELTS (obj
))[i
];
457 scm_wrong_type_arg (0, 0, obj
);
462 /* Convert a vector, weak vector or uniform vector into an array of
463 doubles. If result array in arg 2 is NULL, malloc a new one. If
464 out of memory, return NULL. */
466 gh_scm2doubles (SCM obj
, double *m
)
471 scm_wrong_type_arg (0, 0, obj
);
472 switch (SCM_TYP7 (obj
))
476 n
= SCM_VECTOR_LENGTH (obj
);
477 for (i
= 0; i
< n
; ++i
)
479 val
= SCM_VELTS (obj
)[i
];
481 && !(SCM_BIGP (val
) || SCM_REALP (val
)))
482 scm_wrong_type_arg (0, 0, val
);
485 m
= (double *) malloc (n
* sizeof (double));
488 for (i
= 0; i
< n
; ++i
)
490 val
= SCM_VELTS (obj
)[i
];
492 m
[i
] = SCM_INUM (val
);
493 else if (SCM_BIGP (val
))
494 m
[i
] = scm_num2long (val
, 0, NULL
);
496 m
[i
] = SCM_REAL_VALUE (val
);
501 n
= SCM_UVECTOR_LENGTH (obj
);
503 m
= (double *) malloc (n
* sizeof (double));
506 for (i
= 0; i
< n
; ++i
)
507 m
[i
] = ((float *) SCM_VELTS (obj
))[i
];
511 n
= SCM_UVECTOR_LENGTH (obj
);
513 m
= (double*) malloc (n
* sizeof (double));
516 memcpy (m
, SCM_VELTS (obj
), n
* sizeof (double));
520 scm_wrong_type_arg (0, 0, obj
);
525 /* string conversions between C and Scheme */
527 /* gh_scm2newstr() -- Given a Scheme string STR, return a pointer to a
528 new copy of its contents, followed by a null byte. If lenp is
529 non-null, set *lenp to the string's length.
531 This function uses malloc to obtain storage for the copy; the
532 caller is responsible for freeing it. If out of memory, NULL is
535 Note that Scheme strings may contain arbitrary data, including null
536 characters. This means that null termination is not a reliable way
537 to determine the length of the returned value. However, the
538 function always copies the complete contents of STR, and sets
539 *LEN_P to the true length of the string (when LEN_P is non-null). */
541 gh_scm2newstr (SCM str
, size_t *lenp
)
546 SCM_ASSERT (SCM_STRINGP (str
), str
, SCM_ARG3
, "gh_scm2newstr");
548 len
= SCM_STRING_LENGTH (str
);
550 ret_str
= (char *) malloc ((len
+ 1) * sizeof (char));
553 /* so we copy tmp_str to ret_str, which is what we will allocate */
554 memcpy (ret_str
, SCM_STRING_CHARS (str
), len
);
555 scm_remember_upto_here_1 (str
);
556 /* now make sure we null-terminate it */
568 /* Copy LEN characters at START from the Scheme string SRC to memory
569 at DST. START is an index into SRC; zero means the beginning of
570 the string. DST has already been allocated by the caller.
572 If START + LEN is off the end of SRC, silently truncate the source
573 region to fit the string. If truncation occurs, the corresponding
574 area of DST is left unchanged. */
576 gh_get_substr (SCM src
, char *dst
, long start
, size_t len
)
578 size_t src_len
, effective_length
;
579 SCM_ASSERT (SCM_STRINGP (src
), src
, SCM_ARG3
, "gh_get_substr");
581 src_len
= SCM_STRING_LENGTH (src
);
582 effective_length
= (len
< src_len
) ? len
: src_len
;
583 memcpy (dst
+ start
, SCM_STRING_CHARS (src
), effective_length
* sizeof (char));
584 /* FIXME: must signal an error if len > src_len */
585 scm_remember_upto_here_1 (src
);
589 /* gh_scm2newsymbol() -- Given a Scheme symbol 'identifier, return a
590 pointer to a string with the symbol characters "identifier",
591 followed by a null byte. If lenp is non-null, set *lenp to the
594 This function uses malloc to obtain storage for the copy; the
595 caller is responsible for freeing it. If out of memory, NULL is
598 gh_symbol2newstr (SCM sym
, size_t *lenp
)
603 SCM_ASSERT (SCM_SYMBOLP (sym
), sym
, SCM_ARG3
, "gh_scm2newsymbol");
605 len
= SCM_SYMBOL_LENGTH (sym
);
607 ret_str
= (char *) malloc ((len
+ 1) * sizeof (char));
610 /* so we copy sym to ret_str, which is what we will allocate */
611 memcpy (ret_str
, SCM_SYMBOL_CHARS (sym
), len
);
612 scm_remember_upto_here_1 (sym
);
613 /* now make sure we null-terminate it */
625 /* create a new vector of the given length, all initialized to the
628 gh_make_vector (SCM len
, SCM fill
)
630 return scm_make_vector (len
, fill
);
633 /* set the given element of the given vector to the given value */
635 gh_vector_set_x (SCM vec
, SCM pos
, SCM val
)
637 return scm_vector_set_x (vec
, pos
, val
);
640 /* retrieve the given element of the given vector */
642 gh_vector_ref (SCM vec
, SCM pos
)
644 return scm_vector_ref (vec
, pos
);
647 /* returns the length of the given vector */
649 gh_vector_length (SCM v
)
651 return (unsigned long) SCM_VECTOR_LENGTH (v
);
655 /* uniform vector support */
657 /* returns the length as a C unsigned long integer */
659 gh_uniform_vector_length (SCM v
)
661 return (unsigned long) SCM_UVECTOR_LENGTH (v
);
664 /* gets the given element from a uniform vector; ilist is a list (or
665 possibly a single integer) of indices, and its length is the
666 dimension of the uniform vector */
668 gh_uniform_vector_ref (SCM v
, SCM ilist
)
670 return scm_uniform_vector_ref (v
, ilist
);
673 /* sets an individual element in a uniform vector */
675 /* gh_list_to_uniform_array ( */
678 /* Data lookups between C and Scheme
680 Look up a symbol with a given name, and return the object to which
681 it is bound. gh_lookup examines the Guile top level, and
682 gh_module_lookup checks the module namespace specified by the
685 The return value is the Scheme object to which SNAME is bound, or
686 SCM_UNDEFINED if SNAME is not bound in the given context.
690 gh_lookup (const char *sname
)
692 return gh_module_lookup (scm_current_module (), sname
);
697 gh_module_lookup (SCM module
, const char *sname
)
698 #define FUNC_NAME "gh_module_lookup"
702 SCM_VALIDATE_MODULE (SCM_ARG1
, module
);
704 sym
= scm_str2symbol (sname
);
705 var
= scm_sym2var (sym
, scm_module_lookup_closure (module
), SCM_BOOL_F
);
706 if (var
!= SCM_BOOL_F
)
707 return SCM_VARIABLE_REF (var
);
709 return SCM_UNDEFINED
;