+}
+
+/** Add an xref_value of the given type, with the given key, to the given
+ object, with a use count equal to 0.
+
+ \param type The type of the referenced object
+ \param xrefstr The key for the object
+ \param object The object to be referenced
+
+ \return The new xref_value if success, or \c NULL in one of the following
+ cases:
+ - the key isn't a valid cross-reference key (see detailed description of
+ \ref parsed_xref)
+ - there is already an xref_value with the same key
+ - there was a memory allocation error
+*/
+struct xref_value* gedcom_add_xref(Xref_type type, const char* xrefstr,
+ Gedcom_ctxt object)
+{
+ struct xref_node *xr = NULL;
+
+ if (!is_valid_pointer(xrefstr)) {
+ gedcom_error(_("String '%s' is not a valid cross-reference key"), xrefstr);
+ }
+ else {
+ hnode_t *node = hash_lookup(xrefs, xrefstr);
+ if (node) {
+ gedcom_error(_("Cross-reference %s already exists"), xrefstr);
+ }
+ else {
+ xr = add_xref(type, xrefstr, object);
+ if (xr)
+ set_xref_fields(xr, XREF_DEFINED, type);
+ }
+ }
+ if (xr)
+ return &(xr->xref);
+ else
+ return NULL;
+}
+
+/** Declare the xref_value corresponding to the given key as being used as the
+ given type. The use of this function is not mandatory, but it can aid in
+ spotting places in the code where xref_value objects are deleted while
+ they are still referenced.
+
+ \param type The type of the referenced object
+ \param xrefstr The key for the object
+
+ \return The xref_value object if success, and its use count is incremented.
+ Returns NULL in one of the following cases:
+ - the key isn't a valid cross-reference key (see detailed description of
+ \ref parsed_xref)
+ - there is no xref_value with the given key
+ - the xref_value was previously added as another type than the type
+ provided here
+ */
+struct xref_value* gedcom_link_xref(Xref_type type, const char* xrefstr)
+{
+ struct xref_node *xr = NULL;
+
+ if (!is_valid_pointer(xrefstr)) {
+ gedcom_error(_("String '%s' is not a valid cross-reference key"), xrefstr);
+ }
+ else {
+ hnode_t *node = hash_lookup(xrefs, xrefstr);
+ if (!node) {
+ gedcom_error(_("Cross-reference %s not defined"), xrefstr);
+ }
+ else {
+ xr = (struct xref_node *)hnode_get(node);
+ if (set_xref_fields(xr, XREF_USED, type) != 0)
+ xr = NULL;
+ }
+ }
+
+ if (xr)
+ return &(xr->xref);
+ else
+ return NULL;
+}
+
+/** Declare the xref_value corresponding to the given key no longer used.
+ The use of this function is not mandatory, but it can aid in
+ spotting places in the code where xref_value objects are deleted while
+ they are still referenced.
+
+ \param type The type of the referenced object
+ \param xrefstr The key for the object
+
+ \return The xref_value object if success, and its use count is decremented.
+ Returns NULL in one of the following cases:
+ - the key isn't a valid cross-reference key (see detailed description of
+ \ref parsed_xref)
+ - there is no xref_value with the given key
+ - the xref_value was previously added as another type than the type
+ provided here
+ */
+struct xref_value* gedcom_unlink_xref(Xref_type type, const char* xrefstr)
+{
+ struct xref_node *xr = NULL;
+ if (!is_valid_pointer(xrefstr)) {
+ gedcom_error(_("String '%s' is not a valid cross-reference key"), xrefstr);
+ }
+ else {
+ hnode_t *node = hash_lookup(xrefs, xrefstr);
+ if (! node) {
+ gedcom_error(_("Cross-reference %s not defined"), xrefstr);
+ }
+ else {
+ xr = (struct xref_node*) hnode_get(node);
+ if (xr->defined_type != type && xr->defined_type != XREF_ANY) {
+ gedcom_error
+ (_("Cross-reference %s previously defined as pointer to %s"),
+ xr->xref.string, xref_type_str[xr->defined_type]);
+ xr = NULL;
+ }
+ else
+ xr->use_count--;
+ }
+ }
+ if (xr)
+ return &(xr->xref);
+ else
+ return NULL;
+}
+
+/** Delete the xref_value corresponding to the given key.
+
+ \param xrefstr The key for the object
+
+ \return 0 if success; 1 in one of the following cases:
+ - the key isn't a valid cross-reference key (see detailed description of
+ \ref parsed_xref)
+ - there is no xref_value with the given key
+ - the xref_value is still in use, i.e. its use count is not 0 (see
+ gedcom_link_xref() and gedcom_unlink_xref())
+ */
+int gedcom_delete_xref(const char* xrefstr)
+{
+ struct xref_node *xr = NULL;
+ int result = 1;
+
+ if (!is_valid_pointer(xrefstr)) {
+ gedcom_error(_("String '%s' is not a valid cross-reference key"), xrefstr);
+ }
+ else {
+ hnode_t *node = hash_lookup(xrefs, xrefstr);
+ if (! node) {
+ gedcom_error(_("Cross-reference %s not defined"), xrefstr);
+ }
+ else {
+ xr = (struct xref_node*) hnode_get(node);
+ if (xr->use_count != 0) {
+ gedcom_error(_("Cross-reference %s still in use"), xrefstr);
+ }
+ else {
+ remove_xref(xr);
+ result = 0;
+ }
+ }
+ }
+ return result;