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