(SCM_VALIDATE_VECTOR, SCM_VALIDATE_VECTOR_OR_DVECTOR): use
[bpt/guile.git] / libguile / convert.i.c
CommitLineData
1fa86ca5
SJ
1/* this file is #include'd (x times) by convert.c */
2
94897289
MV
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.
6
7 - CTYPE
8
9 The type of an element of the C array, for example 'char'.
10
11 - FROM_CTYPE
12
13 The function that converts a CTYPE to a SCM, for example
14 scm_from_char.
15
16 - UVEC_TAG
17
18 The tag of a suitable uniform vector that can hold the CTYPE, for
19 example 's8'.
20
21 - UVEC_CTYPE
22
23 The C type of an element of the uniform vector, for example
24 scm_t_int8.
25
26 - SCM2CTYPES
27
28 The name of the 'SCM-to-C' function, for example scm_c_scm2chars.
29
30 - CTYPES2SCM
31
32 The name of the 'C-to-SCM' function, for example, scm_c_chars2scm.
33
34 - CTYPES2UVECT
35
36 The name of the 'C-to-uniform-vector' function, for example
37 scm_c_chars2byvect. It will create a uniform vector of kind
38 UVEC_TAG.
39
40 - CTYPES2UVECT_2
41
42 The name of a second 'C-to-uniform-vector' function. Leave
43 undefined if you want only one such function.
44
45 - CTYPE_2
46 - UVEC_TAG_2
47 - UVEC_CTYPE_2
48
49 The tag and C type of the second kind of uniform vector, for use
50 with the function described above.
51
52*/
53
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
57
58/* But the second level does. */
59#define F(pre,T,suf) paste(pre,T,suf)
60#define S(T) stringify(T)
61
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.
64*/
65
1fa86ca5
SJ
66CTYPE *
67SCM2CTYPES (SCM obj, CTYPE *data)
68{
94897289 69 size_t len, i;
b590aceb 70 const UVEC_CTYPE *uvec_elements;
1fa86ca5 71
94897289
MV
72 obj = F(scm_any_to_,UVEC_TAG,vector) (obj);
73 len = scm_c_uniform_vector_length (obj);
74 uvec_elements = F(scm_,UVEC_TAG,vector_elements) (obj);
1fa86ca5 75
94897289
MV
76 if (data == NULL)
77 data = scm_malloc (len * sizeof (CTYPE));
78 for (i = 0; i < len; i++)
79 data[i] = uvec_elements[i];
80
b590aceb 81 scm_uniform_vector_release_elements (obj);
1fa86ca5
SJ
82 return data;
83}
1fa86ca5 84
94897289 85/* Converts a C array into a vector. */
1fa86ca5 86
1fa86ca5 87SCM
94897289 88CTYPES2SCM (const CTYPE *data, long n)
1fa86ca5 89{
97820583 90 long i;
94897289
MV
91 SCM v;
92
93 v = scm_c_make_vector (n, SCM_UNSPECIFIED);
1fa86ca5 94
97820583 95 for (i = 0; i < n; i++)
94897289 96 SCM_VECTOR_SET (v, i, FROM_CTYPE (data[i]));
d1d2f094 97
94897289 98 return v;
1fa86ca5 99}
1fa86ca5 100
94897289
MV
101/* Converts a C array into a uniform vector. */
102
1fa86ca5 103SCM
94897289 104CTYPES2UVECT (const CTYPE *data, long n)
1fa86ca5 105{
97820583 106 long i;
94897289
MV
107 SCM uvec;
108 UVEC_CTYPE *uvec_elements;
109
110 uvec = F(scm_make_,UVEC_TAG,vector) (scm_from_long (n), SCM_UNDEFINED);
b590aceb 111 uvec_elements = F(scm_,UVEC_TAG,vector_writable_elements) (uvec);
1fa86ca5 112
97820583 113 for (i = 0; i < n; i++)
94897289 114 uvec_elements[i] = data[i];
1fa86ca5 115
b590aceb 116 scm_uniform_vector_release_writable_elements (uvec);
94897289
MV
117 return uvec;
118}
1fa86ca5 119
94897289 120#ifdef CTYPE2UVECT_2
edb810bb 121
1fa86ca5 122SCM
94897289 123CTYPES2UVECT_2 (const CTYPE_2 *data, long n)
1fa86ca5
SJ
124{
125 long i;
94897289
MV
126 SCM uvec;
127 UVEC_CTYPE_2 *uvec_elements;
34d19ef6 128
94897289
MV
129 uvec = F(scm_make_,UVEC_TAG_2,vector) (scm_from_long (n), SCM_UNDEFINED);
130 uvec_elements = F(scm_,UVEC_TAG_2,vector_elements) (obj);
34d19ef6 131
1fa86ca5 132 for (i = 0; i < n; i++)
94897289
MV
133 v[i] = data[i];
134
135 scm_uniform_vector_release (uvec);
136 return uvec;
1fa86ca5 137}
1fa86ca5 138
94897289
MV
139#endif
140
141#undef paste
142#undef stringify
143#undef F
144#undef S
145
146#undef CTYPE
147#undef FROM_CTYPE
148#undef UVEC_TAG
149#undef UVEC_CTYPE
1fa86ca5 150#undef SCM2CTYPES
1fa86ca5 151#undef CTYPES2SCM
1fa86ca5 152#undef CTYPES2UVECT
94897289
MV
153#ifdef CTYPES2UVECT_2
154#undef CTYPES2UVECT_2
155#undef CTYPE_2
156#undef UVEC_TAG_2
157#undef UVEC_CTYPE_2
d1d2f094 158#endif
1fa86ca5
SJ
159
160/*
161 Local Variables:
162 c-file-style: "gnu"
163 End:
164*/