Added program to convert a GEDCOM file to standard GEDCOM.
authorPeter Verthez <Peter.Verthez@advalvas.be>
Fri, 24 Jan 2003 06:48:32 +0000 (06:48 +0000)
committerPeter Verthez <Peter.Verthez@advalvas.be>
Fri, 24 Jan 2003 06:48:32 +0000 (06:48 +0000)
bin/Makefile.am
bin/gedcom-sanitize.c [new file with mode: 0644]
bin/gedcom-sanitize.sh [new file with mode: 0755]

index 80b3059b35c4b27385b82b7c37a613b646e4aef8..2f645568a29d6a8cfe4bfa527d4b6add9c82fc1b 100644 (file)
@@ -6,9 +6,15 @@ localedir = $(datadir)/locale
 INCLUDES = -I$(srcdir)/../intl -I$(srcdir)/../include -I$(srcdir)/../utf8
 CFLAGS   = -O2 -W -Wall -pedantic -Wno-long-long @EXTRA_CFLAGS@
 
 INCLUDES = -I$(srcdir)/../intl -I$(srcdir)/../include -I$(srcdir)/../utf8
 CFLAGS   = -O2 -W -Wall -pedantic -Wno-long-long @EXTRA_CFLAGS@
 
-EXTRA_DIST = gedcom-check.sh
+EXTRA_DIST = gedcom-check.sh gedcom-sanitize.sh
 
 
-bin_PROGRAMS = gedcom-check
+bin_PROGRAMS = gedcom-check gedcom-sanitize
 gedcom_check_SOURCES = gedcom-check.c
 gedcom_check_LDFLAGS = -L../gedcom/.libs -L../utf8/.libs @ICONV_LIBPATH@
 gedcom_check_LDADD   = $(LIBICONV) -lgedcom -lutf8tools $(LIBICONV)
 gedcom_check_SOURCES = gedcom-check.c
 gedcom_check_LDFLAGS = -L../gedcom/.libs -L../utf8/.libs @ICONV_LIBPATH@
 gedcom_check_LDADD   = $(LIBICONV) -lgedcom -lutf8tools $(LIBICONV)
+
+gedcom_sanitize_SOURCES = gedcom-sanitize.c
+gedcom_sanitize_LDFLAGS = -L../gedcom/.libs -L../gom/.libs -L../utf8/.libs \
+                          @ICONV_LIBPATH@
+gedcom_sanitize_LDADD   = $(LIBICONV) -lgedcom_gom -lgedcom -lutf8tools \
+                          $(LIBICONV)
diff --git a/bin/gedcom-sanitize.c b/bin/gedcom-sanitize.c
new file mode 100644 (file)
index 0000000..167019d
--- /dev/null
@@ -0,0 +1,137 @@
+/* Check program using the Gedcom library.
+   Copyright (C) 2001, 2002 The Genes Development Team
+   This file is part of the Gedcom parser library.
+   Contributed by Peter Verthez <Peter.Verthez@advalvas.be>, 2001.
+
+   The Gedcom parser library is free software; you can redistribute it
+   and/or modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The Gedcom parser library is distributed in the hope that it will be
+   useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the Gedcom parser library; if not, write to the
+   Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
+
+/* $Id$ */
+/* $Name$ */
+
+#include "gedcom.h"
+#include "gom.h"
+#include "utf8tools.h"
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+#include <libintl.h>
+
+#define _(string) dgettext(PACKAGE, string)
+#define N_(string) (string)
+
+#ifdef __GNUC__
+#define UNUSED __attribute__((unused))
+#else
+#define UNUSED
+#endif
+
+void show_help ()
+{
+  printf("Converts a GEDCOM file to strict standard GEDCOM\n\n");
+  printf("Usage:  gedcom-sanitize [options] file\n");
+  printf("Options:\n");
+  printf("  -h    Show this help text\n");
+  printf("  -dg   Debug setting: only libgedcom debug messages\n");
+  printf("  -da   Debug setting: libgedcom + yacc debug messages\n");
+  printf("  -e <extension>   Extension to give to file name (default 'new')\n");
+  printf("Errors, warnings, ... are sent to stdout\n");
+}
+
+void gedcom_message_handler(Gedcom_msg_type type UNUSED, char *msg)
+{
+  char *converted = NULL;
+  int  conv_fails = 0;
+  converted = convert_utf8_to_locale(msg, &conv_fails);
+  printf("%s\n", converted);
+}
+
+int main(int argc, char* argv[])
+{
+  Gedcom_err_mech mech = DEFER_FAIL;
+  int compat_enabled   = 1;
+  int debug_level = 0;
+  char* file_name = NULL;
+  int result;
+  char* extension = "new";
+  
+  if (argc > 1) {
+    int i;
+    for (i=1; i<argc; i++) {
+      if (!strncmp(argv[i], "-da", 4))
+       debug_level = 2;
+      else if (!strncmp(argv[i], "-dg", 4))
+       debug_level = 1;
+      else if (!strncmp(argv[i], "-e", 3)) {
+       if (i<argc) {
+         extension = argv[++i];
+       }
+       else {
+         show_help();
+         exit(1);
+       }
+      }
+      else if (!strncmp(argv[i], "-h", 3)) {
+       show_help();
+       exit(1);
+      }
+      else if (strncmp(argv[i], "-", 1)) {
+       file_name = argv[i];
+       break;
+      }
+      else {
+       printf ("Unrecognized option: %s\n", argv[i]);
+       show_help();
+       exit(1);
+      }
+    }
+  }
+  
+  if (!file_name) {
+    printf("No file name given\n");
+    show_help();
+    exit(1);
+  }
+  
+  gedcom_init();
+  setlocale(LC_ALL, "");
+  gedcom_set_debug_level(debug_level, NULL);
+  gedcom_set_compat_handling(compat_enabled);
+  gedcom_set_error_handling(mech);
+  gedcom_set_message_handler(gedcom_message_handler);
+
+  result = gom_parse_file(file_name);
+  
+  if (result == 0) {
+    char* newfile = (char*)malloc(strlen(file_name) + strlen(extension) + 2);
+    sprintf(newfile, "%s.%s", file_name, extension);
+    printf(_("Parse succeeded, now writing file '%s'\n"), newfile);
+    result = gom_write_file(newfile, NULL);
+    free(newfile);
+    if (result == 0) {
+      printf(_("Write succeeded\n"));
+    }
+    else {
+      printf(_("Write failed\n"));
+    }
+  }
+  else {
+    printf(_("Parse failed\n"));
+  }
+  return result;
+}
diff --git a/bin/gedcom-sanitize.sh b/bin/gedcom-sanitize.sh
new file mode 100755 (executable)
index 0000000..eb538ce
--- /dev/null
@@ -0,0 +1,21 @@
+# $Id$
+# $Name$
+# Test script for gedcom-check (when not installed yet)
+
+ltcmd="../libtool --mode=execute"
+for lib in ../gedcom/libgedcom.la ../gom/libgedcom_gom.la ../utf8/libutf8tools.la
+do
+  ltcmd="$ltcmd -dlopen $lib"
+done
+
+ln -s ../data/gedcom.enc .
+ln -s ../data/new.ged .
+ln -s ../iconv/glibc/.libs/ANSI_Z39.47.so .
+ln -s ../iconv/glibc/gconv-modules .
+$ltcmd $GEDCOM_TESTENV ./gedcom-sanitize $*
+result=$?
+rm gedcom.enc
+rm new.ged
+rm ANSI_Z39.47.so
+rm gconv-modules
+exit $result