summaryrefslogtreecommitdiff
path: root/Lib/ocaml/cstring.i
blob: f1190ad5c89bafeeb4c6ac364520072ec13d6583 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* -----------------------------------------------------------------------------
 * cstring.i
 *
 * This file provides typemaps and macros for dealing with various forms
 * of C character string handling.   The primary use of this module
 * is in returning character data that has been allocated or changed in
 * some way.
 * ----------------------------------------------------------------------------- */

/* %cstring_input_binary(TYPEMAP, SIZE)
 * 
 * Macro makes a function accept binary string data along with
 * a size.
 */

%define %cstring_input_binary(TYPEMAP, SIZE)
%apply (char *STRING, int LENGTH) { (TYPEMAP, SIZE) };
%enddef

/*
 * %cstring_bounded_output(TYPEMAP, MAX)
 *
 * This macro is used to return a NULL-terminated output string of
 * some maximum length.  For example:
 *
 *     %cstring_bounded_output(char *outx, 512);
 *     void foo(char *outx) {
 *         sprintf(outx,"blah blah\n");
 *     }
 *
 */

%define %cstring_bounded_output(TYPEMAP,MAX)
%typemap(ignore) TYPEMAP(char temp[MAX+1]) {
    $1 = ($1_ltype) temp;
}
%typemap(argout) TYPEMAP {
    $1[MAX] = 0;
    $result = caml_list_append($result,caml_val_string(str));
}
%enddef

/*
 * %cstring_chunk_output(TYPEMAP, SIZE)
 *
 * This macro is used to return a chunk of binary string data.
 * Embedded NULLs are okay.  For example:
 *
 *     %cstring_chunk_output(char *outx, 512);
 *     void foo(char *outx) {
 *         memmove(outx, somedata, 512);
 *     }
 *
 */

%define %cstring_chunk_output(TYPEMAP,SIZE)
%typemap(ignore) TYPEMAP(char temp[SIZE]) {
    $1 = ($1_ltype) temp;
}
%typemap(argout) TYPEMAP {
    $result = caml_list_append($result,caml_val_string_len($1,SIZE));
}
%enddef

/*
 * %cstring_bounded_mutable(TYPEMAP, SIZE)
 *
 * This macro is used to wrap a string that's going to mutate.
 *
 *     %cstring_bounded_mutable(char *in, 512);
 *     void foo(in *x) {
 *         while (*x) {
 *            *x = toupper(*x);
 *            x++;
 *         }
 *     }
 *
 */


%define %cstring_bounded_mutable(TYPEMAP,MAX)
%typemap(in) TYPEMAP(char temp[MAX+1]) {
    char *t = (char *)caml_ptr_val($input);
    strncpy(temp,t,MAX);
    $1 = ($1_ltype) temp;
}
%typemap(argout) TYPEMAP {
    $result = caml_list_append($result,caml_val_string_len($1,MAX));
}
%enddef

/*
 * %cstring_mutable(TYPEMAP [, expansion])
 *
 * This macro is used to wrap a string that will mutate in place.
 * It may change size up to a user-defined expansion. 
 *
 *     %cstring_mutable(char *in);
 *     void foo(in *x) {
 *         while (*x) {
 *            *x = toupper(*x);
 *            x++;
 *         }
 *     }
 *
 */

%define %cstring_mutable(TYPEMAP,...)
%typemap(in) TYPEMAP {
   char *t = String_val($input);
   int   n = caml_string_length($input);
   $1 = ($1_ltype) t;
#if #__VA_ARGS__ == ""
#ifdef __cplusplus
   $1 = ($1_ltype) new char[n+1];
#else
   $1 = ($1_ltype) malloc(n+1);
#endif
#else
#ifdef __cplusplus
   $1 = ($1_ltype) new char[n+1+__VA_ARGS__];
#else
   $1 = ($1_ltype) malloc(n+1+__VA_ARGS__);
#endif
#endif
   memmove($1,t,n);
   $1[n] = 0;
}

