Commit | Line | Data |
---|---|---|
16d35552 | 1 | /* Copyright (C) 1995,1996,1997,1998, 1999, 2000 Free Software Foundation, Inc. |
ee2a8b9b JB |
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) | |
5 | * any later version. | |
6 | * | |
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. | |
11 | * | |
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 | |
82892bed JB |
14 | * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
15 | * Boston, MA 02111-1307 USA | |
ee2a8b9b JB |
16 | * |
17 | * As a special exception, the Free Software Foundation gives permission | |
18 | * for additional uses of the text contained in its release of GUILE. | |
19 | * | |
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. | |
25 | * | |
26 | * This exception does not however invalidate any other reasons why | |
27 | * the executable file might be covered by the GNU General Public License. | |
28 | * | |
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. | |
36 | * | |
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. | |
82892bed | 39 | * If you do not wish that, delete this exception notice. */ |
ee2a8b9b JB |
40 | \f |
41 | ||
42 | /* data initialization and C<->Scheme data conversion */ | |
43 | ||
44 | #include <stdio.h> | |
45 | ||
a0599745 | 46 | #include "libguile/gh.h" |
bd9e24b3 GH |
47 | #ifdef HAVE_STRING_H |
48 | #include <string.h> | |
49 | #endif | |
ee2a8b9b JB |
50 | |
51 | /* data conversion C->scheme */ | |
bcee10dd DH |
52 | |
53 | #if (SCM_DEBUG_DEPRECATED == 0) | |
54 | ||
ee2a8b9b | 55 | SCM |
dbb3005d MG |
56 | gh_int2scmb (int x) /* this is being phased out */ |
57 | { | |
156dcb09 | 58 | return SCM_BOOL(x); |
dbb3005d | 59 | } |
bcee10dd DH |
60 | |
61 | #endif /* SCM_DEBUG_DEPRECATED == 0 */ | |
62 | ||
dbb3005d MG |
63 | SCM |
64 | gh_bool2scm (int x) | |
ee2a8b9b | 65 | { |
156dcb09 | 66 | return SCM_BOOL(x); |
ee2a8b9b JB |
67 | } |
68 | SCM | |
69 | gh_int2scm (int x) | |
70 | { | |
71 | return scm_long2num ((long) x); | |
72 | } | |
73 | SCM | |
74 | gh_ulong2scm (unsigned long x) | |
75 | { | |
76 | return scm_ulong2num (x); | |
77 | } | |
78 | SCM | |
79 | gh_long2scm (long x) | |
80 | { | |
81 | return scm_long2num (x); | |
82 | } | |
83 | SCM | |
84 | gh_double2scm (double x) | |
85 | { | |
f8de44c1 | 86 | return scm_make_real (x); |
ee2a8b9b JB |
87 | } |
88 | SCM | |
89 | gh_char2scm (char c) | |
90 | { | |
7866a09b | 91 | return SCM_MAKE_CHAR (c); |
ee2a8b9b JB |
92 | } |
93 | SCM | |
70d63753 | 94 | gh_str2scm (const char *s, int len) |
ee2a8b9b JB |
95 | { |
96 | return scm_makfromstr (s, len, 0); | |
97 | } | |
98 | SCM | |
6e706938 | 99 | gh_str02scm (const char *s) |
ee2a8b9b JB |
100 | { |
101 | return scm_makfrom0str (s); | |
102 | } | |
103 | /* Copy LEN characters at SRC into the *existing* Scheme string DST, | |
104 | starting at START. START is an index into DST; zero means the | |
105 | beginning of the string. | |
106 | ||
107 | If START + LEN is off the end of DST, signal an out-of-range | |
108 | error. */ | |
109 | void | |
110 | gh_set_substr (char *src, SCM dst, int start, int len) | |
111 | { | |
2c92112b | 112 | char *dst_ptr; |
fd88bd7c JB |
113 | unsigned long dst_len; |
114 | unsigned long effective_length; | |
ee2a8b9b | 115 | |
b24b5e13 | 116 | SCM_ASSERT (SCM_STRINGP (dst), dst, SCM_ARG3, "gh_set_substr"); |
fd88bd7c | 117 | |
86c991c2 | 118 | dst_ptr = SCM_STRING_CHARS (dst); |
9fd38a3d | 119 | dst_len = SCM_STRING_LENGTH (dst); |
fd88bd7c JB |
120 | SCM_ASSERT (len >= 0 && (unsigned) len <= dst_len, |
121 | dst, SCM_ARG4, "gh_set_substr"); | |
122 | ||
fd88bd7c JB |
123 | effective_length = ((unsigned) len < dst_len) ? len : dst_len; |
124 | memmove (dst_ptr + start, src, effective_length); | |
5d2b97cd | 125 | scm_remember_upto_here_1 (dst); |
ee2a8b9b JB |
126 | } |
127 | ||
128 | /* Return the symbol named SYMBOL_STR. */ | |
129 | SCM | |
4921140c | 130 | gh_symbol2scm (const char *symbol_str) |
ee2a8b9b | 131 | { |
38ae064c | 132 | return scm_str2symbol(symbol_str); |
ee2a8b9b JB |
133 | } |
134 | ||
b774ee1f MD |
135 | SCM |
136 | gh_ints2scm (int *d, int n) | |
137 | { | |
b774ee1f | 138 | int i; |
0acef67a JB |
139 | SCM v = scm_make_vector(SCM_MAKINUM(n), SCM_UNSPECIFIED); |
140 | SCM *velts = SCM_VELTS(v); | |
141 | ||
b774ee1f | 142 | for (i = 0; i < n; ++i) |
0acef67a JB |
143 | velts[i] = (d[i] >= SCM_MOST_NEGATIVE_FIXNUM |
144 | && d[i] <= SCM_MOST_POSITIVE_FIXNUM | |
145 | ? SCM_MAKINUM (d[i]) | |
146 | : scm_long2big (d[i])); | |
147 | return v; | |
3ffc7a36 MD |
148 | } |
149 | ||
150 | SCM | |
bcee10dd | 151 | gh_doubles2scm (const double *d, int n) |
3ffc7a36 | 152 | { |
3ffc7a36 | 153 | int i; |
0acef67a JB |
154 | SCM v = scm_make_vector(SCM_MAKINUM(n), SCM_UNSPECIFIED); |
155 | SCM *velts = SCM_VELTS(v); | |
156 | ||
157 | for(i = 0; i < n; i++) | |
f8de44c1 | 158 | velts[i] = scm_make_real (d[i]); |
0acef67a JB |
159 | return v; |
160 | } | |
161 | ||
afe5177e | 162 | #ifdef HAVE_ARRAYS |
0acef67a JB |
163 | /* Do not use this function for building normal Scheme vectors, unless |
164 | you arrange for the elements to be protected from GC while you | |
165 | initialize the vector. */ | |
166 | static SCM | |
167 | makvect (char* m, int len, int type) | |
168 | { | |
169 | SCM ans; | |
170 | SCM_NEWCELL (ans); | |
171 | SCM_DEFER_INTS; | |
6a0476fd | 172 | SCM_SET_UVECTOR_BASE (ans, m); |
93778877 | 173 | SCM_SET_UVECTOR_LENGTH (ans, len, type); |
0acef67a JB |
174 | SCM_ALLOW_INTS; |
175 | return ans; | |
b774ee1f MD |
176 | } |
177 | ||
3ffc7a36 | 178 | SCM |
bcee10dd | 179 | gh_chars2byvect (const char *d, int n) |
3ffc7a36 MD |
180 | { |
181 | char *m = scm_must_malloc (n * sizeof (char), "vector"); | |
182 | memcpy (m, d, n * sizeof (char)); | |
183 | return makvect (m, n, scm_tc7_byvect); | |
184 | } | |
185 | ||
186 | SCM | |
bcee10dd | 187 | gh_shorts2svect (const short *d, int n) |
3ffc7a36 MD |
188 | { |
189 | char *m = scm_must_malloc (n * sizeof (short), "vector"); | |
190 | memcpy (m, d, n * sizeof (short)); | |
191 | return makvect (m, n, scm_tc7_svect); | |
192 | } | |
193 | ||
b774ee1f | 194 | SCM |
bcee10dd | 195 | gh_longs2ivect (const long *d, int n) |
b774ee1f MD |
196 | { |
197 | char *m = scm_must_malloc (n * sizeof (long), "vector"); | |
198 | memcpy (m, d, n * sizeof (long)); | |
199 | return makvect (m, n, scm_tc7_ivect); | |
200 | } | |
201 | ||
202 | SCM | |
bcee10dd | 203 | gh_ulongs2uvect (const unsigned long *d, int n) |
b774ee1f MD |
204 | { |
205 | char *m = scm_must_malloc (n * sizeof (unsigned long), "vector"); | |
206 | memcpy (m, d, n * sizeof (unsigned long)); | |
207 | return makvect (m, n, scm_tc7_uvect); | |
208 | } | |
209 | ||
210 | SCM | |
bcee10dd | 211 | gh_floats2fvect (const float *d, int n) |
b774ee1f | 212 | { |
3ffc7a36 MD |
213 | char *m = scm_must_malloc (n * sizeof (float), "vector"); |
214 | memcpy (m, d, n * sizeof (float)); | |
215 | return makvect (m, n, scm_tc7_fvect); | |
b774ee1f MD |
216 | } |
217 | ||
f3a2c4cf | 218 | SCM |
bcee10dd | 219 | gh_doubles2dvect (const double *d, int n) |
f3a2c4cf | 220 | { |
f3a2c4cf MD |
221 | char *m = scm_must_malloc (n * sizeof (double), "vector"); |
222 | memcpy (m, d, n * sizeof (double)); | |
b774ee1f | 223 | return makvect (m, n, scm_tc7_dvect); |
f3a2c4cf MD |
224 | } |
225 | #endif | |
ee2a8b9b JB |
226 | |
227 | /* data conversion scheme->C */ | |
228 | int | |
229 | gh_scm2bool (SCM obj) | |
230 | { | |
fbd485ba | 231 | return (SCM_FALSEP (obj)) ? 0 : 1; |
ee2a8b9b JB |
232 | } |
233 | unsigned long | |
234 | gh_scm2ulong (SCM obj) | |
235 | { | |
236 | return scm_num2ulong (obj, (char *) SCM_ARG1, "gh_scm2ulong"); | |
237 | } | |
238 | long | |
239 | gh_scm2long (SCM obj) | |
240 | { | |
241 | return scm_num2long (obj, (char *) SCM_ARG1, "gh_scm2long"); | |
242 | } | |
243 | int | |
244 | gh_scm2int (SCM obj) | |
245 | { | |
246 | /* NOTE: possible loss of precision here */ | |
247 | return (int) scm_num2long (obj, (char *) SCM_ARG1, "gh_scm2int"); | |
248 | } | |
249 | double | |
250 | gh_scm2double (SCM obj) | |
251 | { | |
252 | return scm_num2dbl (obj, "gh_scm2double"); | |
253 | } | |
254 | char | |
255 | gh_scm2char (SCM obj) | |
0e1d5b0a | 256 | #define FUNC_NAME "gh_scm2char" |
ee2a8b9b | 257 | { |
0e1d5b0a | 258 | SCM_VALIDATE_CHAR (SCM_ARG1, obj); |
7866a09b | 259 | return SCM_CHAR (obj); |
ee2a8b9b | 260 | } |
fd336365 | 261 | #undef FUNC_NAME |
ee2a8b9b | 262 | |
3ffc7a36 MD |
263 | /* Convert a vector, weak vector, string, substring or uniform vector |
264 | into an array of chars. If result array in arg 2 is NULL, malloc a | |
265 | new one. */ | |
266 | char * | |
267 | gh_scm2chars (SCM obj, char *m) | |
f3a2c4cf MD |
268 | { |
269 | int i, n; | |
3ffc7a36 | 270 | long v; |
f3a2c4cf | 271 | SCM val; |
1a548472 | 272 | if (SCM_IMP (obj)) |
f3a2c4cf MD |
273 | scm_wrong_type_arg (0, 0, obj); |
274 | switch (SCM_TYP7 (obj)) | |
275 | { | |
276 | case scm_tc7_vector: | |
277 | case scm_tc7_wvect: | |
9fd38a3d | 278 | n = SCM_VECTOR_LENGTH (obj); |
f3a2c4cf MD |
279 | for (i = 0; i < n; ++i) |
280 | { | |
281 | val = SCM_VELTS (obj)[i]; | |
282 | if (SCM_INUMP (val)) | |
3ffc7a36 MD |
283 | { |
284 | v = SCM_INUM (val); | |
285 | if (v < -128 || v > 255) | |
286 | scm_out_of_range (0, obj); | |
287 | } | |
f3a2c4cf | 288 | else |
3ffc7a36 MD |
289 | scm_wrong_type_arg (0, 0, obj); |
290 | } | |
291 | if (m == 0) | |
292 | m = (char *) malloc (n * sizeof (char)); | |
293 | for (i = 0; i < n; ++i) | |
294 | m[i] = SCM_INUM (SCM_VELTS (obj)[i]); | |
295 | break; | |
afe5177e | 296 | #ifdef HAVE_ARRAYS |
3ffc7a36 | 297 | case scm_tc7_byvect: |
b5c2579a DH |
298 | n = SCM_UVECTOR_LENGTH (obj); |
299 | if (m == 0) | |
300 | m = (char *) malloc (n * sizeof (char)); | |
301 | memcpy (m, SCM_VELTS (obj), n * sizeof (char)); | |
302 | break; | |
afe5177e | 303 | #endif |
3ffc7a36 MD |
304 | case scm_tc7_string: |
305 | case scm_tc7_substring: | |
b5c2579a | 306 | n = SCM_STRING_LENGTH (obj); |
3ffc7a36 MD |
307 | if (m == 0) |
308 | m = (char *) malloc (n * sizeof (char)); | |
309 | memcpy (m, SCM_VELTS (obj), n * sizeof (char)); | |
310 | break; | |
311 | default: | |
312 | scm_wrong_type_arg (0, 0, obj); | |
313 | } | |
314 | return m; | |
315 | } | |
316 | ||
317 | /* Convert a vector, weak vector or uniform vector into an array of | |
318 | shorts. If result array in arg 2 is NULL, malloc a new one. */ | |
319 | short * | |
320 | gh_scm2shorts (SCM obj, short *m) | |
321 | { | |
322 | int i, n; | |
323 | long v; | |
324 | SCM val; | |
1a548472 | 325 | if (SCM_IMP (obj)) |
3ffc7a36 MD |
326 | scm_wrong_type_arg (0, 0, obj); |
327 | switch (SCM_TYP7 (obj)) | |
328 | { | |
329 | case scm_tc7_vector: | |
330 | case scm_tc7_wvect: | |
9fd38a3d | 331 | n = SCM_VECTOR_LENGTH (obj); |
3ffc7a36 MD |
332 | for (i = 0; i < n; ++i) |
333 | { | |
334 | val = SCM_VELTS (obj)[i]; | |
335 | if (SCM_INUMP (val)) | |
f3a2c4cf | 336 | { |
3ffc7a36 MD |
337 | v = SCM_INUM (val); |
338 | if (v < -32768 || v > 65535) | |
339 | scm_out_of_range (0, obj); | |
f3a2c4cf | 340 | } |
3ffc7a36 MD |
341 | else |
342 | scm_wrong_type_arg (0, 0, obj); | |
343 | } | |
344 | if (m == 0) | |
345 | m = (short *) malloc (n * sizeof (short)); | |
346 | for (i = 0; i < n; ++i) | |
347 | m[i] = SCM_INUM (SCM_VELTS (obj)[i]); | |
348 | break; | |
afe5177e | 349 | #ifdef HAVE_ARRAYS |
3ffc7a36 | 350 | case scm_tc7_svect: |
9fd38a3d | 351 | n = SCM_UVECTOR_LENGTH (obj); |
3ffc7a36 MD |
352 | if (m == 0) |
353 | m = (short *) malloc (n * sizeof (short)); | |
354 | memcpy (m, SCM_VELTS (obj), n * sizeof (short)); | |
355 | break; | |
afe5177e | 356 | #endif |
3ffc7a36 MD |
357 | default: |
358 | scm_wrong_type_arg (0, 0, obj); | |
359 | } | |
360 | return m; | |
361 | } | |
362 | ||
363 | /* Convert a vector, weak vector or uniform vector into an array of | |
364 | longs. If result array in arg 2 is NULL, malloc a new one. */ | |
365 | long * | |
366 | gh_scm2longs (SCM obj, long *m) | |
367 | { | |
368 | int i, n; | |
369 | SCM val; | |
1a548472 | 370 | if (SCM_IMP (obj)) |
3ffc7a36 MD |
371 | scm_wrong_type_arg (0, 0, obj); |
372 | switch (SCM_TYP7 (obj)) | |
373 | { | |
374 | case scm_tc7_vector: | |
375 | case scm_tc7_wvect: | |
9fd38a3d | 376 | n = SCM_VECTOR_LENGTH (obj); |
3ffc7a36 MD |
377 | for (i = 0; i < n; ++i) |
378 | { | |
379 | val = SCM_VELTS (obj)[i]; | |
0c95b57d | 380 | if (!SCM_INUMP (val) && !SCM_BIGP (val)) |
3ffc7a36 MD |
381 | scm_wrong_type_arg (0, 0, obj); |
382 | } | |
383 | if (m == 0) | |
384 | m = (long *) malloc (n * sizeof (long)); | |
385 | for (i = 0; i < n; ++i) | |
386 | { | |
387 | val = SCM_VELTS (obj)[i]; | |
388 | m[i] = SCM_INUMP (val) ? SCM_INUM (val) : scm_num2long (val, 0, 0); | |
389 | } | |
390 | break; | |
afe5177e | 391 | #ifdef HAVE_ARRAYS |
3ffc7a36 MD |
392 | case scm_tc7_ivect: |
393 | case scm_tc7_uvect: | |
9fd38a3d | 394 | n = SCM_UVECTOR_LENGTH (obj); |
3ffc7a36 MD |
395 | if (m == 0) |
396 | m = (long *) malloc (n * sizeof (long)); | |
397 | memcpy (m, SCM_VELTS (obj), n * sizeof (long)); | |
398 | break; | |
afe5177e | 399 | #endif |
3ffc7a36 MD |
400 | default: |
401 | scm_wrong_type_arg (0, 0, obj); | |
402 | } | |
403 | return m; | |
404 | } | |
405 | ||
406 | /* Convert a vector, weak vector or uniform vector into an array of | |
407 | floats. If result array in arg 2 is NULL, malloc a new one. */ | |
408 | float * | |
409 | gh_scm2floats (SCM obj, float *m) | |
410 | { | |
411 | int i, n; | |
412 | SCM val; | |
1a548472 | 413 | if (SCM_IMP (obj)) |
3ffc7a36 MD |
414 | scm_wrong_type_arg (0, 0, obj); |
415 | switch (SCM_TYP7 (obj)) | |
416 | { | |
417 | case scm_tc7_vector: | |
418 | case scm_tc7_wvect: | |
9fd38a3d | 419 | n = SCM_VECTOR_LENGTH (obj); |
3ffc7a36 MD |
420 | for (i = 0; i < n; ++i) |
421 | { | |
422 | val = SCM_VELTS (obj)[i]; | |
423 | if (!SCM_INUMP (val) | |
0c95b57d | 424 | && !(SCM_BIGP (val) || SCM_REALP (val))) |
3ffc7a36 MD |
425 | scm_wrong_type_arg (0, 0, val); |
426 | } | |
427 | if (m == 0) | |
428 | m = (float *) malloc (n * sizeof (float)); | |
429 | for (i = 0; i < n; ++i) | |
430 | { | |
431 | val = SCM_VELTS (obj)[i]; | |
432 | if (SCM_INUMP (val)) | |
433 | m[i] = SCM_INUM (val); | |
434 | else if (SCM_BIGP (val)) | |
435 | m[i] = scm_num2long (val, 0, 0); | |
436 | else | |
eb42e2f0 | 437 | m[i] = SCM_REAL_VALUE (val); |
f3a2c4cf MD |
438 | } |
439 | break; | |
afe5177e | 440 | #ifdef HAVE_ARRAYS |
f3a2c4cf | 441 | case scm_tc7_fvect: |
9fd38a3d | 442 | n = SCM_UVECTOR_LENGTH (obj); |
3ffc7a36 MD |
443 | if (m == 0) |
444 | m = (float *) malloc (n * sizeof (float)); | |
445 | memcpy (m, (float *) SCM_VELTS (obj), n * sizeof (float)); | |
f3a2c4cf | 446 | break; |
16d35552 | 447 | |
f3a2c4cf | 448 | case scm_tc7_dvect: |
9fd38a3d | 449 | n = SCM_UVECTOR_LENGTH (obj); |
3ffc7a36 MD |
450 | if (m == 0) |
451 | m = (float*) malloc (n * sizeof (float)); | |
f3a2c4cf | 452 | for (i = 0; i < n; ++i) |
3ffc7a36 MD |
453 | m[i] = ((double *) SCM_VELTS (obj))[i]; |
454 | break; | |
455 | #endif | |
456 | default: | |
457 | scm_wrong_type_arg (0, 0, obj); | |
458 | } | |
459 | return m; | |
460 | } | |
461 | ||
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. */ | |
464 | double * | |
465 | gh_scm2doubles (SCM obj, double *m) | |
466 | { | |
467 | int i, n; | |
468 | SCM val; | |
1a548472 | 469 | if (SCM_IMP (obj)) |
3ffc7a36 MD |
470 | scm_wrong_type_arg (0, 0, obj); |
471 | switch (SCM_TYP7 (obj)) | |
472 | { | |
473 | case scm_tc7_vector: | |
474 | case scm_tc7_wvect: | |
9fd38a3d | 475 | n = SCM_VECTOR_LENGTH (obj); |
3ffc7a36 MD |
476 | for (i = 0; i < n; ++i) |
477 | { | |
478 | val = SCM_VELTS (obj)[i]; | |
479 | if (!SCM_INUMP (val) | |
0c95b57d | 480 | && !(SCM_BIGP (val) || SCM_REALP (val))) |
3ffc7a36 MD |
481 | scm_wrong_type_arg (0, 0, val); |
482 | } | |
483 | if (m == 0) | |
484 | m = (double *) malloc (n * sizeof (double)); | |
485 | for (i = 0; i < n; ++i) | |
486 | { | |
487 | val = SCM_VELTS (obj)[i]; | |
488 | if (SCM_INUMP (val)) | |
489 | m[i] = SCM_INUM (val); | |
490 | else if (SCM_BIGP (val)) | |
491 | m[i] = scm_num2long (val, 0, 0); | |
492 | else | |
eb42e2f0 | 493 | m[i] = SCM_REAL_VALUE (val); |
3ffc7a36 MD |
494 | } |
495 | break; | |
afe5177e | 496 | #ifdef HAVE_ARRAYS |
3ffc7a36 | 497 | case scm_tc7_fvect: |
9fd38a3d | 498 | n = SCM_UVECTOR_LENGTH (obj); |
3ffc7a36 MD |
499 | if (m == 0) |
500 | m = (double *) malloc (n * sizeof (double)); | |
501 | for (i = 0; i < n; ++i) | |
502 | m[i] = ((float *) SCM_VELTS (obj))[i]; | |
503 | break; | |
16d35552 | 504 | |
3ffc7a36 | 505 | case scm_tc7_dvect: |
9fd38a3d | 506 | n = SCM_UVECTOR_LENGTH (obj); |
3ffc7a36 MD |
507 | if (m == 0) |
508 | m = (double*) malloc (n * sizeof (double)); | |
509 | memcpy (m, SCM_VELTS (obj), n * sizeof (double)); | |
f3a2c4cf MD |
510 | break; |
511 | #endif | |
512 | default: | |
513 | scm_wrong_type_arg (0, 0, obj); | |
514 | } | |
515 | return m; | |
516 | } | |
517 | ||
ee2a8b9b JB |
518 | /* string conversions between C and Scheme */ |
519 | ||
520 | /* gh_scm2newstr() -- Given a Scheme string STR, return a pointer to a | |
521 | new copy of its contents, followed by a null byte. If lenp is | |
522 | non-null, set *lenp to the string's length. | |
523 | ||
524 | This function uses malloc to obtain storage for the copy; the | |
525 | caller is responsible for freeing it. | |
526 | ||
527 | Note that Scheme strings may contain arbitrary data, including null | |
528 | characters. This means that null termination is not a reliable way | |
529 | to determine the length of the returned value. However, the | |
530 | function always copies the complete contents of STR, and sets | |
531 | *LEN_P to the true length of the string (when LEN_P is non-null). */ | |
532 | char * | |
533 | gh_scm2newstr (SCM str, int *lenp) | |
534 | { | |
535 | char *ret_str; | |
536 | int len; | |
537 | ||
9fd38a3d | 538 | SCM_ASSERT (SCM_STRINGP (str), str, SCM_ARG3, "gh_scm2newstr"); |
ee2a8b9b | 539 | |
9fd38a3d | 540 | len = SCM_STRING_LENGTH (str); |
ee2a8b9b | 541 | |
9b1b00fe JB |
542 | ret_str = (char *) scm_must_malloc ((len + 1) * sizeof (char), |
543 | "gh_scm2newstr"); | |
ee2a8b9b | 544 | /* so we copy tmp_str to ret_str, which is what we will allocate */ |
34f0f2b8 | 545 | memcpy (ret_str, SCM_STRING_CHARS (str), len); |
5d2b97cd | 546 | scm_remember_upto_here_1 (str); |
ee2a8b9b JB |
547 | /* now make sure we null-terminate it */ |
548 | ret_str[len] = '\0'; | |
549 | ||
ee2a8b9b JB |
550 | |
551 | if (lenp != NULL) | |
552 | { | |
553 | *lenp = len; | |
554 | } | |
555 | ||
556 | return ret_str; | |
557 | } | |
558 | ||
559 | ||
560 | /* Copy LEN characters at START from the Scheme string SRC to memory | |
561 | at DST. START is an index into SRC; zero means the beginning of | |
562 | the string. DST has already been allocated by the caller. | |
563 | ||
564 | If START + LEN is off the end of SRC, silently truncate the source | |
565 | region to fit the string. If truncation occurs, the corresponding | |
566 | area of DST is left unchanged. */ | |
567 | void | |
568 | gh_get_substr (SCM src, char *dst, int start, int len) | |
569 | { | |
570 | int src_len, effective_length; | |
9fd38a3d | 571 | SCM_ASSERT (SCM_STRINGP (src), src, SCM_ARG3, "gh_get_substr"); |
ee2a8b9b | 572 | |
9fd38a3d | 573 | src_len = SCM_STRING_LENGTH (src); |
ee2a8b9b | 574 | effective_length = (len < src_len) ? len : src_len; |
34f0f2b8 | 575 | memcpy (dst + start, SCM_STRING_CHARS (src), effective_length * sizeof (char)); |
ee2a8b9b | 576 | /* FIXME: must signal an error if len > src_len */ |
5d2b97cd | 577 | scm_remember_upto_here_1 (src); |
ee2a8b9b JB |
578 | } |
579 | ||
580 | ||
581 | /* gh_scm2newsymbol() -- Given a Scheme symbol 'identifier, return a | |
582 | pointer to a string with the symbol characters "identifier", | |
583 | followed by a null byte. If lenp is non-null, set *lenp to the | |
584 | string's length. | |
585 | ||
586 | This function uses malloc to obtain storage for the copy; the | |
587 | caller is responsible for freeing it. */ | |
588 | char * | |
589 | gh_symbol2newstr (SCM sym, int *lenp) | |
590 | { | |
591 | char *ret_str; | |
592 | int len; | |
593 | ||
b24b5e13 | 594 | SCM_ASSERT (SCM_SYMBOLP (sym), sym, SCM_ARG3, "gh_scm2newsymbol"); |
ee2a8b9b | 595 | |
9fd38a3d | 596 | len = SCM_SYMBOL_LENGTH (sym); |
ee2a8b9b | 597 | |
9b1b00fe JB |
598 | ret_str = (char *) scm_must_malloc ((len + 1) * sizeof (char), |
599 | "gh_symbol2newstr"); | |
b24b5e13 | 600 | /* so we copy sym to ret_str, which is what we will allocate */ |
86c991c2 | 601 | memcpy (ret_str, SCM_SYMBOL_CHARS (sym), len); |
5d2b97cd | 602 | scm_remember_upto_here_1 (sym); |
ee2a8b9b JB |
603 | /* now make sure we null-terminate it */ |
604 | ret_str[len] = '\0'; | |
605 | ||
ee2a8b9b JB |
606 | if (lenp != NULL) |
607 | { | |
608 | *lenp = len; | |
609 | } | |
610 | ||
611 | return ret_str; | |
612 | } | |
613 | ||
614 | ||
615 | /* create a new vector of the given length, all initialized to the | |
616 | given value */ | |
e5eece74 MG |
617 | SCM |
618 | gh_make_vector (SCM len, SCM fill) | |
ee2a8b9b | 619 | { |
a8741caa | 620 | return scm_make_vector (len, fill); |
ee2a8b9b JB |
621 | } |
622 | ||
623 | /* set the given element of the given vector to the given value */ | |
624 | SCM | |
956328d2 | 625 | gh_vector_set_x (SCM vec, SCM pos, SCM val) |
ee2a8b9b JB |
626 | { |
627 | return scm_vector_set_x (vec, pos, val); | |
628 | } | |
629 | ||
630 | /* retrieve the given element of the given vector */ | |
631 | SCM | |
e5eece74 | 632 | gh_vector_ref (SCM vec, SCM pos) |
ee2a8b9b JB |
633 | { |
634 | return scm_vector_ref (vec, pos); | |
635 | } | |
636 | ||
637 | /* returns the length of the given vector */ | |
638 | unsigned long | |
639 | gh_vector_length (SCM v) | |
640 | { | |
641 | return gh_scm2ulong (scm_vector_length (v)); | |
642 | } | |
35379308 | 643 | |
afe5177e | 644 | #ifdef HAVE_ARRAYS |
ef5d3ae1 MG |
645 | /* uniform vector support */ |
646 | ||
647 | /* returns the length as a C unsigned long integer */ | |
648 | unsigned long | |
649 | gh_uniform_vector_length (SCM v) | |
650 | { | |
651 | return gh_scm2ulong (scm_uniform_vector_length (v)); | |
652 | } | |
653 | ||
654 | /* gets the given element from a uniform vector; ilist is a list (or | |
655 | possibly a single integer) of indices, and its length is the | |
656 | dimension of the uniform vector */ | |
657 | SCM | |
658 | gh_uniform_vector_ref (SCM v, SCM ilist) | |
659 | { | |
660 | return scm_uniform_vector_ref (v, ilist); | |
661 | } | |
662 | ||
663 | /* sets an individual element in a uniform vector */ | |
664 | /* SCM */ | |
665 | /* gh_list_to_uniform_array ( */ | |
afe5177e | 666 | #endif |
ef5d3ae1 | 667 | |
35379308 JB |
668 | /* Data lookups between C and Scheme |
669 | ||
670 | Look up a symbol with a given name, and return the object to which | |
671 | it is bound. gh_lookup examines the Guile top level, and | |
672 | gh_module_lookup checks the module namespace specified by the | |
673 | `vec' argument. | |
674 | ||
675 | The return value is the Scheme object to which SNAME is bound, or | |
676 | SCM_UNDEFINED if SNAME is not bound in the given context. [FIXME: | |
677 | should this be SCM_UNSPECIFIED? Can a symbol ever legitimately be | |
678 | bound to SCM_UNDEFINED or SCM_UNSPECIFIED? What is the difference? | |
679 | -twp] */ | |
680 | ||
681 | SCM | |
bcee10dd | 682 | gh_lookup (const char *sname) |
35379308 JB |
683 | { |
684 | return gh_module_lookup (SCM_BOOL_F, sname); | |
685 | } | |
686 | ||
687 | SCM | |
bcee10dd | 688 | gh_module_lookup (SCM vec, const char *sname) |
35379308 JB |
689 | { |
690 | SCM sym = gh_symbol2scm (sname); | |
9a09deb1 | 691 | if (SCM_EQ_P (scm_symbol_bound_p (vec, sym), SCM_BOOL_T)) |
35379308 JB |
692 | return scm_symbol_binding (vec, sym); |
693 | else | |
694 | return SCM_UNDEFINED; | |
695 | } | |
89e00824 ML |
696 | |
697 | /* | |
698 | Local Variables: | |
699 | c-file-style: "gnu" | |
700 | End: | |
701 | */ |