Allow to lookup Italian verbs and show pronouns.
[mverbiste] / verbiste / checkxml.cpp
1 /*  $Id: checkxml.cpp,v 1.3 2010/04/24 22:54:57 sarrazip Exp $
2     checkxml.cpp - Checks on the integrity of the XML files
3
4     verbiste - French conjugation system
5     Copyright (C) 2003-2006 Pierre Sarrazin <http://sarrazip.com/>
6
7     This program is free software; you can redistribute it and/or
8     modify it under the terms of the GNU General Public License
9     as published by the Free Software Foundation; either version 2
10     of the License, or (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20     02111-1307, USA.
21 */
22
23 #ifndef VERBSFRXML
24 #error VERBSFRXML expected to be a macro designating the verbs-fr.xml file
25 #endif
26
27 #include <iostream>
28 #include <set>
29 #include <stdlib.h>
30
31 #include <libxml/xmlmemory.h>
32 #include <libxml/parser.h>
33
34 using namespace std;
35
36
37 static const string testName = "checkxml";
38
39 int main()
40 {
41     xmlDocPtr doc = xmlParseFile(VERBSFRXML);
42     if (doc == NULL)
43     {
44         cout << testName << ": could not open " << VERBSFRXML << endl;
45         return EXIT_FAILURE;
46     }
47
48     xmlNodePtr root = xmlDocGetRootElement(doc);
49     if (root == NULL)
50     {
51         cout << testName << ": could not get XML root element" << endl;
52         return EXIT_FAILURE;
53     }
54     if (xmlStrcmp(root->name, reinterpret_cast<const xmlChar *>("verbs-fr")) != 0)
55     {
56         cout << testName << ": root element is not <verbs-fr>" << endl;
57         return EXIT_FAILURE;
58     }
59
60     size_t numErrors = 0;
61     size_t vCounter = 0;
62     set<string> infinitiveSet;
63     for (xmlNodePtr v = root->xmlChildrenNode; v != NULL; v = v->next)
64     {
65         //cout << "v->name=\"" << v->name << "\"\n";
66         if (xmlStrcmp(v->name, reinterpret_cast<const xmlChar *>("text")) == 0
67                 || xmlStrcmp(v->name, reinterpret_cast<const xmlChar *>("comment")) == 0)
68             continue;
69
70         ++vCounter;
71         xmlNodePtr i = v->xmlChildrenNode;
72         if (i == NULL || i->xmlChildrenNode == NULL)
73         {
74             cout << testName << ": missing <i> node at <v> #" << vCounter << endl;
75             ++numErrors;
76             continue;
77         }
78
79         string inf = reinterpret_cast<char *>(
80                             xmlNodeListGetString(doc, i->xmlChildrenNode, 1));
81         if (infinitiveSet.find(inf) != infinitiveSet.end())
82         {
83             cout << testName << ": infinitive \"" << inf
84                     << "\" found more than once at <v> #"
85                     << vCounter << endl;
86             ++numErrors;
87             continue;
88         }
89
90         infinitiveSet.insert(inf);
91     }
92
93     cout << numErrors << " error(s) found.\n";
94     return numErrors == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
95 }