762a4b0a7cd6c86cca27de62a44ff714a76c3f63
[gedcom-parse.git] / doc / usage.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <html>
3 <head>
4   <title>Using the GEDCOM parser library</title>
5                                          
6   <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
7 </head>
8   <body text="#000000" bgcolor="#ffffff" link="#000099" vlink="#990099" alink="#000099">
9            
10 <h1 align="center">Using the GEDCOM parser library</h1>
11       <br>
12            
13 <h2>Index</h2>
14          
15 <ul>
16        <li><a href="#anchor">Overview</a></li>
17        <li><a href="#Error_handling">Error handling</a></li>
18        <li><a href="#Data_callback_mechanism">Data callback mechanism</a></li>
19                    
20   <ul>
21          <li><a href="#Start_and_end_callbacks">Start and end callbacks</a></li>
22          <li><a href="#Default_callbacks">Default callbacks</a></li>
23                    
24   </ul>
25       <li><a href="#Other_API_functions">Other API functions</a></li>
26                
27   <ul>
28         <li><a href="#Debugging">Debugging</a></li>
29         <li><a href="#Error_treatment">Error treatment</a></li>
30         <li><a href="#Compatibility_mode">Compatibility mode</a></li>
31                
32   </ul>
33       <li><a href="interface.html">Interface details</a><br>
34          </li>
35          
36 </ul>
37          
38 <hr width="100%" size="2">      
39 <h2><a name="Overview"></a>Overview<br>
40       </h2>
41       The GEDCOM parser library is built as a callback-based parser (comparable
42    to the SAX interface of XML). &nbsp;It comes with:<br>
43            
44 <ul>
45         <li>a library (<code>libgedcom.so</code>), to be linked in the application
46    program</li>
47         <li>a header file (<code>gedcom.h</code>), to be used in the sources
48   of  the application program</li>
49     <li>a header file (<code>gedcom-tags.h</code>) that is also installed,
50  but that is automatically included via <code>gedcom.h</code><br>
51     </li>
52            
53 </ul>
54       Next to these, there is also a data directory in <code>$PREFIX/share/gedcom-parse</code>
55        that contains some additional stuff, but which is not immediately
56 important    at first. &nbsp;I'll leave the description of the data directory
57 for later.<br>
58       <br>
59       The very simplest call of the gedcom parser is simply the following 
60 piece   of code (include of the gedcom header is assumed, as everywhere in 
61 this manual):<br>
62            
63 <blockquote><code>int result;<br>
64       ...<br>
65       result = <b>gedcom_parse_file</b>("myfamily.ged");<br>
66         </code>   </blockquote>
67       Although this will not provide much information, one thing it does
68 is  parse  the entire file and return the result. &nbsp;The function returns 
69 0 on success  and 1 on failure. &nbsp;No other information is available using 
70  this function  only.<br>
71        <br>
72      The next sections will refine this to be able to have meaningful errors
73   and the actual data that is in the file.<br>
74                    
75   <hr width="100%" size="2">                  
76   <h2><a name="Error_handling"></a>Error handling</h2>
77      Since this is a relatively simple topic, it is discussed before the
78 actual   callback mechanism, although it also uses a callback...<br>
79        <br>
80      The library can be used in several different circumstances, both terminal-based 
81   as GUI-based. &nbsp;Therefore, it leaves the actual display of the error 
82  message up to the application. &nbsp;For this, the application needs to register
83  a callback before parsing the GEDCOM file, which will be called by the library
84   on errors, warnings and messages.<br>
85        <br>
86      A typical piece of code would be:<br>
87                    
88   <blockquote><code>void <b>my_message_handler</b> (Gedcom_msg_type type, 
89   char *msg)<br>
90      {<br>
91      &nbsp; ...<br>
92      }<br>
93      ...<br>
94          <b>gedcom_set_message_handler</b>(my_message_handler);<br>
95      ...<br>
96      result = <b>gedcom_parse_file</b>("myfamily.ged");</code><br>
97          </blockquote>
98      In the above piece of code, <code>my_message_handler</code> is the callback 
99   that will be called for errors (<code>type=ERROR</code>), warnings (<code>
100     type=WARNING</code>) and messages (<code>type=MESSAGE</code>). &nbsp;The 
101  callback must have the signature as in the example. &nbsp;For errors, the 
102      <code> msg</code> passed to the callback will have the format:<br>
103                              
104     <blockquote><code>Error on line</code> <i>&lt;lineno&gt;</i>: <i>&lt;actual_message&gt;</i><br>
105            </blockquote>
106      Note that the entire string will be properly internationalized, and
107 encoded   in UTF-8 (see "Why UTF-8?" &nbsp;<i>LINK TBD</i>). &nbsp;Also,
108 no newline   is appended, so that the application program can use it in any
109 way it wants.   &nbsp;Warnings are similar, but use "Warning" instead of
110 "Error". &nbsp;Messages   are plain text, without any prefix.<br>
111            <br>
112      With this in place, the resulting code will already show errors and
113 warnings   produced by the parser, e.g. on the terminal if a simple <code>
114 printf</code>      is used in the message handler.<br>
115                                        
116       <hr width="100%" size="2">                                   
117       <h2><a name="Data_callback_mechanism"></a>Data callback mechanism</h2>
118      The most important use of the parser is of course to get the data out
119  of  the GEDCOM file. &nbsp;As already mentioned, the parser uses a callback 
120  mechanism  for that. &nbsp;In fact, the mechanism involves two levels.<br>
121            <br>
122      The primary level is that each of the sections in a GEDCOM file is notified 
123   to the application code via a "start element" callback and an "end element" 
124   callback (much like in a SAX interface for XML), i.e. when a line containing 
125   a certain tag is parsed, the "start element" callback is called for that 
126  tag, and when all its subordinate lines with their tags have been processed, 
127  the "end element" callback is called for the original tag. &nbsp;Since GEDCOM 
128   is hierarchical, this results in properly nested calls to appropriate "start 
129   element" and "end element" callbacks.<br>
130            <br>
131      However, it would be typical for a genealogy program to support only 
132 a  subset  of the GEDCOM standard, certainly a program that is still under 
133 development.   &nbsp;Moreover, under GEDCOM it is allowed for an application 
134 to define its  own tags, which will typically not &nbsp;be supported by another 
135 application.   &nbsp;Still, in that case, data preservation is important; 
136 it would hardly   be accepted that information that is not understood by a
137 certain program  is just removed.<br>
138            <br>
139      Therefore, the second level of callbacks involves a "default callback".
140   &nbsp;An application needs to subscribe to callbacks for tags it does support,
141   and need to provide a "default callback" which will be called for tags
142 it   doesn't support. &nbsp;The application can then choose to just store
143 the  information that comes via the default callback in plain textual format.<br>
144            <br>
145      After this introduction, let's see what the API looks like...<br>
146            <br>
147                                        
148       <h3><a name="Start_and_end_callbacks"></a>Start and end callbacks</h3>
149                                        
150       <h4><i>Callbacks for records</i> <br>
151            </h4>
152      As a simple example, we will get some information from the header of 
153 a  GEDCOM  file. &nbsp;First, have a look at the following piece of code:<br>
154                                        
155       <blockquote><code>Gedcom_ctxt <b>my_header_start_cb</b> (int level, 
156   <br>
157  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
158 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Gedcom_val xref, <br>
159  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
160 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char *tag, <br>
161  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
162 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char *raw_value,<br>
163  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
164 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int parsed_tag, <br>
165  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
166 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Gedcom_val parsed_value)<br>
167      {<br>
168      &nbsp; printf("The header starts\n");<br>
169      &nbsp; return (Gedcom_ctxt)1;<br>
170      }<br>
171              <br>
172      void <b>my_header_end_cb</b> (Gedcom_ctxt self)<br>
173      {<br>
174      &nbsp; printf("The header ends, context is %d\n", self); &nbsp; /* context 
175   will print as "1" */<br>
176      }<br>
177              <br>
178      ...<br>
179              <b>gedcom_subscribe_to_record</b>(REC_HEAD, my_header_start_cb,
180   my_header_end_cb);<br>
181      ...<br>
182      result = <b>gedcom_parse_file</b>("myfamily.ged");</code><br>
183              </blockquote>
184         Using the <code>gedcom_subscribe_to_record</code> function, the application 
185   requests to use the specified callbacks as start and end callback. The end
186   callback is optional: you can pass <code>NULL</code> if you are not interested
187   in the end callback. &nbsp;The identifiers to use as first argument to
188 the   function (here <code>REC_HEAD</code>) are described in the <a href="interface.html#Record_identifiers">
189     interface details</a>.<br>
190              <br>
191      From the name of the function it becomes clear that this function is 
192 specific   to complete records. &nbsp;For the separate elements in records 
193 there is  another function, which we'll see shortly. &nbsp;Again, the callbacks 
194 need  to have the signatures as shown in the example.<br>
195              <br>
196      The <code>Gedcom_ctxt</code> type that is used as a result of the start
197   callback and as an argument to the end callback is vital for passing context
198   necessary for the application. &nbsp;This type is meant to be opaque; in
199  fact, it's a void pointer, so you can pass anything via it. &nbsp;The important
200   thing to know is that the context that the application returns in the start
201   callback will be passed in the end callback as an argument, and as we will
202   see shortly, also to all the directly subordinate elements of the record.<br>
203           <br>
204   The <code>tag</code> is the GEDCOM tag in string format, the <code>parsed_tag</code>
205    is an integer, for which symbolic values are defined as <code>TAG_HEAD,</code>
206    <code>TAG_SOUR,</code> <code>TAG_DATA,</code> ... and <code>USERTAG </code><code></code>
207   for the application-specific tags. &nbsp;These values are defined in the
208  header <code>gedcom-tags.h</code> that is installed, and included via <code>
209   gedcom.h</code> (so no need to include <code>gedcom-tags.h</code> yourself).<br>
210              <br>
211      The example passes a simple integer as context, but an application could 
212   e.g. pass a <code>struct</code> that will contain the information for the 
213   header. &nbsp;In the end callback, the application could then e.g. do some 
214   finalizing operations on the <code>struct</code> to put it in its database.<br>
215              <br>
216      (Note that the <code>Gedcom_val</code> type for the <code>xref</code>
217   and <code>parsed_value</code> arguments  was not discussed, see further 
218 for this)<br>
219              <br>
220                                                  
221         <h4><i>Callbacks for elements</i></h4>
222      We will now retrieve the SOUR field (the name of the program that wrote
223   the file) from the header:<br>
224                                                  
225         <blockquote><code>Gedcom_ctxt <b>my_header_source_start_cb</b>(Gedcom_ctxt 
226   parent,<br>
227      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
228  &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int &nbsp;
229  &nbsp;  &nbsp; &nbsp; level,<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; tag,<br>
233      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
234  &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char* &nbsp;
235  &nbsp;  &nbsp; raw_value,<br>
236   &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
237  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int &nbsp; &nbsp;
238  &nbsp; &nbsp; parsed_tag,<br>
239      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
240  &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Gedcom_val
241  &nbsp;parsed_value)<br>
242      {<br>
243      &nbsp; char *source = GEDCOM_STRING(parsed_value);<br>
244      &nbsp; printf("This file was written by %s\n", source);<br>
245      &nbsp; return parent;<br>
246      }<br>
247                <br>
248      void <b>my_header_source_end_cb</b>(Gedcom_ctxt parent,<br>
249      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
250  &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp;Gedcom_ctxt self,<br>
251      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
252  &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp;Gedcom_val &nbsp;parsed_value)<br>
253      {<br>
254      &nbsp; printf("End of the source description\n");<br>
255      }<br>
256                <br>
257      ...<br>
258                <b>gedcom_subscribe_to_element</b>(ELT_HEAD_SOUR,<br>
259      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
260  &nbsp;  &nbsp; &nbsp; &nbsp; my_header_source_start_cb,<br>
261      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
262  &nbsp;  &nbsp; &nbsp; &nbsp; my_header_source_end_cb);<br>
263      ...<br>
264      result = <b>gedcom_parse_file</b>("myfamily.ged");</code><br>
265                </blockquote>
266      The subscription mechanism for elements is similar, only the signatures
267   of the callbacks differ. &nbsp;The signature for the start callback shows
268   that the context of the parent line (e.g. the <code>struct</code> that
269 describes   the header) is passed to this start callback. &nbsp;The callback
270 itself returns  here the same context, but this can be its own context object
271 of course. &nbsp;The end callback is called with both the context of the
272 parent and the context of itself, which will be the same in the example.
273 &nbsp;Again,  the list of identifiers to use as a first argument for the
274 subscription function  are detailed in the <a href="interface.html#Element_identifiers">
275  interface  details</a> .<br>
276                <br>
277      If we look at the other arguments of the start callback, we see the
278 level   number (the initial number of the line in the GEDCOM file), the tag
279 (e.g.   "SOUR"), and then a raw value, a parsed tag and a parsed value. &nbsp;The
280  raw value is just the raw string that occurs as value on the line next to
281  the tag (in UTF-8 encoding). &nbsp;The parsed value is the meaningful value
282  that is parsed from that raw string. &nbsp;The parsed tag is described in
283  the section for record callbacks.<br>
284                <br>
285      The <code>Gedcom_val</code> type is meant to be an opaque type. &nbsp;The 
286   only thing that needs to be known about it is that it can contain specific 
287   data types, which have to be retrieved from it using pre-defined macros. 
288  &nbsp;These data types are described in the <a href="interface.html#Gedcom_val_types">
289     interface details</a>.           <br>
290               <br>
291      Some extra notes:<br>
292                                                            
293           <ul>
294                  <li>The <code>Gedcom_val</code> argument of the end callback 
295   is currently not used. &nbsp;It is there for future enhancements.</li>
296                  <li>There is also a <code>Gedcom_val</code> argument in
297 the   start callback for records. &nbsp;This argument is currently a string
298 value   giving the pointer in string form.</li>
299                                                            
300           </ul>
301                                                            
302           <h3><a name="Default_callbacks"></a>Default callbacks<br>
303                </h3>
304      As described above, an application doesn't always implement the entire 
305  GEDCOM spec, and application-specific tags may have been added by other applications.
306  &nbsp;To preserve this extra data anyway, a default callback can be registered
307  by the application, as in the following example:<br>
308                                                
309           <blockquote><code>void <b>my_default_cb</b> (Gedcom_ctxt parent,
310   int level, char* tag, char* raw_value, int parsed_tag)<br>
311     {<br>
312     &nbsp; ...<br>
313     }<br>
314                 <br>
315     ...<br>
316                 <b>gedcom_set_default_callback</b>(my_default_cb);<br>
317     ...<br>
318     result = <b>gedcom_parse_file</b>("myfamily.ged");</code><br>
319                 </blockquote>
320                This callback has a similar signature as the previous ones,
321  but  it doesn't contain a parsed value. &nbsp;However, it does contain the
322  parent  context, that was returned by the application for the most specific
323  containing  tag that the application supported.<br>
324                 <br>
325     Suppose e.g. that this callback is called for some tags in the header 
326 that  are specific to some other application, then our application could make
327 sure  that the parent context contains the struct or object that represents
328  the  header, and use the default callback here to add the level, tag and
329 raw_value  as plain text in a member of that struct or object, thus preserving
330 the information.  &nbsp;The application can then write this out when the
331 data is saved again  in a GEDCOM file. &nbsp;To make it more specific, consider
332  the following example:<br>
333                                                        
334             <blockquote><code>struct header {<br>
335     &nbsp; char* source;<br>
336     &nbsp; ...<br>
337     &nbsp; char* extra_text;<br>
338     };<br>
339                   <br>
340     Gedcom_ctxt my_header_start_cb(int level, Gedcom_val xref, char* tag, 
341 char *raw_value,<br>
342  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
343 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int parsed_tag, Gedcom_val parsed_value)<br>
344     {<br>
345     &nbsp; struct header head = my_make_header_struct();<br>
346     &nbsp; return (Gedcom_ctxt)head;<br>
347     }<br>
348                   <br>
349     void my_default_cb(Gedcom_ctxt parent, int level, char* tag, char* raw_value,
350  int parsed_tag)<br>
351     {<br>
352     &nbsp; struct header head = (struct header)parent;<br>
353     &nbsp; my_header_add_to_extra_text(head, level, tag, raw_value);<br>
354     }<br>
355                   <br>
356     gedcom_set_default_callback(my_default_cb);<br>
357     gedcom_subscribe_to_record(REC_HEAD, my_header_start, NULL);<br>
358     ...<br>
359     result = gedcom_parse_file(filename);</code><br>
360                   </blockquote>
361     Note that the default callback will be called for any tag that isn't
362 specifically   subscribed upon by the application, and can thus be called
363 in various contexts.   &nbsp;For simplicity, the example above doesn't take
364 this into account (the                 <code>parent</code> could be of different
365 types, depending  on the context).<br>
366                                                                
367               <hr width="100%" size="2">                                 
368                           
369               <h2><a name="Other_API_functions"></a>Other API functions<br>
370                   </h2>
371     Although the above describes the basic interface of libgedcom, there
372 are   some other functions that allow to customize the behaviour of the library.
373   &nbsp;These will be explained in the current section.<br>
374                                                                
375               <h3><a name="Debugging"></a>Debugging</h3>
376     The library can generate various debugging output, not only from itself,
377   but also the debugging output generated by the yacc parser. &nbsp;By default,
378   no debugging output is generated, but this can be customized using the
379 following   function:<br>
380                                                                
381               <blockquote><code>void <b>gedcom_set_debug_level</b> (int level,
382   FILE* trace_output)</code><br>
383                     </blockquote>
384     The <code>level</code> can be one of the following values:<br>
385                                                                        
386                 <ul>
387                       <li>0: &nbsp;no debugging information (this is the
388 default)</li>
389                       <li>1: &nbsp;only debugging information from libgedcom
390   itself</li>
391                       <li>2: &nbsp;debugging information from libgedcom and 
392  yacc</li>
393                                                                        
394                 </ul>
395     If the <code>trace_output</code> is <code>NULL</code>, debugging information
396   will be written to <code>stderr</code>, otherwise the given file handle
397 is  used (which must be open).<br>
398                     <br>
399                                                                        
400                 <h3><a name="Error_treatment"></a>Error treatment</h3>
401     One of the previous sections already described the callback to be registered
402   to get error messages. &nbsp;The library also allows to customize what
403 happens   on an error, using the following function:<br>
404                                                                        
405                 <blockquote><code>void <b>gedcom_set_error_handling</b> (Gedcom_err_mech
406   mechanism)</code><br>
407                       </blockquote>
408     The <code>mechanism</code> can be one of:<br>
409                                                                         
410       
411                   <ul>
412                         <li><code>IMMED_FAIL</code>: immediately fail the 
413 parsing  on an error (this is the default)</li>
414                         <li><code>DEFER_FAIL</code>: continue parsing after 
415  an error, but return a failure code eventually</li>
416                         <li><code>IGNORE_ERRORS</code>: continue parsing
417 after   an error, return success always</li>
418                                                                         
419       
420                   </ul>
421     This doesn't influence the generation of error or warning messages, only
422   the behaviour of the parser and its return code.<br>
423                       <br>
424                                                                         
425       
426                   <h3><a name="Compatibility_mode"></a>Compatibility mode<br>
427                       </h3>
428     Applications are not necessarily true to the GEDCOM spec (or use a different
429   version than 5.5). &nbsp;The intention is that the library is resilient
430 to  this, and goes in compatibility mode for files written by specific programs
431   (detected via the HEAD.SOUR tag). &nbsp;This compatibility mode can be
432 enabled   and disabled via the following function:<br>
433                                                                         
434       
435                   <blockquote><code>void <b>gedcom_set_compat_handling</b>
436      (int enable_compat)</code><br>
437                         </blockquote>
438     The argument can be:<br>
439                                                                         
440               
441                     <ul>
442                           <li>0: disable compatibility mode</li>
443                           <li>1: allow compatibility mode (this is the default)<br>
444                           </li>
445                                                                         
446               
447                     </ul>
448     Note that, currently, no actual compatibility code is present, but this 
449  is on the to-do list.<br>
450                                                                         
451               
452                     <hr width="100%" size="2">                           
453                                    
454                     <pre>$Id$<br>$Name$<br></pre>
455                                                                  
456                     <pre>                    </pre>
457                                                                         
458                               
459                     </body>
460                     </html>