Debian Codegraph @

src/doc/old-manual/uffi/ref_string.xml

raw symbols SCIP
1 <?xml version="1.0" encoding="utf-8"?>
2 <!DOCTYPE book [
3 <!ENTITY % eclent SYSTEM "../ecl.ent">
4 %eclent;
5 ]>
6 <book xmlns="http://docbook.org/ns/docbook" version="5.0" xml:lang="en">
7 <reference xml:id="uffi.strings">
8 <title>Strings</title>
9 <partintro>
10 <title>Overview</title>
11 <para>
12 &UFFI; has functions to two types of <varname>C</varname>-compatible
13 strings: <emphasis>cstring</emphasis> and <emphasis>foreign</emphasis>
14 strings. cstrings are used <emphasis>only</emphasis> as parameters to
15 and from functions. In some implementations a cstring is not a foreign
16 type but rather the Lisp string itself. On other platforms a cstring
17 is a newly allocated foreign vector for storing characters. The
18 following is an example of using cstrings to both send and return a
19 value.
20 </para>
21
22 <programlisting>
23 (uffi:def-function ("getenv" c-getenv)
24 ((name :cstring))
25 :returning :cstring)
26
27 (defun my-getenv (key)
28 "Returns an environment variable, or NIL if it does not exist"
29 (check-type key string)
30 (uffi:with-cstring (key-native key)
31 (uffi:convert-from-cstring (c-getenv key-native))))
32 </programlisting>
33
34 <para>In contrast, foreign strings are always a foreign vector of characters
35 which have memory allocated. Thus, if you need to allocate memory to hold
36 the return value of a string, you must use a foreign string and not a
37 cstring. The following is an example of using a foreign string for a return
38 value.</para>
39
40 <programlisting>
41 (uffi:def-function ("gethostname" c-gethostname)
42 ((name (* :unsigned-char))
43 (len :int))
44 :returning :int)
45
46 (defun gethostname ()
47 "Returns the hostname"
48 (let* ((name (uffi:allocate-foreign-string 256))
49 (result-code (c-gethostname name 256))
50 (hostname (when (zerop result-code)
51 (uffi:convert-from-foreign-string name))))
52 ;; UFFI does not yet provide a universal way to free
53 ;; memory allocated by C's malloc. At this point, a program
54 ;; needs to call C's free function to free such memory.
55 (unless (zerop result-code)
56 (error "gethostname() failed."))))</programlisting>
57
58 <para>Foreign functions that return pointers to freshly allocated strings
59 should in general not return cstrings, but foreign strings. (There is no
60 portable way to release such cstrings from Lisp.) The following is an
61 example of handling such a function.</para>
62
63 <programlisting>
64 (uffi:def-function ("readline" c-readline)
65 ((prompt :cstring))
66 :returning (* :char))
67
68 (defun readline (prompt)
69 "Reads a string from console with line-editing."
70 (with-cstring (c-prompt prompt)
71 (let* ((c-str (c-readline c-prompt))
72 (str (convert-from-foreign-string c-str)))
73 (uffi:free-foreign-object c-str)
74 str)))
75 </programlisting>
76
77 </partintro>
78
79 <refentry xml:id="uffi.convert-from-cstring">
80 <refnamediv>
81 <refname><function>convert-from-cstring</function></refname>
82 <refpurpose>Converts a cstring to a Lisp string.</refpurpose>
83 <refclass>Macro</refclass>
84 </refnamediv>
85 <refsynopsisdiv>
86 <title>Syntax</title>
87 <synopsis>
88 <function>convert-from-cstring</function>
89 <replaceable>cstring</replaceable>
90 =>
91 <returnvalue>string</returnvalue>
92 </synopsis>
93 </refsynopsisdiv>
94 <refsect1>
95 <title>Arguments and Values</title>
96 <variablelist>
97 <varlistentry>
98 <term><parameter>cstring</parameter></term>
99 <listitem>
100 <para>A cstring.
101 </para>
102 </listitem>
103 </varlistentry>
104 <varlistentry>
105 <term><returnvalue>string</returnvalue></term>
106 <listitem>
107 <para>A Lisp string.
108 </para>
109 </listitem>
110 </varlistentry>
111 </variablelist>
112 </refsect1>
113 <refsect1>
114 <title>Description</title>
115 <para>
116 Converts a Lisp string to a <constant>cstring</constant>. This is
117 most often used when processing the results of a foreign function
118 that returns a cstring.
119 </para>
120 </refsect1>
121 <refsect1>
122 <title>Side Effects</title>
123 <para>None.</para>
124 </refsect1>
125 <refsect1>
126 <title>Affected by</title>
127 <para>None.</para>
128 </refsect1>
129 <refsect1>
130 <title>Exceptional Situations</title>
131 <para>None.</para>
132 </refsect1>
133 </refentry>
134
135
136 <refentry xml:id="uffi.convert-to-cstring">
137 <refnamediv>
138 <refname><function>convert-to-cstring</function></refname>
139 <refpurpose>Converts a Lisp string to a cstring.</refpurpose>
140 <refclass>Macro</refclass>
141 </refnamediv>
142 <refsynopsisdiv>
143 <title>Syntax</title>
144 <synopsis>
145 <function>convert-to-cstring</function>
146 <replaceable>string</replaceable>
147 =>
148 <returnvalue>cstring</returnvalue>
149 </synopsis>
150 </refsynopsisdiv>
151 <refsect1>
152 <title>Arguments and Values</title>
153 <variablelist>
154 <varlistentry>
155 <term><parameter>string</parameter></term>
156 <listitem>
157 <para>A Lisp string.
158 </para>
159 </listitem>
160 </varlistentry>
161 <varlistentry>
162 <term><returnvalue>cstring</returnvalue></term>
163 <listitem>
164 <para>A cstring.
165 </para>
166 </listitem>
167 </varlistentry>
168 </variablelist>
169 </refsect1>
170 <refsect1>
171 <title>Description</title>
172 <para>
173 Converts a Lisp string to a <varname>cstring</varname>. The
174 <varname>cstring</varname> should be freed with
175 <function>free-cstring</function>.
176 </para>
177 </refsect1>
178 <refsect1>
179 <title>Side Effects</title>
180 <para>On some implementations, this function allocates memory.</para>
181 </refsect1>
182 <refsect1>
183 <title>Affected by</title>
184 <para>None.</para>
185 </refsect1>
186 <refsect1>
187 <title>Exceptional Situations</title>
188 <para>None.</para>
189 </refsect1>
190 </refentry>
191
192
193 <refentry xml:id="uffi.free-cstring">
194 <refnamediv>
195 <refname><function>free-cstring</function></refname>
196 <refpurpose>Free memory used by cstring.
197 </refpurpose>
198 <refclass>Macro</refclass>
199 </refnamediv>
200 <refsynopsisdiv>
201 <title>Syntax</title>
202 <synopsis>
203 <function>free-cstring</function> <replaceable>cstring</replaceable>
204 </synopsis>
205 </refsynopsisdiv>
206 <refsect1>
207 <title>Arguments and Values</title>
208 <variablelist>
209 <varlistentry>
210 <term><parameter>cstring</parameter></term>
211 <listitem>
212 <para>A cstring.
213 </para>
214 </listitem>
215 </varlistentry>
216 </variablelist>
217 </refsect1>
218 <refsect1>
219 <title>Description</title>
220 <para>
221 Frees any memory possibly allocated by
222 <function>convert-to-cstring</function>. On some implementions, a cstring is just the Lisp string itself.
223 </para>
224 </refsect1>
225 <refsect1>
226 <title>Side Effects</title>
227 <para>None.</para>
228 </refsect1>
229 <refsect1>
230 <title>Affected by</title>
231 <para>None.</para>
232 </refsect1>
233 <refsect1>
234 <title>Exceptional Situations</title>
235 <para>None.</para>
236 </refsect1>
237 </refentry>
238
239
240 <refentry xml:id="uffi.with-cstring">
241 <refnamediv>
242 <refname><function>with-cstring</function></refname>
243 <refpurpose>Binds a newly created cstring.</refpurpose>
244 <refclass>Macro</refclass>
245 </refnamediv>
246 <refsynopsisdiv>
247 <title>Syntax</title>
248 <synopsis>
249 <function>with-cstring</function>
250 <replaceable>(cstring string) {body}</replaceable>
251 </synopsis>
252 </refsynopsisdiv>
253 <refsect1>
254 <title>Arguments and Values</title>
255 <variablelist>
256 <varlistentry>
257 <term><parameter>cstring</parameter></term>
258 <listitem>
259 <para>A symbol naming the cstring to be created.
260 </para>
261 </listitem>
262 </varlistentry>
263 <varlistentry>
264 <term><parameter>string</parameter></term>
265 <listitem>
266 <para>A Lisp string that will be translated to a cstring.
267 </para>
268 </listitem>
269 </varlistentry>
270 <varlistentry>
271 <term><parameter>body</parameter></term>
272 <listitem>
273 <para>The body of where the cstring will be bound.
274 </para>
275 </listitem>
276 </varlistentry>
277 </variablelist>
278 </refsect1>
279 <refsect1>
280 <title>Description</title>
281 <para>
282 Binds a symbol to a cstring created from conversion of a
283 string. Automatically frees the <varname>cstring</varname>.
284 </para>
285 </refsect1>
286 <refsect1>
287 <title>Examples</title>
288 <para>
289 <programlisting>
290 (def-function ("getenv" c-getenv)
291 ((name :cstring))
292 :returning :cstring)
293
294 (defun getenv (key)
295 "Returns an environment variable, or NIL if it does not exist"
296 (check-type key string)
297 (with-cstring (key-cstring key)
298 (convert-from-cstring (c-getenv key-cstring))))</programlisting>
299 </para>
300 </refsect1>
301 <refsect1>
302 <title>Side Effects</title>
303 <para>None.</para>
304 </refsect1>
305 <refsect1>
306 <title>Affected by</title>
307 <para>None.</para>
308 </refsect1>
309 <refsect1>
310 <title>Exceptional Situations</title>
311 <para>None.</para>
312 </refsect1>
313 </refentry>
314
315
316 <refentry xml:id="uffi.convert-from-foreign-string">
317 <refnamediv>
318 <refname><function>convert-from-foreign-string</function></refname>
319 <refpurpose>Converts a foreign string into a Lisp string.</refpurpose>
320 <refclass>Macro</refclass>
321 </refnamediv>
322 <refsynopsisdiv>
323 <title>Syntax</title>
324 <synopsis>
325 <function>convert-from-foreign-string</function>
326 <replaceable>foreign-string &amp;key length null-terminated-p</replaceable>
327 =>
328 <returnvalue>string</returnvalue>
329 </synopsis>
330 </refsynopsisdiv>
331 <refsect1>
332 <title>Arguments and Values</title>
333 <variablelist>
334 <varlistentry>
335 <term><parameter>foreign-string</parameter></term>
336 <listitem>
337 <para>A foreign string.
338 </para>
339 </listitem>
340 </varlistentry>
341 <varlistentry>
342 <term><parameter>length</parameter></term>
343 <listitem>
344 <para>The length of the foreign string to convert. The
345 default is the length of the string until a &NULL;
346 character is reached.
347 </para>
348 </listitem>
349 </varlistentry>
350 <varlistentry>
351 <term><parameter>null-terminated-p</parameter></term>
352 <listitem>
353 <para>A boolean flag with a default value of &t; When true,
354 the string is converted until the first &NULL; character is reached.
355 </para>
356 </listitem>
357 </varlistentry>
358 <varlistentry>
359 <term><returnvalue>string</returnvalue></term>
360 <listitem>
361 <para>A Lisp string.
362 </para>
363 </listitem>
364 </varlistentry>
365 </variablelist>
366 </refsect1>
367 <refsect1>
368 <title>Description</title>
369 <para>
370 Returns a Lisp string from a foreign string.
371 Can translated ASCII and binary strings.
372 </para>
373 </refsect1>
374 <refsect1>
375 <title>Side Effects</title>
376 <para>None.</para>
377 </refsect1>
378 <refsect1>
379 <title>Affected by</title>
380 <para>None.</para>
381 </refsect1>
382 <refsect1>
383 <title>Exceptional Situations</title>
384 <para>None.</para>
385 </refsect1>
386 </refentry>
387
388
389 <refentry xml:id="uffi.convert-to-foreign-string">
390 <refnamediv>
391 <refname><function>convert-to-foreign-string</function></refname>
392 <refpurpose>Converts a Lisp string to a foreign string.
393 </refpurpose>
394 <refclass>Macro</refclass>
395 </refnamediv>
396 <refsynopsisdiv>
397 <title>Syntax</title>
398 <synopsis>
399 <function>convert-to-foreign-string</function>
400 <replaceable>string</replaceable> =>
401 <returnvalue>foreign-string</returnvalue>
402 </synopsis>
403 </refsynopsisdiv>
404 <refsect1>
405 <title>Arguments and Values</title>
406 <variablelist>
407 <varlistentry>
408 <term><parameter>string</parameter></term>
409 <listitem>
410 <para>A Lisp string.
411 </para>
412 </listitem>
413 </varlistentry>
414 <varlistentry>
415 <term><returnvalue>foreign-string</returnvalue></term>
416 <listitem>
417 <para>A foreign string.
418 </para>
419 </listitem>
420 </varlistentry>
421 </variablelist>
422 </refsect1>
423 <refsect1>
424 <title>Description</title>
425 <para>
426 Converts a Lisp string to a foreign string. Memory should be
427 freed with <function>free-foreign-object</function>.
428 </para>
429 </refsect1>
430 <refsect1>
431 <title>Side Effects</title>
432 <para>None.</para>
433 </refsect1>
434 <refsect1>
435 <title>Affected by</title>
436 <para>None.</para>
437 </refsect1>
438 <refsect1>
439 <title>Exceptional Situations</title>
440 <para>None.</para>
441 </refsect1>
442 </refentry>
443
444 <refentry xml:id="uffi.allocate-foreign-string">
445 <refnamediv>
446 <refname><function>allocate-foreign-string</function></refname>
447 <refpurpose>Allocates space for a foreign string.
448 </refpurpose>
449 </refnamediv>
450 <refsynopsisdiv>
451 <title>Macro</title>
452 <synopsis>
453 <function>allocate-foreign-string</function> <replaceable>size
454 &amp;key unsigned</replaceable> =>
455 <returnvalue>foreign-string</returnvalue>
456 </synopsis>
457 <variablelist>
458 <varlistentry>
459 <term><parameter>size</parameter></term>
460 <listitem>
461 <para>The size of the space to be allocated in bytes.
462 </para>
463 </listitem>
464 </varlistentry>
465 <varlistentry>
466 <term><parameter>unsigned</parameter></term>
467 <listitem>
468 <para>A boolean flag with a default value of &t;. When true,
469 marks the pointer as an <constant>:unsigned-char</constant>.
470 </para>
471 </listitem>
472 </varlistentry>
473 <varlistentry>
474 <term><returnvalue>foreign-string</returnvalue></term>
475 <listitem>
476 <para>A foreign string which has undefined contents.
477 </para>
478 </listitem>
479 </varlistentry>
480 </variablelist>
481 </refsynopsisdiv>
482
483 <refsect1>
484 <title>Description</title>
485 <para>
486 Allocates space for a foreign string. Memory should
487 be freed with <function>free-foreign-object</function>.
488 </para>
489 </refsect1>
490 <refsect1>
491 <title>Side Effects</title>
492 <para>None.</para>
493 </refsect1>
494 <refsect1>
495 <title>Affected by</title>
496 <para>None.</para>
497 </refsect1>
498 <refsect1>
499 <title>Exceptional Situations</title>
500 <para>None.</para>
501 </refsect1>
502 </refentry>
503
504 </reference>
505 </book>
506 <!-- Keep this comment at the end of the file
507 Local variables:
508 mode: nxml
509 sgml-indent-step: 1
510 nxml-child-indent: 1
511 nxml-outline-child-indent: 0
512 fill-column: 79
513 End:
514 -->