1 /* cstring.c: Convert Scheme strings to C strings. The contents of strings
2 * has to be copied, because strings in Elk do not have a terminating null-
3 * byte (strings may _contain_ null-bytes).
4 *
5 * Get_String() and Get_Strsym() allocate memory in NUMSTRBUFS cyclically
6 * reused buffers in the C heap.
7 * The macros Get_String_Stack() and Get_Strsym_Stack() in include/cstring.h
8 * allocate memory on the stack. They have to be used whenever more than
9 * NUMSTRBUFS strings are active in a function at the same time.
10 *
11 * $Id$
12 *
13 * Copyright 1990, 1991, 1992, 1993, 1994, 1995, Oliver Laumann, Berlin
14 * Copyright 2002, 2003 Sam Hocevar <sam@hocevar.net>, Paris
15 *
16 * This software was derived from Elk 1.2, which was Copyright 1987, 1988,
17 * 1989, Nixdorf Computer AG and TELES GmbH, Berlin (Elk 1.2 has been written
18 * by Oliver Laumann for TELES Telematic Services, Berlin, in a joint project
19 * between TELES and Nixdorf Microprocessor Engineering, Berlin).
20 *
21 * Oliver Laumann, TELES GmbH, Nixdorf Computer AG and Sam Hocevar, as co-
22 * owners or individual owners of copyright in this software, grant to any
23 * person or company a worldwide, royalty free, license to
24 *
25 * i) copy this software,
26 * ii) prepare derivative works based on this software,
27 * iii) distribute copies of this software or derivative works,
28 * iv) perform this software, or
29 * v) display this software,
30 *
31 * provided that this notice is not removed and that neither Oliver Laumann
32 * nor Teles nor Nixdorf are deemed to have made any representations as to
33 * the suitability of this software for any purpose nor are held responsible
34 * for any defects of this software.
35 *
36 * THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
37 */
38
39 #include "config.h"
40
41 #include <string.h>
42
43 #include "kernel.h"
44
45 static char *heapstr[NUMSTRBUFS];
46 static unsigned int heaplen[NUMSTRBUFS];
47 static int nextstr;
48
Init_Cstring()49 void Init_Cstring() { /* Preallocate memory to avoid fragmentation */
50 int i;
51
52 for (i = 0; i < NUMSTRBUFS; i++)
53 heapstr[i] = Safe_Malloc (heaplen[i] = 512);
54 }
55
Get_String(Object str)56 char *Get_String (Object str) {
57 char **pp = &heapstr[nextstr];
58 unsigned int len;
59
60 Check_Type (str, T_String);
61 len = STRING(str)->size + 1;
62 if (len > heaplen[nextstr]) {
63 Disable_Interrupts;
64 *pp = Safe_Realloc (*pp, len);
65 heaplen[nextstr] = len;
66 Enable_Interrupts;
67 }
68 memcpy (*pp, STRING(str)->data, --len);
69 (*pp)[len] = '\0';
70 if (++nextstr == NUMSTRBUFS) nextstr = 0;
71 return *pp;
72 }
73
Get_Strsym(Object str)74 char *Get_Strsym (Object str) {
75 if (TYPE(str) == T_Symbol)
76 str = SYMBOL(str)->name;
77 else if (TYPE(str) != T_String)
78 Wrong_Type_Combination (str, "string or symbol");
79 return Get_String (str);
80 }
81