/* Copyright 2002 Cyril Picard This file is part of the GEDCOMParser library (developed within the Genealogy Free Software Tools project). The GEDCOMParser library is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. The GEDCOMParser 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 General Public License for more details. You should have received a copy of the GNU General Public License along with the GEDCOMParser library ; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifdef HAVE_CONFIG_H #include #endif #include "GEDCOMParser/Parser.hh" #include "GEDCOMParser/GEDCOMFactory.hh" #include "GEDCOMParser/GEDCOMFactoryAccessor.hh" #include "GEDCOMParser/ParseErrorManagement/ParseErrorManager.hh" #include "GEDCOMParser/Predicates/PredicateIndividualRecordsAdapter.hh" #include "GEDCOMParser/Predicates/GenericPredicateIndividualRecords.hh" #include "GEDCOMParser/Predicates/PredicateIndividualRecords.hh" #include "GEDCOMParser/Predicates/HasChild.hh" #include "GEDCOMParser/Predicates/IsMale.hh" #include "GEDCOMParser/Predicates/HasSpouse.hh" #include "GEDCOMParser/Predicates/NameEqualsTo.hh" #include #include #include #include #include #include #include #include #include "gettext.h" #define _(String) gettext(String) #define HELP_MESSAGE "Usage : gedcompredicates [--help] [--version] [filename]" struct arguments { std::string gedcom_filename; int version; int help; }; void countrecords(GEDCOMParser::IndividualRecords_t const &individus, std::string const &message, GEDCOMParser::Predicates::PredicateIndividualRecords const * const predicate) { GEDCOMParser::Predicates::PredicateIndividualRecordsAdapter adapter(predicate); int count = std::count_if(individus.begin(), individus.end(), adapter); std::cout << message << count << std::endl; } void displayrecords(GEDCOMParser::IndividualRecords_t const &individus, GEDCOMParser::Predicates::PredicateIndividualRecords const * const predicate) { GEDCOMParser::Predicates::PredicateIndividualRecordsAdapter adapter(predicate); GEDCOMParser::IndividualRecords_t::const_iterator individu_iter = find_if(individus.begin(), individus.end(), adapter); do { if (individu_iter != individus.end()) { std::cout << individu_iter->second->getPersonalNames()[0]->getSurn() << " " << individu_iter->second->getPersonalNames()[0]->getGivn() << std::endl; individu_iter = find_if(++individu_iter, individus.end(), adapter); } } while (individu_iter != individus.end()); } int main(int argc, char **argv) { setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, PACKAGE_LOCALE_DIR); textdomain (PACKAGE); struct arguments arguments; /* Default values. */ arguments.version = 0; arguments.help = 0; int c; static struct option long_options[] = { /* These options set a flag. */ {"version", no_argument, &arguments.version, 1}, {"help", no_argument, &arguments.help, 1}, {0, 0, 0, 0} }; /* getopt_long stores the option index here. */ int option_index = 0; c = getopt_long (argc, argv, "vm",long_options, &option_index); while (c != -1) { switch(c) { case 0: break; case '?': break; default: abort(); } c = getopt_long (argc, argv, "",long_options, &option_index); } if (arguments.help == 1) { std::cout << HELP_MESSAGE << std::endl; } else if (arguments.version == 1) { std::cout << _("gedcompredicates is a part of ") << PACKAGE << " " << VERSION << std::endl; } else { GEDCOMParser::GEDCOMFactory factory; GEDCOMParser::GEDCOMFactoryAccessor::Init(&factory); GEDCOMParser::Parser viewparser(&factory); if (optind == (argc - 1)) { arguments.gedcom_filename = argv[optind++]; } else { arguments.gedcom_filename = ""; } GEDCOMParser::ParseErrorManagement::ParseErrorManager parse_error_manager = viewparser.Parse(arguments.gedcom_filename); if (parse_error_manager.ErrorCount() == 0) { std::cout << _("Parsing passed") << std::endl; } else { std::cout << std::endl << _("Parsing finished with errors : ") << std::endl; for (std::vector < GEDCOMParser::ParseErrorManagement::ParseError >::const_iterator err_iter = parse_error_manager.getErrors().begin(); err_iter != parse_error_manager.getErrors().end(); err_iter++) { std::cerr << err_iter->getDescription() << std::endl; } } GEDCOMParser::LineageLinkageGedcom const * const lineage = viewparser.getLineageLinkageGedcom(); if (lineage != 0) { GEDCOMParser::IndividualRecords_t individus = lineage->getIndividualRecords(); std::cout << _("Individuals records : ") << individus.size() << std::endl; GEDCOMParser::Predicates::PredicateIndividualRecords * predicate; predicate = GEDCOMParser::Predicates::getGenericPredicateIndividualRecords(std::compose1(std::logical_not(), std::bind1st(GEDCOMParser::Predicates::HasChild(), lineage))); countrecords(individus, _("Individuals records without children : "), predicate); delete predicate; predicate = 0; predicate = GEDCOMParser::Predicates::getGenericPredicateIndividualRecords(std::compose1(std::logical_not(), std::bind1st(GEDCOMParser::Predicates::HasChild(), lineage))); countrecords(individus, _("Individuals records female : "), predicate); delete predicate; predicate = 0; predicate = new GEDCOMParser::Predicates::IsMale(); countrecords(individus, _("Individuals records male : "), predicate); delete predicate; predicate = 0; predicate = GEDCOMParser::Predicates::getGenericPredicateIndividualRecords(std::compose1(std::logical_not(), std::bind1st(GEDCOMParser::Predicates::HasSpouse(), lineage))); countrecords(individus, _("Individuals records bachelor : "), predicate); delete predicate; predicate = 0; // count of individus with // -name = 'PICARD' std::string const name = "PICARD"; predicate = GEDCOMParser::Predicates::getGenericPredicateIndividualRecords(std::bind1st(GEDCOMParser::Predicates::NameEqualsTo(), name)); countrecords(individus, _("Individuals records named PICARD : "), predicate); displayrecords(individus, predicate); delete predicate; predicate = 0; // count of individus with // - sex = Female // - do not have child predicate = GEDCOMParser::Predicates::getGenericPredicateIndividualRecords( std::compose2(std::logical_and(), std::compose1( std::logical_not(), std::bind1st(GEDCOMParser::Predicates::HasChild(),lineage)), std::compose1(std::logical_not(), GEDCOMParser::Predicates::IsMale()) ) ); countrecords(individus, _("Individuals records sex Female, without child : "), predicate); displayrecords(individus, predicate); delete predicate; predicate = 0; // count of individus with // - name = 'PICARD' // - sex = Male // - do not have child predicate = GEDCOMParser::Predicates::getGenericPredicateIndividualRecords( std::compose2(std::logical_and(), std::compose1( std::logical_not(), std::bind1st(GEDCOMParser::Predicates::HasChild(),lineage)), std::compose2(std::logical_and(), GEDCOMParser::Predicates::IsMale(), std::bind1st(GEDCOMParser::Predicates::NameEqualsTo(), name) ) ) ); countrecords(individus, _("Individuals records named PICARD, sex Male, without child : "), predicate); displayrecords(individus, predicate); delete predicate; predicate = 0; GEDCOMParser::FamilyRecords_t families = lineage->getFamilyRecords(); std::cout << _("Family records : ") << families.size() << std::endl; } else { std::cerr << _("No transmission found") << std::endl; } } return 0; }