Small fix in link.
[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>
5
6 <body text="#000000" bgcolor="#ffffff" link="#000099" vlink="#990099" alink="#000099">
7            
8 <h1 align="center">Using the GEDCOM parser library</h1>
9       <br>
10            
11 <h2>Index</h2>
12          
13 <ul>
14        <li><a href="#anchor">Overview</a></li>
15        <li><a href="#Error_handling">Error handling</a></li>
16        <li><a href="#Data_callback_mechanism">Data callback mechanism</a></li>
17                    
18   <ul>
19          <li><a href="#Start_and_end_callbacks">Start and end callbacks</a></li>
20          <li><a href="#Default_callbacks">Default callbacks</a></li>
21                    
22   </ul>
23       <li><a href="#Other_API_functions">Other API functions</a></li>
24                
25   <ul>
26         <li><a href="#Debugging">Debugging</a></li>
27         <li><a href="#Error_treatment">Error treatment</a></li>
28         <li><a href="#Compatibility_mode">Compatibility mode</a></li>
29                
30   </ul>
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>
98     type=WARNING</code>) and messages (<code>type=MESSAGE</code>). &nbsp;The 
99  callback must have the signature as in the example. &nbsp;For errors, the 
100      <code> msg</code> passed to the callback will have the format:<br>
101                              
102     <blockquote><code>Error on line</code> <i>&lt;lineno&gt;</i>: <i>&lt;actual_message&gt;</i><br>
103            </blockquote>
104      Note that the entire string will be properly internationalized, and
105 encoded   in UTF-8 (see "Why UTF-8?" &nbsp;<i>LINK TBD</i>). &nbsp;Also,
106 no newline   is appended, so that the application program can use it in any
107 way it wants.   &nbsp;Warnings are similar, but use "Warning" instead of
108 "Error". &nbsp;Messages   are plain text, without any prefix.<br>
109            <br>
110      With this in place, the resulting code will already show errors and
111 warnings   produced by the parser, e.g. on the terminal if a simple <code>
112 printf</code>      is used in the message handler.<br>
113                                        
114       <hr width="100%" size="2">                                   
115       <h2><a name="Data_callback_mechanism"></a>Data callback mechanism</h2>
116      The most important use of the parser is of course to get the data out
117  of  the GEDCOM file. &nbsp;As already mentioned, the parser uses a callback 
118  mechanism  for that. &nbsp;In fact, the mechanism involves two levels.<br>
119            <br>
120      The primary level is that each of the sections in a GEDCOM file is notified 
121   to the application code via a "start element" callback and an "end element" 
122   callback (much like in a SAX interface for XML), i.e. when a line containing 
123   a certain tag is parsed, the "start element" callback is called for that 
124  tag, and when all its subordinate lines with their tags have been processed, 
125  the "end element" callback is called for the original tag. &nbsp;Since GEDCOM 
126   is hierarchical, this results in properly nested calls to appropriate "start 
127   element" and "end element" callbacks.<br>
128            <br>
129      However, it would be typical for a genealogy program to support only 
130 a  subset  of the GEDCOM standard, certainly a program that is still under 
131 development.   &nbsp;Moreover, under GEDCOM it is allowed for an application 
132 to define its  own tags, which will typically not &nbsp;be supported by another 
133 application.   &nbsp;Still, in that case, data preservation is important; 
134 it would hardly   be accepted that information that is not understood by a
135 certain program  is just removed.<br>
136            <br>
137      Therefore, the second level of callbacks involves a "default callback".
138   &nbsp;An application needs to subscribe to callbacks for tags it does support,
139   and need to provide a "default callback" which will be called for tags
140 it   doesn't support. &nbsp;The application can then choose to just store
141 the  information that comes via the default callback in plain textual format.<br>
142            <br>
143      After this introduction, let's see what the API looks like...<br>
144            <br>
145                                        
146       <h3><a name="Start_and_end_callbacks"></a>Start and end callbacks</h3>
147                                        
148       <h4><i>Callbacks for records</i> <br>
149            </h4>
150      As a simple example, we will get some information from the header of 
151 a  GEDCOM  file. &nbsp;First, have a look at the following piece of code:<br>
152                                        
153       <blockquote><code>Gedcom_ctxt <b>my_header_start_cb</b> (int level, 
154   <br>
155  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
156 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Gedcom_val xref, <br>
157  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
158 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char *tag, <br>
159  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
160 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char *raw_value,<br>
161  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
162 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int parsed_tag, <br>
163  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
164 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Gedcom_val parsed_value)<br>
165      {<br>
166      &nbsp; printf("The header starts\n");<br>
167      &nbsp; return (Gedcom_ctxt)1;<br>
168      }<br>
169              <br>
170      void <b>my_header_end_cb</b> (Gedcom_ctxt self)<br>
171      {<br>
172      &nbsp; printf("The header ends, context is %d\n", self); &nbsp; /* context 
173   will print as "1" */<br>
174      }<br>
175              <br>
176      ...<br>
177              <b>gedcom_subscribe_to_record</b>(REC_HEAD, my_header_start_cb,
178   my_header_end_cb);<br>
179      ...<br>
180      result = <b>gedcom_parse_file</b>("myfamily.ged");</code><br>
181              </blockquote>
182         Using the <code>gedcom_subscribe_to_record</code> function, the application 
183   requests to use the specified callbacks as start and end callback. The end
184   callback is optional: you can pass <code>NULL</code> if you are not interested
185   in the end callback. &nbsp;The identifiers to use as first argument to
186 the   function (here <code>REC_HEAD</code>) are described in the <a href="interface.html#Record_identifiers">
187     interface details</a>.<br>
188              <br>
189      From the name of the function it becomes clear that this function is 
190 specific   to complete records. &nbsp;For the separate elements in records 
191 there is  another function, which we'll see shortly. &nbsp;Again, the callbacks 
192 need  to have the signatures as shown in the example.<br>
193              <br>
194      The <code>Gedcom_ctxt</code> type that is used as a result of the start
195   callback and as an argument to the end callback is vital for passing context
196   necessary for the application. &nbsp;This type is meant to be opaque; in
197  fact, it's a void pointer, so you can pass anything via it. &nbsp;The important
198   thing to know is that the context that the application returns in the start
199   callback will be passed in the end callback as an argument, and as we will
200   see shortly, also to all the directly subordinate elements of the record.<br>
201           <br>
202   The <code>tag</code> is the GEDCOM tag in string format, the <code>parsed_tag</code>
203    is an integer, for which symbolic values are defined as <code>TAG_HEAD,</code>
204    <code>TAG_SOUR,</code> <code>TAG_DATA,</code> ... and <code>USERTAG </code><code></code>
205   for the application-specific tags. &nbsp;These values are defined in the
206  header <code>gedcom-tags.h</code> that is installed, and included via <code>
207   gedcom.h</code> (so no need to include <code>gedcom-tags.h</code> yourself).<br>
208              <br>
209      The example passes a simple integer as context, but an application could 
210   e.g. pass a <code>struct</code> that will contain the information for the 
211   header. &nbsp;In the end callback, the application could then e.g. do some 
212   finalizing operations on the <code>struct</code> to put it in its database.<br>
213              <br>
214      (Note that the <code>Gedcom_val</code> type for the <code>xref</code>
215   and <code>parsed_value</code> arguments  was not discussed, see further 
216 for this)<br>
217              <br>
218                                                  
219         <h4><i>Callbacks for elements</i></h4>
220      We will now retrieve the SOUR field (the name of the program that wrote
221   the file) from the header:<br>
222                                                  
223         <blockquote><code>Gedcom_ctxt <b>my_header_source_start_cb</b>(Gedcom_ctxt 
224   parent,<br>
225      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
226  &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int &nbsp;
227  &nbsp;  &nbsp; &nbsp; level,<br>
228      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
229  &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char* &nbsp;
230  &nbsp;  &nbsp; tag,<br>
231      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
232  &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char* &nbsp;
233  &nbsp;  &nbsp; raw_value,<br>
234   &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
235  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int &nbsp; &nbsp;
236  &nbsp; &nbsp; parsed_tag,<br>
237      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
238  &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Gedcom_val
239  &nbsp;parsed_value)<br>
240      {<br>
241      &nbsp; char *source = GEDCOM_STRING(parsed_value);<br>
242      &nbsp; printf("This file was written by %s\n", source);<br>
243      &nbsp; return parent;<br>
244      }<br>
245                <br>
246      void <b>my_header_source_end_cb</b>(Gedcom_ctxt parent,<br>
247      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
248  &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp;Gedcom_ctxt self,<br>
249      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
250  &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp;Gedcom_val &nbsp;parsed_value)<br>
251      {<br>
252      &nbsp; printf("End of the source description\n");<br>
253      }<br>
254                <br>
255      ...<br>
256                <b>gedcom_subscribe_to_element</b>(ELT_HEAD_SOUR,<br>
257      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
258  &nbsp;  &nbsp; &nbsp; &nbsp; my_header_source_start_cb,<br>
259      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
260  &nbsp;  &nbsp; &nbsp; &nbsp; my_header_source_end_cb);<br>
261      ...<br>
262      result = <b>gedcom_parse_file</b>("myfamily.ged");</code><br>
263                </blockquote>
264      The subscription mechanism for elements is similar, only the signatures
265   of the callbacks differ. &nbsp;The signature for the start callback shows
266   that the context of the parent line (e.g. the <code>struct</code> that
267 describes   the header) is passed to this start callback. &nbsp;The callback
268 itself returns  here the same context, but this can be its own context object
269 of course. &nbsp;The end callback is called with both the context of the
270 parent and the context of itself, which will be the same in the example.
271 &nbsp;Again,  the list of identifiers to use as a first argument for the
272 subscription function  are detailed in the <a href="interface.html#Element_identifiers">
273  interface  details</a> .<br>
274                <br>
275      If we look at the other arguments of the start callback, we see the
276 level   number (the initial number of the line in the GEDCOM file), the tag
277 (e.g.   "SOUR"), and then a raw value, a parsed tag and a parsed value. &nbsp;The
278  raw value is just the raw string that occurs as value on the line next to
279  the tag (in UTF-8 encoding). &nbsp;The parsed value is the meaningful value
280  that is parsed from that raw string. &nbsp;The parsed tag is described in
281  the section for record callbacks.<br>
282                <br>
283      The <code>Gedcom_val</code> type is meant to be an opaque type. &nbsp;The 
284   only thing that needs to be known about it is that it can contain specific 
285   data types, which have to be retrieved from it using pre-defined macros. 
286  &nbsp;These data types are described in the <a href="interface.html#Gedcom_val_types">
287     interface details</a>.           <br>
288               <br>
289      Some extra notes:<br>
290                                                            
291           <ul>
292                  <li>The <code>Gedcom_val</code> argument of the end callback 
293   is currently not used. &nbsp;It is there for future enhancements.</li>
294                  <li>There is also a <code>Gedcom_val</code> argument in
295 the   start callback for records. &nbsp;This argument is currently a string
296 value   giving the pointer in string form.</li>
297                                                            
298           </ul>
299                                                            
300           <h3><a name="Default_callbacks"></a>Default callbacks<br>
301                </h3>
302      As described above, an application doesn't always implement the entire 
303  GEDCOM spec, and application-specific tags may have been added by other applications.
304  &nbsp;To preserve this extra data anyway, a default callback can be registered
305  by the application, as in the following example:<br>
306                                                
307           <blockquote><code>void <b>my_default_cb</b> (Gedcom_ctxt parent,
308   int level, char* tag, char* raw_value, int parsed_tag)<br>
309     {<br>
310     &nbsp; ...<br>
311     }<br>
312                 <br>
313     ...<br>
314                 <b>gedcom_set_default_callback</b>(my_default_cb);<br>
315     ...<br>
316     result = <b>gedcom_parse_file</b>("myfamily.ged");</code><br>
317                 </blockquote>
318                This callback has a similar signature as the previous ones,
319  but  it doesn't contain a parsed value. &nbsp;However, it does contain the
320  parent  context, that was returned by the application for the most specific
321  containing  tag that the application supported.<br>
322                 <br>
323     Suppose e.g. that this callback is called for some tags in the header 
324 that  are specific to some other application, then our application could make
325 sure  that the parent context contains the struct or object that represents
326  the  header, and use the default callback here to add the level, tag and
327 raw_value  as plain text in a member of that struct or object, thus preserving
328 the information.  &nbsp;The application can then write this out when the
329 data is saved again  in a GEDCOM file. &nbsp;To make it more specific, consider
330  the following example:<br>
331                                                        
332             <blockquote><code>struct header {<br>
333     &nbsp; char* source;<br>
334     &nbsp; ...<br>
335     &nbsp; char* extra_text;<br>
336     };<br>
337                   <br>
338     Gedcom_ctxt my_header_start_cb(int level, Gedcom_val xref, char* tag, 
339 char *raw_value,<br>
340  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
341 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int parsed_tag, Gedcom_val parsed_value)<br>
342     {<br>
343     &nbsp; struct header head = my_make_header_struct();<br>
344     &nbsp; return (Gedcom_ctxt)head;<br>
345     }<br>
346                   <br>
347     void my_default_cb(Gedcom_ctxt parent, int level, char* tag, char* raw_value,
348  int parsed_tag)<br>
349     {<br>
350     &nbsp; struct header head = (struct header)parent;<br>
351     &nbsp; my_header_add_to_extra_text(head, level, tag, raw_value);<br>
352     }<br>
353                   <br>
354     gedcom_set_default_callback(my_default_cb);<br>
355     gedcom_subscribe_to_record(REC_HEAD, my_header_start, NULL);<br>
356     ...<br>
357     result = gedcom_parse_file(filename);</code><br>
358                   </blockquote>
359     Note that the default callback will be called for any tag that isn't
360 specifically   subscribed upon by the application, and can thus be called
361 in various contexts.   &nbsp;For simplicity, the example above doesn't take
362 this into account (the                 <code>parent</code> could be of different
363 types, depending  on the context).<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                                                                         
449               
450                     <hr width="100%" size="2">                           
451                                    
452                     <pre><font size="-1">$Id$<br>$Name$</font><br></pre>
453                                                                  
454                     <pre>                    </pre>
455                                                                         
456                               
457                     </body></html>