1 <?xml version="1.0" encoding="utf-8"?>
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.inc">
8 <reference id="strings">
11 <title>Overview</title>
13 &uffi; has functions to two types of <varname>C</varname>-compatible
14 strings: <emphasis>cstring</emphasis> and <emphasis>foreign</emphasis>
15 strings. cstrings are used <emphasis>only</emphasis> as parameters to
16 and from functions. In some implementations a cstring is not a foreign
17 type but rather the Lisp string itself. On other platforms a cstring
18 is a newly allocated foreign vector for storing characters. The
19 following is an example of using cstrings to both send and return a
24 (uffi:def-function ("getenv" c-getenv)
28 (defun my-getenv (key)
29 "Returns an environment variable, or NIL if it does not exist"
30 (check-type key string)
31 (uffi:with-cstring (key-native key)
32 (uffi:convert-from-cstring (c-getenv key-native))))
36 In contrast, foreign strings are always a foreign vector of
37 characters which have memory allocated. Thus, if you need to
38 allocate memory to hold the return value of a string, you must
39 use a foreign string and not a cstring. The following is an
40 example of using a foreign string for a return value.
44 (uffi:def-function ("gethostname" c-gethostname)
45 ((name (* :unsigned-char))
50 "Returns the hostname"
51 (let* ((name (uffi:allocate-foreign-string 256))
52 (result-code (c-gethostname name 256))
53 (hostname (when (zerop result-code)
54 (uffi:convert-from-foreign-string name))))
55 (uffi:free-foreign-object name)
56 (unless (zerop result-code)
57 (error "gethostname() failed."))))
61 Foreign functions that return pointers to freshly allocated
62 strings should in general not return cstrings, but foreign
63 strings. (There is no portable way to release such cstrings from
64 Lisp.) The following is an example of handling such a function.
68 (uffi:def-function ("readline" c-readline)
72 (defun readline (prompt)
73 "Reads a string from console with line-editing."
74 (with-cstring (c-prompt prompt)
75 (let* ((c-str (c-readline c-prompt))
76 (str (convert-from-foreign-string c-str)))
77 (uffi:free-foreign-object c-str)
83 <refentry id="convert-from-cstring">
85 <refname>convert-from-cstring</refname>
86 <refpurpose>Converts a cstring to a Lisp string.</refpurpose>
87 <refclass>Macro</refclass>
92 <function>convert-from-cstring</function>
93 <replaceable>cstring</replaceable>
95 <returnvalue>string</returnvalue>
99 <title>Arguments and Values</title>
102 <term><parameter>cstring</parameter></term>
109 <term><returnvalue>string</returnvalue></term>
118 <title>Description</title>
120 Converts a Lisp string to a <constant>cstring</constant>. This is
121 most often used when processing the results of a foreign function
122 that returns a cstring.
126 <title>Side Effects</title>
130 <title>Affected by</title>
134 <title>Exceptional Situations</title>
140 <refentry id="convert-to-cstring">
142 <refname>convert-to-cstring</refname>
143 <refpurpose>Converts a Lisp string to a cstring.</refpurpose>
144 <refclass>Macro</refclass>
147 <title>Syntax</title>
149 <function>convert-to-cstring</function>
150 <replaceable>string</replaceable>
152 <returnvalue>cstring</returnvalue>
156 <title>Arguments and Values</title>
159 <term><parameter>string</parameter></term>
166 <term><returnvalue>cstring</returnvalue></term>
175 <title>Description</title>
177 Converts a Lisp string to a <varname>cstring</varname>. The
178 <varname>cstring</varname> should be freed with
179 <function>free-cstring</function>.
183 <title>Side Effects</title>
184 <para>On some implementations, this function allocates memory.</para>
187 <title>Affected by</title>
191 <title>Exceptional Situations</title>
197 <refentry id="free-cstring">
199 <refname>free-cstring</refname>
200 <refpurpose>Free memory used by cstring.
202 <refclass>Macro</refclass>
205 <title>Syntax</title>
207 <function>free-cstring</function> <replaceable>cstring</replaceable>
211 <title>Arguments and Values</title>
214 <term><parameter>cstring</parameter></term>
223 <title>Description</title>
225 Frees any memory possibly allocated by
226 <function>convert-to-cstring</function>. On some implementions, a cstring is just the Lisp string itself.
230 <title>Side Effects</title>
234 <title>Affected by</title>
238 <title>Exceptional Situations</title>
244 <refentry id="with-cstring">
246 <refname>with-cstring</refname>
247 <refpurpose>Binds a newly created cstring.</refpurpose>
248 <refclass>Macro</refclass>
251 <title>Syntax</title>
253 <function>with-cstring</function>
254 <replaceable>(cstring string) {body}</replaceable>
258 <title>Arguments and Values</title>
261 <term><parameter>cstring</parameter></term>
263 <para>A symbol naming the cstring to be created.
268 <term><parameter>string</parameter></term>
270 <para>A Lisp string that will be translated to a cstring.
275 <term><parameter>body</parameter></term>
277 <para>The body of where the cstring will be bound.
284 <title>Description</title>
286 Binds a symbol to a cstring created from conversion of a
287 string. Automatically frees the <varname>cstring</varname>.
291 <title>Examples</title>
294 (def-function ("getenv" c-getenv)
299 "Returns an environment variable, or NIL if it does not exist"
300 (check-type key string)
301 (with-cstring (key-cstring key)
302 (convert-from-cstring (c-getenv key-cstring))))
307 <title>Side Effects</title>
311 <title>Affected by</title>
315 <title>Exceptional Situations</title>
321 <refentry id="convert-from-foreign-string">
323 <refname>convert-from-foreign-string</refname>
324 <refpurpose>Converts a foreign string into a Lisp string.</refpurpose>
325 <refclass>Macro</refclass>
328 <title>Syntax</title>
330 <function>convert-from-foreign-string</function>
331 <replaceable>foreign-string &key length null-terminated-p</replaceable>
333 <returnvalue>string</returnvalue>
337 <title>Arguments and Values</title>
340 <term><parameter>foreign-string</parameter></term>
342 <para>A foreign string.
347 <term><parameter>length</parameter></term>
349 <para>The length of the foreign string to convert. The
350 default is the length of the string until a &null;
351 character is reached.
356 <term><parameter>null-terminated-p</parameter></term>
358 <para>A boolean flag with a default value of &t; When true,
359 the string is converted until the first &null; character is reached.
364 <term><returnvalue>string</returnvalue></term>
373 <title>Description</title>
375 Returns a Lisp string from a foreign string.
376 Can translated ASCII and binary strings.
380 <title>Side Effects</title>
384 <title>Affected by</title>
388 <title>Exceptional Situations</title>
394 <refentry id="convert-to-foreign-string">
396 <refname>convert-to-foreign-string</refname>
397 <refpurpose>Converts a Lisp string to a foreign string.
399 <refclass>Macro</refclass>
402 <title>Syntax</title>
404 <function>convert-to-foreign-string</function>
405 <replaceable>string</replaceable> =>
406 <returnvalue>foreign-string</returnvalue>
410 <title>Arguments and Values</title>
413 <term><parameter>string</parameter></term>
420 <term><returnvalue>foreign-string</returnvalue></term>
422 <para>A foreign string.
429 <title>Description</title>
431 Converts a Lisp string to a foreign string. Memory should be
432 freed with <function>free-foreign-object</function>.
436 <title>Side Effects</title>
440 <title>Affected by</title>
444 <title>Exceptional Situations</title>
449 <refentry id="allocate-foreign-string">
451 <refname>allocate-foreign-string</refname>
452 <refpurpose>Allocates space for a foreign string.
454 <refclass>Macro</refclass>
457 <title>Syntax</title>
459 <function>allocate-foreign-string</function> <replaceable>size
460 &key unsigned</replaceable> =>
461 <returnvalue>foreign-string</returnvalue>
465 <title>Arguments and Values</title>
468 <term><parameter>size</parameter></term>
470 <para>The size of the space to be allocated in bytes.
475 <term><parameter>unsigned</parameter></term>
477 <para>A boolean flag with a default value of &t;. When true,
478 marks the pointer as an <constant>:unsigned-char</constant>.
483 <term><returnvalue>foreign-string</returnvalue></term>
485 <para>A foreign string which has undefined contents.
492 <title>Description</title>
494 Allocates space for a foreign string. Memory should
495 be freed with <function>free-foreign-object</function>.
499 <title>Side Effects</title>
503 <title>Affected by</title>
507 <title>Exceptional Situations</title>