%typemap(argout) TYPEMAP {
    $result = caml_list_append($result,caml_val_string($1));
#ifdef __cplusplus
   delete[] $1;
#else
   free($1);
#endif
}
%enddef

/*
 * %cstring_output_maxsize(TYPEMAP, SIZE)
 *
 * This macro returns data in a string of some user-defined size.
 *
 *     %cstring_output_maxsize(char *outx, int max) {
 *     void foo(char *outx, int max) {
 *         sprintf(outx,"blah blah\n");
 *     }
 */

%define %cstring_output_maxsize(TYPEMAP, SIZE)
%typemap(in) (TYPEMAP, SIZE) {
   $2 = caml_val_long($input);
#ifdef __cplusplus
   $1 = ($1_ltype) new char[$2+1];
#else
   $1 = ($1_ltype) malloc($2+1);
#endif
}
%typemap(argout) (TYPEMAP,SIZE) {
    $result = caml_list_append($result,caml_val_string($1));
#ifdef __cplusplus
   delete [] $1;
#else
   free($1);
#endif
}
%enddef

/*
 * %cstring_output_withsize(TYPEMAP, SIZE)
 *
 * This macro is used to return character data along with a size
 * parameter.
 *
 *     %cstring_output_maxsize(char *outx, int *max) {
 *     void foo(char *outx, int *max) {
 *         sprintf(outx,"blah blah\n");
 *         *max = strlen(outx);  
 *     }
 */

%define %cstring_output_withsize(TYPEMAP, SIZE)
%typemap(in) (TYPEMAP, SIZE) {
   int n = caml_val_long($input);
#ifdef __cplusplus
   $1 = ($1_ltype) new char[n+1];
   $2 = ($2_ltype) new $*1_ltype;
#else
   $1 = ($1_ltype) malloc(n+1);
   $2 = ($2_ltype) malloc(sizeof($*1_ltype));
#endif
   *$2 = n;
}
%typemap(argout) (TYPEMAP,SIZE) {
    $result = caml_list_append($result,caml_val_string_len($1,$2));
#ifdef __cplusplus
   delete [] $1;
   delete $2;
#else
   free($1);
   free($2);
#endif
}
%enddef

/*
 * %cstring_output_allocate(TYPEMAP, RELEASE)
 *
 * This macro is used to return character data that was
 * allocated with new or malloc.
 *
 *     %cstring_output_allocated(char **outx, free($1));
 *     void foo(char **outx) {
 *         *outx = (char *) malloc(512);
 *         sprintf(outx,"blah blah\n");
 *     }
 */

%define %cstring_output_allocate(TYPEMAP, RELEASE)
%typemap(ignore) TYPEMAP($*1_ltype temp = 0) {
   $1 = &temp;
}

%typemap(argout) TYPEMAP {
    if (*$1) {
	$result = caml_list_append($result,caml_val_string($1));
	RELEASE;
    } else {
	$result = caml_list_append($result,caml_val_ptr($1));
    }
}
%enddef

/*
 * %cstring_output_allocate_size(TYPEMAP, SIZE, RELEASE)
 *
 * This macro is used to return character data that was
 * allocated with new or malloc.
 *
 *     %cstring_output_allocated(char **outx, int *sz, free($1));
 *     void foo(char **outx, int *sz) {
 *         *outx = (char *) malloc(512);
 *         sprintf(outx,"blah blah\n");
 *         *sz = strlen(outx);
 *     }
 */

%define %cstring_output_allocate_size(TYPEMAP, SIZE, RELEASE)
%typemap(ignore) (TYPEMAP, SIZE) ($*1_ltype temp = 0, $*2_ltype tempn) {
   $1 = &temp;
   $2 = &tempn;
}

%typemap(argout)(TYPEMAP,SIZE) {
    if (*$1) {
	$result = caml_list_append($result,caml_val_string_len($1,$2));
	RELEASE;
    } else 
	$result = caml_list_append($result,caml_val_ptr($1));
}
%enddef