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