Moved gedcom_set_error_handling to interface.c to make conversion of docs
[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="#Support_for_writing_GEDCOM_files">Support for writing GEDCOM files</a></li>
21   <ul>
22     <li><a href="#Opening_and_closing_files">Opening and closing files</a></li>
23     <li><a href="#Controlling_some_settings">Controlling some settings</a></li>
24     <li><a href="#Writing_data">Writing data</a><br>
25     </li>
26   </ul>
27 <li><a href="#Other_API_functions">Other API functions</a></li>
28                            
29   <ul>
30            <li><a href="#Debugging">Debugging</a></li>
31            <li><a href="#Error_treatment">Error treatment</a></li>
32            <li><a href="#Compatibility_mode">Compatibility mode</a></li>
33                            
34   </ul>
35     <li><a href="#Converting_character_sets">Converting character sets</a></li>
36     <li><a href="#Support_for_configure.in">Development support</a><br>
37 <br>
38      </li>
39            <li><a href="interface.html">Interface details of the callback parser</a></li><li><a href="gom.html">C object model</a><br>
40             </li>
41
42                
43 </ul>
44                
45 <hr width="100%" size="2">         
46 <h2><a name="Overview"></a>Overview<br>
47          </h2>          The GEDCOM
48 parser library provides two interfaces. &nbsp;At the one hand, it can be
49 used as a callback-based parser (comparable      to the SAX interface of
50 XML); at the other hand, the parser can be used to convert the GEDCOM file
51 into an object model (comparable to the DOM interface of XML). &nbsp;It comes
52 with:<br>
53                  
54 <ul>
55            <li>a library (<code>libgedcom.so</code>), to be linked in the 
56 application     program, which implements the callback parser</li>
57            <li>a header file (<code>gedcom.h</code>), to be used in the sources 
58    of  the application program</li>
59        <li>a header file (<code>gedcom-tags.h</code>) that is also installed, 
60   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>
61 <ul>
62   <li>a library (<code>libgedcom_gom.so</code>), to be linked in the application program, which implements the C object model</li>
63   <li>a header file (<code>gom.h</code>), to be used in the sources of the application program<br>
64   </li>
65
66                  
67 </ul>There is a separate script to help with library and compilation flags, see the <a href="#Support_for_configure.in">development support</a>.<br>
68 <br>
69 Next to these, there is also a data directory in <code>$PREFIX/share/gedcom-parse</code>
70           that contains some additional stuff, but which is not immediately 
71  important    at first. &nbsp;I'll leave the description of the data directory 
72  for later.<br>
73          <br>
74          The very simplest call of the gedcom callback parser is simply the following
75   piece   of code (include of the <code>gedcom.h</code> header is assumed, as everywhere
76 in  this manual):<br>
77                  
78 <blockquote><code>int result;<br>
79   ...<br>
80     <b>gedcom_init</b>();<br>
81          ...<br>
82          result = <b>gedcom_parse_file</b>("myfamily.ged");<br>
83            </code>   </blockquote>
84          Although this will not provide much information, one thing it does 
85  is  parse  the entire file and return the result. &nbsp;The function returns
86   0 on success  and 1 on failure. &nbsp;No other information is available
87 using   this function  only.<br>
88 <br>
89 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>
90   
91 <blockquote><code>int result;<br>
92   ...<br>
93     <b>gedcom_init</b>();<br>
94          ...<br>
95          result = <b>gom_parse_file</b>("myfamily.ged");<br>
96            </code>   </blockquote>
97 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>
98 <br>
99 No matter which of the interfaces you use, the call to <code>gedcom_init</code>() should be one of the first calls 
100 in your program. &nbsp;The requirement is that it should come before the first
101 call to <code>iconv_open</code> (part of the generic character set conversion
102 feature) in the program, either by your program itself, or indirectly by
103 the library calls it makes. &nbsp;Practically, it should e.g. come before
104  any calls to any GTK functions, because GTK uses <code>iconv_open</code>
105  in its initialization.<br>
106 &nbsp; <br>
107 For the same reason it is also advised to put
108 the <code>-lgedcom</code> option
109 on the linking of the program as the last option, so that its initialization
110 code is run first. &nbsp;In the case of using the C object model, the linking
111 options should be: <code>-lgedcom_gom -lgedcom</code><br>
112           <br>The function <code>gedcom_init()</code> also initializes locale handling by calling <code>setlocale(LC_ALL, "")</code>, in case the application would not do this (it doesn't hurt for the application to do the same).<br>
113 &nbsp;<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 <a href="gom.html">here</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 (note: see <a href="#Compatibility_mode">compatibility handling</a>).<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 used to pass 'complete' values, e.g. the full text of a note. &nbsp;See
340  the&nbsp;<a href="file:///home/verthezp/src/external/gedcom-parse/doc/interface.html#Record_identifiers">interface details</a>
341   for the exact type of this argument.</li>
342                     <li>There are also two <code>Gedcom_val</code> arguments
343  in the   start callback for records. &nbsp;The first one (<code>xref</code>
344   ) contains the <code>xref_value</code> corresponding to the cross-reference
345  (or <code>NULL</code> if there isn't one), the second one (<code>parsed_value</code>
346   ) contains the value that is parsed from the <code>raw_value</code>. &nbsp;See
347  the&nbsp;<a href="interface.html#Record_identifiers">interface details</a>
348   .</li>
349                                                                         
350   
351 </ul>
352                                                                         
353   
354 <h3><a name="Default_callbacks"></a>Default callbacks<br>
355                   </h3>
356         As described above, an application doesn't always implement the entire
357    GEDCOM spec, and application-specific tags may have been added by other
358  applications.  &nbsp;To preserve this extra data anyway, a default callback
359  can be registered  by the application, as in the following example:<br>
360                                                                
361 <blockquote><code>void <b>my_default_cb</b> (Gedcom_elt elt, Gedcom_ctxt parent,   int level,
362  char* tag, char* raw_value, int parsed_tag)<br>
363        {<br>
364        &nbsp; ...<br>
365        }<br>
366                    <br>
367        ...<br>
368                    <b>gedcom_set_default_callback</b>(my_default_cb);<br>
369        ...<br>
370        result = <b>gedcom_parse_file</b>("myfamily.ged");</code><br>
371                    </blockquote>
372                   This callback has a similar signature as the previous ones, 
373   but  it doesn't contain a parsed value. &nbsp;However, it does contain the
374   parent  context, that was returned by the application for the most specific
375   containing  tag that the application supported.<br>
376                    <br>
377        Suppose e.g. that this callback is called for some tags in the header
378   that  are specific to some other application, then our application could
379  make sure  that the parent context contains the struct or object that represents 
380   the  header, and use the default callback here to add the level, tag and 
381  raw_value  as plain text in a member of that struct or object, thus preserving 
382  the information.  &nbsp;The application can then write this out when the 
383 data is saved again  in a GEDCOM file. &nbsp;To make it more specific, consider 
384   the following example:<br>
385                                                                          
386 <blockquote><code>struct header {<br>
387        &nbsp; char* source;<br>
388        &nbsp; ...<br>
389        &nbsp; char* extra_text;<br>
390        };<br>
391                      <br>
392        Gedcom_ctxt my_header_start_cb(Gedcom_rec rec, int level, Gedcom_val xref, char* tag,
393   char *raw_value,<br>
394     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
395 &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int parsed_tag, Gedcom_val parsed_value)<br>
396        {<br>
397        &nbsp; struct header head = my_make_header_struct();<br>
398        &nbsp; return (Gedcom_ctxt)head;<br>
399        }<br>
400                      <br>
401        void my_default_cb(Gedcom_elt elt, Gedcom_ctxt parent, int level, char* tag, char* 
402 raw_value,   int parsed_tag)<br>
403        {<br>
404        &nbsp; struct header head = (struct header)parent;<br>
405        &nbsp; my_header_add_to_extra_text(head, level, tag, raw_value);<br>
406        }<br>
407                      <br>
408        gedcom_set_default_callback(my_default_cb);<br>
409        gedcom_subscribe_to_record(REC_HEAD, my_header_start, NULL);<br>
410        ...<br>
411        result = gedcom_parse_file(filename);</code><br>
412                      </blockquote>
413        Note that the default callback will be called for any tag that isn't 
414  specifically   subscribed upon by the application, and can thus be called 
415  in various contexts.   &nbsp;For simplicity, the example above doesn't take 
416  this into account (the                 <code>parent</code> could be of different 
417  types, depending  on the context).<br>
418                  <br>
419    Note also that the default callback is not called when the parent context
420  is&nbsp;<code>NULL</code><code></code>. &nbsp;This is e.g. the case if none
421  of the "upper" tags has been subscribed upon.<br>
422                                                                         
423           
424 <hr width="100%" size="2"><br>
425 <h2><a name="Support_for_writing_GEDCOM_files"></a>Support for writing GEDCOM files</h2>
426 The Gedcom parser library also contains functions to writing GEDCOM files.
427 &nbsp;Similar as for the parsing itself, there are two interfaces: an interface
428 which is very basic, and requires you to call a function for each line in
429 the GEDCOM file, and an interface which just dumps the Gedcom object model
430 to a file in one shot (if you use the Gedcom object model).<br>
431 <br>
432 Again, this section focuses on the basic interface, the Gedcom object model interface is described <a href="gom.html#Writing_the_object_model_to_file">here</a>.<br>
433 <br>
434 <h3><a name="Opening_and_closing_files"></a>Opening and closing files</h3>
435 The basic functions for opening and closing Gedcom files for writing are the following:<br>
436 <code></code>
437 <blockquote><code>Gedcom_write_hndl <b>gedcom_write_open</b> (const char* filename);<br>
438 int &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <b>gedcom_write_close</b> (Gedcom_write_hndl hndl, int* total_conv_fails);<br></code></blockquote>
439 The function <code>gedcom_write_open</code> takes a parameter the name of
440 the file to write, and returns a write handle, which needs to be used in
441 subsequent functions. &nbsp;It returns <code>NULL</code> in case of errors.<br>
442 <br>
443 The function <code>gedcom_write_close</code> takes, next to the write handle,
444 an integer pointer as parameter. &nbsp;If you pass an actual pointer for
445 this, the function will write in it the total number of conversion failures;
446 you can pass <code>NULL</code> if you're not interested. &nbsp;The function returns 0 in case of success, non-zero in case of failure.<br>
447 <br>
448 <h3><a name="Controlling_some_settings"></a>Controlling some settings<br>
449 </h3>
450 Note that by default the file is written in the same encoding as the read file was in. &nbsp;You can change
451 this by calling the following function <i>before</i> calling <code>gedcom_write_open</code>, i.e. it affects all files that are opened after it is being called:<code></code><code><br>
452 </code>
453 <blockquote><code>int <b>gedcom_write_set_encoding</b> (Enc_from from, const char* charset, Encoding width, Enc_bom bom);<br></code></blockquote>The <code>from</code> parameter indicates how you want the encoding to be set:<br>
454 <ul>
455   <li><b><code>ENC_FROM_FILE</code></b>: The same as the read file was in (this is the default).</li>
456   <li><b><code>ENC_FROM_SYS</code></b>: Not a valid value here, see below for <code>gedcom_write_set_terminator</code><br>
457   </li>
458   <li><b><code>ENC_MANUAL</code></b>: From the values given in the following parameters.</li>
459 </ul>
460 When <code>ENC_FROM_FILE</code><b> </b>is selected, the other parameters in the function are ignored (they can be passed as 0). &nbsp;When <code>ENC_MANUAL</code> is chosen, the meaning of the other parameters is as follows:<br>
461 <br>
462 The valid <code>charset</code> values are given in the first column in the file <code>gedcom.enc</code> in the data directory of gedcom-parse (<code>$PREFIX/share/gedcom-parse</code>).
463 &nbsp;The character sets UNICODE, ASCII and ANSEL are always supported (these
464 are standard for GEDCOM), as well as ANSI (not standard), but there may be
465 others.<br>
466 <br>
467 The <code>width</code> parameter takes one of the following values:<br>
468 <ul>
469 </ul>
470 <ul>
471   <li><code><b>ONE_BYTE</b></code>: This should be used for all character sets except UNICODE.</li>
472   <li><code><b>TWO_BYTE_HILO</b></code>: High-low encoding for UNICODE (i.e. big-endian)</li>
473   <li><code><b>TWO_BYTE_LOHI</b></code>: Low-high encoding for UNICODE (i.e. little-endian)</li>
474 </ul>
475 The <code>bom</code> parameter determines whether a byte-order-mark should
476 be written in the file in case of UNICODE encoding (usually preferred because
477 it then clearly indicates the byte ordering). &nbsp;It takes one of the following
478 values:<br>
479 <ul>
480   <li><code><b>WITHOUT_BOM</b></code></li>
481   <li><code><b>WITH_BOM</b></code></li>
482 </ul> For both these parameters you can pass 0 for non-UNICODE encodings,
483 since that corresponds to the correct values (and is ignored anyway). &nbsp;The 
484 function returns 0 in case of success, non-zero in case of error. &nbsp;Note
485 that you still need to pass the correct charset value for the HEAD.CHAR tag,
486 otherwise you will get a warning, and the value will be forced to the correct
487 value.<br>
488 <br>
489 Further, it is possible to control the kind of line terminator that is used, via the following function (also to be used before <code>gedcom_write_open</code>):<br>
490 <blockquote><code>int <b>gedcom_write_set_line_terminator</b> (Enc_from from, Enc_line_end end);<br></code></blockquote>The values for the <code>from</code> parameter are given above. &nbsp;The value <b><code>ENC_FROM_SYS</code></b>
491 is valid here, and means that the normal terminator for the current system
492 is used (the second parameter of the function is then ignored). &nbsp; This
493 is the default for this setting.<br>
494 <br>
495 The <code>end</code> parameter takes one of the following values:<br>
496 <ul>
497   <li><b><code>END_CR</code></b>: only carriage return ("/r") (system value for Macintosh)</li>
498   <li><b><code>END_LF</code></b>: only line feed ("/n") (system value for Unix, Mac OS X)</li>
499   <li><b><code>END_CR_LF</code></b>: first carriage return, then line feed ("/r/n") (system value for DOS, Windows)</li>
500   <li><b><code>END_LF_CR</code></b>: first line feed, then carriage return ("/n/r")</li>
501 </ul>
502 By default, this is set to the appropriate line terminator on the current
503 platform, so it only needs to be changed if there is some special reason
504 for it.<br>
505 <h3><a name="Writing_data"></a>Writing data<br>
506 </h3>
507 For actually writing the data, the principle is that every line in the GEDCOM
508 file to write corresponds to a call to one of the following functions, except
509 that CONT/CONC lines can be automatically taken care of. &nbsp;Note that
510 the resulting GEDCOM file should conform to the GEDCOM standard. &nbsp;Several
511 checks are built in already, and more will follow, to force this. &nbsp;There
512 is no compatibility mode for writing GEDCOM files (and probably never will be).<br>
513 <br>
514 In general, each of the following functions expect their input in UTF-8 encoding (see also <a href="#Converting_character_sets">here</a>). &nbsp;If this is not the case, errors will be returned.<br>
515 <br>
516 Note that for examples of using these functions you can look at the sources for the Gedcom object model (e.g. the function <code>write_header</code> in <code>gom/header.c</code>).<br>
517 <h4>Records</h4>
518 For writing lines corresponding to records (i.e. on level 0), the following function is available:
519 <blockquote><code>int <b>gedcom_write_record_str</b> (Gedcom_write_hndl hndl, Gedcom_rec rec, const char* xrefstr, const char* value);<br></code></blockquote>
520 The <code>hndl</code> parameter is the write handle that was returned by <code>gedcom_write_open</code>. &nbsp;The <code>rec</code> parameter is one of the identifiers given in the first column in <a href="interface.html#Record_identifiers">this table</a> (except <code>REC_USER</code>: see below). &nbsp;The <code>xrefstr</code> and <code>val</code> parameters are respectively the cross-reference key of the record (something like '<code>@FAM01@</code>'), and the value of the record line, which should be <code>NULL</code> for some record types, according to the same table.<br>
521 <h4>Elements</h4>
522 For writing lines corresponding to elements (inside records, i.e. on a level
523 bigger than 0), the following functions are available, depending on the data
524 type:
525 <blockquote><code>int <b>gedcom_write_element_str</b> &nbsp;(Gedcom_write_hndl hndl, Gedcom_elt elt, int parsed_tag, <br>
526 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
527 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int parent_rec_or_elt, const char* value);<br>
528 i</code><code>nt <b>gedcom_write_element_xref</b> (Gedcom_write_hndl hndl, Gedcom_elt elt, int parsed_tag, <br> 
529 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
530 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int parent_rec_or_elt, const struct xref_value*
531 value);</code><br>
532   <code>int <b>gedcom_write_element_date</b> (Gedcom_write_hndl hndl, Gedcom_elt elt, int parsed_tag, <br> 
533 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
534 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int parent_rec_or_elt, const struct date_value*
535 value);</code><br>
536   <code>i</code><code>nt <b>gedcom_write_element_age&nbsp;</b> (Gedcom_write_hndl hndl, Gedcom_elt elt, int parsed_tag, <br> 
537 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
538 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int parent_rec_or_elt, const struct age_value*
539 value);</code><br>
540 </blockquote>
541 <blockquote><code></code></blockquote>
542 These functions only differ in the type of the last argument, which is the value of the element.<br>
543 <br>
544 The <code>hndl</code> parameter is again the write handle returned by <code>gedcom_write_open</code>. &nbsp;The <code>elt</code> parameter is one of the identifiers given in the first column in <a href="interface.html#Element_identifiers">this table</a> (except <code>ELT_USER</code>: see below). &nbsp;The <code>parent_rec_or_elt</code> is the corresponding <code>rec</code> or <code>elt</code>
545 identifier of the logically enclosing statement: this will determine the
546 level number written on the line, as the level number of the parent + 1.<br>
547 <br>
548 Some of the identifiers can actually stand for different tags. &nbsp;For this reason, the <code>parsed_tag</code> has to be passed for some of them. &nbsp;This parsed tag is the same as was returned by the callback functions defined <a href="#Start_and_end_callbacks">above</a>, and is an identifier of the form <code>TAG_<i>name</i></code>. &nbsp;This parameter is needed whenever the second column in <a href="interface.html#Element_identifiers">this table</a> shows several possible tags (this is e.g. the case for <code>ELT_SUB_FAM_EVT</code>).<br>
549 <br>
550 Note that for writing a date value, the given value should be valid, i.e.
551 all its struct fields filled in properly and consistent. &nbsp;This can be
552 done by calling <code>gedcom_normalize_date</code> (see <a href="interface.html#date">here</a>).<br>
553 <h4>User-defined tags</h4>
554 For user-defined tags (tags starting with an underscore), there are separate functions, again depending on the data type:<code></code>
555 <blockquote><code>int <b>gedcom_write_user_str</b> &nbsp;(Gedcom_write_hndl hndl, int level, const char* tag, const char* xrefstr,<br>
556 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const char* value);<br>
557 i</code><code>nt <b>gedcom_write_user_xref</b> (Gedcom_write_hndl hndl, </code><code>int level, const char* tag, const char* xrefstr,</code><br>
558   <code> 
559 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
560 &nbsp; &nbsp; &nbsp; const struct xref_value* value);</code><br>
561   <code></code></blockquote>
562 In the case of user-defined tags, the level and tag string are passed verbatim
563 (not controlled by the library). &nbsp;This allows to write any extra data
564 that doesn't use a standard tag, but is only allowed for tags starting with
565 an underscore.<br>
566 <hr width="100%" size="2">                                              
567                               
568 <h2><a name="Other_API_functions"></a>Other API functions<br>
569                      </h2>
570
571        Although the above describes the basic interface of the gedcom parser, there 
572  are   some other functions that allow to customize the behaviour of the library.
573    &nbsp;These will be explained in the current section.<br>
574                                                                         
575           
576 <h3><a name="Debugging"></a>Debugging</h3>
577        The library can generate various debugging output, not only from itself, 
578    but also the debugging output generated by the yacc parser. &nbsp;By default, 
579    no debugging output is generated, but this can be customized using the 
580 following   function:<br>
581                                                                         
582           
583 <blockquote><code>void <b>gedcom_set_debug_level</b> (int level,   FILE*
584 trace_output)</code><br>
585                        </blockquote>
586        The <code>level</code> can be one of the following values:<br>
587                                                                         
588                     
589 <ul>
590                          <li>0: &nbsp;no debugging information (this is the 
591  default)</li>
592                          <li>1: &nbsp;only debugging information from libgedcom 
593    itself</li>
594                          <li>2: &nbsp;debugging information from libgedcom
595  and   yacc</li>
596                                                                         
597                     
598 </ul>
599        If the <code>trace_output</code> is <code>NULL</code>, debugging information 
600    will be written to <code>stderr</code>, otherwise the given file handle 
601  is  used (which must be open).<br>
602                        <br>
603                                                                         
604                     
605 <h3><a name="Error_treatment"></a>Error treatment</h3>
606        One of the previous sections already described the callback to be
607 registered    to get error messages. &nbsp;The library also allows to customize
608 what happens   on an error, using the following function:<br>
609                                                                         
610                     
611 <blockquote><code>void <b>gedcom_set_error_handling</b> (Gedcom_err_mech 
612  mechanism)</code><br>
613                          </blockquote>
614        The <code>mechanism</code> can be one of:<br>
615                                                                         
616                               
617 <ul>
618                            <li><code>IMMED_FAIL</code>: immediately fail
619 the   parsing  on an error (this is the default)</li>
620                            <li><code>DEFER_FAIL</code>: continue parsing
621 after    an error, but return a failure code eventually</li>
622                            <li><code>IGNORE_ERRORS</code>: continue parsing 
623  after   an error, return success always</li>
624                                                                         
625                               
626 </ul>
627        This doesn't influence the generation of error or warning messages,
628  only   the behaviour of the parser and its return code.<br>
629                          <br>
630                                                                         
631                               
632 <h3><a name="Compatibility_mode"></a>Compatibility mode<br>
633                          </h3>
634        Applications are not necessarily true to the GEDCOM spec (or use a 
635 different    version than 5.5). &nbsp;The intention is that the library is 
636 resilient  to  this, and goes in compatibility mode for files written by specific
637 programs    (detected via the HEAD.SOUR tag). &nbsp;This compatibility mode
638 can be enabled   and disabled via the following function:<br>
639                                                                         
640                               
641 <blockquote><code>void <b>gedcom_set_compat_handling</b>      (int enable_compat)</code><br>
642                            </blockquote>
643        The argument can be:<br>
644                                                                         
645                                         
646 <ul>
647                              <li>0: disable compatibility mode</li>
648                              <li>1: allow compatibility mode (this is the 
649 default)<br>
650                              </li>
651                                                                         
652                                         
653 </ul>
654        Currently, there is (some) compatibility for:<br>
655 <ul>
656   <li>ftree</li>
657   <li>Lifelines (3.0.2)</li>
658   <li>Personal Ancestral File (PAF), version 2, 4 and 5</li>
659   <li>Family Origins</li>
660   <li>EasyTree</li>
661 </ul>
662 The following function allows to set some options for the compatibility handling:<br>
663 <blockquote><code>void <b>gedcom_set_compat_options</b>      (Gedcom_compat options)</code><br>
664                            </blockquote>
665 The parameter can be an OR'ed combination of the following options:<br>
666 <ul>
667   <li><code>COMPAT_ALLOW_OUT_OF_CONTEXT</code></li>
668 </ul>
669 <blockquote>
670   <blockquote>In some compatibility cases, tags are coming out-of-order,
671 i.e. their start element callback would have to come after the end element
672 callback of the parent tag. &nbsp;E.g. instead of the standard GEDCOM<br>
673     <blockquote><code>1 DATE ...<br>
674 2 TIME ...</code><br>
675     </blockquote>
676 the genealogy program has generated something like:<br>
677     <blockquote><code>1 DATE ...<br>
678 1 TIME ...</code><br>
679     </blockquote>
680 This can give a problem if your end element callbacks free some resources. &nbsp;<br>
681     <br>
682 If your program can handle elements out of context, you can enable this option.
683 &nbsp;By default it is disabled, and so the values of these out-of-context
684 tags are lost (the parser generates a warning if this is the case). &nbsp;The Gedcom object model in C has this option enabled.<br>
685   </blockquote>
686 </blockquote>
687 <br>
688
689                          
690 <hr width="100%" size="2">                       
691 <h2><a name="Converting_character_sets"></a>Converting character sets</h2>
692    All strings passed by the GEDCOM parser to the application are in UTF-8
693  encoding. &nbsp;Typically, an application needs to convert this to something
694  else to be able to display it.<br>
695                        <br>
696    The most common case is that the output character set is controlled by 
697 the <code>locale</code> mechanism (i.e. via the <code>LANG</code>, <code>
698  LC_ALL</code>  or <code>LC_CTYPE</code> environment variables), which also 
699 controls the <code>gettext</code>  mechanism in the application. &nbsp;<br>
700                        <br>With<code>
701 gedcom-parse</code>   comes a library implementing help functions for UTF-8 encoding (<code></code>see
702 the <a href="utf8tools.html">documentation</a> for this library).<br>
703                                                                         
704                          
705 <hr width="100%" size="2">                                              
706  
707 <h2><a name="Support_for_configure.in"></a>Development support</h2>
708 <h3>Macro for configure.in<br>
709 </h3>
710 There
711 is a macro available for use in configure.in for applications that are using
712 autoconf to configure their sources. &nbsp;The following macro checks whether
713 the Gedcom parser library is available and whether its version is high enough:<br>
714 <blockquote><code>AM_PATH_GEDCOM_PARSER([<i>min_version</i>,[<i>action_if_found</i>,[<i>action_if_not_found,</i>[<i>modules</i>]]]])</code><br>
715 </blockquote>
716 All the arguments are optional and default to 0. &nbsp;E.g. to check for
717 version 1.34.2, you would put in configure.in the following statement:<br>
718 <blockquote><code>AM_PATH_GEDCOM_PARSER(1.34.2)</code><br>
719 </blockquote>Note that version numbers now contains three parts (since version
720 0.20.0: this is also the first version in which this macro is available).<br>
721 <br>
722 The macro also sets the variables <code>GEDCOM_CFLAGS</code> and <code>GEDCOM_LIBS</code> for use in Makefiles. &nbsp;Typically, this would be done as follows in a Makefile.am:<br>
723 <blockquote><code>bin_programs &nbsp; = myprg</code><br>
724   <code>myprg_SOURCES &nbsp;= myprg.c foo.c bar.c<br>
725 INCLUDES &nbsp; &nbsp; &nbsp; = @GEDCOM_CFLAGS@<br>
726 LDADD &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= @GEDCOM_LIBS@</code></blockquote>
727 If your program uses some extra modules, they can be passed as fourth argument
728 in the macro, so that the CFLAGS and LIBS are correctly filled in. &nbsp;Currently,
729 the only available module is <code>gom</code> (the Gedcom object model). &nbsp;For example:<br>
730 <blockquote><code>AM_PATH_GEDCOM_PARSER(0.21.2, , ,gom)</code><br>
731 </blockquote>
732 To be able to use this macro in the sources of your application, you have three options:<br>
733 <ul>
734   <li>Put the file <code>m4/gedcom.m4</code> in your autoconf data directory (i.e. the path given by '<code>aclocal --print-ac-dir</code>', usually <code>/usr/share/aclocal</code>). &nbsp;You can do this automatically by going into the m4 subdirectory and typing '<code>make install-m4</code>'.<br>
735     <br>
736   </li>
737   <li>If you're using autoconf, but not automake, copy the contents of <code>m4/gedcom.m4</code> in the <code>aclocal.m4</code> file in your sources.<br>
738     <br>
739   </li>
740   <li>If you're using automake, copy the contents of <code>m4/gedcom.m4</code> in the <code>acinclude.m4</code> file in your sources.<br>
741   </li>
742 </ul>
743 <br>
744 There are three preprocessor symbols defined for version checks in the
745  header (but their direct use is deprecated: please use the macro above):<br>
746                                                      
747 <ul>
748                                                      <li><code>GEDCOM_PARSE_VERSION_MAJOR</code></li>
749                                                      <li><code>GEDCOM_PARSE_VERSION_MINOR</code></li>
750                                                      <li><code>GEDCOM_PARSE_VERSION</code><br>
751                                                      </li>
752                                                      
753 </ul>
754    The last one is equal to <code>(GEDCOM_PARSE_VERSION_MAJOR * 1000) + GEDCOM_PARSE_VERSION_MINOR.</code> As you see, this only checked the major and minor version, not the patch number, so this is obsolete.<br>
755 <br>
756 <h3>Compilation and linking flags</h3>
757 Similar to other libraries, the GEDCOM parse library installs a script <code>gedcom-config</code> to help with compilation and linking flags for programs that don't use autoconf/automake.<br>
758 <br>
759 To get compilation flags for your program, use (depending on whether you
760 only use the callback parser, or also the GEDCOM object model):
761 <blockquote><code>gedcom-config --cflags<br>
762 gedcom-config --cflags gom</code><br>
763 </blockquote>
764 Similarly, to get linking flags, use one of the following:
765 <blockquote><code>gedcom-config --libs<br>
766 gedcom-config --libs gom</code><br>
767 </blockquote>
768
769
770      
771 <hr width="100%" size="2">                                              
772                                        
773 <pre><font size="-1">$Id$<br>$Name$</font><br></pre>
774                                                                         
775                   
776 <pre>                    </pre>
777                                                                         
778                                                         
779 <br>
780 <br>
781 <br>
782 <br>
783 <br>
784 <br>
785 <br>
786 <br>
787 <br>
788 <br>
789 <br>
790 <br>
791 </body></html>