00003be0d0b4484dd762b1c4b4e7c0b174d1065f
[gedcom-parse.git] / doc / usage.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>Using the GEDCOM parser library</title>
2   
3                                          
4   <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"></head><body text="#000000" bgcolor="#ffffff" link="#000099" vlink="#990099" alink="#000099">
5            
6 <h1 align="center">Using the GEDCOM parser library</h1>
7       <br>
8            
9 <h2>Index</h2>
10          
11 <ul>
12        <li><a href="#anchor">Overview</a></li>
13        <li><a href="#Error_handling">Error handling</a></li>
14        <li><a href="#Data_callback_mechanism">Data callback mechanism</a></li>
15                    
16   <ul>
17          <li><a href="#Start_and_end_callbacks">Start and end callbacks</a></li>
18          <li><a href="#Default_callbacks">Default callbacks</a></li>
19                    
20   </ul>
21       <li><a href="#Other_API_functions">Other API functions</a></li>
22                
23   <ul>
24         <li><a href="#Debugging">Debugging</a></li>
25         <li><a href="#Error_treatment">Error treatment</a></li>
26         <li><a href="#Compatibility_mode">Compatibility mode</a></li>
27                
28   </ul><li><a href="#Converting_character_sets">Converting character sets</a><br>
29   </li>
30
31       <li><a href="interface.html">Interface details</a><br>
32          </li>
33          
34 </ul>
35          
36 <hr width="100%" size="2">      
37 <h2><a name="Overview"></a>Overview<br>
38       </h2>
39       The GEDCOM parser library is built as a callback-based parser (comparable
40    to the SAX interface of XML). &nbsp;It comes with:<br>
41            
42 <ul>
43         <li>a library (<code>libgedcom.so</code>), to be linked in the application
44    program</li>
45         <li>a header file (<code>gedcom.h</code>), to be used in the sources
46   of  the application program</li>
47     <li>a header file (<code>gedcom-tags.h</code>) that is also installed,
48  but that is automatically included via <code>gedcom.h</code><br>
49     </li>
50            
51 </ul>
52       Next to these, there is also a data directory in <code>$PREFIX/share/gedcom-parse</code>
53        that contains some additional stuff, but which is not immediately
54 important    at first. &nbsp;I'll leave the description of the data directory
55 for later.<br>
56       <br>
57       The very simplest call of the gedcom parser is simply the following 
58 piece   of code (include of the gedcom header is assumed, as everywhere in 
59 this manual):<br>
60            
61 <blockquote><code>int result;<br>
62       ...<br>
63       result = <b>gedcom_parse_file</b>("myfamily.ged");<br>
64         </code>   </blockquote>
65       Although this will not provide much information, one thing it does
66 is  parse  the entire file and return the result. &nbsp;The function returns 
67 0 on success  and 1 on failure. &nbsp;No other information is available using 
68  this function  only.<br>
69        <br>
70      The next sections will refine this to be able to have meaningful errors
71   and the actual data that is in the file.<br>
72                    
73   <hr width="100%" size="2">                  
74   <h2><a name="Error_handling"></a>Error handling</h2>
75      Since this is a relatively simple topic, it is discussed before the
76 actual   callback mechanism, although it also uses a callback...<br>
77        <br>
78      The library can be used in several different circumstances, both terminal-based 
79   as GUI-based. &nbsp;Therefore, it leaves the actual display of the error 
80  message up to the application. &nbsp;For this, the application needs to register
81  a callback before parsing the GEDCOM file, which will be called by the library
82   on errors, warnings and messages.<br>
83        <br>
84      A typical piece of code would be:<br>
85                    
86   <blockquote><code>void <b>my_message_handler</b> (Gedcom_msg_type type, 
87   char *msg)<br>
88      {<br>
89      &nbsp; ...<br>
90      }<br>
91      ...<br>
92          <b>gedcom_set_message_handler</b>(my_message_handler);<br>
93      ...<br>
94      result = <b>gedcom_parse_file</b>("myfamily.ged");</code><br>
95          </blockquote>
96      In the above piece of code, <code>my_message_handler</code> is the callback 
97   that will be called for errors (<code>type=ERROR</code>), warnings (<code>type=WARNING</code>) and messages (<code>type=MESSAGE</code>). &nbsp;The 
98  callback must have the signature as in the example. &nbsp;For errors, the 
99      <code> msg</code> passed to the callback will have the format:<br>
100                              
101     <blockquote><code>Error on line</code> <i>&lt;lineno&gt;</i>: <i>&lt;actual_message&gt;</i><br>
102            </blockquote>
103      Note that the entire string will be properly internationalized, and
104 encoded   in UTF-8 (<a href="encoding.html">Why UTF-8?</a>). &nbsp;Also,
105 no newline   is appended, so that the application program can use it in any
106 way it wants.   &nbsp;Warnings are similar, but use "Warning" instead of
107 "Error". &nbsp;Messages   are plain text, without any prefix.<br>
108            <br>
109      With this in place, the resulting code will already show errors and
110 warnings   produced by the parser, e.g. on the terminal if a simple <code>
111 printf</code>      is used in the message handler.<br>
112                                        
113       <hr width="100%" size="2">                                   
114       <h2><a name="Data_callback_mechanism"></a>Data callback mechanism</h2>
115      The most important use of the parser is of course to get the data out
116  of  the GEDCOM file. &nbsp;As already mentioned, the parser uses a callback 
117  mechanism  for that. &nbsp;In fact, the mechanism involves two levels.<br>
118            <br>
119      The primary level is that each of the sections in a GEDCOM file is notified 
120   to the application code via a "start element" callback and an "end element" 
121   callback (much like in a SAX interface for XML), i.e. when a line containing 
122   a certain tag is parsed, the "start element" callback is called for that 
123  tag, and when all its subordinate lines with their tags have been processed, 
124  the "end element" callback is called for the original tag. &nbsp;Since GEDCOM 
125   is hierarchical, this results in properly nested calls to appropriate "start 
126   element" and "end element" callbacks.<br>
127            <br>
128      However, it would be typical for a genealogy program to support only 
129 a  subset  of the GEDCOM standard, certainly a program that is still under 
130 development.   &nbsp;Moreover, under GEDCOM it is allowed for an application 
131 to define its  own tags, which will typically not &nbsp;be supported by another 
132 application.   &nbsp;Still, in that case, data preservation is important; 
133 it would hardly   be accepted that information that is not understood by a
134 certain program  is just removed.<br>
135            <br>
136      Therefore, the second level of callbacks involves a "default callback".
137   &nbsp;An application needs to subscribe to callbacks for tags it does support,
138   and need to provide a "default callback" which will be called for tags
139 it   doesn't support. &nbsp;The application can then choose to just store
140 the  information that comes via the default callback in plain textual format.<br>
141            <br>
142      After this introduction, let's see what the API looks like...<br>
143            <br>
144                                        
145       <h3><a name="Start_and_end_callbacks"></a>Start and end callbacks</h3>
146                                        
147       <h4><i>Callbacks for records</i> <br>
148            </h4>
149      As a simple example, we will get some information from the header of 
150 a  GEDCOM  file. &nbsp;First, have a look at the following piece of code:<br>
151                                        
152       <blockquote><code>Gedcom_ctxt <b>my_header_start_cb</b> (int level, 
153   <br>
154  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
155 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Gedcom_val xref, <br>
156  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
157 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char *tag, <br>
158  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
159 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char *raw_value,<br>
160  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
161 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int parsed_tag, <br>
162  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
163 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Gedcom_val parsed_value)<br>
164      {<br>
165      &nbsp; printf("The header starts\n");<br>
166      &nbsp; return (Gedcom_ctxt)1;<br>
167      }<br>
168              <br>
169      void <b>my_header_end_cb</b> (Gedcom_ctxt self)<br>
170      {<br>
171      &nbsp; printf("The header ends, context is %d\n", (int)self); &nbsp; /* context 
172   will print as "1" */<br>
173      }<br>
174              <br>
175      ...<br>
176              <b>gedcom_subscribe_to_record</b>(REC_HEAD, my_header_start_cb,
177   my_header_end_cb);<br>
178      ...<br>
179      result = <b>gedcom_parse_file</b>("myfamily.ged");</code><br>
180              </blockquote>
181         Using the <code>gedcom_subscribe_to_record</code> function, the application 
182   requests to use the specified callbacks as start and end callback. The end
183   callback is optional: you can pass <code>NULL</code> if you are not interested
184   in the end callback. &nbsp;The identifiers to use as first argument to
185 the   function (here <code>REC_HEAD</code>) are described in the <a href="interface.html#Record_identifiers">
186     interface details</a>.<br>
187              <br>
188      From the name of the function it becomes clear that this function is 
189 specific   to complete records. &nbsp;For the separate elements in records 
190 there is  another function, which we'll see shortly. &nbsp;Again, the callbacks 
191 need  to have the signatures as shown in the example.<br>
192              <br>
193      The <code>Gedcom_ctxt</code> type that is used as a result of the start
194   callback and as an argument to the end callback is vital for passing context
195   necessary for the application. &nbsp;This type is meant to be opaque; in
196  fact, it's a void pointer, so you can pass anything via it. &nbsp;The important
197   thing to know is that the context that the application returns in the start
198   callback will be passed in the end callback as an argument, and as we will
199   see shortly, also to all the directly subordinate elements of the record.<br>
200           <br>
201   The <code>tag</code> is the GEDCOM tag in string format, the <code>parsed_tag</code>
202    is an integer, for which symbolic values are defined as <code>TAG_HEAD,</code>
203    <code>TAG_SOUR,</code> <code>TAG_DATA,</code> ... and <code>USERTAG </code><code></code>
204   for the application-specific tags. &nbsp;These values are defined in the
205  header <code>gedcom-tags.h</code> that is installed, and included via <code>
206   gedcom.h</code> (so no need to include <code>gedcom-tags.h</code> yourself).<br>
207              <br>
208      The example passes a simple integer as context, but an application could 
209   e.g. pass a <code>struct</code> (or an object in a C++ application) that will contain the information for the 
210   header. &nbsp;In the end callback, the application could then e.g. do some 
211   finalizing operations on the <code>struct</code> to put it in its database.<br>
212              <br>
213      (Note that the <code>Gedcom_val</code> type for the <code>xref</code>
214   and <code>parsed_value</code> arguments  was not discussed, see further 
215 for this)<br>
216              <br>
217                                                  
218         <h4><i>Callbacks for elements</i></h4>
219      We will now retrieve the SOUR field (the name of the program that wrote
220   the file) from the header:<br>
221                                                  
222         <blockquote><code>Gedcom_ctxt <b>my_header_source_start_cb</b>(Gedcom_ctxt 
223   parent,<br>
224      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
225  &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int &nbsp;
226  &nbsp;  &nbsp; &nbsp; level,<br>
227      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
228  &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char* &nbsp;
229  &nbsp;  &nbsp; tag,<br>
230      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
231  &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char* &nbsp;
232  &nbsp;  &nbsp; raw_value,<br>
233   &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
234  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int &nbsp; &nbsp;
235  &nbsp; &nbsp; parsed_tag,<br>
236      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
237  &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Gedcom_val
238  &nbsp;parsed_value)<br>
239      {<br>
240      &nbsp; char *source = GEDCOM_STRING(parsed_value);<br>
241      &nbsp; printf("This file was written by %s\n", source);<br>
242      &nbsp; return parent;<br>
243      }<br>
244                <br>
245      void <b>my_header_source_end_cb</b>(Gedcom_ctxt parent,<br>
246      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
247  &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp;Gedcom_ctxt self,<br>
248      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
249  &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp;Gedcom_val &nbsp;parsed_value)<br>
250      {<br>
251      &nbsp; printf("End of the source description\n");<br>
252      }<br>
253                <br>
254      ...<br>
255                <b>gedcom_subscribe_to_element</b>(ELT_HEAD_SOUR,<br>
256      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
257  &nbsp;  &nbsp; &nbsp; &nbsp; my_header_source_start_cb,<br>
258      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
259  &nbsp;  &nbsp; &nbsp; &nbsp; my_header_source_end_cb);<br>
260      ...<br>
261      result = <b>gedcom_parse_file</b>("myfamily.ged");</code><br>
262                </blockquote>
263      The subscription mechanism for elements is similar, only the signatures
264   of the callbacks differ. &nbsp;The signature for the start callback shows
265   that the context of the parent line (here e.g. the <code>struct</code> that
266 describes   the header) is passed to this start callback. &nbsp;The callback
267 itself returns  here in this example the same context, but this can be its own context object
268 of course. &nbsp;The end callback is called with both the context of the
269 parent and the context of itself, which in this example will be the same.
270 &nbsp;Again,  the list of identifiers to use as a first argument for the
271 subscription function  are detailed in the <a href="interface.html#Element_identifiers">
272  interface  details</a> .<br>
273                <br>
274      If we look at the other arguments of the start callback, we see the
275 level   number (the initial number of the line in the GEDCOM file), the tag
276 (e.g.   "SOUR"), and then a raw value, a parsed tag and a parsed value. &nbsp;The
277  raw value is just the raw string that occurs as value on the line next to
278  the tag (in UTF-8 encoding). &nbsp;The parsed value is the meaningful value
279  that is parsed from that raw string. &nbsp;The parsed tag is described in
280  the section for record callbacks above.<br>
281                <br>
282      The <code>Gedcom_val</code> type is meant to be an opaque type. &nbsp;The 
283   only thing that needs to be known about it is that it can contain specific 
284   data types, which have to be retrieved from it using pre-defined macros. 
285  &nbsp;These data types are described in the <a href="interface.html#Gedcom_val_types">
286     interface details</a>.           <br>
287               <br>
288      Some extra notes:<br>
289                                                            
290           <ul>
291                  <li>The <code>Gedcom_val</code> argument of the end callback 
292   is currently not used. &nbsp;It is there for future enhancements.</li>
293                  <li>There are also two <code>Gedcom_val</code> arguments in
294 the   start callback for records. &nbsp;The first one (<code>xref</code>) contains the <code>xref_value</code> corresponding to the cross-reference (or <code>NULL</code> if there isn't one), the second one (<code>parsed_value</code>) contains the value that is parsed from the <code>raw_value</code>. &nbsp;See the&nbsp;<a href="interface.html#Record_identifiers">interface details</a>.</li>
295                                                            
296           </ul>
297                                                            
298           <h3><a name="Default_callbacks"></a>Default callbacks<br>
299                </h3>
300      As described above, an application doesn't always implement the entire 
301  GEDCOM spec, and application-specific tags may have been added by other applications.
302  &nbsp;To preserve this extra data anyway, a default callback can be registered
303  by the application, as in the following example:<br>
304                                                
305           <blockquote><code>void <b>my_default_cb</b> (Gedcom_ctxt parent,
306   int level, char* tag, char* raw_value, int parsed_tag)<br>
307     {<br>
308     &nbsp; ...<br>
309     }<br>
310                 <br>
311     ...<br>
312                 <b>gedcom_set_default_callback</b>(my_default_cb);<br>
313     ...<br>
314     result = <b>gedcom_parse_file</b>("myfamily.ged");</code><br>
315                 </blockquote>
316                This callback has a similar signature as the previous ones,
317  but  it doesn't contain a parsed value. &nbsp;However, it does contain the
318  parent  context, that was returned by the application for the most specific
319  containing  tag that the application supported.<br>
320                 <br>
321     Suppose e.g. that this callback is called for some tags in the header 
322 that  are specific to some other application, then our application could make
323 sure  that the parent context contains the struct or object that represents
324  the  header, and use the default callback here to add the level, tag and
325 raw_value  as plain text in a member of that struct or object, thus preserving
326 the information.  &nbsp;The application can then write this out when the
327 data is saved again  in a GEDCOM file. &nbsp;To make it more specific, consider
328  the following example:<br>
329                                                        
330             <blockquote><code>struct header {<br>
331     &nbsp; char* source;<br>
332     &nbsp; ...<br>
333     &nbsp; char* extra_text;<br>
334     };<br>
335                   <br>
336     Gedcom_ctxt my_header_start_cb(int level, Gedcom_val xref, char* tag, 
337 char *raw_value,<br>
338  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
339 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int parsed_tag, Gedcom_val parsed_value)<br>
340     {<br>
341     &nbsp; struct header head = my_make_header_struct();<br>
342     &nbsp; return (Gedcom_ctxt)head;<br>
343     }<br>
344                   <br>
345     void my_default_cb(Gedcom_ctxt parent, int level, char* tag, char* raw_value,
346  int parsed_tag)<br>
347     {<br>
348     &nbsp; struct header head = (struct header)parent;<br>
349     &nbsp; my_header_add_to_extra_text(head, level, tag, raw_value);<br>
350     }<br>
351                   <br>
352     gedcom_set_default_callback(my_default_cb);<br>
353     gedcom_subscribe_to_record(REC_HEAD, my_header_start, NULL);<br>
354     ...<br>
355     result = gedcom_parse_file(filename);</code><br>
356                   </blockquote>
357     Note that the default callback will be called for any tag that isn't
358 specifically   subscribed upon by the application, and can thus be called
359 in various contexts.   &nbsp;For simplicity, the example above doesn't take
360 this into account (the                 <code>parent</code> could be of different
361 types, depending  on the context).<br>
362               <br>
363 Note also that the default callback is not called when the parent context is&nbsp;<code>NULL</code><code></code>. &nbsp;This is e.g. the case if none of the "upper" tags has been subscribed upon.<br>
364                                                                
365               <hr width="100%" size="2">                                 
366                           
367               <h2><a name="Other_API_functions"></a>Other API functions<br>
368                   </h2>
369     Although the above describes the basic interface of libgedcom, there
370 are   some other functions that allow to customize the behaviour of the library.
371   &nbsp;These will be explained in the current section.<br>
372                                                                
373               <h3><a name="Debugging"></a>Debugging</h3>
374     The library can generate various debugging output, not only from itself,
375   but also the debugging output generated by the yacc parser. &nbsp;By default,
376   no debugging output is generated, but this can be customized using the
377 following   function:<br>
378                                                                
379               <blockquote><code>void <b>gedcom_set_debug_level</b> (int level,
380   FILE* trace_output)</code><br>
381                     </blockquote>
382     The <code>level</code> can be one of the following values:<br>
383                                                                        
384                 <ul>
385                       <li>0: &nbsp;no debugging information (this is the
386 default)</li>
387                       <li>1: &nbsp;only debugging information from libgedcom
388   itself</li>
389                       <li>2: &nbsp;debugging information from libgedcom and 
390  yacc</li>
391                                                                        
392                 </ul>
393     If the <code>trace_output</code> is <code>NULL</code>, debugging information
394   will be written to <code>stderr</code>, otherwise the given file handle
395 is  used (which must be open).<br>
396                     <br>
397                                                                        
398                 <h3><a name="Error_treatment"></a>Error treatment</h3>
399     One of the previous sections already described the callback to be registered
400   to get error messages. &nbsp;The library also allows to customize what
401 happens   on an error, using the following function:<br>
402                                                                        
403                 <blockquote><code>void <b>gedcom_set_error_handling</b> (Gedcom_err_mech
404   mechanism)</code><br>
405                       </blockquote>
406     The <code>mechanism</code> can be one of:<br>
407                                                                         
408       
409                   <ul>
410                         <li><code>IMMED_FAIL</code>: immediately fail the 
411 parsing  on an error (this is the default)</li>
412                         <li><code>DEFER_FAIL</code>: continue parsing after 
413  an error, but return a failure code eventually</li>
414                         <li><code>IGNORE_ERRORS</code>: continue parsing
415 after   an error, return success always</li>
416                                                                         
417       
418                   </ul>
419     This doesn't influence the generation of error or warning messages, only
420   the behaviour of the parser and its return code.<br>
421                       <br>
422                                                                         
423       
424                   <h3><a name="Compatibility_mode"></a>Compatibility mode<br>
425                       </h3>
426     Applications are not necessarily true to the GEDCOM spec (or use a different
427   version than 5.5). &nbsp;The intention is that the library is resilient
428 to  this, and goes in compatibility mode for files written by specific programs
429   (detected via the HEAD.SOUR tag). &nbsp;This compatibility mode can be
430 enabled   and disabled via the following function:<br>
431                                                                         
432       
433                   <blockquote><code>void <b>gedcom_set_compat_handling</b>
434      (int enable_compat)</code><br>
435                         </blockquote>
436     The argument can be:<br>
437                                                                         
438               
439                     <ul>
440                           <li>0: disable compatibility mode</li>
441                           <li>1: allow compatibility mode (this is the default)<br>
442                           </li>
443                                                                         
444               
445                     </ul>
446     Note that, currently, no actual compatibility code is present, but this 
447  is on the to-do list.<br>
448                     <hr width="100%" size="2">
449                     <h2><a name="Converting_character_sets"></a>Converting character sets</h2>
450 All strings passed by the GEDCOM parser to the application are in UTF-8 encoding.
451 &nbsp;Typically, an application needs to convert this to something else to
452 be able to display it.<br>
453                     <br>
454 The most common case is that the output character set is controlled by the <code>locale</code> mechanism (i.e. via the <code>LANG</code>, <code>LC_ALL</code> or <code>LC_CTYPE</code> environment variables), which also controls the <code>gettext</code>
455  mechanism in the application. &nbsp;For this, the following steps need to
456 be taken by the application (more detailed info can be found in the info
457 file of the GNU libc library in the "Generic Charset Conversion" section
458 under "Character Set Handling" or online <a href="http://www.gnu.org/manual/glibc-2.2.3/html_chapter/libc_6.html#SEC99">here</a>):<br>
459                     <ul>
460                       <li>inclusion of some headers:</li>
461                     </ul>
462                     <blockquote>
463                       <blockquote>
464                         <pre><code>#include &lt;locale.h&gt;    /* for setlocale */<br>#include &lt;langinfo.h&gt;  /* for nl_langinfo */<br>#include &lt;iconv.h&gt;     /* for iconv_* functions */<br></code></pre>
465                         </blockquote>
466                         </blockquote>
467                         <ul>
468                           <li>set the program's current locale to what the user configured in the environment:</li>
469                         </ul>
470                         <blockquote>
471                           <blockquote>
472                             <pre><code>setlocale(LC_ALL, "");</code><br></pre>
473                             </blockquote>
474                             </blockquote>
475                             <ul>
476                               <li>open a conversion handle for conversion from UTF-8 to the character set of the current locale (once for the entire program):</li>
477                             </ul>
478                             <blockquote>
479                               <blockquote>
480                                 <pre><code>iconv_t iconv_handle;<br>...<br>iconv_handle = iconv_open(nl_langinfo(CODESET), "UTF-8");</code><br>if (iconv_handle == (iconv_t) -1)<br>  /* signal an error */<br></pre>
481                                 </blockquote>
482                                 </blockquote>
483                                 <ul>
484                                   <li>then, every string can be converted using the following:</li>
485                                 </ul>
486                                 <blockquote>
487                                   <blockquote>
488                                     <pre><code>/* char* in_buf is the input buffer,    size_t in_len is its length */<br>/* char* out_buf is the output buffer,  size_t out_len is its length */<br><br>size_t nconv;<br>char *in_ptr = in_buf;<br>char *out_ptr = out_buf;<br>nconv = iconv(iconv_handle, &amp;in_ptr, &amp;in_len,&nbsp;&amp;out_ptr, &amp;out_len);</code></pre>
489                                     </blockquote>
490                                     </blockquote>
491                                     <blockquote>If the output buffer is not big enough, <code>iconv</code> will return -1 and set <code>errno</code> to <code>E2BIG</code>. &nbsp;Also, the <code>in_ptr</code> and <code>out_ptr</code> will point just after the last successfully converted character in the respective buffers, and the <code>in_len</code> and <code>out_len</code> will be updated to show the remaining lengths. &nbsp;There can be two strategies here:<br>
492                                       <ul>
493                                         <li>Make sure from the beginning
494 that the output buffer is big enough. &nbsp;However, it's difficult to find
495 an absolute maximum length in advance, even given the length of the input
496 string.<br>
497                                           <br>
498                                         </li>
499                                         <li>Do the conversion in several steps, growing the output buffer each time to make more space, and calling <code>iconv</code>
500  consecutively until the conversion is complete. &nbsp;This is the preferred
501 way (a function could be written to encapsulate all this).</li>
502                                       </ul>
503 Another error case is when the conversion was unsuccessful (if one of the
504 characters can't be represented in the target character set). &nbsp;The <code>iconv</code> function will then also return -1 and set <code>errno</code> to <code>EILSEQ</code>; the <code>in_ptr</code> will point to the character that couldn't be converted. &nbsp;In that case, again two strategies are possible:<br>
505                                       <ul>
506                                         <li>Just fail the conversion, and show an error. &nbsp;This is not very user friendly, of course.<br>
507                                           <br>
508                                         </li>
509                                         <li>Skip over the character that can't be converted and append a "?" to the output buffer, then call <code>iconv</code> again. &nbsp;Skipping over a UTF-8 character is fairly simple, as follows from the <a href="http://www.cl.cam.ac.uk/%7Emgk25/unicode.html#utf-8">encoding rules</a>:</li>
510                                       </ul>
511                                       <ol>
512                                         <ol>
513                                           <li>if the first byte is in binary 0xxxxxxx, then the character is only one byte long, just skip over that byte<br>
514                                             <br>
515                                           </li>
516                                           <li>if the first byte is in binary 11xxxxxx, then skip over that byte and all bytes 10xxxxxx that follow.<br>
517                                           </li>
518                                         </ol>
519                                       </ol>
520                                       </blockquote>
521                                       <ul>
522                                         <li>eventually, the conversion handle needs to be closed (when the program exits):<br>
523                                         </li>
524                                       </ul>
525                                       <blockquote>
526                                         <blockquote>
527                                           <pre><code>iconv_close(iconv_handle);<br></code></pre>
528                                           </blockquote>
529                                           </blockquote>
530
531                                                                         
532               
533                     The source distribution of <code>gedcom-parse</code> contains an example implementation (<code>utf8-locale.c</code> and <code>utf8-locale.h</code>
534  in the top directory) that grows the output buffer dynamically and outputs
535 "?" for characters that can't be converted. &nbsp;Feel free to use it in
536 your source code (it is not part of the library, and it isn't installed anywhere,
537 so you need to take over the source and header file in your application).
538 &nbsp;<br>
539                                           <br>
540 Its interface is:<br>
541                                           <blockquote>
542                                             <pre><code>char *<b>convert_utf8_to_locale</b> (char *input);<br>char *<b>convert_locale_to_utf8</b> (char *input);<br></code></pre>
543                                             </blockquote>
544 Both functions return a pointer to a static buffer that is overwritten on
545 each call. &nbsp;To function properly, the application must first set the
546 locale using the <code>setlocale</code> function (the second step above).
547 &nbsp;All other steps, including setting up and closing down the conversion
548 handles, are transparantly handled by the two functions.<br>
549                                             <br>
550 You can change the "?" that is output for characters that can't be converted
551 to any string you want, using the following function before the conversion
552 calls:<br>
553                                             <blockquote>
554                                               <pre><code>void <b>convert_set_unknown</b> (const char *unknown);</code></pre>
555                                               </blockquote>
556                                               <hr width="100%" size="2">                           
557                                    
558                     <pre><font size="-1">$Id$<br>$Name$</font><br></pre>
559                                                                  
560                     <pre>                    </pre>
561                                                                         
562                               
563                     </body></html>