1
/* this file is #include'd (x times) by convert.c */
3
/* You need to define the following macros before including this
4
template. They are undefined at the end of this file to give a
5
clean slate for the next inclusion.
9
The type of an element of the C array, for example 'char'.
13
The function that converts a CTYPE to a SCM, for example
18
The tag of a suitable uniform vector that can hold the CTYPE, for
23
The C type of an element of the uniform vector, for example
28
The name of the 'SCM-to-C' function, for example scm_c_scm2chars.
32
The name of the 'C-to-SCM' function, for example, scm_c_chars2scm.
36
The name of the 'C-to-uniform-vector' function, for example
37
scm_c_chars2byvect. It will create a uniform vector of kind
42
The name of a second 'C-to-uniform-vector' function. Leave
43
undefined if you want only one such function.
49
The tag and C type of the second kind of uniform vector, for use
50
with the function described above.
54
/* The first level does not expand macros in the arguments. */
55
#define paste(a1,a2,a3) a1##a2##a3
56
#define stringify(a) #a
58
/* But the second level does. */
59
#define F(pre,T,suf) paste(pre,T,suf)
60
#define S(T) stringify(T)
62
/* Convert a vector, list or uniform vector into a C array. If the
63
result array in argument 2 is NULL, malloc() a new one.
67
SCM2CTYPES (SCM obj, CTYPE *data)
69
scm_t_array_handle handle;
72
const UVEC_CTYPE *uvec_elements;
74
obj = F(scm_any_to_,UVEC_TAG,vector) (obj);
75
uvec_elements = F(scm_,UVEC_TAG,vector_elements) (obj, &handle, &len, &inc);
78
data = scm_malloc (len * sizeof (CTYPE));
79
for (i = 0; i < len; i++, uvec_elements += inc)
80
data[i] = uvec_elements[i];
82
scm_array_handle_release (&handle);
87
/* Converts a C array into a vector. */
90
CTYPES2SCM (const CTYPE *data, long n)
95
v = scm_c_make_vector (n, SCM_UNSPECIFIED);
97
for (i = 0; i < n; i++)
98
SCM_SIMPLE_VECTOR_SET (v, i, FROM_CTYPE (data[i]));
103
/* Converts a C array into a uniform vector. */
106
CTYPES2UVECT (const CTYPE *data, long n)
108
scm_t_array_handle handle;
111
UVEC_CTYPE *uvec_elements;
113
uvec = F(scm_make_,UVEC_TAG,vector) (scm_from_long (n), SCM_UNDEFINED);
114
uvec_elements = F(scm_,UVEC_TAG,vector_writable_elements) (uvec, &handle,
116
for (i = 0; i < n; i++)
117
uvec_elements[i] = data[i];
119
scm_array_handle_release (&handle);
127
CTYPES2UVECT_2 (const CTYPE_2 *data, long n)
129
scm_t_array_handle handle;
132
UVEC_CTYPE_2 *uvec_elements;
134
uvec = F(scm_make_,UVEC_TAG_2,vector) (scm_from_long (n), SCM_UNDEFINED);
135
uvec_elements = F(scm_,UVEC_TAG_2,vector_writable_elements) (uvec, &handle,
138
for (i = 0; i < n; i++)
139
uvec_elements[i] = data[i];
141
scm_array_handle_release (&handle);
160
#ifdef CTYPES2UVECT_2
161
#undef CTYPES2UVECT_2