1 <?xml version='1.0' ?> <!-- -*- DocBook -*- -->
2 <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd" [
4 <!ENTITY % myents SYSTEM "entities.xml">
8 <reference id="strings">
11 <title>Overview</title>
14 &uffi; has functions to two types of
15 <varname>C</varname>-compatible
16 strings: <emphasis>cstring</emphasis> and
17 <emphasis>foreign</emphasis> strings.
19 cstrings are used <emphasis>only</emphasis> as parameters to and from
20 functions. In some implementations a cstring is not a foreign type but
21 rather the Lisp string itself. On other platforms a cstring is a newly
22 allocated foreign vector for storing characters. The following is an
23 example of using cstrings to both send and return a value.
27 (uffi:def-function ("getenv" c-getenv)
31 (defun my-getenv (key)
32 "Returns an environment variable, or NIL if it does not exist"
33 (check-type key string)
34 (uffi:with-cstring (key-native key)
35 (uffi:convert-from-cstring (c-getenv key-native))))
38 <para> In contrast, foreign strings are always a foreign vector of
39 characters which have memory allocated. Thus, if you need to allocate
40 memory to hold the return value of a string, you must use a foreign
41 string and not a cstring. The following is an example of using a foreign
42 string for a return value. </para>
45 (uffi:def-function ("gethostname" c-gethostname)
46 ((name (* :unsigned-char))
51 "Returns the hostname"
52 (let* ((name (uffi:allocate-foreign-string 256))
53 (result-code (c-gethostname name 256))
54 (hostname (when (zerop result-code)
55 (uffi:convert-from-foreign-string name))))
56 (uffi:free-foreign-object name)
57 (unless (zerop result-code)
58 (error "gethostname() failed."))))
61 <para> Foreign functions that return pointers to freshly allocated
62 strings should in general not return cstrings, but foreign strings.
63 (There is no portable way to release such cstrings from Lisp.) The
64 following is an example of handling such a function. </para>
67 (uffi:def-function ("readline" c-readline)
71 (defun readline (prompt)
72 "Reads a string from console with line-editing."
73 (with-cstring (c-prompt prompt)
74 (let* ((c-str (c-readline c-prompt))
75 (str (convert-from-foreign-string c-str)))
76 (uffi:free-foreign-object c-str)
82 <refentry id="convert-from-cstring">
84 <refname>convert-from-cstring</refname>
85 <refpurpose>Converts a cstring to a Lisp string.
87 <refclass>Macro</refclass>
92 <function>convert-from-cstring</function> <replaceable>cstring</replaceable> => <returnvalue>string</returnvalue>
96 <title>Arguments and Values</title>
99 <term><parameter>cstring</parameter></term>
106 <term><returnvalue>string</returnvalue></term>
115 <title>Description</title>
117 Converts a Lisp string to a <constant>cstring</constant>. This is
118 most often used when processing the results of a foreign function
119 that returns a cstring.
123 <title>Side Effects</title>
127 <title>Affected by</title>
131 <title>Exceptional Situations</title>
137 <refentry id="convert-to-cstring">
139 <refname>convert-to-cstring</refname>
140 <refpurpose>Converts a Lisp string to a cstring.
142 <refclass>Macro</refclass>
145 <title>Syntax</title>
147 <function>convert-to-cstring</function> <replaceable>string</replaceable> => <returnvalue>cstring</returnvalue>
151 <title>Arguments and Values</title>
154 <term><parameter>string</parameter></term>
161 <term><returnvalue>cstring</returnvalue></term>
170 <title>Description</title>
172 Converts a Lisp string to a
173 <varname>cstring</varname>. The
174 <varname>cstring</varname> should be freed with
175 <function>free-cstring</function>.
179 <title>Side Effects</title>
180 <para>On some implementations, this function allocates memory.</para>
183 <title>Affected by</title>
187 <title>Exceptional Situations</title>
193 <refentry id="free-cstring">
195 <refname>free-cstring</refname>
196 <refpurpose>Free memory used by cstring.
198 <refclass>Macro</refclass>
201 <title>Syntax</title>
203 <function>free-cstring</function> <replaceable>cstring</replaceable>
207 <title>Arguments and Values</title>
210 <term><parameter>cstring</parameter></term>
219 <title>Description</title>
221 Frees any memory possibly allocated by
222 <function>convert-to-cstring</function>. On some implementions, a cstring is just the Lisp string itself.
226 <title>Side Effects</title>
230 <title>Affected by</title>
234 <title>Exceptional Situations</title>
240 <refentry id="with-cstring">
242 <refname>with-cstring</refname>
243 <refpurpose>Binds a newly created cstring.
245 <refclass>Macro</refclass>
248 <title>Syntax</title>
250 <function>with-cstring</function> <replaceable>(cstring string) {body}</replaceable>
254 <title>Arguments and Values</title>
257 <term><parameter>cstring</parameter></term>
259 <para>A symbol naming the cstring to be created.
264 <term><parameter>string</parameter></term>
266 <para>A Lisp string that will be translated to a cstring.
271 <term><parameter>body</parameter></term>
273 <para>The body of where the cstring will be bound.
280 <title>Description</title>
282 Binds a symbol to a cstring created from conversion of a string. Automatically frees the <varname>cstring</varname>.
286 <title>Examples</title>
289 (def-function ("getenv" c-getenv)
294 "Returns an environment variable, or NIL if it does not exist"
295 (check-type key string)
296 (with-cstring (key-cstring key)
297 (convert-from-cstring (c-getenv key-cstring))))
302 <title>Side Effects</title>
306 <title>Affected by</title>
310 <title>Exceptional Situations</title>
316 <refentry id="convert-from-foreign-string">
318 <refname>convert-from-foreign-string</refname>
319 <refpurpose>Converts a foreign string into a Lisp string.
321 <refclass>Macro</refclass>
324 <title>Syntax</title>
326 <function>convert-from-foreign-string</function> <replaceable>foreign-string &key length null-terminated-p</replaceable> => <returnvalue>string</returnvalue>
330 <title>Arguments and Values</title>
333 <term><parameter>foreign-string</parameter></term>
335 <para>A foreign string.
340 <term><parameter>length</parameter></term>
342 <para>The length of the foreign string to
343 convert. The default is the length of the string until a &null;
344 character is reached.
349 <term><parameter>null-terminated-p</parameter></term>
351 <para>A boolean flag with a default value of &t; When true,
352 the string is converted until the first &null; character is reached.
357 <term><returnvalue>string</returnvalue></term>
366 <title>Description</title>
368 Returns a Lisp string from a foreign string.
369 Can translated ASCII and binary strings.
373 <title>Side Effects</title>
377 <title>Affected by</title>
381 <title>Exceptional Situations</title>
387 <refentry id="convert-to-foreign-string">
389 <refname>convert-to-foreign-string</refname>
390 <refpurpose>Converts a Lisp string to a foreign string.
392 <refclass>Macro</refclass>
395 <title>Syntax</title>
397 <function>convert-to-foreign-string</function> <replaceable>string</replaceable> => <returnvalue>foreign-string</returnvalue>
401 <title>Arguments and Values</title>
404 <term><parameter>string</parameter></term>
411 <term><returnvalue>foreign-string</returnvalue></term>
413 <para>A foreign string.
420 <title>Description</title>
422 Converts a Lisp string to a foreign string. Memory should be
423 freed with <function>free-foreign-object</function>.
427 <title>Side Effects</title>
431 <title>Affected by</title>
435 <title>Exceptional Situations</title>
442 <refentry id="allocate-foreign-string">
444 <refname>allocate-foreign-string</refname>
445 <refpurpose>Allocates space for a foreign string.
447 <refclass>Macro</refclass>
450 <title>Syntax</title>
452 <function>allocate-foreign-string</function> <replaceable>size &key unsigned</replaceable> => <returnvalue>foreign-string</returnvalue>
456 <title>Arguments and Values</title>
459 <term><parameter>size</parameter></term>
461 <para>The size of the space to be allocated in bytes.
466 <term><parameter>unsigned</parameter></term>
468 <para>A boolean flag with a default value of &t;. When true,
469 marks the pointer as an <constant>:unsigned-char</constant>.
474 <term><returnvalue>foreign-string</returnvalue></term>
476 <para>A foreign string which has undefined contents.
483 <title>Description</title>
485 Allocates space for a foreign string. Memory should
486 be freed with <function>free-foreign-object</function>.
490 <title>Side Effects</title>
494 <title>Affected by</title>
498 <title>Exceptional Situations</title>