Automatically set parent family in individual, when adding child to the family.
[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 void familia_gedcom_individual_family_end(Gedcom_elt  elt,
172                                         Gedcom_ctxt parent,
173                                         Gedcom_ctxt self,
174                                         Gedcom_val  parsed_value)
175 {
176 }*/
177
178 void familia_gedcom_error (Gedcom_msg_type type, char *msg)
179 {
180         fprintf(stderr, "> %s\n", msg);
181 }
182
183
184
185 void familia_gedcom_init()
186 {
187         gedcom_set_message_handler(familia_gedcom_error);
188
189         gedcom_subscribe_to_record(REC_HEAD,
190                                 familia_gedcom_header_start,
191                                 familia_gedcom_header_end);
192         gedcom_subscribe_to_element(ELT_HEAD_SOUR,
193                                 familia_gedcom_header_source_start,
194                                 NULL);
195         gedcom_subscribe_to_element(ELT_HEAD_SOUR_VERS,
196                                 familia_gedcom_header_version_start,
197                                 NULL);
198         gedcom_subscribe_to_record(REC_FAM,
199                                 familia_gedcom_family_start,
200                                 NULL);
201         gedcom_subscribe_to_record(REC_INDI,
202                                 familia_gedcom_individual_start,
203                                 NULL);
204         gedcom_subscribe_to_element(ELT_SUB_PERS_NAME,
205                                 familia_gedcom_individual_set_first_name,
206                                 NULL);
207         gedcom_subscribe_to_element(ELT_SUB_PERS_NAME_SURN,
208                                 familia_gedcom_individual_last_name_start,
209                                 NULL);
210         gedcom_subscribe_to_element(ELT_SUB_FAMS,
211                                 familia_gedcom_individual_add_family,
212                                 NULL);
213
214         familia_memory_stack_init();
215 }
216
217 void familia_gedcom_dump_val(Gedcom_val val)
218 {
219         if (GEDCOM_IS_NULL(val)) {
220                 fprintf(stderr, "| %25s |\n", "NULL");
221         }
222         else if (GEDCOM_IS_STRING(val)) {
223                 fprintf(stderr, "| %4s | %20s |\n", "STRING", GEDCOM_STRING(val));
224         }
225         else if (GEDCOM_IS_DATE(val)) {
226                 fprintf(stderr, "| %4s | %20s |\n", "DATE", "(@todo)");
227         }
228         else if (GEDCOM_IS_AGE(val)) {
229                 fprintf(stderr, "| %4s | %20s |\n", "AGE", "(@todo)");
230         }
231         else if (GEDCOM_IS_XREF_PTR(val)) {
232                 struct xref_value *xref = NULL;
233                 fprintf(stderr, "| %25s | %15s | %4s |\n", "STRING", "OBJECT", "TYPE");
234                 xref = GEDCOM_XREF_PTR(val);
235                 fprintf(stderr, "| %25s | %15s | %4s |\n",
236                         xref->string,
237                         xref->object ? "initialized" : "NULL",
238                         (xref->type == XREF_NONE) ? "NONE" :
239                                 (xref->type == XREF_FAM ) ? "FAM" :
240                                 (xref->type == XREF_INDI) ? "INDI" :
241                                 (xref->type == XREF_NOTE) ? "NOTE" :
242                                 (xref->type == XREF_OBJE) ? "OBJE" :
243                                 (xref->type == XREF_REPO) ? "REPO" :
244                                 (xref->type == XREF_SOUR) ? "SOUR" :
245                                 (xref->type == XREF_SUBM) ? "SUBM" :
246                                 (xref->type == XREF_SUBN) ? "SUBN" :
247                                 (xref->type == XREF_ANY ) ? "ANY" :
248                                 (xref->type == XREF_USER) ? "USER" : "???");
249         } 
250         else {
251                 fprintf(stderr, "| %20s |\n", "???");
252         }
253 }