1 /**********************************************************************
2 Copyright (C) 2003-2006 Andreas Rottmann
3 Copyright (C) 2005 Ludovic Courtès
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1, or (at
8 your option) any later version.
9 
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 Lesser General Public License for more details.
14 
15 You should have received a copy of the GNU Lesser General Public
16 License along with this software; see the file COPYING.  If not, write
17 to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
18 USA.
19 **********************************************************************/
20 #include <string.h>
21 
22 #include "g-wrap/guile-compatibility.h"
23 
24 #ifndef SCM_VERSION_17X
25 
26 void *
scm_malloc(size_t size)27 scm_malloc(size_t size)
28 {
29   void *result = malloc (size);
30 
31   if (result)
32     return result;
33 
34   scm_memory_error("scm_malloc");
35 }
36 
37 void *
scm_realloc(void * mem,size_t size)38 scm_realloc(void *mem, size_t size)
39 {
40   mem = realloc (mem, size);
41   if (mem)
42     return mem;
43 
44   scm_memory_error("scm_realloc");
45 }
46 
47 void
scm_gc_free(void * mem,size_t size,const char * what)48 scm_gc_free(void *mem, size_t size, const char *what)
49 {
50   scm_must_free (mem);
51   scm_done_free (size);
52 }
53 
54 
55 /* Strings.  */
56 
scm_take_locale_string(char * str)57 SCM scm_take_locale_string (char *str)
58 {
59   SCM result = scm_makfrom0str (str);
60   free (str);
61   return result;
62 }
63 
64 char *
scm_to_locale_string(SCM str)65 scm_to_locale_string (SCM str)
66 {
67   char *result;
68   size_t len = SCM_STRING_LENGTH (str);
69 
70   result = scm_malloc (len + 1);
71   memcpy (result, SCM_STRING_CHARS (str), len);
72   result[len] = '\0';
73 
74   return result;
75 }
76 
77 size_t
scm_to_locale_stringbuf(SCM str,char * buf,size_t buf_len)78 scm_to_locale_stringbuf (SCM str, char *buf, size_t buf_len)
79 {
80   size_t len = SCM_STRING_LENGTH (str);
81 
82   /* Note:  No terminating `\0' will be stored.  */
83   len = (len > buf_len) ? buf_len : len;
84   memcpy (buf, SCM_STRING_CHARS (str), len);
85 
86   return len;
87 }
88 
89 void *
scm_without_guile(void * (* func)(void *),void * data)90 scm_without_guile (void*(*func)(void*), void *data)
91 {
92     return func(data);
93 }
94 
95 #endif
96 
97 #if defined (SCM_MAJOR_VERSION) && (SCM_MAJOR_VERSION < 2)
98 
99 SCM
scm_module_variable(SCM module,SCM sym)100 scm_module_variable (SCM module, SCM sym)
101 {
102   return scm_sym2var (sym, scm_module_lookup_closure (module), SCM_BOOL_F);
103 }
104 
105 #endif
106