Removed newlines from error strings.
[gedcom-parse.git] / gedcom / xref.c
index ea3e1fa7ded85cc6cbb8f445175e313aa606d489..bc05ac3748cc5321a9fc9c6cfdbc202daf057610 100644 (file)
@@ -68,6 +68,7 @@ void clear_xref_node(struct xref_node *xr)
   /* 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;
@@ -78,13 +79,31 @@ void clear_xref_node(struct xref_node *xr)
 struct xref_node *make_xref_node()
 {
   struct xref_node *xr = (struct xref_node *)malloc(sizeof(struct xref_node));
-  xr->xref.string = NULL;
-  clear_xref_node(xr);
+  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);
 }
@@ -111,10 +130,20 @@ int check_xref_table()
     }
   }
   
-  hash_free(xrefs);
   return result;
 }
 
+struct xref_value *gedcom_get_by_xref(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(char *raw_value,
                                     Xref_ctxt ctxt, Xref_type xref_type)
 {
@@ -126,11 +155,17 @@ struct xref_value *gedcom_parse_xref(char *raw_value,
   }
   else {
     char *key = strdup(raw_value);
-    xr = make_xref_node();
-    xr->xref.type = xref_type;
-    free(xr->xref.string);
-    xr->xref.string = strdup(raw_value);
-    hash_alloc_insert(xrefs, key, xr);
+    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) {
@@ -142,9 +177,11 @@ 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],