X-Git-Url: https://git.dlugolecki.net.pl/?a=blobdiff_plain;f=doc%2Fusage.html;h=ee3e248bae1331b1f9d175c2320c416ef024881d;hb=b0398a40626f3e1e91ce2d1e0fd64b5c2b5d9460;hp=c50bd4714bb91eeb416109fb878da995fd8f922d;hpb=3e8ff940702b6de227339021fac2edbf81c47702;p=gedcom-parse.git diff --git a/doc/usage.html b/doc/usage.html index c50bd47..ee3e248 100644 --- a/doc/usage.html +++ b/doc/usage.html @@ -1,13 +1,7 @@ - - - - Using the GEDCOM parser library +Using the GEDCOM parser library + - - - +

Using the GEDCOM parser library


@@ -23,7 +17,14 @@
  • Start and end callbacks
  • Default callbacks
  • +
  • C object model
  • + +
  • Other API functions
  • Converting character sets
  • Support for configure.in
    +
  • -
  • Interface details
    +
  • Interface details of the callback parser
  • C object model details
  • +

    Overview
    -

    - The GEDCOM parser library is built as a callback-based parser (comparable - to the SAX interface of XML).  It comes with:
    + The GEDCOM +parser library provides two interfaces.  At the one hand, it can be +used as a callback-based parser (comparable to the SAX interface of +XML); at the other hand, the parser can be used to convert the GEDCOM file +into an object model (comparable to the DOM interface of XML).  It comes +with:
    Additionally, if you want to use the GEDCOM C object model, the following should be used (note that libgedcom.so is also needed in this case, because the object model uses the callback parser internally):
    + + Next to these, there is also a data directory in $PREFIX/share/gedcom-parse that contains some additional stuff, but which is not immediately important at first.  I'll leave the description of the data directory for later.

    - The very simplest call of the gedcom parser is simply the following - piece of code (include of the gedcom header is assumed, as everywhere + The very simplest call of the gedcom callback parser is simply the following + piece of code (include of the gedcom.h header is assumed, as everywhere in this manual):
    int result;
    @@ -75,32 +86,42 @@ in this manual):
    is parse the entire file and return the result.  The function returns 0 on success and 1 on failure.  No other information is available using this function only.
    -
    - The call to gedcom_init() should be one of the first calls +
    +Alternatively, programs using the C object model should use the following (in this case, the inclusion of both gedcom.h and gom.h is required):
    + +
    int result;
    + ...
    + gedcom_init();
    + ...
    + result = gom_parse_file("myfamily.ged");
    +
    +The call to gom_parse_file will build the C object model, which is then a complete representation of the GEDCOM file.
    +
    +No matter which of the interfaces you use, the call to gedcom_init() should be one of the first calls in your program.  The requirement is that it should come before the first call to iconv_open (part of the generic character set conversion feature) in the program, either by your program itself, or indirectly by the library calls it makes.  Practically, it should e.g. come before any calls to any GTK functions, because GTK uses iconv_open - in its initialization.  For the same reason it is also advised to put -the -lgedcom option on the linking of the program as the last -option, so that its initialization code is run first.
    + in its initialization.

    +For the same reason it is also advised to put +the -lgedcom option +on the linking of the program as the last option, so that its initialization +code is run first.  In the case of using the C object model, the linking +options should be: -lgedcom_gom -lgedcom

    The next sections will refine this piece of code to be able to have meaningful errors and the actual data that is in the file.

    -

    Error handling

    - Since this is a relatively simple topic, it is discussed before the - actual callback mechanism, although it also uses a callback...
    -
    - The library can be used in several different circumstances, both +

    Error handling

    The library can be used in several different circumstances, both terminal-based as GUI-based.  Therefore, it leaves the actual display of the error message up to the application.  For this, the application needs to register a callback before parsing the GEDCOM file, which will be called by the library on errors, warnings and messages.

    - A typical piece of code would be:
    + A typical piece of code would be (gom_parse_file would be called in case the C object model is used):
    void my_message_handler (Gedcom_msg_type type, char *msg)
    @@ -133,8 +154,7 @@ way it wants.  Warnings are similar, but use "Warning" instead of "Error"

    Data callback mechanism

    The most important use of the parser is of course to get the data -out of the GEDCOM file.  As already mentioned, the parser uses a callback - mechanism for that.  In fact, the mechanism involves two levels.
    +out of the GEDCOM file.  This section focuses on the callback mechanism (see the next section for the C object model).  In fact, the mechanism involves two levels.

    The primary level is that each of the sections in a GEDCOM file is notified to the application code via a "start element" callback and an @@ -169,7 +189,8 @@ the information that comes via the default callback in plain textual format.
    -
    Gedcom_ctxt my_header_start_cb (int level,
    +
    Gedcom_ctxt my_header_start_cb (Gedcom_rec rec,
    +                                int level,
                                    Gedcom_val xref,
                        @@ -185,7 +206,7 @@ of a GEDCOM file.  First, have a look at the following piece of code:
    }

    - void my_header_end_cb (Gedcom_ctxt self)
    + void my_header_end_cb (Gedcom_rec rec, Gedcom_ctxt self)
    {
      printf("The header ends, context is %d\n", (int)self);   /* context will print as "1" */
    @@ -203,7 +224,7 @@ callback. The end callback is optional: you can pass NULL if you are not interested in the end callback.  The identifiers to use as first argument to the function (here REC_HEAD) are described in the interface -details .
    +details .  These are also passed as first argument in the callbacks (the Gedcom_rec argument).

    From the name of the function it becomes clear that this function is specific to complete records.  For the separate elements in records @@ -242,7 +263,9 @@ and included via gedcom.h (so no need to include gedcom-t We will now retrieve the SOUR field (the name of the program that wrote the file) from the header:
    -
    Gedcom_ctxt my_header_source_start_cb(Gedcom_ctxt +
    Gedcom_ctxt my_header_source_start_cb(Gedcom_elt  elt,
    +                      +                Gedcom_ctxt parent,
                                          int   @@ -265,7 +288,8 @@ wrote the file) from the header:
      return parent;
    }

    - void my_header_source_end_cb(Gedcom_ctxt parent,
    + void my_header_source_end_cb(Gedcom_elt  elt,
    +                             Gedcom_ctxt parent,
                                 Gedcom_ctxt self,
                        @@ -291,8 +315,7 @@ wrote the file) from the header:
    can be its own context object of course.  The end callback is called with both the context of the parent and the context of itself, which in this example will be the same.  Again, the list of identifiers to use as -a first argument for the subscription function are detailed in the interface details .
    +a first argument for the subscription function are detailed in the interface details .  Again, these are passed as first argument in the callback (the Gedcom_elt argument).

    If we look at the other arguments of the start callback, we see the level number (the initial number of the line in the GEDCOM file), the tag @@ -305,8 +328,7 @@ in the section for record callbacks above.
    The Gedcom_val type is meant to be an opaque type.  The only thing that needs to be known about it is that it can contain specific data types, which have to be retrieved from it using pre-defined macros. -  These data types are described in the interface details. +  These data types are described in the interface details.

    Some extra notes:
    @@ -334,7 +356,7 @@ in the section for record callbacks above.
    applications.  To preserve this extra data anyway, a default callback can be registered by the application, as in the following example:
    -
    void my_default_cb (Gedcom_ctxt parent, int level, +
    void my_default_cb (Gedcom_elt elt, Gedcom_ctxt parent, int level, char* tag, char* raw_value, int parsed_tag)
    {
      ...
    @@ -365,7 +387,7 @@ data is saved again in a GEDCOM file.  To make it more specific, consider   char* extra_text;
    };

    - Gedcom_ctxt my_header_start_cb(int level, Gedcom_val xref, char* tag, + Gedcom_ctxt my_header_start_cb(Gedcom_rec rec, int level, Gedcom_val xref, char* tag, char *raw_value,
                                   int parsed_tag, Gedcom_val parsed_value)
    @@ -374,7 +396,7 @@ data is saved again in a GEDCOM file.  To make it more specific, consider   return (Gedcom_ctxt)head;
    }

    - void my_default_cb(Gedcom_ctxt parent, int level, char* tag, char* + void my_default_cb(Gedcom_elt elt, Gedcom_ctxt parent, int level, char* tag, char* raw_value, int parsed_tag)
    {
      struct header head = (struct header)parent;
    @@ -397,11 +419,78 @@ raw_value, int parsed_tag)
    of the "upper" tags has been subscribed upon.
    +

    +

    C object model

    +In the GEDCOM object model, all the data is immediately available after calling gom_parse_file().  For this, an entire model based on C structs is used.  These structs are documented here, +and follow the GEDCOM syntax quite closely.  Each of the records in +a GEDCOM file are modelled by a separate struct, and some common sub-structures +have their own struct definition.
    +
    +

    Main functions
    +

    +The following functions are available to get at these structs:
    +
      +
    • First, there are two functions to get the header record and the submission +record (there can be only one of them in a GEDCOM file):
      +
      struct header*      gom_get_header();
      +struct submission*  gom_get_submission();
      +
      +
    • +
    • Further, for each of the other records, there are two functions, one +to get the first of such records, and one to get a record via its cross-reference +tag in the GEDCOM file:
      +
      struct XXX*   gom_get_first_XXX();
      +struct XXX*   gom_get_XXX_by_xref(char* xref);

      +
      +
    • +
    +
    The XXX stands for one of the following: family, individual, multimedia, note, repository, source, submitter, user_rec.
    +
    +

    Object model structure
    +

    +All records of a certain type are linked together in a linked list.  The +above functions only give access to the first record of each linked list. + The others can be accessed by traversing the linked list via the next member of the structs.  This means that e.g. the following piece of code will traverse the linked list of family records:
    +
    struct family* fam;
    +
    +for (fam = gom_get_first_family() ; fam ; fam = fam->next) {
    +  ...
    +}

    +
    +The next member of the last element in the list is guaranteed to have the NULL value.
    +
    +Actually, the linked list is a doubly-linked list: each record also has a previous member.  But for implementation reasons the behaviour of this previous member on the edges of the linked list will not be guaranteed, i.e. it can be circular or terminated with NULL, no assumptions can be made in the application code.
    +
    +This linked-list model applies also to all sub-structures of the main record structs, i.e. each struct that has a next and previous +member following the above conventions.  This means that the following +piece of code traverses all children of a family (see the details of the +different structs here):
    +
    struct family* fam = ...;
    +
    +struct xref_list* xrl;
    +for (xrl = fam->children ; xrl ; xrl = xrl->next) {
    +  ...
    +}

    +
    +Note that all character strings in the object model are encoded in UTF-8 (Why UTF-8?).
    +

    User data

    +Each of the structs has an extra member called extra (of type struct user_data*). + This gathers all non-standard GEDCOM tags within the scope of the struct +in a flat linked list, no matter what the internal structure of the non-standard +tags is.  Each element of the linked list has:
    +
      +
    • a level: the level number in the GEDCOM file
    • +
    • a tag: the tag given in the GEDCOM file
    • +
    • a value: the value, which can be a string value or a cross-reference value (one of the two will be non-NULL)
      +
    • +
    +This way, none of the information in the GEDCOM file is lost, even the non-standard information.

    Other API functions

    - Although the above describes the basic interface of libgedcom, there + + Although the above describes the basic interface of the gedcom parser, there are some other functions that allow to customize the behaviour of the library.  These will be explained in the current section.
    @@ -538,8 +627,7 @@ because any locale can be converted to UTF-8.
    implementation, the following steps need to be taken by the application (more detailed info can be found in the info file of the GNU libc library in the "Generic Charset Conversion" section under "Character Set Handling" -or online +or online here):
      @@ -626,8 +714,7 @@ and show an error.  This is not very user friendly, of course.
    • Skip over the character that can't be converted and append a "?" to the output buffer, then call iconv again.  Skipping over a UTF-8 character is fairly simple, - as follows from the encoding rules + as follows from the encoding rules :
    @@ -717,5 +804,6 @@ handle needs to be closed (when the program exits):
                        
    - - +
    +
    + \ No newline at end of file