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