X-Git-Url: https://git.dlugolecki.net.pl/?a=blobdiff_plain;f=doc%2Fusage.html;h=409ebbf308878bf17f0b2726ade6faf57a8eedb3;hb=43c83792bc560e33abb7eeea683f86bbcc7627ce;hp=762a4b0a7cd6c86cca27de62a44ff714a76c3f63;hpb=1316fee80103b38bf682f5c626c0abaa525db41c;p=gedcom-parse.git diff --git a/doc/usage.html b/doc/usage.html index 762a4b0..409ebbf 100644 --- a/doc/usage.html +++ b/doc/usage.html @@ -1,11 +1,7 @@ - - - - Using the GEDCOM parser library +Using the GEDCOM parser library + - - - +

Using the GEDCOM parser library


@@ -29,7 +25,10 @@
  • Error treatment
  • Compatibility mode
  • - +
  • Converting character sets
  • Support for configure.in
    +
  • + +
  • Interface details
  • @@ -96,15 +95,14 @@ actual callback mechanism, although it also uses a callback...
    result = gedcom_parse_file("myfamily.ged");
    In the above piece of code, my_message_handler is the callback - that will be called for errors (type=ERROR), warnings ( - type=WARNING) and messages (type=MESSAGE).  The + that will be called for errors (type=ERROR), warnings (type=WARNING) and messages (type=MESSAGE).  The callback must have the signature as in the example.  For errors, the msg passed to the callback will have the format:
    Error on line <lineno>: <actual_message>
    Note that the entire string will be properly internationalized, and -encoded in UTF-8 (see "Why UTF-8?"  LINK TBD).  Also, +encoded in UTF-8 (Why UTF-8?).  Also, no newline is appended, so that the application program can use it in any way it wants.  Warnings are similar, but use "Warning" instead of "Error".  Messages are plain text, without any prefix.
    @@ -171,7 +169,7 @@ a GEDCOM file.  First, have a look at the following piece of code:

    void my_header_end_cb (Gedcom_ctxt self)
    {
    -   printf("The header ends, context is %d\n", self);   /* context +   printf("The header ends, context is %d\n", (int)self);   /* context will print as "1" */
    }

    @@ -209,7 +207,7 @@ need to have the signatures as shown in the example.
    gedcom.h (so no need to include gedcom-tags.h yourself).

    The example passes a simple integer as context, but an application could - e.g. pass a struct that will contain the information for the + e.g. pass a struct (or an object in a C++ application) that will contain the information for the header.  In the end callback, the application could then e.g. do some finalizing operations on the struct to put it in its database.

    @@ -265,11 +263,11 @@ for this)
    The subscription mechanism for elements is similar, only the signatures of the callbacks differ.  The signature for the start callback shows - that the context of the parent line (e.g. the struct that + that the context of the parent line (here e.g. the struct that describes the header) is passed to this start callback.  The callback -itself returns here the same context, but this can be its own context object +itself returns here in this example the same context, but this 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 will be the same in the example. +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 .
    @@ -280,7 +278,7 @@ level number (the initial number of the line in the GEDCOM file), the tag raw value is just the raw string that occurs as value on the line next to the tag (in UTF-8 encoding).  The parsed value is the meaningful value that is parsed from that raw string.  The parsed tag is described in - the section for record callbacks.
    + 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 @@ -293,9 +291,8 @@ level number (the initial number of the line in the GEDCOM file), the tag @@ -363,6 +360,8 @@ specifically subscribed upon by the application, and can thus be called in various contexts.  For simplicity, the example above doesn't take this into account (the parent could be of different types, depending on the context).
    +
    +Note also that the default callback is not called when the parent context is NULL.  This is e.g. the case if none of the "upper" tags has been subscribed upon.

    @@ -447,14 +446,170 @@ enabled and disabled via the following function:
    Note that, currently, no actual compatibility code is present, but this is on the to-do list.
    +
    +

    Converting character sets

    +All strings passed by the GEDCOM parser to the application are in UTF-8 encoding. + Typically, an application needs to convert this to something else to +be able to display it.
    +
    +The most common case is that the output character set is controlled by the locale mechanism (i.e. via the LANG, LC_ALL or LC_CTYPE environment variables), which also controls the gettext + mechanism in the application.  
    +
    +
    + + -
    + The source distribution of gedcom-parse contains an example implementation (utf8-locale.c and utf8-locale.h + in the top directory).   Feel free to use it in +your source code (it is not part of the library, and it isn't installed anywhere, +so you need to take over the source and header file in your application). + 
    +
    + +Its interface is:
    +
    char *convert_utf8_to_locale (char *input, int *conv_failures);
    char *convert_locale_to_utf8 (char *input);
    + +Both functions return a pointer to a static buffer that is overwritten on +each call.  To function properly, the application must first set the +locale using the setlocale function (the second step detailed below). + All other steps given below, including setting up and closing down the conversion +handles, are transparantly handled by the two functions.  
    +
    +If you pass a pointer to an integer to the first function, it will be set +to the number of conversion failures, i.e. characters that couldn't be converted; +you can also just pass NULL if you are not interested (note that usually, the interesting information is just whether there were + conversion failures or not, which is then given by the integer being bigger +than zero or not).  The second function doesn't need this, because any +locale can be converted to UTF-8.
    +
    + +You can change the "?" that is output for characters that can't be converted +to any string you want, using the following function before the conversion +calls:
    +
    void convert_set_unknown (const char *unknown);
    +
    +If you want to have your own functions for it instead of this example 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 here):
    + +
    +
    +
    #include <locale.h>    /* for setlocale */
    #include <langinfo.h> /* for nl_langinfo */
    #include <iconv.h> /* for iconv_* functions */
    +
    +
    + +
    +
    +
    setlocale(LC_ALL, "");
    +
    +
    + +
    +
    +
    iconv_t iconv_handle;
    ...
    iconv_handle = iconv_open(nl_langinfo(CODESET), "UTF-8");

    if (iconv_handle == (iconv_t) -1)
    /* signal an error */
    +
    +
    + +
    +
    +
    /* char* in_buf is the input buffer,    size_t in_len is its length */
    /* char* out_buf is the output buffer, size_t out_len is its length */

    size_t nconv;
    char *in_ptr = in_buf;
    char *out_ptr = out_buf;
    nconv = iconv(iconv_handle, &in_ptr, &in_len, &out_ptr, &out_len);
    +
    +
    +
    If the output buffer is not big enough, iconv will return -1 and set errno to E2BIG.  Also, the in_ptr and out_ptr will point just after the last successfully converted character in the respective buffers, and the in_len and out_len will be updated to show the remaining lengths.  There can be two strategies here:
    + +Another error case is when the conversion was unsuccessful (if one of the +characters can't be represented in the target character set).  The iconv function will then also return -1 and set errno to EILSEQ; the in_ptr will point to the character that couldn't be converted.  In that case, again two strategies are possible:
    + +
      +
        +
      1. if the first byte is in binary 0xxxxxxx, then the character is only one byte long, just skip over that byte
        +
        +
      2. +
      3. if the first byte is in binary 11xxxxxx, then skip over that byte and all bytes 10xxxxxx that follow.
        +
      4. +
      +
    +
    + +
    +
    +
    iconv_close(iconv_handle);
    +
    +
    + The example implementation +mentioned above grows the output buffer dynamically and outputs "?" for characters +that can't be converted.
    + +
    +

    Support for configure.in

    +Programs using the GEDCOM parser library and using autoconf to configure +their sources can use the following statements in configure.in (the example +is checking for gedcom-parse, version 1.34):
    +
    AC_CHECK_LIB(gedcom, gedcom_parse_file,,
    +             AC_MSG_ERROR(Cannot find libgedcom: Please install gedcom-parse))
    +AC_MSG_CHECKING(for libgedcom version)
    +AC_TRY_RUN([
    +#include <stdio.h>
    +#include <stdlib.h>
    +#include <gedcom.h>
    +int
    +main()
    +{
    +if (GEDCOM_PARSE_VERSION >= 1034) exit(0);
    +exit(1);
    +}],
    +ac_gedcom_version_ok='yes',
    +ac_gedcom_version_ok='no',
    +ac_gedcom_version_ok='no')
    +if test "$ac_gedcom_version_ok" = 'yes' ; then
    +  AC_MSG_RESULT(ok)
    +else
    +  AC_MSG_RESULT(not ok)
    +  AC_MSG_ERROR(You need at least version 1.34 of gedcom-parse)
    +fi

    +
    + +There are three preprocessor symbols defined for version checks in the header:
    + +The last one is equal to (GEDCOM_PARSE_VERSION_MAJOR * 1000) + GEDCOM_PARSE_VERSION_MINOR.
    +
    -
    $Id$
    $Name$
    +
    $Id$
    $Name$

                        
    - - + \ No newline at end of file