Only try to delete address if present.
[gedcom-parse.git] / doc / gom.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>Gedcom object model in C</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">Gedcom object model in C</h1>
7          <br>
8                  
9 <h2>Index</h2>
10                
11 <ul>
12           <li><a href="#Main_functions">Main functions</a></li>
13
14     <li><a href="#Object_model_structure">Object model structure</a></li>
15   <ul>
16     <li><a href="#Object_lists">Object lists</a><br>
17     </li>
18   </ul>
19
20
21     
22   <ul>
23     <li><a href="#User_data">User data</a></li>
24   </ul>
25   <li><a href="#Other_functions">Modifying the object model</a></li>
26   <ul>
27     <li><a href="#Manipulating_strings">Manipulating strings</a></li>
28   </ul><li><a href="#Writing_the_object_model_to_file">Writing the object model to file</a><br>
29 <br>
30     </li>
31
32
33
34   
35
36          <li><a href="gomxref.html">C object model details</a><br>
37             </li>
38
39                
40 </ul>
41                
42 <hr width="100%" size="2">         
43
44 <h2><a name="Main_functions"></a>Main functions<br>
45
46 </h2>
47 There are two ways to start with a GEDCOM object model (after having called <code>gedcom_init</code>): either by starting from scratch, or by starting from a given GEDCOM file. &nbsp;This is done via the following two functions:<br>
48 <blockquote><code>int <b>gom_parse_file</b> (const char* file_name);<br>
49   </code>
50   <blockquote>This initializes the object model by parsing the GEDCOM file given by <code>file_name</code>. &nbsp;It returns 0 on success and 1 on failure.<br>
51   </blockquote>
52 </blockquote>
53 <blockquote><code>int <b>gom_new_model</b> ();<br>
54   </code>
55   <blockquote>This starts an empty model. &nbsp;Actually, this is done by processing the file "<code>new.ged</code>" in the gedcom-parse data directory.<br>
56   </blockquote>
57 </blockquote>
58 In the GEDCOM object model, all the data is immediately available after calling <code>gom_parse_file()</code> or <code>gom_new_model()</code>. &nbsp;For this, an entire model based on C structs is used. &nbsp;These structs are documented <a href="file:///home/verthezp/src/external/gedcom-parse/doc/gomxref.html">here</a>,
59 and follow the GEDCOM syntax quite closely. &nbsp;Each of the records in
60 a GEDCOM file are modelled by a separate struct, and some common sub-structures
61 have their own struct definition.<br>
62 <br>
63
64 The following functions are available to get at these structs:<br>
65 <ul>
66   <li>First, there are two functions to get the header record and the submission
67 record (there can be only one of them in a GEDCOM file):<br>
68     <blockquote><code>struct header* &nbsp; &nbsp; &nbsp;<b>gom_get_header</b>();<br>
69 struct submission* &nbsp;<b>gom_get_submission</b>();<br>
70       </code></blockquote>
71   </li>
72   <li>Further, for each of the other records, there are two functions, one
73 to get the first of such records, and one to get a record via its cross-reference
74 tag in the GEDCOM file:<br>
75     <blockquote><code>struct XXX* &nbsp; <b>gom_get_first_XXX</b>();<br>
76 struct XXX* &nbsp; <b>gom_get_XXX_by_xref</b>(char* xref);</code><br>
77     </blockquote>
78   </li>
79 </ul>
80 <blockquote>The <code><b>XXX</b></code> stands for one of the following: <code><b>family</b>, </code><code><b>individual</b>, <b>multimedia</b>, <b>note</b>, <b>repository</b>, <b>source</b>, <b>submitter</b>, <b>user_rec</b></code>.<br>
81 </blockquote>
82 <hr width="100%" size="2">
83 <h2><a name="Object_model_structure"></a>Object model structure<br>
84
85 </h2>
86 <h3><a name="Object_lists"></a>Object lists<br>
87 </h3>
88 All records of a certain type are linked together in a linked list. &nbsp;The
89 above functions only give access to the first record of each linked list.
90 &nbsp;The others can be accessed by traversing the linked list via the <code>next</code> member of the structs. &nbsp;This means that e.g. the following piece of code will traverse the linked list of family records:<br>
91 <blockquote><code>struct family* fam;<br>
92   <br>
93 for (fam = gom_get_first_family() ; fam ; fam = fam-&gt;next) {<br>
94 &nbsp; ...<br>
95 }</code><br>
96 </blockquote>
97 The <code>next</code> member of the last element in the list is guaranteed to have the <code>NULL</code> value.<br>
98 <br>
99 Actually, the linked list is a doubly-linked list: each record also has a <code>previous</code> member. &nbsp;But for implementation reasons the behaviour of this <code>previous</code> member on the edges of the linked list will not be guaranteed, i.e. it can be circular or terminated with <code>NULL</code>, no assumptions can be made in the application code.<br>
100 <br>
101 This linked-list model applies also to all sub-structures of the main record structs, i.e. each struct that has a <code>next </code>and <code>previous</code>
102 member follows the above conventions. &nbsp;This means that the following
103 piece of code traverses all children of a family (see the details of the
104 different structs <a href="gomxref.html">here</a>):<br>
105 <blockquote><code>struct family* fam = ...;<br>
106   <br>
107 struct xref_list* xrl;<br>
108 for (xrl = fam-&gt;children ; xrl ; xrl = xrl-&gt;next) {<br>
109 &nbsp; ...<br>
110 }</code> <br>
111 </blockquote>
112 Note that all character strings in the object model are encoded in UTF-8 (<a href="file:///home/verthezp/src/external/gedcom-parse/doc/encoding.html">Why UTF-8?</a>), but see <a href="#Manipulating_strings">below</a> for how to convert these automatically.<br>
113 <h3><a name="User_data"></a>User data</h3>
114
115
116 Each of the structs has an extra member called <code>extra</code> (of type <code>struct user_data*</code>).
117 &nbsp;This gathers all non-standard GEDCOM tags within the scope of the struct
118 in a flat linked list, no matter what the internal structure of the non-standard
119 tags is. &nbsp;Each element of the linked list has:<br>
120 <ul>
121   <li>a level: the level number in the GEDCOM file</li>
122   <li>a tag: the tag given in the GEDCOM file</li>
123   <li>a value: the value, which can be a string value or a cross-reference value (one of the two will be non-NULL)<br>
124   </li>
125 </ul>
126 This way, none of the information in the GEDCOM file is lost, even the non-standard information.<br>
127 <br>
128 <hr width="100%" size="2">
129 <h2><a name="Other_functions"></a>Modifying the object model</h2>Currently, there are only functions available to modify strings in the model (and the date manipulation functions described <a href="interface.html#date_value">here</a>).
130 &nbsp;In principle it is possible to add new records, notes, ... yourself,
131 but you have to know what you are doing, because you should keep the model
132 consistent. &nbsp;In the next release, functions will be available to add,
133 remove and modify anything in the model.<br>
134
135 <h3><a name="Manipulating_strings"></a>Manipulating strings<br>
136 </h3>
137 There are some functions available to retrieve and change strings in the
138 Gedcom object model, depending whether you use UTF-8 strings in your application
139 or locale-defined strings.<br>
140 <br>
141 The following functions retrieve and set the string in UTF-8 encoding:<br>
142 <blockquote><code>char* <b>gom_get_string</b> (char* data);<br>
143 char* <b>gom_set_string</b> (char** data, const char* str_in_utf8);</code><br>
144 </blockquote>
145 The first function is in fact superfluous, because it just returns the <code>data</code>, but it is there for symmetry with the functions given below for the locale-defined input and output. &nbsp;<br>
146 <br>
147 The second function returns the new value if successful, or <code>NULL</code>
148 if an error occurred (e.g. failure to allocate memory or the given string is not a valid UTF-8 string). &nbsp;It makes a
149 copy of the input string to store it in the object model. &nbsp;It also takes
150 care of deallocating the old value of the data if needed. &nbsp;Note that
151 the set function needs the address of the data variable, to be able to modify
152 it. &nbsp;In the case of an error, the target data variable is not modified.<br>
153 <br>
154 Examples of use of these strings would be, e.g. for retrieving and setting the system ID in the header:<br>
155 <blockquote><code>struct header* head = gom_get_header();</code><code></code><br>
156   <code>char* oldvalue = gom_get_string(head-&gt;source.id);<br>
157 char* newvalue = "My_Gedcom_Tool";<br>
158   </code><br>
159   <code>if (gom_set_string(&amp;head-&gt;source.id, newvalue)) {<br>
160 &nbsp; printf("Modified system id from %s to %s\n", oldvalue, newvalue);<br>
161 }</code><br>
162 </blockquote>
163 <br>
164 A second couple of functions retrieve and set the string in the format defined by the current locale:<br>
165 <blockquote><code>char* <b>gom_get_string_for_locale</b> (char* data, int* conversion_failures);<br>
166 char* <b>gom_set_string_for_locale</b> (char** data, const char* str_in_locale)</code>;<br>
167 </blockquote>
168 The use of these functions is the same as the previous ones, but e.g. in
169 the "en_US" locale the string will be returned by the first function in the
170 ISO-8859-1 encoding and the second function expects the <code>str_in_locale</code> to be in this encoding. &nbsp;Conversion to and from UTF-8 for the object model is done on the fly.<br>
171 <br>
172 Since the conversion from UTF-8 to the locale encoding is not always possible,
173 the get function has a second parameter that can return the number of conversion
174 failures for the result string. &nbsp;Pass a pointer to an integer if you
175 want to know this. &nbsp;You can pass <code>NULL</code> if you're not interested. &nbsp;The function returns <code>NULL</code>
176 if an error occurred (e.g. if the given string is not a valid string for
177 the current locale); in that case the target data variable is not modified.<br>
178 <hr width="100%" size="2">
179 <h2><a name="Writing_the_object_model_to_file"></a>Writing the object model to file<br>
180 </h2>
181 Writing the current object model to a file is simply done using the following function:<br>
182 <blockquote><code>int <b>gom_write_file</b> (const char* filename, int* total_conv_fails);<br></code></blockquote>
183 This writes the model to the file <code>filename</code>. &nbsp;The second parameter can return the total number of conversion failures (pass&nbsp;<code>NULL</code><code></code> if you're not interested). &nbsp;The functions in <a href="usage.html#Controlling_some_settings">this section</a> can be used before <code>gom_write_file</code> to control some settings.<br>
184 <br>
185 Before you write the file, you can update the timestamp in the header using the following function:<br>
186 <blockquote><code>int <b>gom_header_update_timestamp</b> (time_t tval);<br></code></blockquote>
187 This sets the <code>date</code> and <code>time</code> fields of the header to the time indicated by <code>tval</code>.
188 &nbsp;The function returns 0 on success, non-zero if an error occurred. &nbsp;Typically,
189 the function would be used as follows, to set the current time in the timestamp:<br>
190 <blockquote><code>int result;<br>
191 result = gom_header_update_timestamp(time(NULL));</code><br>
192 </blockquote>
193 <hr width="100%" size="2"><br>
194
195 <pre><font size="-1">$Id$<br>$Name$</font><br></pre>
196
197                                                                         
198                   
199 <pre>                    </pre>
200                                                                         
201                                                         
202 <br>
203 <br>
204 <br>
205 <br>
206 <br>
207 <br>
208 <br>
209 <br>
210 <br>
211 </body></html>