Removed support for testing with dmalloc (valgrind is better...).
[gedcom-parse.git] / gedcom / date.c
index c145f213d3fe9a6c6911e1980c9b891025b5bc93..1a2232a11f05a54376eec3e59ce7cf5cce4aa40e 100644 (file)
@@ -1,5 +1,5 @@
 /* Date manipulation routines.
-   Copyright (C) 2001 The Genes Development Team
+   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.
 
@@ -21,7 +21,6 @@
 /* $Id$ */
 /* $Name$ */
 
-#include <stdio.h>
 #include "gedcom_internal.h"
 #include "sdncal.h"
 #include "date.h"
@@ -32,6 +31,8 @@ struct date date_s;
 struct date_value def_date_val;
 struct date def_date;
 
+const char* curr_line_value;
+
 int max_month[] = { 12,  /* CAL_GREGORIAN */
                    12,  /* CAL_JULIAN */
                    13,  /* CAL_HEBREW */
@@ -39,6 +40,14 @@ int max_month[] = { 12,  /* CAL_GREGORIAN */
                    0    /* CAL_UNKNOWN */
                   };
 
+typedef long int (*cal_func_type) (int, int, int);
+
+cal_func_type cal_func[] = { &GregorianToSdn,   /* CAL_GREGORIAN */
+                            &JulianToSdn,      /* CAL_JULIAN */
+                            &JewishToSdn,      /* CAL_JEWISH */
+                            &FrenchToSdn       /* CAL_FRENCH_REV */
+                           };
+
 void copy_date(struct date *to, struct date from)
 {
   memcpy(to, &from, sizeof(struct date));
@@ -60,7 +69,7 @@ void init_date(struct date *d)
 }
 
 struct date_value make_date_value(Date_value_type t, struct date d1,
-                                 struct date d2, char* p)
+                                 struct date d2, const char* p)
 {
   dv_s.type = t;
   copy_date(&dv_s.date1, d1);
@@ -75,6 +84,7 @@ void make_date_complete(struct date *d)
     d->type = DATE_UNRECOGNIZED;
   else {
     struct date end_date;
+    cal_func_type to_sdn;
     if (d->day == -1 || d->month == -1 || d->year == -1) {
       d->type = DATE_BOUNDED;
       copy_date(&end_date, *d);
@@ -95,42 +105,21 @@ void make_date_complete(struct date *d)
     else {
       d->type = DATE_EXACT;
     }
-    
-    if (d->cal == CAL_GREGORIAN) {
-      d->sdn1 = GregorianToSdn(d->year, d->month, d->day);
-      if (d->type == DATE_BOUNDED) {
-       d->sdn2 = GregorianToSdn(end_date.year, end_date.month, end_date.day);
-       d->sdn2 -= 1;
-      }
-    }
-    else if (d->cal == CAL_JULIAN) {
-      d->sdn1 = JulianToSdn(d->year, d->month, d->day);
-      if (d->type == DATE_BOUNDED) {
-       d->sdn2 = JulianToSdn(end_date.year, end_date.month, end_date.day);
-       d->sdn2 -= 1;
-      }
-    }
-    else if (d->cal == CAL_HEBREW) {
-      d->sdn1 = JewishToSdn(d->year, d->month, d->day);
-      if (d->type == DATE_BOUNDED) {
-       d->sdn2 = JewishToSdn(end_date.year, end_date.month, end_date.day);
-       d->sdn2 -= 1;
-      }
-    }
-    else if (d->cal == CAL_FRENCH_REV) {
-      d->sdn1 = FrenchToSdn(d->year, d->month, d->day);
-      if (d->type == DATE_BOUNDED) {
-       d->sdn2 = FrenchToSdn(end_date.year, end_date.month, end_date.day);
-       d->sdn2 -= 1;
-      }
+
+    to_sdn = cal_func[d->cal];
+    d->sdn1 = (*to_sdn)(d->year, d->month, d->day);
+    if (d->type == DATE_BOUNDED) {
+      d->sdn2 = (*to_sdn)(end_date.year, end_date.month, end_date.day);
+      d->sdn2 -= 1;
     }
   }
 }
 
-struct date_value gedcom_parse_date(char* line_value)
+struct date_value gedcom_parse_date(const char* line_value)
 {
   init_date(&date_s);
   init_date(&def_date);
+  curr_line_value = line_value;
   init_gedcom_date_lex(line_value);
   gedcom_date_parse();
   close_gedcom_date_lex();