Take care of const correctness.
[gedcom-parse.git] / gedcom / xref.c
1 /* Cross-reference manipulation routines.
2    Copyright (C) 2001,2002 The Genes Development Team
3    This file is part of the Gedcom parser library.
4    Contributed by Peter Verthez <Peter.Verthez@advalvas.be>, 2001.
5
6    The Gedcom parser library is free software; you can redistribute it
7    and/or modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The Gedcom parser library is distributed in the hope that it will be
12    useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the Gedcom parser library; if not, write to the
18    Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 /* $Id$ */
22 /* $Name$ */
23
24 #include "gedcom_internal.h"
25 #include "xref.h"
26 #include "hash.h"
27
28 struct xref_value def_xref_val = { XREF_NONE, "<error>", NULL };
29 static hash_t *xrefs = NULL;
30
31 char* xref_type_str[] = { N_("nothing"),
32                           N_("a family"),
33                           N_("an individual"),
34                           N_("a note"),
35                           N_("a multimedia object"),
36                           N_("a source repository"),
37                           N_("a source"),
38                           N_("a submitter"),
39                           N_("a submission record"),
40                           N_("an application-specific record"),
41                         };
42
43 struct xref_node {
44   struct xref_value xref;
45   Xref_type defined_type;
46   Xref_type used_type;
47   int defined_line;
48   int used_line;
49 };
50
51 hnode_t *xref_alloc(void *c __attribute__((unused)))
52 {
53   return malloc(sizeof *xref_alloc(NULL));
54 }
55
56 void xref_free(hnode_t *n, void *c __attribute__((unused)))
57 {
58   struct xref_node *xr = (struct xref_node *)hnode_get(n);
59   free((void*)hnode_getkey(n));
60   free(xr->xref.string);
61   free(xr);
62   free(n);
63 }
64
65 void clear_xref_node(struct xref_node *xr)
66 {
67   xr->xref.type    = XREF_NONE;
68   /* Make sure that the 'string' member always contains a valid string */
69   if (!xr->xref.string)
70     xr->xref.string  = strdup("");
71   if (!xr->xref.string) MEMORY_ERROR;
72   xr->xref.object  = NULL;
73   xr->defined_type = XREF_NONE;
74   xr->used_type    = XREF_NONE;
75   xr->defined_line = -1;
76   xr->used_line    = -1;
77 }
78
79 struct xref_node *make_xref_node()
80 {
81   struct xref_node *xr = (struct xref_node *)malloc(sizeof(struct xref_node));
82   if (xr) {
83     xr->xref.string = NULL;
84     clear_xref_node(xr);
85   }
86   else
87     MEMORY_ERROR;
88   return xr;
89 }
90
91 void cleanup_xrefs()
92 {
93   hash_free(xrefs);
94   xrefs = NULL;
95 }
96
97 void make_xref_table()
98 {
99   if (xrefs)
100     cleanup_xrefs();
101   else
102     /* Only register initially (if xrefs is still NULL) */
103     /* So that it is only registered once */
104     if (atexit(cleanup_xrefs) != 0) {
105       gedcom_warning(_("Could not register xref cleanup function"));
106     }
107   xrefs = hash_create(HASHCOUNT_T_MAX, NULL, NULL);
108   hash_set_allocator(xrefs, xref_alloc, xref_free, NULL);
109 }
110
111 int check_xref_table()
112 {
113   int result = 0;
114   hscan_t hs;
115   hnode_t *node;
116   struct xref_node *xr;
117
118   /* Check for undefined and unused xrefs */
119   hash_scan_begin(&hs, xrefs);
120   while ((node = hash_scan_next(&hs))) {
121     xr = (struct xref_node *)hnode_get(node);
122     if (xr->defined_type == XREF_NONE && xr->used_type != XREF_NONE) {
123       gedcom_error(_("Cross-reference %s used on line %d is not defined"),
124                    xr->xref.string, xr->used_line);
125       result |= 1;
126     }
127     if (xr->used_type == XREF_NONE && xr->defined_type != XREF_NONE) {
128       gedcom_warning(_("Cross-reference %s defined on line %d is never used"),
129                      xr->xref.string, xr->defined_line);
130     }
131   }
132   
133   return result;
134 }
135
136 struct xref_value *gedcom_get_by_xref(const char *key)
137 {
138   hnode_t *node = hash_lookup(xrefs, key);
139   if (node) {
140     struct xref_node *xr = (struct xref_node *)hnode_get(node);
141     return &(xr->xref);
142   }
143   else
144     return NULL;
145 }
146
147 struct xref_value *gedcom_parse_xref(char *raw_value,
148                                      Xref_ctxt ctxt, Xref_type xref_type)
149 {
150   struct xref_node *xr = NULL;
151   
152   hnode_t *node = hash_lookup(xrefs, raw_value);
153   if (node) {
154     xr = (struct xref_node *)hnode_get(node);
155   }
156   else {
157     char *key = strdup(raw_value);
158     if (key) {
159       xr = make_xref_node();
160       xr->xref.type = xref_type;
161       if (xr->xref.string)
162         free(xr->xref.string);
163       xr->xref.string = strdup(raw_value);
164       if (! xr->xref.string) MEMORY_ERROR;
165       hash_alloc_insert(xrefs, key, xr);
166     }
167     else
168       MEMORY_ERROR;
169   }
170     
171   if (ctxt == XREF_DEFINED && xr->defined_type == XREF_NONE) {
172     xr->defined_type = xref_type;
173     xr->defined_line = line_no;
174   }
175   else if (ctxt == XREF_USED && xr->used_type == XREF_NONE) {
176     xr->used_type = xref_type;
177     xr->used_line = line_no;
178   }
179   
180   if ((ctxt == XREF_DEFINED && xr->defined_type != xref_type &&
181        xr->defined_type != XREF_ANY)
182       || (ctxt == XREF_USED &&
183           (xr->defined_type != XREF_NONE && xr->defined_type != xref_type &&
184            xr->defined_type != XREF_ANY))) {
185     gedcom_error(_("Cross-reference %s previously defined as pointer to %s, "
186                    "on line %d"),
187                  xr->xref.string, xref_type_str[xr->defined_type],
188                  xr->defined_line);
189     clear_xref_node(xr);
190   }  
191   else if ((ctxt == XREF_USED && xr->used_type != xref_type)
192            || (ctxt == XREF_DEFINED &&
193                (xr->used_type != XREF_NONE && xr->used_type != xref_type))) {
194     gedcom_error(_("Cross-reference %s previously used as pointer to %s, "
195                    "on line %d"),
196                  xr->xref.string, xref_type_str[xr->used_type], xr->used_line);
197     clear_xref_node(xr);
198   }
199   
200   return &(xr->xref);
201 }