X-Git-Url: https://git.dlugolecki.net.pl/?a=blobdiff_plain;f=gedcom%2Fxref.c;h=a9979181653d14d513ceca7ea379042c9ca613a0;hb=5fe34c1f751845fb0912834069c8e357a4cc0d6d;hp=3c33909339870d3b6d6e9cb14aa4159ac3743392;hpb=3068ed1f64a96d42cdde19cfebf1468ec9169e62;p=gedcom-parse.git diff --git a/gedcom/xref.c b/gedcom/xref.c index 3c33909..a997918 100644 --- a/gedcom/xref.c +++ b/gedcom/xref.c @@ -28,17 +28,17 @@ struct xref_value def_xref_val = { XREF_NONE, "", NULL }; static hash_t *xrefs = NULL; -char* xref_type_str[] = { N_("nothing"), - N_("a family"), - N_("an individual"), - N_("a note"), - N_("a multimedia object"), - N_("a source repository"), - N_("a source"), - N_("a submitter"), - N_("a submission record"), - N_("an application-specific record"), - }; +const char* xref_type_str[] = { N_("nothing"), + N_("a family"), + N_("an individual"), + N_("a note"), + N_("a multimedia object"), + N_("a source repository"), + N_("a source"), + N_("a submitter"), + N_("a submission record"), + N_("an application-specific record"), + }; struct xref_node { struct xref_value xref; @@ -62,21 +62,48 @@ void xref_free(hnode_t *n, void *c __attribute__((unused))) free(n); } -struct xref_node *make_xref_node() +void clear_xref_node(struct xref_node *xr) { - struct xref_node *xr = (struct xref_node *)malloc(sizeof(struct xref_node)); xr->xref.type = XREF_NONE; - xr->xref.string = NULL; + /* Make sure that the 'string' member always contains a valid string */ + if (!xr->xref.string) + xr->xref.string = strdup(""); + if (!xr->xref.string) MEMORY_ERROR; xr->xref.object = NULL; xr->defined_type = XREF_NONE; xr->used_type = XREF_NONE; xr->defined_line = -1; xr->used_line = -1; +} + +struct xref_node *make_xref_node() +{ + struct xref_node *xr = (struct xref_node *)malloc(sizeof(struct xref_node)); + if (xr) { + xr->xref.string = NULL; + clear_xref_node(xr); + } + else + MEMORY_ERROR; return xr; } +void cleanup_xrefs() +{ + hash_free(xrefs); + xrefs = NULL; +} + void make_xref_table() { + if (xrefs) + cleanup_xrefs(); + else + /* Only register initially (if xrefs is still NULL) */ + /* So that it is only registered once */ + if (atexit(cleanup_xrefs) != 0) { + gedcom_warning(_("Could not register xref cleanup function")); + } xrefs = hash_create(HASHCOUNT_T_MAX, NULL, NULL); hash_set_allocator(xrefs, xref_alloc, xref_free, NULL); } @@ -103,11 +130,21 @@ int check_xref_table() } } - hash_free(xrefs); return result; } -struct xref_value *gedcom_parse_xref(char *raw_value, +struct xref_value *gedcom_get_by_xref(const char *key) +{ + hnode_t *node = hash_lookup(xrefs, key); + if (node) { + struct xref_node *xr = (struct xref_node *)hnode_get(node); + return &(xr->xref); + } + else + return NULL; +} + +struct xref_value *gedcom_parse_xref(const char *raw_value, Xref_ctxt ctxt, Xref_type xref_type) { struct xref_node *xr = NULL; @@ -117,11 +154,18 @@ struct xref_value *gedcom_parse_xref(char *raw_value, xr = (struct xref_node *)hnode_get(node); } else { - char *key = strdup(raw_value); - xr = make_xref_node(); - xr->xref.type = xref_type; - xr->xref.string = strdup(raw_value); - hash_alloc_insert(xrefs, key, xr); + const char *key = strdup(raw_value); + if (key) { + xr = make_xref_node(); + xr->xref.type = xref_type; + if (xr->xref.string) + free(xr->xref.string); + xr->xref.string = strdup(raw_value); + if (! xr->xref.string) MEMORY_ERROR; + hash_alloc_insert(xrefs, key, xr); + } + else + MEMORY_ERROR; } if (ctxt == XREF_DEFINED && xr->defined_type == XREF_NONE) { @@ -133,23 +177,24 @@ struct xref_value *gedcom_parse_xref(char *raw_value, xr->used_line = line_no; } - if ((ctxt == XREF_DEFINED && xr->defined_type != xref_type) + if ((ctxt == XREF_DEFINED && xr->defined_type != xref_type && + xr->defined_type != XREF_ANY) || (ctxt == XREF_USED && - (xr->defined_type != XREF_NONE && xr->defined_type != xref_type))) { + (xr->defined_type != XREF_NONE && xr->defined_type != xref_type && + xr->defined_type != XREF_ANY))) { gedcom_error(_("Cross-reference %s previously defined as pointer to %s, " "on line %d"), xr->xref.string, xref_type_str[xr->defined_type], xr->defined_line); - return NULL; - } - - if ((ctxt == XREF_USED && xr->used_type != xref_type) - || (ctxt == XREF_DEFINED && - (xr->used_type != XREF_NONE && xr->used_type != xref_type))) { + clear_xref_node(xr); + } + else if ((ctxt == XREF_USED && xr->used_type != xref_type) + || (ctxt == XREF_DEFINED && + (xr->used_type != XREF_NONE && xr->used_type != xref_type))) { gedcom_error(_("Cross-reference %s previously used as pointer to %s, " "on line %d"), xr->xref.string, xref_type_str[xr->used_type], xr->used_line); - return NULL; + clear_xref_node(xr); } return &(xr->xref);