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