X-Git-Url: https://git.dlugolecki.net.pl/?a=blobdiff_plain;f=doc%2Fusage.html;h=1c59ae9dc2a126295800cfeb46ea2b61cf507c86;hb=bc80a055217eb3e2bc32974fdbcbe891d2f3aed3;hp=a39d0ac13bb5b9d940995e902970e93221c5e8ac;hpb=960e063dcab7beb1f75a4ff5e05a1035a6499af5;p=gedcom-parse.git diff --git a/doc/usage.html b/doc/usage.html index a39d0ac..1c59ae9 100644 --- a/doc/usage.html +++ b/doc/usage.html @@ -1,13 +1,7 @@ - - -
-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
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
+There is a separate script to help with library and compilation flags, see the development support.$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;
@@ -75,32 +85,43 @@ 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 it's initialization code is run first.
-
- The next sections will refine this piece of code to be able to have
+ 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 function 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).
+
+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 here 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,140 @@ raw_value, int parsed_tag)
of the "upper" tags has been subscribed upon.
+
+Support for writing GEDCOM files
+The Gedcom parser library also contains functions to writing GEDCOM files.
+ Similar as for the parsing itself, there are two interfaces: an interface
+which is very basic, and requires you to call a function for each line in
+the GEDCOM file, and an interface which just dumps the Gedcom object model
+to a file in one shot (if you use the Gedcom object model).
+
+Again, this section focuses on the basic interface, the Gedcom object model interface is described here.
+
+Opening and closing files
+The basic functions for opening and closing Gedcom files for writing are the following:
+
+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.
+
+The function 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.
+
+Controlling some settings
+
+Note that by default the file is written in ASCII encoding (and hence e.g.
+accented characters will cause a conversion failure). You can change
+this by calling the following function before calling gedcom_write_open
, i.e. it affects all files that are opened after it is being called:
+
+int gedcom_write_set_encoding (const char* charset, Encoding width, Enc_bom bom);
+The valid 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.
+
+The 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)
+
+The 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
+
+For both these parameters you can pass 0 for non-UNICODE encodings, since
+that corresponds to the correct values (and is ignored anyway). The
+function returns 0 in case of success, non-zero in case of error.
+
+Further, it is possible to control the kind of line terminator that is used, via the following function (also to be used before gedcom_write_open
):
+int gedcom_write_set_line_terminator (Enc_line_end end);
+The end
parameter takes one of the following values:
+
+ END_CR
: only carriage return ("/r") (cf. Macintosh)
+ END_LF
: only line feed ("/n") (cf. Unix, Mac OS X)
+ END_CR_LF
: first carriage return, then line feed ("/r/n") (cf. DOS, Windows)
+ END_LF_CR
: first line feed, then carriage return ("/n/r")
+
+By default, this is set to the appropriate line terminator on the current
+platform, so it only needs to be changed if there is some special reason
+for it.
+Writing data
+
+For actually writing the data, the principle is that every line in the GEDCOM
+file to write corresponds to a call to one of the following functions, except
+that CONT/CONC lines can be automatically taken care of. Note that
+the resulting GEDCOM file should conform to the GEDCOM standard. Several
+checks are built in already, and more will follow, to force this. There
+is (currently) no compatibility mode for writing GEDCOM files.
+
+In general, each of the following functions expect their input in UTF-8 encoding (see also here). If this is not the case, errors will be returned.
+
+Note that for examples of using these functions you can look at the sources for the Gedcom object model (e.g. the function write_header
in gom/header.c
).
+Records
+For writing lines corresponding to records (i.e. on level 0), the following function is available:
+int gedcom_write_record_str (Gedcom_write_hndl hndl, Gedcom_rec rec, char* xrefstr, 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.
+Elements
+For writing lines corresponding to elements (inside records, i.e. on a level
+bigger than 0), the following functions are available, depending on the data
+type:
+int gedcom_write_element_str (Gedcom_write_hndl hndl, Gedcom_elt elt, int parsed_tag,
+
+ int parent_rec_or_elt, char* value);
+i
nt gedcom_write_element_xref (Gedcom_write_hndl hndl, Gedcom_elt elt, int parsed_tag,
+
+ int parent_rec_or_elt, struct xref_value*
+value);
+ int gedcom_write_element_date (Gedcom_write_hndl hndl, Gedcom_elt elt, int parsed_tag,
+
+ int parent_rec_or_elt, 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, struct age_value*
+value);
+
+
+These functions only differ in the type of the last argument, which is the value of the element.
+
+The 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.
+
+Some of the identifiers can actually stand for different tags. For this reason, the 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
).
+
+Note that for writing a date value, the given value should be valid, i.e.
+all its struct fields filled in properly and consistent. This can be
+done by calling gedcom_normalize_date
(see here).
+User-defined tags
+For user-defined tags (tags starting with an underscore), there are separate functions, again depending on the data type:
+int gedcom_write_user_str (Gedcom_write_hndl hndl, int level, char* tag, char* xrefstr,
+ char* value);
+i
nt gedcom_write_user_xref (Gedcom_write_hndl hndl,
int level, char* tag, char* xrefstr,
+
+ struct xref_value* value);
+
+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.
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.
@@ -484,8 +635,7 @@ default)
- Note that, currently, no actual compatibility code is present, but
-this is on the to-do list.
+ Currently, there is a beginning for compatibility for ftree and Lifelines (3.0.2).
Converting character sets
@@ -497,209 +647,40 @@ this is on the to-do list.
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 "t" subdirectory of 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):
-
-
- - inclusion of some headers:
-
-
-
-
-
- #include <locale.h> /* for setlocale */
#include <langinfo.h> /* for nl_langinfo */
#include <iconv.h> /* for iconv_* functions */
-
-
-
-
- - set the program's current locale to what
-the user configured in the environment:
-
-
-
-
-
- setlocale(LC_ALL, "");
-
-
-
-
- - open a conversion handle for conversion
- from UTF-8 to the character set of the current locale (once for the entire
- program):
-
-
-
-
-
- iconv_t iconv_handle;
...
iconv_handle = iconv_open(nl_langinfo(CODESET), "UTF-8");
if (iconv_handle == (iconv_t) -1)
/* signal an error */
-
-
-
-
- - then, every string can be converted
- using the following:
-
-
-
-
-
- /* 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:
-
-
- - Make sure from the beginning
- that the output buffer is big enough. However, it's difficult to find
- an absolute maximum length in advance, even given the length of the input
- string.
-
-
- - Do the conversion in several
- steps, growing the output buffer each time to make more space, and calling
-
iconv
consecutively until the conversion is complete.
- This is the preferred way (a function could be written to encapsulate
- all this).
-
-
- 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:
-
-
- - Just fail the conversion,
-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
- :
-
-
-
-
-
-
- - if the first byte is in
-binary 0xxxxxxx, then the character is only one byte long, just skip over
-that byte
-
-
- - if the first byte is in
-binary 11xxxxxx, then skip over that byte and all bytes 10xxxxxx that follow.
-
-
-
-
-
-
-
-
- - eventually, the conversion
-handle needs to be closed (when the program exits):
-
-
-
-
-
-
- iconv_close(iconv_handle);
-
-
- The example implementation
- mentioned above grows the output buffer dynamically and outputs "?" for characters
- that can't be converted.
+
With
+gedcom-parse
comes a library implementing help functions for UTF-8 encoding (
see
+the documentation for this library).
-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:
+Development support
+Macro for configure.in
+
+There
+is a macro available for use in configure.in for applications that are using
+autoconf to configure their sources. The following macro checks whether
+the Gedcom parser library is available and whether its version is high enough:
+AM_LIB_GEDCOM_PARSER([major,[minor,[patch]]])
+
+All the arguments are optional and default to 0. E.g. to check for
+version 1.34, you would put in configure.in the following statement:
+AM_LIB_GEDCOM_PARSER(1,34)
+
+To be able to use this macro in the sources of your application, you have three options:
+
+ - Put the file
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
'.
+
+
+ - If you're using autoconf, but not automake, copy the contents of
m4/gedcom.m4
in the aclocal.m4
file in your sources.
+
+
+ - If you're using automake, copy the contents of
m4/gedcom.m4
in the acinclude.m4
file in your sources.
+
+
+
+There are three preprocessor symbols defined for version checks in the
+ header (but their direct use is deprecated: please use the macro above):
GEDCOM_PARSE_VERSION_MAJOR
@@ -709,6 +690,21 @@ handle needs to be closed (when the program exits):
The last one is equal to (GEDCOM_PARSE_VERSION_MAJOR * 1000) + GEDCOM_PARSE_VERSION_MINOR.
+
+Compilation and linking flags
+Similar to other libraries, the GEDCOM parse library installs a script gedcom-config
to help with compilation and linking flags.
+
+To get compilation flags for your program, use (depending on whether you
+only use the callback parser, or also the GEDCOM object model):
+gedcom-config --cflags
+gedcom-config --cflags gom
+
+Similarly, to get linking flags, use one of the following:
+gedcom-config --libs
+gedcom-config --libs gom
+
+
+
@@ -718,5 +714,12 @@ handle needs to be closed (when the program exits):
-
-
+
+
+
+
+
+
+
+
+
\ No newline at end of file