Added debugging code for dumping data from memory using GUI.
[familia.git] / src / gedcom / familia_gedcom.c
1 #include <assert.h>
2
3 #include "familia_gedcom.h"
4
5 #include "../storage/storage.h"
6 #include "../storage/individual.h"
7 #include "../memory_stack.h"
8
9 Gedcom_ctxt familia_gedcom_header_start (Gedcom_rec rec,
10                                         int level,
11                                         Gedcom_val xref,
12                                         char *tag,
13                                         char *raw_value,
14                                         int parsed_tag,
15                                         Gedcom_val parsed_value)
16 {
17         fprintf(stderr, "The header starts (l: %d, tag: %s, raw:%s) \n", level, tag, raw_value);
18         return (Gedcom_ctxt)1;
19 }
20
21 void familia_gedcom_header_end (Gedcom_rec rec, Gedcom_ctxt self)
22 {
23         /* context will print as "1" */
24         fprintf(stderr, "The header ends, context is %d\n", (int)self);
25 }
26
27 Gedcom_ctxt familia_gedcom_header_source_start(Gedcom_elt  elt,
28                                         Gedcom_ctxt parent,
29                                         int         level,
30                                         char*       tag,
31                                         char*       raw_value,
32                                         int         parsed_tag,
33                                         Gedcom_val  parsed_value)
34 {
35         char *source = GEDCOM_STRING(parsed_value);
36         printf("This file was created by %s\n", source);
37         return parent;
38 }
39
40 /*
41 void familia_gedcom_header_source_end(Gedcom_elt  elt,
42                                         Gedcom_ctxt parent,
43                                         Gedcom_ctxt self,
44                                         Gedcom_val  parsed_value)
45 {
46         printf("End of the source description\n");
47 }
48 */
49
50 Gedcom_ctxt familia_gedcom_header_version_start(Gedcom_elt  elt,
51                                         Gedcom_ctxt parent,
52                                         int         level,
53                                         char*       tag,
54                                         char*       raw_value,
55                                         int         parsed_tag,
56                                         Gedcom_val  parsed_value)
57 {
58         char *source = GEDCOM_STRING(parsed_value);
59         printf("Program version: %s (l: %d, tag: %s, raw:%s) \n", source, level, tag, raw_value);
60         return parent;
61 }
62 /*
63 void familia_gedcom_header_version_end(Gedcom_elt  elt,
64                                         Gedcom_ctxt parent,
65                                         Gedcom_ctxt self,
66                                         Gedcom_val  parsed_value)
67 {
68         printf("End of the version description\n");
69 }*/
70
71 Gedcom_ctxt familia_gedcom_family_start (Gedcom_rec rec,
72                                         int level,
73                                         Gedcom_val xref,
74                                         char *tag,
75                                         char *raw_value,
76                                         int parsed_tag,
77                                         Gedcom_val parsed_value)
78 {
79         struct familia_family * family = NULL;
80         struct xref_value *xr = GEDCOM_XREF_PTR(xref);
81
82         family = familia_memory_stack_find(xr->string, FS_FAMILY);
83
84         return (Gedcom_ctxt)family;
85 }
86 /*
87 void familia_gedcom_family_end (Gedcom_rec rec, Gedcom_ctxt self)
88 {
89         familia_storage_add_family(familia_storage_get_current(), self);
90 }
91 */
92 Gedcom_ctxt familia_gedcom_individual_start (Gedcom_rec rec,
93                                         int level,
94                                         Gedcom_val xref,
95                                         char *tag,
96                                         char *raw_value,
97                                         int parsed_tag,
98                                         Gedcom_val parsed_value)
99 {
100         struct familia_individual * individual = NULL;
101         struct xref_value *xr = GEDCOM_XREF_PTR(xref);
102
103         individual = familia_memory_stack_find(xr->string, FS_INDIVIDUAL);
104
105         return (Gedcom_ctxt)individual;
106 }
107 /*
108 void familia_gedcom_individual_end (Gedcom_rec rec, Gedcom_ctxt self)
109 {
110         familia_storage_add_individual(familia_storage_get_current(), self);
111 }
112 */
113 Gedcom_ctxt familia_gedcom_individual_set_first_name(Gedcom_elt  elt,
114                                         Gedcom_ctxt parent,
115                                         int         level,
116                                         char*       tag,
117                                         char*       raw_value,
118                                         int         parsed_tag,
119                                         Gedcom_val  parsed_value)
120 {
121         familia_individual_set_first_name(parent, GEDCOM_STRING(parsed_value));
122         return parent;
123 }
124 /*
125 void familia_gedcom_individual_first_name_end(Gedcom_elt  elt,
126                                         Gedcom_ctxt parent,
127                                         Gedcom_ctxt self,
128                                         Gedcom_val  parsed_value)
129 {
130 }
131 */
132 Gedcom_ctxt familia_gedcom_individual_last_name_start(Gedcom_elt  elt,
133                                         Gedcom_ctxt parent,
134                                         int         level,
135                                         char*       tag,
136                                         char*       raw_value,
137                                         int         parsed_tag,
138                                         Gedcom_val  parsed_value)
139 {
140         if (GEDCOM_IS_STRING(parsed_value)) {
141                 familia_individual_set_last_name(parent, GEDCOM_STRING(parsed_value));
142         }
143         return parent;
144 }
145 /*
146 void familia_gedcom_individual_last_name_end(Gedcom_elt  elt,
147                                         Gedcom_ctxt parent,
148                                         Gedcom_ctxt self,
149                                         Gedcom_val  parsed_value)
150 {
151 }
152 */
153
154 Gedcom_ctxt familia_gedcom_individual_add_family(Gedcom_elt  elt,
155                                         Gedcom_ctxt parent,
156                                         int         level,
157                                         char*       tag,
158                                         char*       raw_value,
159                                         int         parsed_tag,
160                                         Gedcom_val  parsed_value)
161 {
162         void * object = NULL;
163         struct xref_value *xr = GEDCOM_XREF_PTR(parsed_value);
164
165         object = familia_memory_stack_find(xr->string, FS_FAMILY);
166         familia_individual_add_family(parent, object);
167
168         return parent;
169 }
170
171 /**
172  * Adds child to the family
173  */
174 Gedcom_ctxt familia_gedcom_family_add_child(Gedcom_elt  elt,
175                                         Gedcom_ctxt parent,
176                                         int         level,
177                                         char*       tag,
178                                         char*       raw_value,
179                                         int         parsed_tag,
180                                         Gedcom_val  parsed_value)
181 {
182         void * object = NULL;
183         /* XREF_PTR(FAM) */
184         struct xref_value *xr = GEDCOM_XREF_PTR(parsed_value);
185
186         object = familia_memory_stack_find(xr->string, FS_FAMILY);
187         /* parent: REC_INDI */
188         familia_family_add_child(object, parent);
189
190         return parent;
191 }
192
193 /*
194 void familia_gedcom_individual_family_end(Gedcom_elt  elt,
195                                         Gedcom_ctxt parent,
196                                         Gedcom_ctxt self,
197                                         Gedcom_val  parsed_value)
198 {
199 }*/
200
201 void familia_gedcom_error (Gedcom_msg_type type, char *msg)
202 {
203         fprintf(stderr, "> %s\n", msg);
204 }
205
206
207
208 void familia_gedcom_init()
209 {
210         gedcom_set_message_handler(familia_gedcom_error);
211
212         gedcom_subscribe_to_record(REC_HEAD,
213                                 familia_gedcom_header_start,
214                                 familia_gedcom_header_end);
215         gedcom_subscribe_to_element(ELT_HEAD_SOUR,
216                                 familia_gedcom_header_source_start,
217                                 NULL);
218         gedcom_subscribe_to_element(ELT_HEAD_SOUR_VERS,
219                                 familia_gedcom_header_version_start,
220                                 NULL);
221         gedcom_subscribe_to_record(REC_FAM,
222                                 familia_gedcom_family_start,
223                                 NULL);
224         gedcom_subscribe_to_record(REC_INDI,
225                                 familia_gedcom_individual_start,
226                                 NULL);
227         gedcom_subscribe_to_element(ELT_SUB_PERS_NAME,
228                                 familia_gedcom_individual_set_first_name,
229                                 NULL);
230         gedcom_subscribe_to_element(ELT_SUB_PERS_NAME_SURN,
231                                 familia_gedcom_individual_last_name_start,
232                                 NULL);
233         gedcom_subscribe_to_element(ELT_SUB_FAMS,
234                                 familia_gedcom_family_add_child,
235                                 NULL);
236         gedcom_subscribe_to_element(ELT_SUB_FAMC,
237                                 familia_gedcom_family_add_child,
238                                 NULL);
239
240         familia_memory_stack_init();
241 }
242
243 void familia_gedcom_dump_val(Gedcom_val val)
244 {
245         if (GEDCOM_IS_NULL(val)) {
246                 fprintf(stderr, "| %25s |\n", "NULL");
247         }
248         else if (GEDCOM_IS_STRING(val)) {
249                 fprintf(stderr, "| %4s | %20s |\n", "STRING", GEDCOM_STRING(val));
250         }
251         else if (GEDCOM_IS_DATE(val)) {
252                 fprintf(stderr, "| %4s | %20s |\n", "DATE", "(@todo)");
253         }
254         else if (GEDCOM_IS_AGE(val)) {
255                 fprintf(stderr, "| %4s | %20s |\n", "AGE", "(@todo)");
256         }
257         else if (GEDCOM_IS_XREF_PTR(val)) {
258                 struct xref_value *xref = NULL;
259                 fprintf(stderr, "| %25s | %15s | %4s |\n", "STRING", "OBJECT", "TYPE");
260                 xref = GEDCOM_XREF_PTR(val);
261                 fprintf(stderr, "| %25s | %15s | %4s |\n",
262                         xref->string,
263                         xref->object ? "initialized" : "NULL",
264                         (xref->type == XREF_NONE) ? "NONE" :
265                                 (xref->type == XREF_FAM ) ? "FAM" :
266                                 (xref->type == XREF_INDI) ? "INDI" :
267                                 (xref->type == XREF_NOTE) ? "NOTE" :
268                                 (xref->type == XREF_OBJE) ? "OBJE" :
269                                 (xref->type == XREF_REPO) ? "REPO" :
270                                 (xref->type == XREF_SOUR) ? "SOUR" :
271                                 (xref->type == XREF_SUBM) ? "SUBM" :
272                                 (xref->type == XREF_SUBN) ? "SUBN" :
273                                 (xref->type == XREF_ANY ) ? "ANY" :
274                                 (xref->type == XREF_USER) ? "USER" : "???");
275         } 
276         else {
277                 fprintf(stderr, "| %20s |\n", "???");
278         }
279 }