From: Peter Verthez Date: Wed, 28 Aug 2002 16:19:32 +0000 (+0000) Subject: New files for parsing age values. X-Git-Url: https://git.dlugolecki.net.pl/?a=commitdiff_plain;h=6432f3c3174135b38fc4084556cc487619510366;p=gedcom-parse.git New files for parsing age values. --- diff --git a/gedcom/age.c b/gedcom/age.c new file mode 100644 index 0000000..c597ef3 --- /dev/null +++ b/gedcom/age.c @@ -0,0 +1,132 @@ +/* Age manipulation routines. + Copyright (C) 2001,2002 The Genes Development Team + This file is part of the Gedcom parser library. + Contributed by Peter Verthez , 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 +#include +#include +#include +#include +#include "gedcom_internal.h" +#include "age.h" + +struct age_value age_s; +struct age_value def_age_val; + +void copy_age(struct age_value *to, struct age_value from) +{ + memcpy(to, &from, sizeof(struct age_value)); +} + +void init_age(struct age_value *age) +{ + age->type = AGE_UNRECOGNIZED; + age->mod = AGE_NO_MODIFIER; + age->years = -1; + age->months = -1; + age->days = -1; +} + +int parse_numeric_age(struct age_value *age, char *ptr) +{ + char *endptr; + while (ptr) { + long int number = strtol(ptr, &endptr, 10); + if (errno == ERANGE || number < 0 || number > INT_MAX) { + gedcom_error(_("Number out of range in age")); + return 1; + } + else { + ptr = endptr; + if (*ptr == '\0') + break; + else if (*ptr == 'Y' || *ptr == 'y') { + if (age->years == -1) + age->years = number; + else { + gedcom_error(_("Duplicate year indication in age")); + return 1; + } + } + else if (*ptr == 'M' || *ptr == 'm') { + if (age->months == -1) + age->months = number; + else { + gedcom_error(_("Duplicate month indication in age")); + return 1; + } + } + else if (*ptr == 'D' || *ptr == 'd') { + if (age->days == -1) + age->days = number; + else { + gedcom_error(_("Duplicate day indication in age")); + return 1; + } + } + else { + gedcom_error(_("Unrecognized indication in age: '%s'"), ptr); + return 1; + } + ptr++; + while (*ptr == ' ') ptr++; + } + } + return 0; +} + +struct age_value gedcom_parse_age(char* line_value) +{ + char *ptr = line_value; + init_age(&age_s); + init_age(&def_age_val); + + if (*ptr == '<') { + age_s.mod = AGE_LESS_THAN; + ptr++; + while (*ptr == ' ') ptr++; + } + else if (*ptr == '>') { + age_s.mod = AGE_GREATER_THAN; + ptr++; + while (*ptr == ' ') ptr++; + } + + if (isdigit(*ptr)) { + int result = parse_numeric_age(&age_s, ptr); + if (result == 0) { + age_s.type = AGE_NUMERIC; + } + } + else if (!strcasecmp(line_value, "CHILD")) + age_s.type = AGE_CHILD; + else if (!strcasecmp(line_value, "INFANT")) + age_s.type = AGE_INFANT; + else if (!strcasecmp(line_value, "STILLBORN")) + age_s.type = AGE_STILLBORN; + else + gedcom_error(_("Unrecognized age format")); + if (age_s.type == AGE_UNRECOGNIZED) + strncpy(age_s.phrase, line_value, MAX_PHRASE_LEN + 1); + return age_s; +} + diff --git a/gedcom/age.h b/gedcom/age.h new file mode 100644 index 0000000..92c16b0 --- /dev/null +++ b/gedcom/age.h @@ -0,0 +1,38 @@ +/* Header for age manipulation routines. + Copyright (C) 2001 The Genes Development Team + This file is part of the Gedcom parser library. + Contributed by Peter Verthez , 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$ */ + +#ifndef __AGE_H +#define __AGE_H + +#include +#include "gedcom_internal.h" +#include "gedcom.h" + +extern struct age_value age_s; + +void copy_age(struct age_value *to, struct age_value from); + +#define GEDCOM_MAKE_AGE(VAR, AGE) \ + GEDCOM_MAKE(VAR, AGE, GV_AGE_VALUE, age_val) + +#endif /* __AGE_H */