1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2004 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 */
34 return scm_from_bool(x
);
39 return scm_from_long ((long) x
);
42 gh_ulong2scm (unsigned long x
)
44 return scm_from_ulong (x
);
49 return scm_from_long (x
);
52 gh_double2scm (double x
)
54 return scm_from_double (x
);
59 return SCM_MAKE_CHAR (c
);
62 gh_str2scm (const char *s
, size_t len
)
64 return scm_from_locale_stringn (s
, len
);
67 gh_str02scm (const char *s
)
69 return scm_from_locale_string (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 (const char *src
, SCM dst
, long start
, size_t len
)
83 SCM_ASSERT (scm_is_string (dst
), dst
, SCM_ARG3
, "gh_set_substr");
85 dst_len
= scm_i_string_length (dst
);
86 SCM_ASSERT (start
+ len
<= dst_len
, dst
, SCM_ARG4
, "gh_set_substr");
88 dst_ptr
= scm_i_string_writable_chars (dst
);
89 memmove (dst_ptr
+ start
, src
, len
);
90 scm_i_string_stop_writing ();
91 scm_remember_upto_here_1 (dst
);
94 /* Return the symbol named SYMBOL_STR. */
96 gh_symbol2scm (const char *symbol_str
)
98 return scm_from_locale_symbol(symbol_str
);
102 gh_ints2scm (const int *d
, long n
)
105 SCM v
= scm_c_make_vector (n
, SCM_UNSPECIFIED
);
106 for (i
= 0; i
< n
; ++i
)
107 SCM_VECTOR_SET (v
, i
, scm_from_int (d
[i
]));
113 gh_doubles2scm (const double *d
, long n
)
116 SCM v
= scm_c_make_vector (n
, SCM_UNSPECIFIED
);
118 for(i
= 0; i
< n
; i
++)
119 SCM_VECTOR_SET (v
, i
, scm_from_double (d
[i
]));
124 /* Do not use this function for building normal Scheme vectors, unless
125 you arrange for the elements to be protected from GC while you
126 initialize the vector. */
128 makvect (char *m
, size_t len
, int type
)
130 return scm_cell (SCM_MAKE_UVECTOR_TAG (len
, type
), (scm_t_bits
) m
);
134 gh_chars2byvect (const char *d
, long n
)
136 char *m
= scm_gc_malloc (n
* sizeof (char), "vector");
137 memcpy (m
, d
, n
* sizeof (char));
138 return makvect (m
, n
, scm_tc7_byvect
);
142 gh_shorts2svect (const short *d
, long n
)
144 char *m
= scm_gc_malloc (n
* sizeof (short), "vector");
145 memcpy (m
, d
, n
* sizeof (short));
146 return makvect (m
, n
, scm_tc7_svect
);
150 gh_longs2ivect (const long *d
, long n
)
152 char *m
= scm_gc_malloc (n
* sizeof (long), "vector");
153 memcpy (m
, d
, n
* sizeof (long));
154 return makvect (m
, n
, scm_tc7_ivect
);
158 gh_ulongs2uvect (const unsigned long *d
, long n
)
160 char *m
= scm_gc_malloc (n
* sizeof (unsigned long), "vector");
161 memcpy (m
, d
, n
* sizeof (unsigned long));
162 return makvect (m
, n
, scm_tc7_uvect
);
166 gh_floats2fvect (const float *d
, long n
)
168 char *m
= scm_gc_malloc (n
* sizeof (float), "vector");
169 memcpy (m
, d
, n
* sizeof (float));
170 return makvect (m
, n
, scm_tc7_fvect
);
174 gh_doubles2dvect (const double *d
, long n
)
176 char *m
= scm_gc_malloc (n
* sizeof (double), "vector");
177 memcpy (m
, d
, n
* sizeof (double));
178 return makvect (m
, n
, scm_tc7_dvect
);
182 /* data conversion scheme->C */
184 gh_scm2bool (SCM obj
)
186 return (scm_is_false (obj
)) ? 0 : 1;
189 gh_scm2ulong (SCM obj
)
191 return scm_to_ulong (obj
);
194 gh_scm2long (SCM obj
)
196 return scm_to_long (obj
);
201 return scm_to_int (obj
);
204 gh_scm2double (SCM obj
)
206 return scm_to_double (obj
);
209 gh_scm2char (SCM obj
)
210 #define FUNC_NAME "gh_scm2char"
212 SCM_VALIDATE_CHAR (SCM_ARG1
, obj
);
213 return SCM_CHAR (obj
);
217 /* Convert a vector, weak vector, string, substring or uniform vector
218 into an array of chars. If result array in arg 2 is NULL, malloc a
219 new one. If out of memory, return NULL. */
221 gh_scm2chars (SCM obj
, char *m
)
227 scm_wrong_type_arg (0, 0, obj
);
228 switch (SCM_TYP7 (obj
))
232 n
= SCM_VECTOR_LENGTH (obj
);
233 for (i
= 0; i
< n
; ++i
)
235 val
= SCM_VELTS (obj
)[i
];
236 if (SCM_I_INUMP (val
))
238 v
= SCM_I_INUM (val
);
239 if (v
< -128 || v
> 255)
240 scm_out_of_range (0, obj
);
243 scm_wrong_type_arg (0, 0, obj
);
246 m
= (char *) malloc (n
* sizeof (char));
249 for (i
= 0; i
< n
; ++i
)
250 m
[i
] = SCM_I_INUM (SCM_VELTS (obj
)[i
]);
254 n
= SCM_UVECTOR_LENGTH (obj
);
256 m
= (char *) malloc (n
* sizeof (char));
259 memcpy (m
, SCM_VELTS (obj
), n
* sizeof (char));
263 n
= scm_i_string_length (obj
);
265 m
= (char *) malloc (n
* sizeof (char));
268 memcpy (m
, scm_i_string_chars (obj
), n
* sizeof (char));
271 scm_wrong_type_arg (0, 0, obj
);
276 /* Convert a vector, weak vector or uniform vector into an array of
277 shorts. If result array in arg 2 is NULL, malloc a new one. If
278 out of memory, return NULL. */
280 gh_scm2shorts (SCM obj
, short *m
)
286 scm_wrong_type_arg (0, 0, obj
);
287 switch (SCM_TYP7 (obj
))
291 n
= SCM_VECTOR_LENGTH (obj
);
292 for (i
= 0; i
< n
; ++i
)
294 val
= SCM_VELTS (obj
)[i
];
295 if (SCM_I_INUMP (val
))
297 v
= SCM_I_INUM (val
);
298 if (v
< -32768 || v
> 65535)
299 scm_out_of_range (0, obj
);
302 scm_wrong_type_arg (0, 0, obj
);
305 m
= (short *) malloc (n
* sizeof (short));
308 for (i
= 0; i
< n
; ++i
)
309 m
[i
] = SCM_I_INUM (SCM_VELTS (obj
)[i
]);
313 n
= SCM_UVECTOR_LENGTH (obj
);
315 m
= (short *) malloc (n
* sizeof (short));
318 memcpy (m
, SCM_VELTS (obj
), n
* sizeof (short));
322 scm_wrong_type_arg (0, 0, obj
);
327 /* Convert a vector, weak vector or uniform vector into an array of
328 longs. If result array in arg 2 is NULL, malloc a new one. If out
329 of memory, return NULL. */
331 gh_scm2longs (SCM obj
, long *m
)
336 scm_wrong_type_arg (0, 0, obj
);
337 switch (SCM_TYP7 (obj
))
341 n
= SCM_VECTOR_LENGTH (obj
);
342 for (i
= 0; i
< n
; ++i
)
344 val
= SCM_VELTS (obj
)[i
];
345 if (!SCM_I_INUMP (val
) && !SCM_BIGP (val
))
346 scm_wrong_type_arg (0, 0, obj
);
349 m
= (long *) malloc (n
* sizeof (long));
352 for (i
= 0; i
< n
; ++i
)
354 val
= SCM_VELTS (obj
)[i
];
355 m
[i
] = SCM_I_INUMP (val
)
363 n
= SCM_UVECTOR_LENGTH (obj
);
365 m
= (long *) malloc (n
* sizeof (long));
368 memcpy (m
, SCM_VELTS (obj
), n
* sizeof (long));
372 scm_wrong_type_arg (0, 0, obj
);
377 /* Convert a vector, weak vector or uniform vector into an array of
378 floats. If result array in arg 2 is NULL, malloc a new one. If
379 out of memory, return NULL. */
381 gh_scm2floats (SCM obj
, float *m
)
386 scm_wrong_type_arg (0, 0, obj
);
387 switch (SCM_TYP7 (obj
))
391 n
= SCM_VECTOR_LENGTH (obj
);
392 for (i
= 0; i
< n
; ++i
)
394 val
= SCM_VELTS (obj
)[i
];
395 if (!SCM_I_INUMP (val
)
396 && !(SCM_BIGP (val
) || SCM_REALP (val
)))
397 scm_wrong_type_arg (0, 0, val
);
400 m
= (float *) malloc (n
* sizeof (float));
403 for (i
= 0; i
< n
; ++i
)
405 val
= SCM_VELTS (obj
)[i
];
406 if (SCM_I_INUMP (val
))
407 m
[i
] = SCM_I_INUM (val
);
408 else if (SCM_BIGP (val
))
409 m
[i
] = scm_to_long (val
);
411 m
[i
] = SCM_REAL_VALUE (val
);
416 n
= SCM_UVECTOR_LENGTH (obj
);
418 m
= (float *) malloc (n
* sizeof (float));
421 memcpy (m
, (float *) SCM_VELTS (obj
), n
* sizeof (float));
425 n
= SCM_UVECTOR_LENGTH (obj
);
427 m
= (float*) malloc (n
* sizeof (float));
430 for (i
= 0; i
< n
; ++i
)
431 m
[i
] = ((double *) SCM_VELTS (obj
))[i
];
435 scm_wrong_type_arg (0, 0, obj
);
440 /* Convert a vector, weak vector or uniform vector into an array of
441 doubles. If result array in arg 2 is NULL, malloc a new one. If
442 out of memory, return NULL. */
444 gh_scm2doubles (SCM obj
, double *m
)
449 scm_wrong_type_arg (0, 0, obj
);
450 switch (SCM_TYP7 (obj
))
454 n
= SCM_VECTOR_LENGTH (obj
);
455 for (i
= 0; i
< n
; ++i
)
457 val
= SCM_VELTS (obj
)[i
];
458 if (!SCM_I_INUMP (val
)
459 && !(SCM_BIGP (val
) || SCM_REALP (val
)))
460 scm_wrong_type_arg (0, 0, val
);
463 m
= (double *) malloc (n
* sizeof (double));
466 for (i
= 0; i
< n
; ++i
)
468 val
= SCM_VELTS (obj
)[i
];
469 if (SCM_I_INUMP (val
))
470 m
[i
] = SCM_I_INUM (val
);
471 else if (SCM_BIGP (val
))
472 m
[i
] = scm_to_long (val
);
474 m
[i
] = SCM_REAL_VALUE (val
);
479 n
= SCM_UVECTOR_LENGTH (obj
);
481 m
= (double *) malloc (n
* sizeof (double));
484 for (i
= 0; i
< n
; ++i
)
485 m
[i
] = ((float *) SCM_VELTS (obj
))[i
];
489 n
= SCM_UVECTOR_LENGTH (obj
);
491 m
= (double*) malloc (n
* sizeof (double));
494 memcpy (m
, SCM_VELTS (obj
), n
* sizeof (double));
498 scm_wrong_type_arg (0, 0, obj
);
503 /* string conversions between C and Scheme */
505 /* gh_scm2newstr() -- Given a Scheme string STR, return a pointer to a
506 new copy of its contents, followed by a null byte. If lenp is
507 non-null, set *lenp to the string's length.
509 This function uses malloc to obtain storage for the copy; the
510 caller is responsible for freeing it. If out of memory, NULL is
513 Note that Scheme strings may contain arbitrary data, including null
514 characters. This means that null termination is not a reliable way
515 to determine the length of the returned value. However, the
516 function always copies the complete contents of STR, and sets
517 *LEN_P to the true length of the string (when LEN_P is non-null). */
519 gh_scm2newstr (SCM str
, size_t *lenp
)
523 /* We can't use scm_to_locale_stringn directly since it does not
524 guarantee null-termination when lenp is non-NULL.
527 ret_str
= scm_to_locale_string (str
);
529 *lenp
= scm_i_string_length (str
);
533 /* Copy LEN characters at START from the Scheme string SRC to memory
534 at DST. START is an index into SRC; zero means the beginning of
535 the string. DST has already been allocated by the caller.
537 If START + LEN is off the end of SRC, silently truncate the source
538 region to fit the string. If truncation occurs, the corresponding
539 area of DST is left unchanged. */
541 gh_get_substr (SCM src
, char *dst
, long start
, size_t len
)
543 size_t src_len
, effective_length
;
544 SCM_ASSERT (scm_is_string (src
), src
, SCM_ARG3
, "gh_get_substr");
546 src_len
= scm_i_string_length (src
);
547 effective_length
= (len
< src_len
) ? len
: src_len
;
548 memcpy (dst
+ start
, scm_i_string_chars (src
), effective_length
* sizeof (char));
549 /* FIXME: must signal an error if len > src_len */
550 scm_remember_upto_here_1 (src
);
554 /* gh_scm2newsymbol() -- Given a Scheme symbol 'identifier, return a
555 pointer to a string with the symbol characters "identifier",
556 followed by a null byte. If lenp is non-null, set *lenp to the
559 This function uses malloc to obtain storage for the copy; the
560 caller is responsible for freeing it. If out of memory, NULL is
563 gh_symbol2newstr (SCM sym
, size_t *lenp
)
565 return gh_scm2newstr (scm_symbol_to_string (sym
), lenp
);
569 /* create a new vector of the given length, all initialized to the
572 gh_make_vector (SCM len
, SCM fill
)
574 return scm_make_vector (len
, fill
);
577 /* set the given element of the given vector to the given value */
579 gh_vector_set_x (SCM vec
, SCM pos
, SCM val
)
581 return scm_vector_set_x (vec
, pos
, val
);
584 /* retrieve the given element of the given vector */
586 gh_vector_ref (SCM vec
, SCM pos
)
588 return scm_vector_ref (vec
, pos
);
591 /* returns the length of the given vector */
593 gh_vector_length (SCM v
)
595 return (unsigned long) SCM_VECTOR_LENGTH (v
);
599 /* uniform vector support */
601 /* returns the length as a C unsigned long integer */
603 gh_uniform_vector_length (SCM v
)
605 return (unsigned long) SCM_UVECTOR_LENGTH (v
);
608 /* gets the given element from a uniform vector; ilist is a list (or
609 possibly a single integer) of indices, and its length is the
610 dimension of the uniform vector */
612 gh_uniform_vector_ref (SCM v
, SCM ilist
)
614 return scm_uniform_vector_ref (v
, ilist
);
617 /* sets an individual element in a uniform vector */
619 /* gh_list_to_uniform_array ( */
622 /* Data lookups between C and Scheme
624 Look up a symbol with a given name, and return the object to which
625 it is bound. gh_lookup examines the Guile top level, and
626 gh_module_lookup checks the module namespace specified by the
629 The return value is the Scheme object to which SNAME is bound, or
630 SCM_UNDEFINED if SNAME is not bound in the given context.
634 gh_lookup (const char *sname
)
636 return gh_module_lookup (scm_current_module (), sname
);
641 gh_module_lookup (SCM module
, const char *sname
)
642 #define FUNC_NAME "gh_module_lookup"
646 SCM_VALIDATE_MODULE (SCM_ARG1
, module
);
648 sym
= scm_from_locale_symbol (sname
);
649 var
= scm_sym2var (sym
, scm_module_lookup_closure (module
), SCM_BOOL_F
);
650 if (var
!= SCM_BOOL_F
)
651 return SCM_VARIABLE_REF (var
);
653 return SCM_UNDEFINED
;