1 /* Cross-reference manipulation routines.
2 Copyright (C) 2001,2002 The Genes Development Team
3 This file is part of the Gedcom parser library.
4 Contributed by Peter Verthez <Peter.Verthez@advalvas.be>, 2001.
6 The Gedcom parser library is free software; you can redistribute it
7 and/or modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The Gedcom parser library is distributed in the hope that it will be
12 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the Gedcom parser library; if not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24 #include "gedcom_internal.h"
26 #include "gedcom.tabgen.h"
30 struct xref_value def_xref_val = { XREF_NONE, "<error>", NULL };
31 static hash_t *xrefs = NULL;
33 const char* xref_type_str[] = { N_("nothing"),
37 N_("a multimedia object"),
38 N_("a source repository"),
41 N_("a submission record"),
42 N_("an application-specific record"),
46 struct xref_value xref;
47 Xref_type defined_type;
54 hnode_t *xref_alloc(void *c UNUSED)
56 return malloc(sizeof *xref_alloc(NULL));
59 void xref_free(hnode_t *n, void *c UNUSED)
61 struct xref_node *xr = (struct xref_node *)hnode_get(n);
62 free((void*)hnode_getkey(n));
63 free(xr->xref.string);
68 void clear_xref_node(struct xref_node *xr)
70 xr->xref.type = XREF_NONE;
71 /* Make sure that the 'string' member always contains a valid string */
73 xr->xref.string = strdup("");
74 if (!xr->xref.string) MEMORY_ERROR;
75 xr->xref.object = NULL;
76 xr->defined_type = XREF_NONE;
77 xr->used_type = XREF_NONE;
78 xr->defined_line = -1;
83 struct xref_node *make_xref_node()
85 struct xref_node *xr = (struct xref_node *)malloc(sizeof(struct xref_node));
87 xr->xref.string = NULL;
95 void delete_xref_node(struct xref_node* xr)
98 free(xr->xref.string);
108 void make_xref_table()
113 /* Only register initially (if xrefs is still NULL) */
114 /* So that it is only registered once */
115 if (atexit(cleanup_xrefs) != 0) {
116 gedcom_warning(_("Could not register xref cleanup function"));
118 xrefs = hash_create(HASHCOUNT_T_MAX, NULL, NULL);
119 hash_set_allocator(xrefs, xref_alloc, xref_free, NULL);
122 int check_xref_table()
127 struct xref_node *xr;
129 /* Check for undefined and unused xrefs */
130 hash_scan_begin(&hs, xrefs);
131 while ((node = hash_scan_next(&hs))) {
132 xr = (struct xref_node *)hnode_get(node);
133 if (xr->defined_type == XREF_NONE && xr->used_type != XREF_NONE) {
134 gedcom_error(_("Cross-reference %s used on line %d is not defined"),
135 xr->xref.string, xr->used_line);
138 if (xr->used_type == XREF_NONE && xr->defined_type != XREF_NONE) {
139 gedcom_warning(_("Cross-reference %s defined on line %d is never used"),
140 xr->xref.string, xr->defined_line);
147 struct xref_node* add_xref(Xref_type xref_type, const char* xrefstr,
150 struct xref_node *xr = NULL;
151 char *key = strdup(xrefstr);
153 xr = make_xref_node();
154 xr->xref.type = xref_type;
155 xr->xref.object = object;
157 free(xr->xref.string);
158 xr->xref.string = strdup(xrefstr);
159 if (! xr->xref.string) {
162 delete_xref_node(xr);
166 hash_alloc_insert(xrefs, key, xr);
174 void remove_xref(struct xref_node* xr)
176 hnode_t *node = hash_lookup(xrefs, xr->xref.string);
177 hash_delete_free(xrefs, node);
180 int set_xref_fields(struct xref_node* xr, Xref_ctxt ctxt, Xref_type xref_type)
184 if (xr->defined_type != XREF_NONE && xr->defined_type != xref_type &&
185 xr->defined_type != XREF_ANY) {
186 if (xr->defined_line != 0)
187 gedcom_error(_("Cross-reference %s previously defined as pointer to %s, "
189 xr->xref.string, xref_type_str[xr->defined_type],
192 gedcom_error(_("Cross-reference %s previously defined as pointer to %s"),
193 xr->xref.string, xref_type_str[xr->defined_type]);
197 else if (xr->used_type != XREF_NONE && xr->used_type != xref_type) {
198 if (xr->used_line != 0)
199 gedcom_error(_("Cross-reference %s previously used as pointer to %s, "
201 xr->xref.string, xref_type_str[xr->used_type],
204 gedcom_error(_("Cross-reference %s previously used as pointer to %s"),
205 xr->xref.string, xref_type_str[xr->used_type]);
211 if (ctxt == XREF_USED)
213 if (ctxt == XREF_DEFINED && xr->defined_type == XREF_NONE) {
214 xr->defined_type = xref_type;
215 xr->defined_line = line_no;
217 else if (ctxt == XREF_USED && xr->used_type == XREF_NONE) {
218 xr->used_type = xref_type;
219 xr->used_line = line_no;
226 struct xref_value *gedcom_parse_xref(const char *raw_value,
227 Xref_ctxt ctxt, Xref_type xref_type)
229 struct xref_node *xr = NULL;
231 hnode_t *node = hash_lookup(xrefs, raw_value);
233 xr = (struct xref_node *)hnode_get(node);
236 xr = add_xref(xref_type, raw_value, NULL);
240 set_xref_fields(xr, ctxt, xref_type);
247 /* Functions for retrieving, modifying and deleting cross-references */
249 int is_valid_pointer(const char *key)
251 return (strlen(key) <= 22 &&
252 gedcom_check_token(key, STATE_NORMAL, POINTER) == 0);
255 /** Retrieve an xref_value by its key.
257 \param key The given cross-reference key
259 \return The object referenced by the key, or \c NULL if the given key
260 isn't a valid cross-reference key (see detailed description of
261 \ref parsed_xref) or isn't used.
263 struct xref_value* gedcom_get_by_xref(const char *key)
265 if (!is_valid_pointer(key)) {
266 gedcom_error(_("String '%s' is not a valid cross-reference key"), key);
270 hnode_t *node = hash_lookup(xrefs, key);
272 struct xref_node *xr = (struct xref_node *)hnode_get(node);
280 /** Add an xref_value of the given type, with the given key, to the given
281 object, with a use count equal to 0.
283 \param type The type of the referenced object
284 \param xrefstr The key for the object
285 \param object The object to be referenced
287 \return The new xref_value if success, or \c NULL in one of the following
289 - the key isn't a valid cross-reference key (see detailed description of
291 - there is already an xref_value with the same key
292 - there was a memory allocation error
294 struct xref_value* gedcom_add_xref(Xref_type type, const char* xrefstr,
297 struct xref_node *xr = NULL;
299 if (!is_valid_pointer(xrefstr)) {
300 gedcom_error(_("String '%s' is not a valid cross-reference key"), xrefstr);
303 hnode_t *node = hash_lookup(xrefs, xrefstr);
305 gedcom_error(_("Cross-reference %s already exists"), xrefstr);
308 xr = add_xref(type, xrefstr, object);
310 set_xref_fields(xr, XREF_DEFINED, type);
319 /** Declare the xref_value corresponding to the given key as being used as the
320 given type. The use of this function is not mandatory, but it can aid in
321 spotting places in the code where xref_value objects are deleted while
322 they are still referenced.
324 \param type The type of the referenced object
325 \param xrefstr The key for the object
327 \return The xref_value object if success, and its use count is incremented.
328 Returns NULL in one of the following cases:
329 - the key isn't a valid cross-reference key (see detailed description of
331 - there is no xref_value with the given key
332 - the xref_value was previously added as another type than the type
335 struct xref_value* gedcom_link_xref(Xref_type type, const char* xrefstr)
337 struct xref_node *xr = NULL;
339 if (!is_valid_pointer(xrefstr)) {
340 gedcom_error(_("String '%s' is not a valid cross-reference key"), xrefstr);
343 hnode_t *node = hash_lookup(xrefs, xrefstr);
345 gedcom_error(_("Cross-reference %s not defined"), xrefstr);
348 xr = (struct xref_node *)hnode_get(node);
349 if (set_xref_fields(xr, XREF_USED, type) != 0)
360 /** Declare the xref_value corresponding to the given key no longer used.
361 The use of this function is not mandatory, but it can aid in
362 spotting places in the code where xref_value objects are deleted while
363 they are still referenced.
365 \param type The type of the referenced object
366 \param xrefstr The key for the object
368 \return The xref_value object if success, and its use count is decremented.
369 Returns NULL in one of the following cases:
370 - the key isn't a valid cross-reference key (see detailed description of
372 - there is no xref_value with the given key
373 - the xref_value was previously added as another type than the type
376 struct xref_value* gedcom_unlink_xref(Xref_type type, const char* xrefstr)
378 struct xref_node *xr = NULL;
379 if (!is_valid_pointer(xrefstr)) {
380 gedcom_error(_("String '%s' is not a valid cross-reference key"), xrefstr);
383 hnode_t *node = hash_lookup(xrefs, xrefstr);
385 gedcom_error(_("Cross-reference %s not defined"), xrefstr);
388 xr = (struct xref_node*) hnode_get(node);
389 if (xr->defined_type != type && xr->defined_type != XREF_ANY) {
391 (_("Cross-reference %s previously defined as pointer to %s"),
392 xr->xref.string, xref_type_str[xr->defined_type]);
405 /** Delete the xref_value corresponding to the given key.
407 \param xrefstr The key for the object
409 \return 0 if success; 1 in one of the following cases:
410 - the key isn't a valid cross-reference key (see detailed description of
412 - there is no xref_value with the given key
413 - the xref_value is still in use, i.e. its use count is not 0 (see
414 gedcom_link_xref() and gedcom_unlink_xref())
416 int gedcom_delete_xref(const char* xrefstr)
418 struct xref_node *xr = NULL;
421 if (!is_valid_pointer(xrefstr)) {
422 gedcom_error(_("String '%s' is not a valid cross-reference key"), xrefstr);
425 hnode_t *node = hash_lookup(xrefs, xrefstr);
427 gedcom_error(_("Cross-reference %s not defined"), xrefstr);
430 xr = (struct xref_node*) hnode_get(node);
431 if (xr->use_count != 0) {
432 gedcom_error(_("Cross-reference %s still in use"), xrefstr);