Retrieved non-empty version...
[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><li><a href="#C_object_model">C object model</a></li>
21   <ul>
22     <li><a href="#Main_functions">Main functions</a></li>
23     <li><a href="#Object_model_structure">Object model structure</a></li>
24     <li><a href="#User_data">User data</a><br>
25     </li>
26   </ul>
27
28          <li><a href="#Other_API_functions">Other API functions</a></li>
29                            
30   <ul>
31            <li><a href="#Debugging">Debugging</a></li>
32            <li><a href="#Error_treatment">Error treatment</a></li>
33            <li><a href="#Compatibility_mode">Compatibility mode</a></li>
34                            
35   </ul>
36     <li><a href="#Converting_character_sets">Converting character sets</a></li>
37     <li><a href="#Support_for_configure.in">Support for configure.in</a><br>
38 <br>
39      </li>
40            <li><a href="interface.html">Interface details of the callback parser</a></li><li><a href="gomxref.html">C object model details</a><br>
41             </li>
42
43                
44 </ul>
45                
46 <hr width="100%" size="2">         
47 <h2><a name="Overview"></a>Overview<br>
48          </h2>          The GEDCOM
49 parser library provides two interfaces. &nbsp;At the one hand, it can be
50 used as a callback-based parser (comparable      to the SAX interface of
51 XML); at the other hand, the parser can be used to convert the GEDCOM file
52 into an object model (comparable to the DOM interface of XML). &nbsp;It comes
53 with:<br>
54                  
55 <ul>
56            <li>a library (<code>libgedcom.so</code>), to be linked in the 
57 application     program, which implements the callback parser</li>
58            <li>a header file (<code>gedcom.h</code>), to be used in the sources 
59    of  the application program</li>
60        <li>a header file (<code>gedcom-tags.h</code>) that is also installed, 
61   but that is automatically included via <code>gedcom.h</code></li></ul>Additionally, if you want to use the GEDCOM C object model, the following should be used (note that <code>libgedcom.so</code> is also needed in this case, because the object model uses the callback parser internally):<br>
62 <ul>
63   <li>a library (<code>libgedcom_gom.so</code>), to be linked in the application program, which implements the C object model</li>
64   <li>a header file (<code>gom.h</code>), to be used in the sources of the application program<br>
65   </li>
66
67                  
68 </ul>
69
70          Next to these, there is also a data directory in <code>$PREFIX/share/gedcom-parse</code>
71           that contains some additional stuff, but which is not immediately 
72  important    at first. &nbsp;I'll leave the description of the data directory 
73  for later.<br>
74          <br>
75          The very simplest call of the gedcom callback parser is simply the following
76   piece   of code (include of the <code>gedcom.h</code> header is assumed, as everywhere
77 in  this manual):<br>
78                  
79 <blockquote><code>int result;<br>
80   ...<br>
81     <b>gedcom_init</b>();<br>
82          ...<br>
83          result = <b>gedcom_parse_file</b>("myfamily.ged");<br>
84            </code>   </blockquote>
85          Although this will not provide much information, one thing it does 
86  is  parse  the entire file and return the result. &nbsp;The function returns
87   0 on success  and 1 on failure. &nbsp;No other information is available
88 using   this function  only.<br>
89 <br>
90 Alternatively, programs using the C object model should use the following (in this case, the inclusion of both <code>gedcom.h</code> and <code>gom.h</code> is required):<br>
91   
92 <blockquote><code>int result;<br>
93   ...<br>
94     <b>gedcom_init</b>();<br>
95          ...<br>
96          result = <b>gom_parse_file</b>("myfamily.ged");<br>
97            </code>   </blockquote>
98 The call to <code>gom_parse_file</code> will build the C object model, which is then a complete representation of the GEDCOM file.<br>
99 <br>
100 No matter which of the interfaces you use, the call to <code>gedcom_init</code>() should be one of the first calls 
101 in your program. &nbsp;The requirement is that it should come before the first
102 call to <code>iconv_open</code> (part of the generic character set conversion
103 feature) in the program, either by your program itself, or indirectly by
104 the library calls it makes. &nbsp;Practically, it should e.g. come before
105  any calls to any GTK functions, because GTK uses <code>iconv_open</code>
106  in its initialization.<br>
107 &nbsp; <br>
108 For the same reason it is also advised to put
109 the <code>-lgedcom</code> option
110 on the linking of the program as the last option, so that its initialization
111 code is run first. &nbsp;In the case of using the C object model, the linking
112 options should be: <code>-lgedcom_gom -lgedcom</code><br>
113           <br>
114         The next sections will refine this piece of code to be able to have
115  meaningful errors   and the actual data that is in the file.<br>
116                            
117 <hr width="100%" size="2">                       
118 <h2><a name="Error_handling"></a>Error handling</h2>The library can be used in several different circumstances, both
119 terminal-based     as GUI-based. &nbsp;Therefore, it leaves the actual display
120 of the error    message up to the application. &nbsp;For this, the application
121 needs to  register  a callback before parsing the GEDCOM file, which will
122 be called  by the library   on errors, warnings and messages.<br>
123           <br>
124         A typical piece of code would be (<code>gom_parse_file</code> would be called in case the C object model is used):<br>
125                            
126 <blockquote><code>void <b>my_message_handler</b> (Gedcom_msg_type type,  
127  char *msg)<br>
128         {<br>
129         &nbsp; ...<br>
130         }<br>
131         ...<br>
132             <b>gedcom_set_message_handler</b>(my_message_handler);<br>
133         ...<br>
134         result = <b>gedcom_parse_file</b>("myfamily.ged");</code><br>
135             </blockquote>
136         In the above piece of code, <code>my_message_handler</code> is the
137  callback    that will be called for errors (<code>type=ERROR</code>), warnings
138  (<code>type=WARNING</code>) and messages (<code>type=MESSAGE</code>). &nbsp;The
139    callback must have the signature as in the example. &nbsp;For errors,
140 the        <code> msg</code> passed to the callback will have the format:<br>
141                                        
142 <blockquote><code>Error on line</code> <i>&lt;lineno&gt;</i>: <i>&lt;actual_message&gt;</i><br>
143               </blockquote>
144         Note that the entire string will be properly internationalized, and 
145  encoded   in UTF-8 (<a href="encoding.html">Why UTF-8?</a>). &nbsp;Also, 
146 no newline   is appended, so that the application program can use it in any 
147 way it wants.   &nbsp;Warnings are similar, but use "Warning" instead of "Error".
148 &nbsp;Messages   are plain text, without any prefix.<br>
149               <br>
150         With this in place, the resulting code will already show errors and 
151  warnings   produced by the parser, e.g. on the terminal if a simple <code>
152    printf</code>      is used in the message handler.<br>
153                                                    
154 <hr width="100%" size="2">                                            
155 <h2><a name="Data_callback_mechanism"></a>Data callback mechanism</h2>
156         The most important use of the parser is of course to get the data 
157 out   of  the GEDCOM file. &nbsp;This section focuses on the callback mechanism (see the <a href="#C_object_model">next section</a> for the C object model). &nbsp;In fact, the mechanism involves two levels.<br>
158               <br>
159         The primary level is that each of the sections in a GEDCOM file is
160  notified    to the application code via a "start element" callback and an
161  "end element"    callback (much like in a SAX interface for XML), i.e. when
162  a line containing    a certain tag is parsed, the "start element" callback
163  is called for that   tag, and when all its subordinate lines with their
164 tags  have been processed,   the "end element" callback is called for the
165 original  tag. &nbsp;Since GEDCOM    is hierarchical, this results in properly
166 nested  calls to appropriate "start    element" and "end element" callbacks.<br>
167               <br>
168         However, it would be typical for a genealogy program to support only
169   a  subset  of the GEDCOM standard, certainly a program that is still under
170   development.   &nbsp;Moreover, under GEDCOM it is allowed for an application
171   to define its  own tags, which will typically not &nbsp;be supported by
172 another  application.   &nbsp;Still, in that case, data preservation is important;
173   it would hardly   be accepted that information that is not understood by
174  a certain program  is just removed.<br>
175               <br>
176         Therefore, the second level of callbacks involves a "default callback". 
177    &nbsp;An application needs to subscribe to callbacks for tags it does support,
178    and need to provide a "default callback" which will be called for tags
179 it   doesn't support. &nbsp;The application can then choose to just store
180 the  information that comes via the default callback in plain textual format.<br>
181               <br>
182         After this introduction, let's see what the API looks like...<br>
183               <br>
184                                                    
185 <h3><a name="Start_and_end_callbacks"></a>Start and end callbacks</h3>
186                                                    
187 <h4><i>Callbacks for records</i> <br>
188               </h4>
189         As a simple example, we will get some information from the header 
190 of  a  GEDCOM  file. &nbsp;First, have a look at the following piece of code:<br>
191                                                    
192 <blockquote><code>Gedcom_ctxt <b>my_header_start_cb</b> (Gedcom_rec rec,<br>
193 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int level,    <br>
194     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
195 &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Gedcom_val xref, <br>
196     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
197 &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char *tag, <br>
198     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
199 &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char *raw_value,<br>
200     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
201 &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int parsed_tag, <br>
202     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
203 &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Gedcom_val parsed_value)<br>
204         {<br>
205         &nbsp; printf("The header starts\n");<br>
206         &nbsp; return (Gedcom_ctxt)1;<br>
207         }<br>
208                 <br>
209         void <b>my_header_end_cb</b> (Gedcom_rec rec, Gedcom_ctxt self)<br>
210         {<br>
211         &nbsp; printf("The header ends, context is %d\n", (int)self); &nbsp;
212  /* context    will print as "1" */<br>
213         }<br>
214                 <br>
215         ...<br>
216                 <b>gedcom_subscribe_to_record</b>(REC_HEAD, my_header_start_cb, 
217    my_header_end_cb);<br>
218         ...<br>
219         result = <b>gedcom_parse_file</b>("myfamily.ged");</code><br>
220                 </blockquote>
221            Using the <code>gedcom_subscribe_to_record</code> function, the
222  application    requests to use the specified callbacks as start and end
223 callback.  The end   callback is optional: you can pass <code>NULL</code>
224  if you are  not interested   in the end callback. &nbsp;The identifiers
225 to use as first  argument to the   function (here <code>REC_HEAD</code>)
226 are described in the <a href="interface.html#Record_identifiers">     interface
227 details</a> . &nbsp;These are also passed as first argument in the callbacks (the <code>Gedcom_rec</code> argument).<br>
228                 <br>
229         From the name of the function it becomes clear that this function 
230 is  specific   to complete records. &nbsp;For the separate elements in records
231   there is  another function, which we'll see shortly. &nbsp;Again, the callbacks
232   need  to have the signatures as shown in the example.<br>
233                 <br>
234         The <code>Gedcom_ctxt</code> type that is used as a result of the 
235 start    callback and as an argument to the end callback is vital for passing 
236 context    necessary for the application. &nbsp;This type is meant to be opaque;
237 in   fact, it's a void pointer, so you can pass anything via it. &nbsp;The
238 important    thing to know is that the context that the application returns
239 in the start    callback will be passed in the end callback as an argument,
240 and as we will    see shortly, also to all the directly subordinate elements
241 of the record.<br>
242              <br>
243      The <code>tag</code> is the GEDCOM tag in string format, the <code>parsed_tag</code>
244       is an integer, for which symbolic values are defined as <code>TAG_HEAD,</code>
245       <code>TAG_SOUR,</code> <code>TAG_DATA,</code> ... and <code>USERTAG 
246 </code><code></code>    for the application-specific tags. &nbsp;These values 
247 are defined in the   header <code>gedcom-tags.h</code> that is installed, 
248 and included via <code>    gedcom.h</code> (so no need to include <code>gedcom-tags.h</code>
249   yourself).<br>
250                 <br>
251         The example passes a simple integer as context, but an application
252  could    e.g. pass a <code>struct</code> (or an object in a C++ application)
253  that will contain the information for the    header. &nbsp;In the end callback,
254  the application could then e.g. do some    finalizing operations on the
255 <code>  struct</code> to put it in its database.<br>
256                 <br>
257         (Note that the <code>Gedcom_val</code> type for the <code>xref</code>
258      and <code>parsed_value</code> arguments  was not discussed, see further
259   for this)<br>
260                 <br>
261                                                                
262 <h4><i>Callbacks for elements</i></h4>
263         We will now retrieve the SOUR field (the name of the program that 
264 wrote    the file) from the header:<br>
265                                                                
266 <blockquote><code>Gedcom_ctxt <b>my_header_source_start_cb</b>(Gedcom_elt &nbsp;elt,<br>
267 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
268 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Gedcom_ctxt
269     parent,<br>
270         &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
271   &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int &nbsp; 
272   &nbsp;  &nbsp; &nbsp; level,<br>
273         &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
274   &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char* &nbsp; 
275   &nbsp;  &nbsp; tag,<br>
276         &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
277   &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char* &nbsp; 
278   &nbsp;  &nbsp; raw_value,<br>
279      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
280  &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int &nbsp;
281  &nbsp;  &nbsp; &nbsp; parsed_tag,<br>
282         &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
283   &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Gedcom_val 
284   &nbsp;parsed_value)<br>
285         {<br>
286         &nbsp; char *source = GEDCOM_STRING(parsed_value);<br>
287         &nbsp; printf("This file was written by %s\n", source);<br>
288         &nbsp; return parent;<br>
289         }<br>
290                   <br>
291         void <b>my_header_source_end_cb</b>(Gedcom_elt &nbsp;elt,<br>
292 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Gedcom_ctxt parent,<br>
293         &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
294   &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp;Gedcom_ctxt self,<br>
295         &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
296   &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp;Gedcom_val &nbsp;parsed_value)<br>
297         {<br>
298         &nbsp; printf("End of the source description\n");<br>
299         }<br>
300                   <br>
301         ...<br>
302                   <b>gedcom_subscribe_to_element</b>(ELT_HEAD_SOUR,<br>
303         &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
304   &nbsp;  &nbsp; &nbsp; &nbsp; my_header_source_start_cb,<br>
305         &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
306   &nbsp;  &nbsp; &nbsp; &nbsp; my_header_source_end_cb);<br>
307         ...<br>
308         result = <b>gedcom_parse_file</b>("myfamily.ged");</code><br>
309                   </blockquote>
310         The subscription mechanism for elements is similar, only the signatures 
311    of the callbacks differ. &nbsp;The signature for the start callback shows 
312    that the context of the parent line (here e.g. the <code>struct</code>
313   that describes   the header) is passed to this start callback. &nbsp;The
314  callback itself returns  here in this example the same context, but this
315 can be its own context object of course. &nbsp;The end callback is called
316 with both the context of the parent and the context of itself, which in this
317 example will be the same. &nbsp;Again,  the list of identifiers to use as
318 a first argument for the subscription function  are detailed in the <a href="interface.html#Element_identifiers">  interface  details</a> . &nbsp;Again, these are passed as first argument in the callback (the <code>Gedcom_elt</code> argument).<br>
319                   <br>
320         If we look at the other arguments of the start callback, we see the 
321  level   number (the initial number of the line in the GEDCOM file), the tag
322  (e.g.   "SOUR"), and then a raw value, a parsed tag and a parsed value. &nbsp;The
323   raw value is just the raw string that occurs as value on the line next
324 to   the tag (in UTF-8 encoding). &nbsp;The parsed value is the meaningful
325 value   that is parsed from that raw string. &nbsp;The parsed tag is described
326 in   the section for record callbacks above.<br>
327                   <br>
328         The <code>Gedcom_val</code> type is meant to be an opaque type. &nbsp;The
329     only thing that needs to be known about it is that it can contain specific
330     data types, which have to be retrieved from it using pre-defined macros.
331    &nbsp;These data types are described in the <a href="interface.html#Gedcom_val_types">     interface details</a>.     
332      <br>
333                  <br>
334         Some extra notes:<br>
335                                                                         
336   
337 <ul>
338                     <li>The <code>Gedcom_val</code> argument of the end callback
339     is currently not used. &nbsp;It is there for future enhancements.</li>
340                     <li>There are also two <code>Gedcom_val</code> arguments
341  in the   start callback for records. &nbsp;The first one (<code>xref</code>
342   ) contains the <code>xref_value</code> corresponding to the cross-reference
343  (or <code>NULL</code> if there isn't one), the second one (<code>parsed_value</code>
344   ) contains the value that is parsed from the <code>raw_value</code>. &nbsp;See
345  the&nbsp;<a href="interface.html#Record_identifiers">interface details</a>
346   .</li>
347                                                                         
348   
349 </ul>
350                                                                         
351   
352 <h3><a name="Default_callbacks"></a>Default callbacks<br>
353                   </h3>
354         As described above, an application doesn't always implement the entire
355    GEDCOM spec, and application-specific tags may have been added by other
356  applications.  &nbsp;To preserve this extra data anyway, a default callback
357  can be registered  by the application, as in the following example:<br>
358                                                                
359 <blockquote><code>void <b>my_default_cb</b> (Gedcom_elt elt, Gedcom_ctxt parent,   int level,
360  char* tag, char* raw_value, int parsed_tag)<br>
361        {<br>
362        &nbsp; ...<br>
363        }<br>
364                    <br>
365        ...<br>
366                    <b>gedcom_set_default_callback</b>(my_default_cb);<br>
367        ...<br>
368        result = <b>gedcom_parse_file</b>("myfamily.ged");</code><br>
369                    </blockquote>
370                   This callback has a similar signature as the previous ones, 
371   but  it doesn't contain a parsed value. &nbsp;However, it does contain the
372   parent  context, that was returned by the application for the most specific
373   containing  tag that the application supported.<br>
374                    <br>
375        Suppose e.g. that this callback is called for some tags in the header
376   that  are specific to some other application, then our application could
377  make sure  that the parent context contains the struct or object that represents 
378   the  header, and use the default callback here to add the level, tag and 
379  raw_value  as plain text in a member of that struct or object, thus preserving 
380  the information.  &nbsp;The application can then write this out when the 
381 data is saved again  in a GEDCOM file. &nbsp;To make it more specific, consider 
382   the following example:<br>
383                                                                          
384 <blockquote><code>struct header {<br>
385        &nbsp; char* source;<br>
386        &nbsp; ...<br>
387        &nbsp; char* extra_text;<br>
388        };<br>
389                      <br>
390        Gedcom_ctxt my_header_start_cb(Gedcom_rec rec, int level, Gedcom_val xref, char* tag,
391   char *raw_value,<br>
392     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
393 &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int parsed_tag, Gedcom_val parsed_value)<br>
394        {<br>
395        &nbsp; struct header head = my_make_header_struct();<br>
396        &nbsp; return (Gedcom_ctxt)head;<br>
397        }<br>
398                      <br>
399        void my_default_cb(Gedcom_elt elt, Gedcom_ctxt parent, int level, char* tag, char* 
400 raw_value,   int parsed_tag)<br>
401        {<br>
402        &nbsp; struct header head = (struct header)parent;<br>
403        &nbsp; my_header_add_to_extra_text(head, level, tag, raw_value);<br>
404        }<br>
405                      <br>
406        gedcom_set_default_callback(my_default_cb);<br>
407        gedcom_subscribe_to_record(REC_HEAD, my_header_start, NULL);<br>
408        ...<br>
409        result = gedcom_parse_file(filename);</code><br>
410                      </blockquote>
411        Note that the default callback will be called for any tag that isn't 
412  specifically   subscribed upon by the application, and can thus be called 
413  in various contexts.   &nbsp;For simplicity, the example above doesn't take 
414  this into account (the                 <code>parent</code> could be of different 
415  types, depending  on the context).<br>
416                  <br>
417    Note also that the default callback is not called when the parent context
418  is&nbsp;<code>NULL</code><code></code>. &nbsp;This is e.g. the case if none
419  of the "upper" tags has been subscribed upon.<br>
420                                                                         
421           
422 <hr width="100%" size="2"><br>
423 <h2><a name="C_object_model"></a>C object model</h2>
424 In the GEDCOM object model, all the data is immediately available after calling <code>gom_parse_file()</code>. &nbsp;For this, an entire model based on C structs is used. &nbsp;These structs are documented <a href="gomxref.html">here</a>,
425 and follow the GEDCOM syntax quite closely. &nbsp;Each of the records in
426 a GEDCOM file are modelled by a separate struct, and some common sub-structures
427 have their own struct definition.<br>
428 <br>
429 <h3><a name="Main_functions"></a>Main functions<br>
430 </h3>
431 The following functions are available to get at these structs:<br>
432 <ul>
433   <li>First, there are two functions to get the header record and the submission
434 record (there can be only one of them in a GEDCOM file):<br>
435     <blockquote><code>struct header* &nbsp; &nbsp; &nbsp;gom_get_header();<br>
436 struct submission* &nbsp;gom_get_submission();<br>
437       </code></blockquote>
438   </li>
439   <li>Further, for each of the other records, there are two functions, one
440 to get the first of such records, and one to get a record via its cross-reference
441 tag in the GEDCOM file:<br>
442     <blockquote><code>struct XXX* &nbsp; gom_get_first_XXX();<br>
443 struct XXX* &nbsp; gom_get_XXX_by_xref(char* xref);</code><br>
444     </blockquote>
445   </li>
446 </ul>
447 <blockquote>The XXX stands for one of the following: <code>family, </code><code>individual, multimedia, note, repository, source, submitter, user_rec</code>.<br>
448 </blockquote>
449 <h3><a name="Object_model_structure"></a>Object model structure<br>
450 </h3>
451 All records of a certain type are linked together in a linked list. &nbsp;The
452 above functions only give access to the first record of each linked list.
453 &nbsp;The others can be accessed by traversing the linked list via the <code>next</code> member of the structs. &nbsp;This means that e.g. the following piece of code will traverse the linked list of family records:<br>
454 <blockquote><code>struct family* fam;<br>
455   <br>
456 for (fam = gom_get_first_family() ; fam ; fam = fam-&gt;next) {<br>
457 &nbsp; ...<br>
458 }</code><br>
459 </blockquote>
460 The <code>next</code> member of the last element in the list is guaranteed to have the <code>NULL</code> value.<br>
461 <br>
462 Actually, the linked list is a doubly-linked list: each record also has a <code>previous</code> member. &nbsp;But for implementation reasons the behaviour of this <code>previous</code> member on the edges of the linked list will not be guaranteed, i.e. it can be circular or terminated with <code>NULL</code>, no assumptions can be made in the application code.<br>
463 <br>
464 This linked-list model applies also to all sub-structures of the main record structs, i.e. each struct that has a <code>next </code>and <code>previous</code>
465 member following the above conventions. &nbsp;This means that the following
466 piece of code traverses all children of a family (see the details of the
467 different structs <a href="gomxref.html">here</a>):<br>
468 <blockquote><code>struct family* fam = ...;<br>
469   <br>
470 struct xref_list* xrl;<br>
471 for (xrl = fam-&gt;children ; xrl ; xrl = xrl-&gt;next) {<br>
472 &nbsp; ...<br>
473 }</code> <br>
474 </blockquote>
475 Note that all character strings in the object model are encoded in UTF-8 (<a href="file:///home/verthezp/src/external/gedcom-parse/doc/encoding.html">Why UTF-8?</a>).<br>
476 <h3><a name="User_data"></a>User data</h3>
477 Each of the structs has an extra member called <code>extra</code> (of type <code>struct user_data*</code>).
478 &nbsp;This gathers all non-standard GEDCOM tags within the scope of the struct
479 in a flat linked list, no matter what the internal structure of the non-standard
480 tags is. &nbsp;Each element of the linked list has:<br>
481 <ul>
482   <li>a level: the level number in the GEDCOM file</li>
483   <li>a tag: the tag given in the GEDCOM file</li>
484   <li>a value: the value, which can be a string value or a cross-reference value (one of the two will be non-NULL)<br>
485   </li>
486 </ul>
487 This way, none of the information in the GEDCOM file is lost, even the non-standard information.<br>
488 <hr width="100%" size="2">                                              
489                               
490 <h2><a name="Other_API_functions"></a>Other API functions<br>
491                      </h2>
492
493        Although the above describes the basic interface of the gedcom parser, there 
494  are   some other functions that allow to customize the behaviour of the library.
495    &nbsp;These will be explained in the current section.<br>
496                                                                         
497           
498 <h3><a name="Debugging"></a>Debugging</h3>
499        The library can generate various debugging output, not only from itself, 
500    but also the debugging output generated by the yacc parser. &nbsp;By default, 
501    no debugging output is generated, but this can be customized using the 
502 following   function:<br>
503                                                                         
504           
505 <blockquote><code>void <b>gedcom_set_debug_level</b> (int level,   FILE*
506 trace_output)</code><br>
507                        </blockquote>
508        The <code>level</code> can be one of the following values:<br>
509                                                                         
510                     
511 <ul>
512                          <li>0: &nbsp;no debugging information (this is the 
513  default)</li>
514                          <li>1: &nbsp;only debugging information from libgedcom 
515    itself</li>
516                          <li>2: &nbsp;debugging information from libgedcom
517  and   yacc</li>
518                                                                         
519                     
520 </ul>
521        If the <code>trace_output</code> is <code>NULL</code>, debugging information 
522    will be written to <code>stderr</code>, otherwise the given file handle 
523  is  used (which must be open).<br>
524                        <br>
525                                                                         
526                     
527 <h3><a name="Error_treatment"></a>Error treatment</h3>
528        One of the previous sections already described the callback to be
529 registered    to get error messages. &nbsp;The library also allows to customize
530 what happens   on an error, using the following function:<br>
531                                                                         
532                     
533 <blockquote><code>void <b>gedcom_set_error_handling</b> (Gedcom_err_mech 
534  mechanism)</code><br>
535                          </blockquote>
536        The <code>mechanism</code> can be one of:<br>
537                                                                         
538                               
539 <ul>
540                            <li><code>IMMED_FAIL</code>: immediately fail
541 the   parsing  on an error (this is the default)</li>
542                            <li><code>DEFER_FAIL</code>: continue parsing
543 after    an error, but return a failure code eventually</li>
544                            <li><code>IGNORE_ERRORS</code>: continue parsing 
545  after   an error, return success always</li>
546                                                                         
547                               
548 </ul>
549        This doesn't influence the generation of error or warning messages,
550  only   the behaviour of the parser and its return code.<br>
551                          <br>
552                                                                         
553                               
554 <h3><a name="Compatibility_mode"></a>Compatibility mode<br>
555                          </h3>
556        Applications are not necessarily true to the GEDCOM spec (or use a 
557 different    version than 5.5). &nbsp;The intention is that the library is 
558 resilient  to  this, and goes in compatibility mode for files written by specific
559 programs    (detected via the HEAD.SOUR tag). &nbsp;This compatibility mode
560 can be enabled   and disabled via the following function:<br>
561                                                                         
562                               
563 <blockquote><code>void <b>gedcom_set_compat_handling</b>      (int enable_compat)</code><br>
564                            </blockquote>
565        The argument can be:<br>
566                                                                         
567                                         
568 <ul>
569                              <li>0: disable compatibility mode</li>
570                              <li>1: allow compatibility mode (this is the 
571 default)<br>
572                              </li>
573                                                                         
574                                         
575 </ul>
576        Currently, there is a beginning for compatibility for ftree and Lifelines (3.0.2).<br>
577                          
578 <hr width="100%" size="2">                       
579 <h2><a name="Converting_character_sets"></a>Converting character sets</h2>
580    All strings passed by the GEDCOM parser to the application are in UTF-8
581  encoding. &nbsp;Typically, an application needs to convert this to something
582  else to be able to display it.<br>
583                        <br>
584    The most common case is that the output character set is controlled by 
585 the <code>locale</code> mechanism (i.e. via the <code>LANG</code>, <code>
586  LC_ALL</code>  or <code>LC_CTYPE</code> environment variables), which also 
587 controls the <code>gettext</code>  mechanism in the application. &nbsp;<br>
588                        <br>
589                        <br>
590                                                                         
591                                         The source distribution of <code>
592 gedcom-parse</code>   contains an example implementation (<code>utf8-locale.c</code>
593  and <code>  utf8-locale.h</code>  in the "t" subdirectory of the top directory).&nbsp;
594 &nbsp;Feel free to use  it in your source code (it is not part of the library,
595 and it isn't installed  anywhere, so you need to take over the source and
596 header file in your application).  &nbsp;<br>
597                        <br>
598     Its interface is:<br>
599                          
600 <blockquote>      
601   <pre><code>char *<b>convert_utf8_to_locale</b> (char *input, int *conv_failures);<br>char *<b>convert_locale_to_utf8</b> (char *input);<br></code></pre>
602   </blockquote>
603     Both functions return a pointer to a static buffer that is overwritten
604  on each call. &nbsp;To function properly, the application must first set
605 the locale using the <code>setlocale</code> function (the second step detailed
606  below). &nbsp;All other steps given below, including setting up and closing
607  down the conversion handles, are transparantly handled by the two functions.
608  &nbsp;<br>
609                          <br>
610    If you pass a pointer to an integer to the first function, it will be
611 set  to the number of conversion failures, i.e. characters that couldn't
612 be converted;  you can also just pass <code>NULL</code> if you are not interested
613 (note  that usually, the interesting information is just whether there <i>
614 were</i>    conversion failures or not, which is then given by the integer
615 being bigger  than zero or not). &nbsp;The second function doesn't need this,
616 because any  locale can be converted to UTF-8.<br>
617                          <br>
618     You can change the "?" that is output for characters that can't be converted 
619  to any string you want, using the following function before the conversion 
620  calls:<br>
621                            
622 <blockquote>      
623   <pre><code>void <b>convert_set_unknown</b> (const char *unknown);</code></pre>
624   </blockquote>
625                            <br>
626    If you want to have your own functions for it instead of this example
627 implementation,  the following steps need to be taken by the application
628 (more detailed info  can be found in the info file of the GNU libc library
629 in the "Generic Charset  Conversion" section under "Character Set Handling"
630 or online <a href="http://www.gnu.org/manual/glibc-2.2.3/html_chapter/libc_6.html#SEC99">
631   here</a>):<br>
632                          
633 <ul>
634                          <li>inclusion of some headers:</li>
635                          
636 </ul>
637                          
638 <blockquote>                             
639   <blockquote>                                   
640     <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>
641                            </blockquote>
642                            </blockquote>
643                              
644 <ul>
645                              <li>set the program's current locale to what 
646 the user configured in the environment:</li>
647                              
648 </ul>
649                              
650 <blockquote>                                 
651   <blockquote>                                       
652     <pre><code>setlocale(LC_ALL, "");</code><br></pre>
653                                </blockquote>
654                                </blockquote>
655                                  
656 <ul>
657                                  <li>open a conversion handle for conversion
658  from UTF-8 to the character set of the current locale (once for the entire
659  program):</li>
660                                  
661 </ul>
662                                  
663 <blockquote>                                     
664   <blockquote>                                           
665     <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>
666                                    </blockquote>
667                                    </blockquote>
668                                      
669 <ul>
670                                      <li>then, every string can be converted
671  using the following:</li>
672                                      
673 </ul>
674                                      
675 <blockquote>                                         
676   <blockquote>                                               
677     <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>
678                                        </blockquote>
679                                        </blockquote>
680                                          
681 <blockquote>If the output buffer is not big enough, <code>iconv</code> will
682  return -1 and set <code>errno</code> to <code>E2BIG</code>. &nbsp;Also,
683 the    <code>in_ptr</code> and <code>out_ptr</code> will point just after
684 the last successfully converted character in the respective buffers, and
685 the   <code> in_len</code> and <code>out_len</code> will be updated to show
686 the remaining lengths. &nbsp;There can be two strategies here:<br>
687                                                
688   <ul>
689                                            <li>Make sure from the beginning 
690  that the output buffer is big enough. &nbsp;However, it's difficult to find 
691  an absolute maximum length in advance, even given the length of the input 
692  string.<br>
693                                              <br>
694                                            </li>
695                                            <li>Do the conversion in several
696  steps, growing the output buffer each time to make more space, and calling
697        <code>iconv</code>  consecutively until the conversion is complete.
698  &nbsp;This is the preferred way (a function could be written to encapsulate
699  all this).</li>
700                                                
701   </ul>
702    Another error case is when the conversion was unsuccessful (if one of
703 the  characters can't be represented in the target character set). &nbsp;The 
704   <code> iconv</code> function will then also return -1 and set <code>errno</code>
705    to <code>EILSEQ</code>; the <code>in_ptr</code> will point to the character
706  that couldn't be converted. &nbsp;In that case, again two strategies are
707 possible:<br>
708                                                
709   <ul>
710                                            <li>Just fail the conversion,
711 and  show an error. &nbsp;This is not very user friendly, of course.<br>
712                                              <br>
713                                            </li>
714                                            <li>Skip over the character that
715  can't be converted and append a "?" to the output buffer, then call <code>
716   iconv</code> again. &nbsp;Skipping over a UTF-8 character is fairly simple,
717  as follows from the <a href="http://www.cl.cam.ac.uk/%7Emgk25/unicode.html#utf-8">encoding rules</a>
718   :</li>
719                                                
720   </ul>
721                                                
722   <ol>
723                                                      
724     <ol>
725                                              <li>if the first byte is in
726 binary  0xxxxxxx, then the character is only one byte long, just skip over
727 that byte<br>
728                                                <br>
729                                              </li>
730                                              <li>if the first byte is in
731 binary  11xxxxxx, then skip over that byte and all bytes 10xxxxxx that follow.<br>
732                                              </li>
733                                                      
734     </ol>
735                                                
736   </ol>
737                                          </blockquote>
738                                            
739 <ul>
740                                            <li>eventually, the conversion 
741 handle needs to be closed (when the program exits):<br>
742                                            </li>
743                                            
744 </ul>
745                                            
746 <blockquote>                                               
747   <blockquote>                                                     
748     <pre><code>iconv_close(iconv_handle);<br></code></pre>
749                                              </blockquote>
750                                              </blockquote>
751                                                   The example implementation 
752  mentioned above grows the output buffer dynamically and outputs "?" for characters
753  that can't be converted.<br>
754                                                                         
755                          
756 <hr width="100%" size="2">                                              
757  
758 <h2><a name="Support_for_configure.in"></a>Support for configure.in</h2>
759    Programs using the GEDCOM parser library and using autoconf to configure 
760  their sources can use the following statements in configure.in (the example 
761  is checking for gedcom-parse, version 1.34):<br>
762                                                    
763 <blockquote><code>AC_CHECK_LIB(gedcom, gedcom_parse_file,,<br>
764    &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AC_MSG_ERROR(Cannot
765  find libgedcom: Please install gedcom-parse))<br>
766    AC_MSG_CHECKING(for libgedcom version)<br>
767    AC_TRY_RUN([<br>
768    #include &lt;stdio.h&gt;<br>
769    #include &lt;stdlib.h&gt;<br>
770    #include &lt;gedcom.h&gt;<br>
771    int<br>
772    main()<br>
773    {<br>
774    if (GEDCOM_PARSE_VERSION &gt;= 1034) exit(0);<br>
775    exit(1);<br>
776    }],<br>
777    ac_gedcom_version_ok='yes',<br>
778    ac_gedcom_version_ok='no',<br>
779    ac_gedcom_version_ok='no')<br>
780    if test "$ac_gedcom_version_ok" = 'yes' ; then<br>
781    &nbsp; AC_MSG_RESULT(ok)<br>
782    else<br>
783    &nbsp; AC_MSG_RESULT(not ok)<br>
784    &nbsp; AC_MSG_ERROR(You need at least version 1.34 of gedcom-parse)<br>
785    fi</code><br>
786                                                    </blockquote>
787     There are three preprocessor symbols defined for version checks in the
788  header:<br>
789                                                      
790 <ul>
791                                                      <li><code>GEDCOM_PARSE_VERSION_MAJOR</code></li>
792                                                      <li><code>GEDCOM_PARSE_VERSION_MINOR</code></li>
793                                                      <li><code>GEDCOM_PARSE_VERSION</code><br>
794                                                      </li>
795                                                      
796 </ul>
797    The last one is equal to <code>(GEDCOM_PARSE_VERSION_MAJOR * 1000) + GEDCOM_PARSE_VERSION_MINOR.</code><br>
798      
799 <hr width="100%" size="2">                                              
800                                        
801 <pre><font size="-1">$Id$<br>$Name$</font><br></pre>
802                                                                         
803                   
804 <pre>                    </pre>
805                                                                         
806                                                         
807 <br>
808 <br>
809 </body></html>