1 /* guile-gnome
2  * Copyright (C) 2003,2004,2011 Andy Wingo <wingo at pobox dot com>
3  *
4  * guile-support.c: Support routines for old Guile versions
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, contact:
18  *
19  * Free Software Foundation           Voice:  +1-617-542-5942
20  * 59 Temple Place - Suite 330        Fax:    +1-617-542-2652
21  * Boston, MA  02111-1307,  USA       gnu@gnu.org
22  */
23 
24 #include <guile-support.h>
25 #include <string.h>
26 
27 
28 #if SCM_MAJOR_VERSION < 2
29 char *
scm_to_utf8_string(SCM str)30 scm_to_utf8_string (SCM str)
31 {
32   return scm_to_utf8_stringn (str, NULL);
33 }
34 
35 /* This fallback is for Guile 1.8, where we don't know the encoding of the
36  * strings.  We just have to assume that the user is running in a UTF-8 locale,
37  * and that the string is encoded correctly.  Cross your fingers!  */
38 char *
scm_to_utf8_stringn(SCM str,size_t * lenp)39 scm_to_utf8_stringn (SCM str, size_t *lenp)
40 {
41   return scm_to_locale_stringn (str, NULL);
42 }
43 #endif
44 
45 char*
scm_to_locale_string_dynwind(SCM s)46 scm_to_locale_string_dynwind (SCM s)
47 {
48     char *ret = scm_to_locale_string (s);
49     scm_dynwind_free (ret);
50     return ret;
51 }
52 
53 char*
scm_to_utf8_stringn_dynwind(SCM s,size_t * lenp)54 scm_to_utf8_stringn_dynwind (SCM s, size_t *lenp)
55 {
56     char *ret = scm_to_utf8_stringn (s, lenp);
57     scm_dynwind_free (ret);
58     return ret;
59 }
60 
61 char*
scm_symbol_chars(SCM s)62 scm_symbol_chars (SCM s)
63 {
64     return scm_to_locale_string (scm_symbol_to_string (s));
65 }
66 
67 char*
scm_symbol_chars_dynwind(SCM s)68 scm_symbol_chars_dynwind (SCM s)
69 {
70     char *ret = scm_symbol_chars (s);
71     scm_dynwind_free (ret);
72     return ret;
73 }
74 
75 char*
scm_keyword_chars(SCM s)76 scm_keyword_chars (SCM s)
77 {
78     return scm_symbol_chars (scm_keyword_to_symbol (s));
79 }
80 
81 char*
scm_keyword_chars_dynwind(SCM s)82 scm_keyword_chars_dynwind (SCM s)
83 {
84     return scm_symbol_chars_dynwind (scm_keyword_to_symbol (s));
85 }
86 
87 typedef struct {
88     void *func;
89     void *p[5];
90     unsigned int u[3];
91     int d[3];
92     const void *c[4];
93 } arg_data;
94 
95 static void*
_invoke_v__p_p(void * p)96 _invoke_v__p_p (void *p)
97 {
98     arg_data *a = p;
99     void (*func)(void*, void*) = a->func;
100     func(a->p[0], a->p[1]);
101     return NULL;
102 }
103 
104 void
scm_dynwind_guile_v__p_p(void * (* dynwind)(void * (*)(void *),void *),void * func,void * arg1,void * arg2)105 scm_dynwind_guile_v__p_p (void* (*dynwind)(void*(*)(void*), void*),
106                           void *func, void *arg1, void *arg2)
107 {
108     arg_data args = {func, {arg1, arg2,},};
109     dynwind (_invoke_v__p_p, &args);
110 }
111 
112 static void*
_invoke_v__p_p_p_p_p(void * p)113 _invoke_v__p_p_p_p_p (void *p)
114 {
115     arg_data *a = p;
116     void (*func)(void*, void*, void*, void*, void*) = a->func;
117     func(a->p[0], a->p[1], a->p[2], a->p[3], a->p[4]);
118     return NULL;
119 }
120 
121 void
scm_dynwind_guile_v__p_p_p_p_p(void * (* dynwind)(void * (*)(void *),void *),void * func,void * arg1,void * arg2,void * arg3,void * arg4,void * arg5)122 scm_dynwind_guile_v__p_p_p_p_p (void* (*dynwind)(void*(*)(void*), void*),
123                                 void *func, void *arg1, void *arg2,
124                                 void *arg3, void *arg4, void *arg5)
125 {
126     arg_data args = {func, {arg1, arg2, arg3, arg4, arg5},};
127     dynwind (_invoke_v__p_p_p_p_p, &args);
128 }
129 
130 static void*
_invoke_v__p_u_p_p(void * p)131 _invoke_v__p_u_p_p (void *p)
132 {
133     arg_data *a = p;
134     void (*func)(void*, unsigned int, void*, void*) = a->func;
135     func(a->p[0], a->u[0], a->p[1], a->p[2]);
136     return NULL;
137 }
138 
139 void
scm_dynwind_guile_v__p_u_p_p(void * (* dynwind)(void * (*)(void *),void *),void * func,void * arg1,unsigned int arg2,void * arg3,void * arg4)140 scm_dynwind_guile_v__p_u_p_p (void* (*dynwind)(void*(*)(void*), void*),
141                               void *func, void *arg1, unsigned int arg2,
142                               void *arg3, void *arg4)
143 {
144     arg_data args = {func, {arg1, arg3, arg4,}, {arg2,},};
145     dynwind (_invoke_v__p_u_p_p, &args);
146 }
147 
148 static void*
_invoke_v__p_u_c_p(void * p)149 _invoke_v__p_u_c_p (void *p)
150 {
151     arg_data *a = p;
152     void (*func)(void*, unsigned int, const void*, void*) = a->func;
153     func(a->p[0], a->u[0], a->c[0], a->p[1]);
154     return NULL;
155 }
156 
157 void
scm_dynwind_guile_v__p_u_c_p(void * (* dynwind)(void * (*)(void *),void *),void * func,void * arg1,unsigned int arg2,const void * arg3,void * arg4)158 scm_dynwind_guile_v__p_u_c_p (void* (*dynwind)(void*(*)(void*), void*),
159                               void *func, void *arg1, unsigned int arg2,
160                               const void *arg3, void *arg4)
161 {
162     arg_data args = {func, {arg1, arg4,}, {arg2,}, {0,}, {arg3,}};
163     dynwind (_invoke_v__p_u_c_p, &args);
164 }
165