1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001 Free Software Foundation, Inc.
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
7 * This library 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 GNU
10 * Lesser General Public License for more details.
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 /* data initialization and C<->Scheme data conversion */
24 #include "libguile/gh.h"
29 /* data conversion C->scheme */
39 return scm_long2num ((long) x
);
42 gh_ulong2scm (unsigned long x
)
44 return scm_ulong2num (x
);
49 return scm_long2num (x
);
52 gh_double2scm (double x
)
54 return scm_make_real (x
);
59 return SCM_MAKE_CHAR (c
);
62 gh_str2scm (const char *s
, size_t len
)
64 return scm_mem2string (s
, len
);
67 gh_str02scm (const char *s
)
69 return scm_makfrom0str (s
);
71 /* Copy LEN characters at SRC into the *existing* Scheme string DST,
72 starting at START. START is an index into DST; zero means the
73 beginning of the string.
75 If START + LEN is off the end of DST, signal an out-of-range
78 gh_set_substr (char *src
, SCM dst
, long start
, size_t len
)
83 SCM_ASSERT (SCM_STRINGP (dst
), dst
, SCM_ARG3
, "gh_set_substr");
85 dst_ptr
= SCM_STRING_CHARS (dst
);
86 dst_len
= SCM_STRING_LENGTH (dst
);
87 SCM_ASSERT (start
+ len
<= dst_len
, dst
, SCM_ARG4
, "gh_set_substr");
89 memmove (dst_ptr
+ start
, src
, len
);
90 scm_remember_upto_here_1 (dst
);
93 /* Return the symbol named SYMBOL_STR. */
95 gh_symbol2scm (const char *symbol_str
)
97 return scm_str2symbol(symbol_str
);
101 gh_ints2scm (const int *d
, long n
)
104 SCM v
= scm_c_make_vector (n
, SCM_UNSPECIFIED
);
105 for (i
= 0; i
< n
; ++i
)
106 SCM_VECTOR_SET (v
, i
, (SCM_FIXABLE (d
[i
]) ? SCM_MAKINUM (d
[i
]) : scm_i_long2big (d
[i
])));
112 gh_doubles2scm (const double *d
, long n
)
115 SCM v
= scm_c_make_vector (n
, SCM_UNSPECIFIED
);
117 for(i
= 0; i
< n
; i
++)
118 SCM_VECTOR_SET (v
, i
, scm_make_real (d
[i
]));
123 /* Do not use this function for building normal Scheme vectors, unless
124 you arrange for the elements to be protected from GC while you
125 initialize the vector. */
127 makvect (char *m
, size_t len
, int type
)
129 return scm_cell (SCM_MAKE_UVECTOR_TAG (len
, type
), (scm_t_bits
) m
);
133 gh_chars2byvect (const char *d
, long n
)
135 char *m
= scm_gc_malloc (n
* sizeof (char), "vector");
136 memcpy (m
, d
, n
* sizeof (char));
137 return makvect (m
, n
, scm_tc7_byvect
);
141 gh_shorts2svect (const short *d
, long n
)
143 char *m
= scm_gc_malloc (n
* sizeof (short), "vector");
144 memcpy (m
, d
, n
* sizeof (short));
145 return makvect (m
, n
, scm_tc7_svect
);
149 gh_longs2ivect (const long *d
, long n
)
151 char *m
= scm_gc_malloc (n
* sizeof (long), "vector");
152 memcpy (m
, d
, n
* sizeof (long));
153 return makvect (m
, n
, scm_tc7_ivect
);
157 gh_ulongs2uvect (const unsigned long *d
, long n
)
159 char *m
= scm_gc_malloc (n
* sizeof (unsigned long), "vector");
160 memcpy (m
, d
, n
* sizeof (unsigned long));
161 return makvect (m
, n
, scm_tc7_uvect
);
165 gh_floats2fvect (const float *d
, long n
)
167 char *m
= scm_gc_malloc (n
* sizeof (float), "vector");
168 memcpy (m
, d
, n
* sizeof (float));
169 return makvect (m
, n
, scm_tc7_fvect
);
173 gh_doubles2dvect (const double *d
, long n
)
175 char *m
= scm_gc_malloc (n
* sizeof (double), "vector");
176 memcpy (m
, d
, n
* sizeof (double));
177 return makvect (m
, n
, scm_tc7_dvect
);
181 /* data conversion scheme->C */
183 gh_scm2bool (SCM obj
)
185 return (SCM_FALSEP (obj
)) ? 0 : 1;
188 gh_scm2ulong (SCM obj
)
190 return scm_num2ulong (obj
, SCM_ARG1
, "gh_scm2ulong");
193 gh_scm2long (SCM obj
)
195 return scm_num2long (obj
, SCM_ARG1
, "gh_scm2long");
200 return (int) scm_num2int (obj
, SCM_ARG1
, "gh_scm2int");
203 gh_scm2double (SCM obj
)
205 return scm_num2dbl (obj
, "gh_scm2double");
208 gh_scm2char (SCM obj
)
209 #define FUNC_NAME "gh_scm2char"
211 SCM_VALIDATE_CHAR (SCM_ARG1
, obj
);
212 return SCM_CHAR (obj
);
216 /* Convert a vector, weak vector, string, substring or uniform vector
217 into an array of chars. If result array in arg 2 is NULL, malloc a
218 new one. If out of memory, return NULL. */
220 gh_scm2chars (SCM obj
, char *m
)
226 scm_wrong_type_arg (0, 0, obj
);
227 switch (SCM_TYP7 (obj
))
231 n
= SCM_VECTOR_LENGTH (obj
);
232 for (i
= 0; i
< n
; ++i
)
234 val
= SCM_VELTS (obj
)[i
];
238 if (v
< -128 || v
> 255)
239 scm_out_of_range (0, obj
);
242 scm_wrong_type_arg (0, 0, obj
);
245 m
= (char *) malloc (n
* sizeof (char));
248 for (i
= 0; i
< n
; ++i
)
249 m
[i
] = SCM_INUM (SCM_VELTS (obj
)[i
]);
253 n
= SCM_UVECTOR_LENGTH (obj
);
255 m
= (char *) malloc (n
* sizeof (char));
258 memcpy (m
, SCM_VELTS (obj
), n
* sizeof (char));
262 n
= SCM_STRING_LENGTH (obj
);
264 m
= (char *) malloc (n
* sizeof (char));
267 memcpy (m
, SCM_VELTS (obj
), n
* sizeof (char));
270 scm_wrong_type_arg (0, 0, obj
);
275 /* Convert a vector, weak vector or uniform vector into an array of
276 shorts. If result array in arg 2 is NULL, malloc a new one. If
277 out of memory, return NULL. */
279 gh_scm2shorts (SCM obj
, short *m
)
285 scm_wrong_type_arg (0, 0, obj
);
286 switch (SCM_TYP7 (obj
))
290 n
= SCM_VECTOR_LENGTH (obj
);
291 for (i
= 0; i
< n
; ++i
)
293 val
= SCM_VELTS (obj
)[i
];
297 if (v
< -32768 || v
> 65535)
298 scm_out_of_range (0, obj
);
301 scm_wrong_type_arg (0, 0, obj
);
304 m
= (short *) malloc (n
* sizeof (short));
307 for (i
= 0; i
< n
; ++i
)
308 m
[i
] = SCM_INUM (SCM_VELTS (obj
)[i
]);
312 n
= SCM_UVECTOR_LENGTH (obj
);
314 m
= (short *) malloc (n
* sizeof (short));
317 memcpy (m
, SCM_VELTS (obj
), n
* sizeof (short));
321 scm_wrong_type_arg (0, 0, obj
);
326 /* Convert a vector, weak vector or uniform vector into an array of
327 longs. If result array in arg 2 is NULL, malloc a new one. If out
328 of memory, return NULL. */
330 gh_scm2longs (SCM obj
, long *m
)
335 scm_wrong_type_arg (0, 0, obj
);
336 switch (SCM_TYP7 (obj
))
340 n
= SCM_VECTOR_LENGTH (obj
);
341 for (i
= 0; i
< n
; ++i
)
343 val
= SCM_VELTS (obj
)[i
];
344 if (!SCM_INUMP (val
) && !SCM_BIGP (val
))
345 scm_wrong_type_arg (0, 0, obj
);
348 m
= (long *) malloc (n
* sizeof (long));
351 for (i
= 0; i
< n
; ++i
)
353 val
= SCM_VELTS (obj
)[i
];
354 m
[i
] = SCM_INUMP (val
)
356 : scm_num2long (val
, 0, NULL
);
362 n
= SCM_UVECTOR_LENGTH (obj
);
364 m
= (long *) malloc (n
* sizeof (long));
367 memcpy (m
, SCM_VELTS (obj
), n
* sizeof (long));
371 scm_wrong_type_arg (0, 0, obj
);
376 /* Convert a vector, weak vector or uniform vector into an array of
377 floats. If result array in arg 2 is NULL, malloc a new one. If
378 out of memory, return NULL. */
380 gh_scm2floats (SCM obj
, float *m
)
385 scm_wrong_type_arg (0, 0, obj
);
386 switch (SCM_TYP7 (obj
))
390 n
= SCM_VECTOR_LENGTH (obj
);
391 for (i
= 0; i
< n
; ++i
)
393 val
= SCM_VELTS (obj
)[i
];
395 && !(SCM_BIGP (val
) || SCM_REALP (val
)))
396 scm_wrong_type_arg (0, 0, val
);
399 m
= (float *) malloc (n
* sizeof (float));
402 for (i
= 0; i
< n
; ++i
)
404 val
= SCM_VELTS (obj
)[i
];
406 m
[i
] = SCM_INUM (val
);
407 else if (SCM_BIGP (val
))
408 m
[i
] = scm_num2long (val
, 0, NULL
);
410 m
[i
] = SCM_REAL_VALUE (val
);
415 n
= SCM_UVECTOR_LENGTH (obj
);
417 m
= (float *) malloc (n
* sizeof (float));
420 memcpy (m
, (float *) SCM_VELTS (obj
), n
* sizeof (float));
424 n
= SCM_UVECTOR_LENGTH (obj
);
426 m
= (float*) malloc (n
* sizeof (float));
429 for (i
= 0; i
< n
; ++i
)
430 m
[i
] = ((double *) SCM_VELTS (obj
))[i
];
434 scm_wrong_type_arg (0, 0, obj
);
439 /* Convert a vector, weak vector or uniform vector into an array of
440 doubles. If result array in arg 2 is NULL, malloc a new one. If
441 out of memory, return NULL. */
443 gh_scm2doubles (SCM obj
, double *m
)
448 scm_wrong_type_arg (0, 0, obj
);
449 switch (SCM_TYP7 (obj
))
453 n
= SCM_VECTOR_LENGTH (obj
);
454 for (i
= 0; i
< n
; ++i
)
456 val
= SCM_VELTS (obj
)[i
];
458 && !(SCM_BIGP (val
) || SCM_REALP (val
)))
459 scm_wrong_type_arg (0, 0, val
);
462 m
= (double *) malloc (n
* sizeof (double));
465 for (i
= 0; i
< n
; ++i
)
467 val
= SCM_VELTS (obj
)[i
];
469 m
[i
] = SCM_INUM (val
);
470 else if (SCM_BIGP (val
))
471 m
[i
] = scm_num2long (val
, 0, NULL
);
473 m
[i
] = SCM_REAL_VALUE (val
);
478 n
= SCM_UVECTOR_LENGTH (obj
);
480 m
= (double *) malloc (n
* sizeof (double));
483 for (i
= 0; i
< n
; ++i
)
484 m
[i
] = ((float *) SCM_VELTS (obj
))[i
];
488 n
= SCM_UVECTOR_LENGTH (obj
);
490 m
= (double*) malloc (n
* sizeof (double));
493 memcpy (m
, SCM_VELTS (obj
), n
* sizeof (double));
497 scm_wrong_type_arg (0, 0, obj
);
502 /* string conversions between C and Scheme */
504 /* gh_scm2newstr() -- Given a Scheme string STR, return a pointer to a
505 new copy of its contents, followed by a null byte. If lenp is
506 non-null, set *lenp to the string's length.
508 This function uses malloc to obtain storage for the copy; the
509 caller is responsible for freeing it. If out of memory, NULL is
512 Note that Scheme strings may contain arbitrary data, including null
513 characters. This means that null termination is not a reliable way
514 to determine the length of the returned value. However, the
515 function always copies the complete contents of STR, and sets
516 *LEN_P to the true length of the string (when LEN_P is non-null). */
518 gh_scm2newstr (SCM str
, size_t *lenp
)
523 SCM_ASSERT (SCM_STRINGP (str
), str
, SCM_ARG3
, "gh_scm2newstr");
525 len
= SCM_STRING_LENGTH (str
);
527 ret_str
= (char *) malloc ((len
+ 1) * sizeof (char));
530 /* so we copy tmp_str to ret_str, which is what we will allocate */
531 memcpy (ret_str
, SCM_STRING_CHARS (str
), len
);
532 scm_remember_upto_here_1 (str
);
533 /* now make sure we null-terminate it */
545 /* Copy LEN characters at START from the Scheme string SRC to memory
546 at DST. START is an index into SRC; zero means the beginning of
547 the string. DST has already been allocated by the caller.
549 If START + LEN is off the end of SRC, silently truncate the source
550 region to fit the string. If truncation occurs, the corresponding
551 area of DST is left unchanged. */
553 gh_get_substr (SCM src
, char *dst
, long start
, size_t len
)
555 size_t src_len
, effective_length
;
556 SCM_ASSERT (SCM_STRINGP (src
), src
, SCM_ARG3
, "gh_get_substr");
558 src_len
= SCM_STRING_LENGTH (src
);
559 effective_length
= (len
< src_len
) ? len
: src_len
;
560 memcpy (dst
+ start
, SCM_STRING_CHARS (src
), effective_length
* sizeof (char));
561 /* FIXME: must signal an error if len > src_len */
562 scm_remember_upto_here_1 (src
);
566 /* gh_scm2newsymbol() -- Given a Scheme symbol 'identifier, return a
567 pointer to a string with the symbol characters "identifier",
568 followed by a null byte. If lenp is non-null, set *lenp to the
571 This function uses malloc to obtain storage for the copy; the
572 caller is responsible for freeing it. If out of memory, NULL is
575 gh_symbol2newstr (SCM sym
, size_t *lenp
)
580 SCM_ASSERT (SCM_SYMBOLP (sym
), sym
, SCM_ARG3
, "gh_scm2newsymbol");
582 len
= SCM_SYMBOL_LENGTH (sym
);
584 ret_str
= (char *) malloc ((len
+ 1) * sizeof (char));
587 /* so we copy sym to ret_str, which is what we will allocate */
588 memcpy (ret_str
, SCM_SYMBOL_CHARS (sym
), len
);
589 scm_remember_upto_here_1 (sym
);
590 /* now make sure we null-terminate it */
602 /* create a new vector of the given length, all initialized to the
605 gh_make_vector (SCM len
, SCM fill
)
607 return scm_make_vector (len
, fill
);
610 /* set the given element of the given vector to the given value */
612 gh_vector_set_x (SCM vec
, SCM pos
, SCM val
)
614 return scm_vector_set_x (vec
, pos
, val
);
617 /* retrieve the given element of the given vector */
619 gh_vector_ref (SCM vec
, SCM pos
)
621 return scm_vector_ref (vec
, pos
);
624 /* returns the length of the given vector */
626 gh_vector_length (SCM v
)
628 return (unsigned long) SCM_VECTOR_LENGTH (v
);
632 /* uniform vector support */
634 /* returns the length as a C unsigned long integer */
636 gh_uniform_vector_length (SCM v
)
638 return (unsigned long) SCM_UVECTOR_LENGTH (v
);
641 /* gets the given element from a uniform vector; ilist is a list (or
642 possibly a single integer) of indices, and its length is the
643 dimension of the uniform vector */
645 gh_uniform_vector_ref (SCM v
, SCM ilist
)
647 return scm_uniform_vector_ref (v
, ilist
);
650 /* sets an individual element in a uniform vector */
652 /* gh_list_to_uniform_array ( */
655 /* Data lookups between C and Scheme
657 Look up a symbol with a given name, and return the object to which
658 it is bound. gh_lookup examines the Guile top level, and
659 gh_module_lookup checks the module namespace specified by the
662 The return value is the Scheme object to which SNAME is bound, or
663 SCM_UNDEFINED if SNAME is not bound in the given context.
667 gh_lookup (const char *sname
)
669 return gh_module_lookup (scm_current_module (), sname
);
674 gh_module_lookup (SCM module
, const char *sname
)
675 #define FUNC_NAME "gh_module_lookup"
679 SCM_VALIDATE_MODULE (SCM_ARG1
, module
);
681 sym
= scm_str2symbol (sname
);
682 var
= scm_sym2var (sym
, scm_module_lookup_closure (module
), SCM_BOOL_F
);
683 if (var
!= SCM_BOOL_F
)
684 return SCM_VARIABLE_REF (var
);
686 return SCM_UNDEFINED
;