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