Start of documentation in doxygen.
[gedcom-parse.git] / include / gedcom.h.in
1 /* External header for the Gedcom parser library.
2    Copyright (C) 2001,2002 The Genes Development Team
3    This file is part of the Gedcom parser library.
4    Contributed by Peter Verthez <Peter.Verthez@advalvas.be>, 2001.
5
6    The Gedcom parser library is free software; you can redistribute it
7    and/or modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The Gedcom parser library is distributed in the hope that it will be
12    useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the Gedcom parser library; if not, write to the
18    Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 /* $Id$ */
22 /* $Name$ */
23
24 #ifndef __GEDCOM_H
25 #define __GEDCOM_H
26
27 #include <stdio.h>
28 #include <stdarg.h>
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 #ifndef GEDCOM_INTERNAL
35 #include <gedcom-tags.h>
36 #endif
37
38 #define GEDCOM_PARSE_VERSION_MAJOR @VERSION_MAJOR@
39 #define GEDCOM_PARSE_VERSION_MINOR @VERSION_MINOR@
40 #define GEDCOM_PARSE_VERSION_PATCH @VERSION_PATCH@
41 #define GEDCOM_PARSE_VERSION \
42   (GEDCOM_PARSE_VERSION_MAJOR * 1000 + GEDCOM_PARSE_VERSION_MINOR)
43
44 int gedcom_check_version(int major, int minor, int patch);
45
46 /**************************************************************************/
47 /***  First the records and elements to subscribe upon                  ***/
48 /**************************************************************************/
49                                                                            
50 typedef enum Gedcom_rec {
51   REC_HEAD,
52   REC_FAM,
53   REC_INDI,
54   REC_OBJE,
55   REC_NOTE,
56   REC_REPO,
57   REC_SOUR,
58   REC_SUBN,
59   REC_SUBM,
60   REC_USER,
61   NR_OF_RECS     /* Just a final value to be used in array boundaries */
62 } Gedcom_rec;
63
64 typedef enum Gedcom_elt {
65   ELT_HEAD_SOUR = NR_OF_RECS,
66   ELT_HEAD_SOUR_VERS,
67   ELT_HEAD_SOUR_NAME,
68   ELT_HEAD_SOUR_CORP,
69   ELT_HEAD_SOUR_DATA,
70   ELT_HEAD_SOUR_DATA_DATE,
71   ELT_HEAD_SOUR_DATA_COPR,
72   ELT_HEAD_DEST,
73   ELT_HEAD_DATE,
74   ELT_HEAD_DATE_TIME,
75   ELT_HEAD_SUBM,
76   ELT_HEAD_SUBN,
77   ELT_HEAD_FILE,
78   ELT_HEAD_COPR,
79   ELT_HEAD_GEDC,
80   ELT_HEAD_GEDC_VERS,
81   ELT_HEAD_GEDC_FORM,
82   ELT_HEAD_CHAR,
83   ELT_HEAD_CHAR_VERS,
84   ELT_HEAD_LANG,
85   ELT_HEAD_PLAC,
86   ELT_HEAD_PLAC_FORM,
87   ELT_HEAD_NOTE,
88   
89   ELT_FAM_HUSB,
90   ELT_FAM_WIFE,
91   ELT_FAM_CHIL,
92   ELT_FAM_NCHI,
93   ELT_FAM_SUBM,
94   
95   ELT_INDI_RESN,
96   ELT_INDI_SEX,
97   ELT_INDI_SUBM,
98   ELT_INDI_ALIA,
99   ELT_INDI_ANCI,
100   ELT_INDI_DESI,
101   ELT_INDI_RFN,
102   ELT_INDI_AFN,
103   
104   ELT_OBJE_FORM,
105   ELT_OBJE_TITL,
106   ELT_OBJE_BLOB,
107   ELT_OBJE_BLOB_CONT,
108   ELT_OBJE_OBJE,
109   
110   ELT_REPO_NAME,
111
112   ELT_SOUR_DATA,
113   ELT_SOUR_DATA_EVEN,
114   ELT_SOUR_DATA_EVEN_DATE,
115   ELT_SOUR_DATA_EVEN_PLAC,
116   ELT_SOUR_DATA_AGNC,
117   ELT_SOUR_AUTH,
118   ELT_SOUR_TITL,
119   ELT_SOUR_ABBR,
120   ELT_SOUR_PUBL,
121   ELT_SOUR_TEXT,
122
123   ELT_SUBN_SUBM,
124   ELT_SUBN_FAMF,
125   ELT_SUBN_TEMP,
126   ELT_SUBN_ANCE,
127   ELT_SUBN_DESC,
128   ELT_SUBN_ORDI,
129   ELT_SUBN_RIN,
130
131   ELT_SUBM_NAME,
132   ELT_SUBM_LANG,
133   ELT_SUBM_RFN,
134   ELT_SUBM_RIN,
135
136   ELT_SUB_ADDR,
137   ELT_SUB_ADDR_CONT,
138   ELT_SUB_ADDR_ADR1,
139   ELT_SUB_ADDR_ADR2,
140   ELT_SUB_ADDR_CITY,
141   ELT_SUB_ADDR_STAE,
142   ELT_SUB_ADDR_POST,
143   ELT_SUB_ADDR_CTRY,
144   
145   ELT_SUB_PHON,
146
147   ELT_SUB_ASSO,
148   ELT_SUB_ASSO_TYPE,
149   ELT_SUB_ASSO_RELA,
150
151   ELT_SUB_CHAN,
152   ELT_SUB_CHAN_DATE,
153   ELT_SUB_CHAN_TIME,
154
155   ELT_SUB_FAMC,
156   ELT_SUB_FAMC_PEDI,
157
158   ELT_SUB_CONT,
159   ELT_SUB_CONC,
160
161   ELT_SUB_EVT_TYPE,
162   ELT_SUB_EVT_DATE,
163   ELT_SUB_EVT_AGE,
164   ELT_SUB_EVT_AGNC,
165   ELT_SUB_EVT_CAUS,
166
167   ELT_SUB_FAM_EVT,
168   ELT_SUB_FAM_EVT_HUSB,
169   ELT_SUB_FAM_EVT_WIFE,
170   ELT_SUB_FAM_EVT_AGE,
171   ELT_SUB_FAM_EVT_EVEN,
172
173   ELT_SUB_IDENT_REFN,
174   ELT_SUB_IDENT_REFN_TYPE,
175   ELT_SUB_IDENT_RIN,
176
177   ELT_SUB_INDIV_ATTR,
178   ELT_SUB_INDIV_RESI,
179   ELT_SUB_INDIV_BIRT,
180   ELT_SUB_INDIV_BIRT_FAMC,
181   ELT_SUB_INDIV_GEN,
182   ELT_SUB_INDIV_ADOP,
183   ELT_SUB_INDIV_ADOP_FAMC,
184   ELT_SUB_INDIV_ADOP_FAMC_ADOP,
185   ELT_SUB_INDIV_EVEN,
186
187   ELT_SUB_LIO_BAPL,
188   ELT_SUB_LIO_BAPL_STAT,
189   ELT_SUB_LIO_BAPL_DATE,
190   ELT_SUB_LIO_BAPL_TEMP,
191   ELT_SUB_LIO_BAPL_PLAC,
192   ELT_SUB_LIO_SLGC,
193   ELT_SUB_LIO_SLGC_FAMC,
194
195   ELT_SUB_LSS_SLGS,
196   ELT_SUB_LSS_SLGS_STAT,
197   ELT_SUB_LSS_SLGS_DATE,
198   ELT_SUB_LSS_SLGS_TEMP,
199   ELT_SUB_LSS_SLGS_PLAC,
200
201   ELT_SUB_MULTIM_OBJE,
202   ELT_SUB_MULTIM_OBJE_FORM,
203   ELT_SUB_MULTIM_OBJE_TITL,
204   ELT_SUB_MULTIM_OBJE_FILE,
205
206   ELT_SUB_NOTE,
207
208   ELT_SUB_PERS_NAME,
209   ELT_SUB_PERS_NAME_NPFX,
210   ELT_SUB_PERS_NAME_GIVN,
211   ELT_SUB_PERS_NAME_NICK,
212   ELT_SUB_PERS_NAME_SPFX,
213   ELT_SUB_PERS_NAME_SURN,
214   ELT_SUB_PERS_NAME_NSFX,
215
216   ELT_SUB_PLAC,
217   ELT_SUB_PLAC_FORM,
218
219   ELT_SUB_SOUR,
220   ELT_SUB_SOUR_PAGE,
221   ELT_SUB_SOUR_EVEN,
222   ELT_SUB_SOUR_EVEN_ROLE,
223   ELT_SUB_SOUR_DATA,
224   ELT_SUB_SOUR_DATA_DATE,
225   ELT_SUB_SOUR_TEXT,
226   ELT_SUB_SOUR_QUAY,
227
228   ELT_SUB_REPO,
229   ELT_SUB_REPO_CALN,
230   ELT_SUB_REPO_CALN_MEDI,
231
232   ELT_SUB_FAMS,
233   
234   ELT_USER,
235   
236   NR_OF_ELTS     /* Just a final value to be used in array boundaries */
237 } Gedcom_elt;
238
239 /**************************************************************************/
240 /***  Definition of some auxiliary types                                ***/
241 /**************************************************************************/
242                                                                            
243 typedef enum Gedcom_err_mech {
244   IMMED_FAIL,
245   DEFER_FAIL,
246   IGNORE_ERRORS
247 } Gedcom_err_mech;
248
249   /** \brief Message type in message handler callbacks
250       \ingroup error */
251 typedef enum Gedcom_msg_type {
252   ERROR,
253   WARNING,
254   MESSAGE
255 } Gedcom_msg_type;
256
257 typedef enum Date_type {
258   DATE_UNRECOGNIZED,   /* Neither jday1 as jday2 are significant */
259   DATE_EXACT,          /* Only jday1 is significant */
260   DATE_BOUNDED         /* Both jday1 and jday2 are significant */
261 } Date_type;
262
263 typedef enum Calendar_type {
264   CAL_GREGORIAN,
265   CAL_JULIAN,
266   CAL_HEBREW,
267   CAL_FRENCH_REV,
268   CAL_UNKNOWN
269 } Calendar_type;
270
271 typedef enum Year_type {
272   YEAR_SINGLE,
273   YEAR_DOUBLE     /* In this case, the 'year' indicates the last value */
274 } Year_type;
275
276 typedef enum Date_value_type {
277   /* Simple date */
278   DV_NO_MODIFIER,
279   /* Range values */
280   DV_BEFORE,
281   DV_AFTER,
282   DV_BETWEEN,       /* Two dates are given */
283   /* Period values */
284   DV_FROM,
285   DV_TO,
286   DV_FROM_TO,       /* Two dates are given */
287   /* Approx values */
288   DV_ABOUT,
289   DV_CALCULATED,
290   DV_ESTIMATED,
291   /* Other */
292   DV_INTERPRETED,   /* One date and a phrase is given */
293   DV_PHRASE         /* Only phrase is given */
294 } Date_value_type;
295
296 /* All Unicode characters between U+0000 and U+FFFF can be encoded in
297    UTF-8 with 3 or less bytes */
298 #define UTF_FACTOR 3
299
300 #define MAX_DAY_LEN    2
301 #define MAX_MONTH_LEN  4
302 #define MAX_YEAR_LEN   7
303 #define MAX_PHRASE_LEN 35 * UTF_FACTOR
304
305 struct date {
306   Calendar_type cal;
307   char day_str[MAX_DAY_LEN + 1];
308   char month_str[MAX_MONTH_LEN + 1];
309   char year_str[MAX_YEAR_LEN + 1];
310   int day;    /* starts at 1 */
311   int month;  /* starts at 1 */
312   int year;   /* the highest value for double years */
313   Year_type year_type;
314   Date_type type;
315   long int sdn1;
316   long int sdn2;
317 };
318
319 struct date_value {
320   Date_value_type type;
321   struct date date1;
322   struct date date2;
323   char phrase[MAX_PHRASE_LEN + 1];
324 };
325
326 /* Type for context handling, meant to be opaque */
327 typedef void* Gedcom_ctxt;
328
329 typedef enum Xref_type {
330   XREF_NONE,
331   XREF_FAM,
332   XREF_INDI,
333   XREF_NOTE,
334   XREF_OBJE,
335   XREF_REPO,
336   XREF_SOUR,
337   XREF_SUBM,
338   XREF_SUBN,
339   XREF_USER,
340   XREF_ANY
341 } Xref_type;
342
343 struct xref_value {
344   Xref_type type;
345   char *string;
346   Gedcom_ctxt object;
347 };
348
349 typedef enum Age_type {
350   AGE_UNRECOGNIZED,
351   AGE_CHILD,
352   AGE_INFANT,
353   AGE_STILLBORN,
354   AGE_NUMERIC
355 } Age_type;
356
357 typedef enum Age_modifier {
358   AGE_NO_MODIFIER,
359   AGE_LESS_THAN,
360   AGE_GREATER_THAN
361 } Age_modifier;
362
363 struct age_value {
364   Age_type type;
365   Age_modifier mod;
366   int years;
367   int months;
368   int days;
369   char phrase[MAX_PHRASE_LEN + 1];
370 };
371
372 typedef enum Encoding {
373   ONE_BYTE      = 0x00,
374   TWO_BYTE_HILO = 0x01,
375   TWO_BYTE_LOHI = 0x02
376 } Encoding;
377
378 typedef enum Enc_bom {
379   WITHOUT_BOM = 0x00,
380   WITH_BOM    = 0x10
381 } Enc_bom;
382
383 typedef enum Enc_line_end {
384   END_CR    = 0,
385   END_LF    = 1,
386   END_CR_LF = 2,
387   END_LF_CR = 3
388 } Enc_line_end;
389
390 typedef enum Enc_from {
391   ENC_FROM_FILE = 0,
392   ENC_FROM_SYS  = 1,
393   ENC_MANUAL    = 2
394 } Enc_from;
395
396 struct encoding_state;
397
398 typedef enum Date_input {
399   DI_FROM_STRINGS,
400   DI_FROM_NUMBERS,
401   DI_FROM_SDN
402 } Date_input;
403
404 typedef enum Gedcom_compat {
405   COMPAT_ALLOW_OUT_OF_CONTEXT = 0x01
406 } Gedcom_compat;
407
408 /**************************************************************************/
409 /***  Things meant to be internal, susceptible to changes               ***/
410 /***  Use the GEDCOM_STRING/GEDCOM_DATE interface instead of relying    ***/
411 /***  on this !!                                                        ***/
412 /**************************************************************************/
413
414 /* Update strings in interface.c if this changes */
415 typedef enum _GEDCOM_VAL_TYPE {
416   GV_NULL       = 0x01,
417   GV_CHAR_PTR   = 0x02,
418   GV_DATE_VALUE = 0x04,
419   GV_AGE_VALUE  = 0x08,
420   GV_XREF_PTR   = 0x10
421 } Gedcom_val_type;
422
423 union _Gedcom_val_union {
424   char* string_val;
425   struct date_value date_val;
426   struct age_value age_val;
427   struct xref_value *xref_val;
428 };
429
430 typedef struct _Gedcom_val_struct {
431   Gedcom_val_type type;
432   union _Gedcom_val_union value;
433 } Gedcom_val_struct;
434
435 void gedcom_cast_error(const char* file, int line,
436                        Gedcom_val_type tried_type,
437                        Gedcom_val_type real_type);
438
439 extern struct date_value def_date_val;
440 extern struct age_value  def_age_val;
441 extern struct xref_value def_xref_val;
442
443 #define GV_CHECK_CAST(VAL, TYPE, MEMBER, DEFVAL)                              \
444    (((VAL)->type == TYPE) ?                                                   \
445     (VAL)->value.MEMBER :                                                     \
446     (gedcom_cast_error(__FILE__,__LINE__, TYPE, (VAL)->type), DEFVAL))
447
448 #define GV_IS_TYPE(VAL, TYPE)                                                 \
449    ((VAL)->type == TYPE)
450
451 /**************************************************************************/
452 /***  Function interface                                                ***/
453 /**************************************************************************/
454
455 /* Type for parsed values, meant to be opaque */
456 typedef Gedcom_val_struct* Gedcom_val;
457
458 /* Type for write handle, meant to be opaque */
459 struct Gedcom_write_struct;
460 typedef struct Gedcom_write_struct* Gedcom_write_hndl;
461
462 /* Check to determine whether there is a parsed value or not */
463 #define GEDCOM_IS_NULL(VAL) \
464    GV_IS_TYPE(VAL, GV_NULL)
465
466 /* This returns the char* from a Gedcom_val, if appropriate */
467 /* It gives a gedcom_warning if the cast is not correct     */
468 #define GEDCOM_STRING(VAL) \
469    GV_CHECK_CAST(VAL, GV_CHAR_PTR, string_val, "<error>")
470 #define GEDCOM_IS_STRING(VAL) \
471    GV_IS_TYPE(VAL, GV_CHAR_PTR)
472
473 /* This returns the struct date_value from a Gedcom_val, if appropriate */
474 /* It gives a gedcom_warning if the cast is not correct                 */
475 #define GEDCOM_DATE(VAL) \
476    GV_CHECK_CAST(VAL, GV_DATE_VALUE, date_val, def_date_val)
477 #define GEDCOM_IS_DATE(VAL) \
478    GV_IS_TYPE(VAL, GV_DATE_VALUE)
479
480 /* This returns the struct age_value from a Gedcom_val, if appropriate  */
481 /* It gives a gedcom_warning if the cast is not correct                 */
482 #define GEDCOM_AGE(VAL) \
483    GV_CHECK_CAST(VAL, GV_AGE_VALUE, age_val, def_age_val)
484 #define GEDCOM_IS_AGE(VAL) \
485    GV_IS_TYPE(VAL, GV_AGE_VALUE)
486
487 /* This returns the (struct xref_value *) from a Gedcom_val, if appropriate */
488 /* It gives a gedcom_warning if the cast is not correct                     */
489 #define GEDCOM_XREF_PTR(VAL) \
490    GV_CHECK_CAST(VAL, GV_XREF_PTR, xref_val, &def_xref_val)
491 #define GEDCOM_IS_XREF_PTR(VAL) \
492    GV_IS_TYPE(VAL, GV_XREF_PTR)
493
494   /** \brief Message handler callback
495       \ingroup error */
496 typedef void
497         (*Gedcom_msg_handler)
498         (Gedcom_msg_type type, char *msg);
499
500 typedef Gedcom_ctxt
501         (*Gedcom_rec_start_cb)
502         (Gedcom_rec rec, int level, Gedcom_val xref, char *tag,
503          char *raw_value, int tag_value, Gedcom_val parsed_value);
504 typedef void
505         (*Gedcom_rec_end_cb)
506         (Gedcom_rec rec, Gedcom_ctxt self, Gedcom_val parsed_value);
507
508 typedef Gedcom_ctxt
509         (*Gedcom_elt_start_cb)
510         (Gedcom_elt elt, Gedcom_ctxt parent,
511          int level, char *tag, char *raw_value,
512          int tag_value, Gedcom_val parsed_value);
513 typedef void
514         (*Gedcom_elt_end_cb)
515         (Gedcom_elt elt, Gedcom_ctxt parent, Gedcom_ctxt self,
516          Gedcom_val parsed_value);
517
518 typedef void
519         (*Gedcom_def_cb)
520         (Gedcom_elt elt, Gedcom_ctxt parent, int level, char *tag,
521          char *raw_value, int tag_value);
522
523   /** \brief Initializes the Gedcom parser library
524       \ingroup main */
525 int     gedcom_init();
526   /** \brief Parses an existing Gedcom file
527       \ingroup main */
528 int     gedcom_parse_file(const char* file_name);
529   /** \brief Starts a new Gedcom model
530       \ingroup main */
531 int     gedcom_new_model();
532
533   /** \brief Sets the error handler callback
534       \ingroup error */
535 void    gedcom_set_message_handler(Gedcom_msg_handler func);
536   
537 void    gedcom_set_error_handling(Gedcom_err_mech mechanism);
538 void    gedcom_set_debug_level(int level, FILE* trace_output);
539 void    gedcom_set_compat_handling(int enable_compat);
540 void    gedcom_set_compat_options(Gedcom_compat options);
541 void    gedcom_set_default_callback(Gedcom_def_cb func);
542
543 void    gedcom_subscribe_to_record(Gedcom_rec rec,
544                                    Gedcom_rec_start_cb cb_start,
545                                    Gedcom_rec_end_cb cb_end);
546 void    gedcom_subscribe_to_element(Gedcom_elt elt,
547                                     Gedcom_elt_start_cb cb_start,
548                                     Gedcom_elt_end_cb cb_end);
549
550 /* Separate value parsing functions */
551 struct date_value  gedcom_parse_date(const char* line_value);
552 char*              gedcom_date_to_string(const struct date_value* val);
553 struct date_value* gedcom_new_date_value(const struct date_value* copy_from);
554 int   gedcom_normalize_date(Date_input compute_from, struct date_value *val);
555
556 struct age_value  gedcom_parse_age(const char* line_value);
557 struct age_value* gedcom_new_age_value(const struct age_value* copy_from);
558 char*             gedcom_age_to_string(const struct age_value* val);
559
560 /* Handling cross-references */
561 struct xref_value *gedcom_get_by_xref(const char *key);
562 struct xref_value *gedcom_add_xref(Xref_type type, const char* xrefstr,
563                                    Gedcom_ctxt object);
564 struct xref_value *gedcom_link_xref(Xref_type type, const char* xrefstr);
565 struct xref_value *gedcom_unlink_xref(Xref_type type, const char* xrefstr);
566 int                gedcom_delete_xref(const char* xrefstr);
567
568 /* Writing support */
569 Gedcom_write_hndl  gedcom_write_open(const char* filename);
570 int  gedcom_write_close(Gedcom_write_hndl hndl, int *total_conv_fails);
571 int  gedcom_write_set_encoding(Enc_from from,
572                                const char* charset, Encoding width,
573                                Enc_bom bom);
574 int  gedcom_write_set_line_terminator(Enc_from from, Enc_line_end end);
575
576 int  gedcom_write_record_str(Gedcom_write_hndl hndl,
577                              Gedcom_rec rec, const char* xrefstr,
578                              const char* val);
579   
580 int gedcom_write_element_str(Gedcom_write_hndl hndl, Gedcom_elt elt,
581                              int parsed_tag, int parent_rec_or_elt,
582                              const char* val);
583 int gedcom_write_element_xref(Gedcom_write_hndl hndl, Gedcom_elt elt,
584                               int parsed_tag, int parent_rec_or_elt,
585                               const struct xref_value* val);
586
587 int gedcom_write_element_date(Gedcom_write_hndl hndl,
588                               Gedcom_elt elt, int tag, int parent_rec_or_elt,
589                               const struct date_value* val);
590 int gedcom_write_element_age(Gedcom_write_hndl hndl,
591                              Gedcom_elt elt, int tag, int parent_rec_or_elt,
592                              const struct age_value* val);
593
594 int gedcom_write_user_str(Gedcom_write_hndl hndl, int level, const char* tag,
595                           const char* xrefstr, const char* value);
596 int gedcom_write_user_xref(Gedcom_write_hndl hndl, int level, const char* tag,
597                            const char* xrefstr, const struct xref_value* val);
598
599 /* For use in gom */
600 int        gedcom_error(const char* s, ...);
601 int        gedcom_warning(const char* s, ...);
602 int        gedcom_message(const char* s, ...);
603 int        gedcom_debug_print(const char* s, ...);
604
605 #ifdef __cplusplus
606 }
607 #endif
608
609 #endif /* __GEDCOM_H */