X-Git-Url: https://git.dlugolecki.net.pl/?a=blobdiff_plain;f=doc%2Fusage.html;h=a71b855221b5643a9fd96d5fbd29b8835e163748;hb=e64a3ac05141381579c407b779d6db66ecdb367d;hp=762a4b0a7cd6c86cca27de62a44ff714a76c3f63;hpb=1316fee80103b38bf682f5c626c0abaa525db41c;p=gedcom-parse.git diff --git a/doc/usage.html b/doc/usage.html index 762a4b0..a71b855 100644 --- a/doc/usage.html +++ b/doc/usage.html @@ -1,460 +1,792 @@ - - -
-libgedcom.so
), to be linked in the application
- programgedcom.h
), to be used in the sources
- of the application programgedcom-tags.h
) that is also installed,
- but that is automatically included via gedcom.h
$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.libgedcom.so
), to be linked in the
+application program, which implements the callback parsergedcom.h
), to be used in the sources
+ of the application programgedcom-tags.h
) that is also installed,
+ but that is automatically included via gedcom.h
libgedcom.so
is also needed in this case, because the object model uses the callback parser internally):libgedcom_gom.so
), to be linked in the application program, which implements the C object modelgom.h
), to be used in the sources of the application program$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.gedcom.h
header is assumed, as everywhere
+in this manual):int result;
+ ...
+ gedcom_init();
+ ...
+ result = gedcom_parse_file("myfamily.ged");
+
+ Although this will not provide much information, one thing it does
+ 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.gedcom.h
and gom.h
is required):int result;
- ...
- result = gedcom_parse_file("myfamily.ged");
-
- Although this will not provide much information, one thing it does
-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.- In the above piece of code,void my_message_handler (Gedcom_msg_type type, - char *msg)
- {
- ...
- }
- ...
- gedcom_set_message_handler(my_message_handler);
- ...
- result = gedcom_parse_file("myfamily.ged");
-
my_message_handler
is the callback
- 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:- Note that the entire string will be properly internationalized, and -encoded in UTF-8 (see "Why UTF-8?" LINK TBD). 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.Error on line
<lineno>: <actual_message>
-
-printf
is used in the message handler.gom_parse_file
will build the C object model, which is then a complete representation of the GEDCOM file.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.-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
gedcom_init()
also initializes locale handling by calling setlocale(LC_ALL, "")
, in case the application would not do this (it doesn't hurt for the application to do the same).gom_parse_file
would be called in case the C object model is used):+ In the above piece of code,void my_message_handler (Gedcom_msg_type type, + char *msg)
+ {
+ ...
+ }
+ ...
+ gedcom_set_message_handler(my_message_handler);
+ ...
+ result = gedcom_parse_file("myfamily.ged");
+
my_message_handler
is the
+ callback 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:+ Note that the entire string will be properly internationalized, and + 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.Error on line
<lineno>: <actual_message>
+
+ printf
is used in the message handler.- Using theGedcom_ctxt my_header_start_cb (int level, -
- - Gedcom_val xref,
- - char *tag,
- - char *raw_value,
- - int parsed_tag,
- - Gedcom_val parsed_value)
- {
- printf("The header starts\n");
- return (Gedcom_ctxt)1;
- }
-
- void my_header_end_cb (Gedcom_ctxt self)
- {
- printf("The header ends, context is %d\n", self); /* context - will print as "1" */
- }
-
- ...
- gedcom_subscribe_to_record(REC_HEAD, my_header_start_cb, - my_header_end_cb);
- ...
- result = gedcom_parse_file("myfamily.ged");
-
gedcom_subscribe_to_record
function, the application
- requests to use the specified callbacks as start and end 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.Gedcom_ctxt
type that is used as a result of the start
- callback and as an argument to the end callback is vital for passing context
- necessary for the application. This type is meant to be opaque; in
- fact, it's a void pointer, so you can pass anything via it. The important
- thing to know is that the context that the application returns in the start
- callback will be passed in the end callback as an argument, and as we will
- see shortly, also to all the directly subordinate elements of the record.tag
is the GEDCOM tag in string format, the parsed_tag
- is an integer, for which symbolic values are defined as TAG_HEAD,
- TAG_SOUR,
TAG_DATA,
... and USERTAG
- for the application-specific tags. These values are defined in the
- header gedcom-tags.h
that is installed, and included via
- gedcom.h
(so no need to include gedcom-tags.h
yourself).struct
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.Gedcom_val
type for the xref
- and parsed_value
arguments was not discussed, see further
-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. theGedcom_ctxt my_header_source_start_cb(Gedcom_ctxt - parent,
- - int - level,
- - char* - tag,
- - char* - raw_value,
- - int - parsed_tag,
- - Gedcom_val - parsed_value)
- {
- char *source = GEDCOM_STRING(parsed_value);
- printf("This file was written by %s\n", source);
- return parent;
- }
-
- void my_header_source_end_cb(Gedcom_ctxt parent,
- - Gedcom_ctxt self,
- - Gedcom_val parsed_value)
- {
- printf("End of the source description\n");
- }
-
- ...
- gedcom_subscribe_to_element(ELT_HEAD_SOUR,
- - my_header_source_start_cb,
- - my_header_source_end_cb);
- ...
- result = gedcom_parse_file("myfamily.ged");
-
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
-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.
- Again, the list of identifiers to use as a first argument for the
-subscription function are detailed in the
- interface details .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. Gedcom_val
argument of the end callback
- is currently not used. It is there for future enhancements.Gedcom_val
argument in
-the start callback for records. This argument is currently a string
-value giving the pointer in string form.- This callback has a similar signature as the previous ones, - but it doesn't contain a parsed value. However, it does contain the - parent context, that was returned by the application for the most specific - containing tag that the application supported.void my_default_cb (Gedcom_ctxt parent, - int level, char* tag, char* raw_value, int parsed_tag)
- {
- ...
- }
+ After this introduction, let's see what the API looks like...
+
+ +Start and end callbacks
+ +Callbacks for records
+ As a simple example, we will get some information from the header +of a GEDCOM file. First, have a look at the following piece of code:
+
+ +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)
+ {
+ printf("The header starts\n");
+ return (Gedcom_ctxt)1;
+ }
+
+ 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" */
+ }
- ...
- gedcom_set_default_callback(my_default_cb);
- ...
- result = gedcom_parse_file("myfamily.ged");
+ ...
+ gedcom_subscribe_to_record(REC_HEAD, my_header_start_cb, + my_header_end_cb);
+ ...
+ result = gedcom_parse_file("myfamily.ged");
gedcom_subscribe_to_record
function, the
+ application requests to use the specified callbacks as start and end
+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 . These are also passed as first argument in the callbacks (the Gedcom_rec
argument).- Note that the default callback will be called for any tag that isn't -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 (thestruct header {
- char* source;
- ...
- char* extra_text;
- };
-
- Gedcom_ctxt my_header_start_cb(int level, Gedcom_val xref, char* tag, -char *raw_value,
- - int parsed_tag, Gedcom_val parsed_value)
- {
- struct header head = my_make_header_struct();
- return (Gedcom_ctxt)head;
- }
+ From the name of the function it becomes clear that this function +is specific to complete records. For the separate elements in records + there is another function, which we'll see shortly. Again, the callbacks + need to have the signatures as shown in the example.
+
+ TheGedcom_ctxt
type that is used as a result of the +start callback and as an argument to the end callback is vital for passing +context necessary for the application. This type is meant to be opaque; +in fact, it's a void pointer, so you can pass anything via it. The +important thing to know is that the context that the application returns +in the start callback will be passed in the end callback as an argument, +and as we will see shortly, also to all the directly subordinate elements +of the record.
+
+ Thetag
is the GEDCOM tag in string format, theparsed_tag
+ is an integer, for which symbolic values are defined asTAG_HEAD,
+TAG_SOUR,
TAG_DATA,
... andUSERTAG +
for the application-specific tags. These values +are defined in the header
gedcom-tags.h
that is installed, +and included viagedcom.h
(so no need to includegedcom-tags.h
+ yourself).
+
+ The example passes a simple integer as context, but an application + could e.g. pass astruct
(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.
+
+ (Note that theGedcom_val
type for thexref
+ andparsed_value
arguments was not discussed, see further + for this)
+
+ +Callbacks for elements
+ 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_elt elt,
+ + Gedcom_ctxt + parent,
+ + int + level,
+ + char* + tag,
+ + char* + raw_value,
+ + int + parsed_tag,
+ + Gedcom_val + parsed_value)
+ {
+ char *source = GEDCOM_STRING(parsed_value);
+ printf("This file was written by %s\n", source);
+ return parent;
+ }
- void my_default_cb(Gedcom_ctxt parent, int level, char* tag, char* raw_value, - int parsed_tag)
- {
- struct header head = (struct header)parent;
- my_header_add_to_extra_text(head, level, tag, raw_value);
- }
+ void my_header_source_end_cb(Gedcom_elt elt,
+ Gedcom_ctxt parent,
+ + Gedcom_ctxt self,
+ + Gedcom_val parsed_value)
+ {
+ printf("End of the source description\n");
+ }
- gedcom_set_default_callback(my_default_cb);
- gedcom_subscribe_to_record(REC_HEAD, my_header_start, NULL);
- ...
- result = gedcom_parse_file(filename);
+ ...
+ gedcom_subscribe_to_element(ELT_HEAD_SOUR,
+ + my_header_source_start_cb,
+ + my_header_source_end_cb);
+ ...
+ result = gedcom_parse_file("myfamily.ged");
parent
could be of different
-types, depending on the context).struct
+ that describes the header) is passed to this start callback. The
+ callback 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 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 . Again, these are passed as first argument in the callback (the Gedcom_elt
argument).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.
+ Gedcom_val
argument of the end callback
+ is used to pass 'complete' values, e.g. the full text of a note. See
+ the interface details
+ for the exact type of this argument.Gedcom_val
arguments
+ in the start callback for records. The first one (xref
+ ) contains the xref_value
corresponding to the cross-reference
+ (or NULL
if there isn't one), the second one (parsed_value
+ ) contains the value that is parsed from the raw_value
. See
+ the interface details
+ .+ This callback has a similar signature as the previous ones, + but it doesn't contain a parsed value. However, it does contain the + parent context, that was returned by the application for the most specific + containing tag that the application supported.void my_default_cb (Gedcom_elt elt, Gedcom_ctxt parent, int level, + char* tag, char* raw_value, int parsed_tag)
+ {
+ ...
+ }
+
+ ...
+ gedcom_set_default_callback(my_default_cb);
+ ...
+ result = gedcom_parse_file("myfamily.ged");
+
+ Note that the default callback will be called for any tag that isn't + 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 (thestruct header {
+ char* source;
+ ...
+ char* extra_text;
+ };
+
+ 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)
+ {
+ struct header head = my_make_header_struct();
+ return (Gedcom_ctxt)head;
+ }
+
+ 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;
+ my_header_add_to_extra_text(head, level, tag, raw_value);
+ }
+
+ gedcom_set_default_callback(my_default_cb);
+ gedcom_subscribe_to_record(REC_HEAD, my_header_start, NULL);
+ ...
+ result = gedcom_parse_file(filename);
+
parent
could be of different
+ types, depending on the context).NULL
. This is e.g. the case if none
+ of the "upper" tags has been subscribed upon.
+Gedcom_write_hndl gedcom_write_open (const char* filename);
+int gedcom_write_close (Gedcom_write_hndl hndl, int* total_conv_fails);
+The function gedcom_write_open
takes a parameter the name of
+the file to write, and returns a write handle, which needs to be used in
+subsequent functions. It returns NULL
in case of errors.gedcom_write_close
takes, next to the write handle,
+an integer pointer as parameter. If you pass an actual pointer for
+this, the function will write in it the total number of conversion failures;
+you can pass NULL
if you're not interested. The function returns 0 in case of success, non-zero in case of failure.gedcom_write_open
, i.e. it affects all files that are opened after it is being called:
+
+int gedcom_write_set_encoding (Enc_from from, const char* charset, Encoding width, Enc_bom bom);
The from
parameter indicates how you want the encoding to be set:ENC_FROM_FILE
: The same as the read file was in (this is the default).ENC_FROM_SYS
: Not a valid value here, see below for gedcom_write_set_terminator
ENC_MANUAL
: From the values given in the following parameters.ENC_FROM_FILE
is selected, the other parameters in the function are ignored (they can be passed as 0). When ENC_MANUAL
is chosen, the meaning of the other parameters is as follows:charset
values are given in the first column in the file gedcom.enc
in the data directory of gedcom-parse ($PREFIX/share/gedcom-parse
).
+ The character sets UNICODE, ASCII and ANSEL are always supported (these
+are standard for GEDCOM), as well as ANSI (not standard), but there may be
+others.width
parameter takes one of the following values:ONE_BYTE
: This should be used for all character sets except UNICODE.TWO_BYTE_HILO
: High-low encoding for UNICODE (i.e. big-endian)TWO_BYTE_LOHI
: Low-high encoding for UNICODE (i.e. little-endian)bom
parameter determines whether a byte-order-mark should
+be written in the file in case of UNICODE encoding (usually preferred because
+it then clearly indicates the byte ordering). It takes one of the following
+values:WITHOUT_BOM
WITH_BOM
gedcom_write_open
):int gedcom_write_set_line_terminator (Enc_from from, Enc_line_end end);
The values for the from
parameter are given above. The value ENC_FROM_SYS
+is valid here, and means that the normal terminator for the current system
+is used (the second parameter of the function is then ignored). This
+is the default for this setting.end
parameter takes one of the following values:END_CR
: only carriage return ("/r") (system value for Macintosh)END_LF
: only line feed ("/n") (system value for Unix, Mac OS X)END_CR_LF
: first carriage return, then line feed ("/r/n") (system value for DOS, Windows)END_LF_CR
: first line feed, then carriage return ("/n/r")write_header
in gom/header.c
).int gedcom_write_record_str (Gedcom_write_hndl hndl, Gedcom_rec rec, const char* xrefstr, const char* value);
+The hndl
parameter is the write handle that was returned by gedcom_write_open
. The rec
parameter is one of the identifiers given in the first column in this table (except REC_USER
: see below). The xrefstr
and val
parameters are respectively the cross-reference key of the record (something like '@FAM01@
'), and the value of the record line, which should be NULL
for some record types, according to the same table.+int gedcom_write_element_str (Gedcom_write_hndl hndl, Gedcom_elt elt, int parsed_tag,
+ + int parent_rec_or_elt, const char* value);
+int gedcom_write_element_xref (Gedcom_write_hndl hndl, Gedcom_elt elt, int parsed_tag,
+ + int parent_rec_or_elt, const struct xref_value* +value);
+int gedcom_write_element_date (Gedcom_write_hndl hndl, Gedcom_elt elt, int parsed_tag,
+ + int parent_rec_or_elt, const struct date_value* +value);
+i
nt gedcom_write_element_age (Gedcom_write_hndl hndl, Gedcom_elt elt, int parsed_tag,
+ + int parent_rec_or_elt, const struct age_value* +value);
+
+These functions only differ in the type of the last argument, which is the value of the element.hndl
parameter is again the write handle returned by gedcom_write_open
. The elt
parameter is one of the identifiers given in the first column in this table (except ELT_USER
: see below). The parent_rec_or_elt
is the corresponding rec
or elt
+identifier of the logically enclosing statement: this will determine the
+level number written on the line, as the level number of the parent + 1.parsed_tag
has to be passed for some of them. This parsed tag is the same as was returned by the callback functions defined above, and is an identifier of the form TAG_name
. This parameter is needed whenever the second column in this table shows several possible tags (this is e.g. the case for ELT_SUB_FAM_EVT
).gedcom_normalize_date
(see here).
++In the case of user-defined tags, the level and tag string are passed verbatim +(not controlled by the library). This allows to write any extra data +that doesn't use a standard tag, but is only allowed for tags starting with +an underscore.int gedcom_write_user_str (Gedcom_write_hndl hndl, int level, const char* tag, const char* xrefstr,
+ const char* value);
+int gedcom_write_user_xref (Gedcom_write_hndl hndl,
int level, const char* tag, const char* xrefstr,
++ + const struct xref_value* value);
+
- Thevoid gedcom_set_debug_level (int level, - FILE* trace_output)
-
level
can be one of the following values:trace_output
is NULL
, debugging information
- will be written to stderr
, otherwise the given file handle
-is used (which must be open).- Thevoid gedcom_set_error_handling (Gedcom_err_mech - mechanism)
-
mechanism
can be one of:IMMED_FAIL
: immediately fail the
-parsing on an error (this is the default)DEFER_FAIL
: continue parsing after
- an error, but return a failure code eventuallyIGNORE_ERRORS
: continue parsing
-after an error, return success always+ Thevoid gedcom_set_debug_level (int level, FILE* +trace_output)
+
level
can be one of the following values:trace_output
is NULL
, debugging information
+ will be written to stderr
, otherwise the given file handle
+ is used (which must be open).- The argument can be:void gedcom_set_compat_handling - (int enable_compat)
-
+ Thevoid gedcom_set_error_handling (Gedcom_err_mech + mechanism)
+
mechanism
can be one of:IMMED_FAIL
: immediately fail
+the parsing on an error (this is the default)DEFER_FAIL
: continue parsing
+after an error, but return a failure code eventuallyIGNORE_ERRORS
: continue parsing
+ after an error, return success always$Id$- -
$Name$
+ +
+ The argument can be:void gedcom_set_compat_handling (int enable_compat)
+
+The parameter can be an OR'ed combination of the following options:void gedcom_set_compat_options (Gedcom_compat options)
+
COMPAT_ALLOW_OUT_OF_CONTEXT
++In some compatibility cases, tags are coming out-of-order, +i.e. their start element callback would have to come after the end element +callback of the parent tag. E.g. instead of the standard GEDCOM+
++the genealogy program has generated something like:1 DATE ...
+2 TIME ...
+
++This can give a problem if your end element callbacks free some resources.1 DATE ...
+1 TIME ...
+
+
+If your program can handle elements out of context, you can enable this option. + By default it is disabled, and so the values of these out-of-context +tags are lost (the parser generates a warning if this is the case). Note: +currently the Gedcom object model in C has this option disabled too, although +this will change in the future.
+
locale
mechanism (i.e. via the LANG
,
+ LC_ALL
or LC_CTYPE
environment variables), which also
+controls the gettext
mechanism in the application.
+gedcom-parse
comes a library implementing help functions for UTF-8 encoding (
see
+the documentation for this library).+All the arguments are optional and default to 0. E.g. to check for +version 1.34.2, you would put in configure.in the following statement:AM_PATH_GEDCOM_PARSER([min_version,[action_if_found,[action_if_not_found,[modules]]]])
+
Note that version numbers now contains three parts (since version +0.20.0: this is also the first version in which this macro is available).AM_PATH_GEDCOM_PARSER(1.34.2)
+
GEDCOM_CFLAGS
and GEDCOM_LIBS
for use in Makefiles. Typically, this would be done as follows in a Makefile.am:+If your program uses some extra modules, they can be passed as fourth argument +in the macro, so that the CFLAGS and LIBS are correctly filled in. Currently, +the only available module isbin_programs = myprg
+myprg_SOURCES = myprg.c foo.c bar.c
+INCLUDES = @GEDCOM_CFLAGS@
+LDADD = @GEDCOM_LIBS@
gom
(the Gedcom object model). For example:+To be able to use this macro in the sources of your application, you have three options:AM_PATH_GEDCOM_PARSER(0.21.2, , ,gom)
+
m4/gedcom.m4
in your autoconf data directory (i.e. the path given by 'aclocal --print-ac-dir
', usually /usr/share/aclocal
). You can do this automatically by going into the m4 subdirectory and typing 'make install-m4
'.m4/gedcom.m4
in the aclocal.m4
file in your sources.m4/gedcom.m4
in the acinclude.m4
file in your sources.GEDCOM_PARSE_VERSION_MAJOR
GEDCOM_PARSE_VERSION_MINOR
GEDCOM_PARSE_VERSION
(GEDCOM_PARSE_VERSION_MAJOR * 1000) + GEDCOM_PARSE_VERSION_MINOR.
As you see, this only checked the major and minor version, not the patch number, so this is obsolete.gedcom-config
to help with compilation and linking flags for programs that don't use autoconf/automake.+Similarly, to get linking flags, use one of the following: +gedcom-config --cflags
+gedcom-config --cflags gom
+
+ + + +gedcom-config --libs
+gedcom-config --libs gom
+
$Id$+ + +
$Name$
+ + +