Use def_elt_end instead of destroying the context directly.
[gedcom-parse.git] / gom / gom.c
index cc388ee2be9dbd7484d4a5d55e9ed3b4a26eea72..743d2a4b975274ceb8320e6272005ba363b2fce0 100644 (file)
--- a/gom/gom.c
+++ b/gom/gom.c
 #include "gom.h"
 #include "gom_internal.h"
 
+const char* ctxt_names[] =
+{
+  "NULL",
+  
+  "header", "submission", "submitter", "family", "individual",
+  "multimedia", "note", "repository", "source", "user_rec",
+  
+  "address", "event", "place", "source_citation", "text",
+  "note_sub", "multimedia_link", "lds_event", "user_ref_number",
+  "change_date", "personal_name", "family_link", "pedigree",
+  "association", "source_event", "source_description"
+};
+
 void gom_default_callback (Gedcom_elt elt, Gedcom_ctxt parent, int level,
                           char* tag, char* raw_value, int parsed_tag);
 
@@ -103,18 +116,69 @@ void subscribe_all()
   }
 }
 
+int gom_active = 0;
+
 int gom_parse_file(const char* file_name)
 {
-  subscribe_all();
+  if (gom_active) {
+    gom_cleanup();
+  }
+  else {
+    subscribe_all();
+  }
+  gom_active = 1;
+  gedcom_set_compat_options(0);
   return gedcom_parse_file(file_name);
 }
 
 int gom_new_model()
 {
-  subscribe_all();
+  if (gom_active) {
+    gom_cleanup();
+  }
+  else {
+    subscribe_all();
+  }
+  gom_active = 1;
   return gedcom_new_model();
 }
 
+int gom_write_file(const char* file_name, int *total_conv_fails)
+{
+  Gedcom_write_hndl hndl;
+  int result = 1;
+
+  hndl = gedcom_write_open(file_name);
+  if (hndl) {
+    result = write_header(hndl);
+    result |= write_submission(hndl);
+    result |= write_submitters(hndl);
+    result |= write_individuals(hndl);
+    result |= write_families(hndl);
+    result |= write_multimedia_recs(hndl);
+    result |= write_notes(hndl);
+    result |= write_repositories(hndl);
+    result |= write_sources(hndl);
+    result |= write_user_recs(hndl);
+    result |= gedcom_write_close(hndl, total_conv_fails);
+  }
+
+  return result;
+}
+
+int gom_write_xref_list(Gedcom_write_hndl hndl,
+                       Gedcom_elt elt, int tag, int parent_rec_or_elt,
+                       struct xref_list* val)
+{
+  int result = 0;
+  struct xref_list* xrl;
+  for (xrl = val; xrl; xrl = xrl->next) {
+    result |= gedcom_write_element_xref(hndl, elt, tag, parent_rec_or_elt,
+                                       xrl->xref);
+  }
+  return result;
+}
+
 Gom_ctxt make_gom_ctxt(int ctxt_type, OBJ_TYPE obj_type, void *ctxt_ptr)
 {
   Gom_ctxt ctxt   = (Gom_ctxt)malloc(sizeof(struct Gom_ctxt_struct));
@@ -140,9 +204,15 @@ void destroy_gom_ctxt(Gom_ctxt ctxt)
 void gom_cast_error(const char* file, int line,
                    OBJ_TYPE expected, OBJ_TYPE found)
 {
+  const char* expected_name = "<out-of-bounds>";
+  const char* found_name    = "<out-of-bounds>";
+  if (expected < T_LAST)
+    expected_name = ctxt_names[expected];
+  if (found < T_LAST)
+    found_name = ctxt_names[found];
   fprintf(stderr,
-         "Wrong gom ctxt cast at %s, line %d: expected %d, found %d\n",
-         file, line, expected, found);
+         "Wrong gom ctxt cast at %s, line %d: expected %s, found %s\n",
+         file, line, expected_name, found_name);
   abort();
 }
 
