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