@@ -151,10 +221,18 @@ void gom_mem_error(const char *filename, int line)
   gedcom_error(_("Could not allocate memory at %s, %d"), filename, line);
 }
 
+void gom_xref_already_in_use(const char *xrefstr)
+{
+  gedcom_error(_("Cross-reference key '%s' is already in use"), xrefstr);
+}
+
 void gom_unexpected_context(const char* file, int line, OBJ_TYPE found)
 {
-  gedcom_warning(_("Internal error: Unexpected context at %s, line %d: %d"),
-                file, line, found);
+  const char* found_name    = "<out-of-bounds>";
+  if (found < T_LAST)
+    found_name = ctxt_names[found];
+  gedcom_warning(_("Internal error: Unexpected context at %s, line %d: %s"),
+                file, line, found_name);
 }
 
 void gom_no_context(const char* file, int line)
@@ -163,6 +241,16 @@ void gom_no_context(const char* file, int line)
                 file, line);
 }
 
+void gom_move_error(const char* type)
+{
+  gedcom_warning(_("Could not move struct of type %s"), type);
+}
+
+void gom_find_error(const char* type)
+{
+  gedcom_warning(_("Could not find struct of type %s in chain"), type);
+}
+
 void gom_default_callback (Gedcom_elt elt UNUSED, Gedcom_ctxt parent UNUSED,
                           int level, char* tag, char* raw_value,
                           int parsed_tag UNUSED)
@@ -171,12 +259,14 @@ void gom_default_callback (Gedcom_elt elt UNUSED, Gedcom_ctxt parent UNUSED,
                  level, tag, raw_value);
 }
 
-void def_rec_end(Gedcom_rec rec UNUSED, Gedcom_ctxt self)
+void def_rec_end(Gedcom_rec rec UNUSED, Gedcom_ctxt self,
+                Gedcom_val parsed_value UNUSED)
 {
   Gom_ctxt ctxt = (Gom_ctxt)self;
   destroy_gom_ctxt(ctxt);
 }
 
+/* TODO: do this in a way so that elements out of context can be handled */
 void def_elt_end(Gedcom_elt elt UNUSED, Gedcom_ctxt parent UNUSED,
                 Gedcom_ctxt self, Gedcom_val parsed_value UNUSED)
 {
@@ -205,52 +295,3 @@ void set_xref_type(struct xref_value* xr, const char *str)
   else
     xr->type = XREF_ANY;
 }
-
-char* concat_strings(NL_TYPE type, char *str1, const char *str2)
-{
-  if (str1 != NULL && str2 != NULL) {
-    char *newp;
-    char *wp;
-    size_t len1 = strlen(str1);
-    size_t len2 = strlen(str2);
-    size_t len  = len1 + len2 + 1;
-    if (type == WITH_NL)
-      len++;
-    newp = (char*) realloc(str1, len);
-    if (newp == NULL)
-      return NULL;
-    wp   = newp + len1;
-    str1 = newp;
-    if (type == WITH_NL)
-      *wp++ = '\n';
-    wp = memcpy (wp, str2, len2);
-    wp += len2;
-    *wp++ = '\0';
-  }
-
-  return str1;
-}
-
-struct date_value* dup_date(struct date_value dv)
-{
-  struct date_value* dv_ptr;
-  dv_ptr = (struct date_value*) malloc(sizeof(struct date_value));
-  if (! dv_ptr)
-    MEMORY_ERROR;
-  else {
-    memcpy(dv_ptr, &dv, sizeof(struct date_value));
-  }
-  return dv_ptr;
-}
-
-struct age_value* dup_age(struct age_value age)
-{
-  struct age_value* age_ptr;
-  age_ptr = (struct age_value*) malloc(sizeof(struct age_value));
-  if (! age_ptr)
-    MEMORY_ERROR;
-  else {
-    memcpy(age_ptr, &age, sizeof(struct age_value));
-  }
-  return age_ptr;
-}