Test writing of a read gedcom file.
[gedcom-parse.git] / t / src / gom_write.c
1 /* Test program for the Gedcom library.
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.h"
25 #include "gom.h"
26 #include "output.h"
27 #include "portability.h"
28 #include <locale.h>
29 #include <stdio.h>
30
31 #define WRITE_GEDCOM "gom_write.ged"
32 #define PROG_NAME "writegomtest"
33 #define PROG_VERSION "3.14"
34
35 void gedcom_message_handler(Gedcom_msg_type type, char *msg)
36 {
37   if (type == MESSAGE)
38     output(1, "MESSAGE: ");
39   else if (type == WARNING)
40     output(1, "WARNING: ");
41   else if (type == ERROR)
42     output(1, "ERROR: ");
43   output(1, "%s\n", msg);
44 }
45
46 void show_help ()
47 {
48   printf("gedcom-parse test program for libgedcom\n\n");
49   printf("Usage:  writegomtest [options]\n");
50   printf("Options:\n");
51   printf("  -h    Show this help text\n");
52   printf("  -q    No output to standard output\n");
53   printf("  -o <outfile>  File to generate errors to (def. testgedcom.out)\n");
54   printf("  -i <gedfile>  File to read gedcom from (default: new file)\n");
55   printf("  -w <gedfile>  File to write gedcom to (def. %s)\n", WRITE_GEDCOM);
56   printf("  -e <encoding> Encoding (UNICODE, ASCII, ANSEL, ...: see gedcom.enc)\n");
57   printf("  -u <unicode_enc> Encoding details for Unicode\n");
58   printf("        <unicode_enc> can be: HILO, LOHI, HILO_BOM, LOHI_BOM\n");
59   printf("  -t <terminator>  Line terminator\n");
60   printf("        <terminator> can be CR, LF, CR_LF, LF_CR\n");
61 }
62
63 int update_header(char* encoding)
64 {
65   struct header* head = NULL;
66   char* value;
67   char* long_note = "This note is for testing the continuation stuff\n"
68     "Some Specials: This line is very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long but too long (255 caharcters is the limit), so this is going over the border\n"
69     "And now we have an at character: @, which should be doubled";
70
71   head = gom_get_header();
72   if (head == NULL)
73     return 1;
74   else {
75     /*
76     value = gom_set_string(&head->charset.name, encoding);
77     if (value == NULL || strcmp(value, encoding))
78       return 1;
79     else
80       return 0;
81     */
82     value = gom_set_string(&head->note, long_note);
83     if (value == NULL || strcmp(value, long_note))
84       return 1;
85     else
86       return 0;
87   }
88 }
89
90 int main(int argc, char* argv[])
91 {
92   int result;
93   int total_conv_fails = 0;
94   char* outfilename = NULL;
95   char* infilename  = NULL;
96   char* gedfilename = WRITE_GEDCOM;
97   char* encoding    = "ASCII";
98   Encoding enc      = ONE_BYTE;
99   Enc_bom bom       = WITHOUT_BOM;
100   Enc_line_end end  = END_LF;
101   
102   if (argc > 1) {
103     int i;
104     for (i=1; i<argc; i++) {
105       if (!strncmp(argv[i], "-h", 3)) {
106         show_help();
107         exit(1);
108       }
109       else if (!strncmp(argv[i], "-q", 3)) {
110         output_set_quiet(1);
111       }
112       else if (!strncmp(argv[i], "-o", 3)) {
113         i++;
114         if (i < argc) {
115           outfilename = argv[i];
116         }
117         else {
118           printf ("Missing output file name\n");
119           show_help();
120           exit(1);
121         }
122       }
123       else if (!strncmp(argv[i], "-w", 3)) {
124         i++;
125         if (i < argc) {
126           gedfilename = argv[i];
127         }
128         else {
129           printf ("Missing output file name\n");
130           show_help();
131           exit(1);
132         }
133       }
134       else if (!strncmp(argv[i], "-i", 3)) {
135         i++;
136         if (i < argc) {
137           infilename = argv[i];
138         }
139         else {
140           printf ("Missing input file name\n");
141           show_help();
142           exit(1);
143         }
144       }
145       else if (!strncmp(argv[i], "-e", 3)) {
146         i++;
147         if (i < argc) {
148           encoding = argv[i];
149         }
150         else {
151           printf ("Missing encoding\n");
152           show_help();
153           exit(1);
154         }
155       }
156       else if (!strncmp(argv[i], "-u", 3)) {
157         i++;
158         if (i < argc) {
159           char* details = argv[i];
160           if (!strncmp(details, "HILO", 4))
161             enc = TWO_BYTE_HILO;
162           else if (!strncmp(details, "LOHI", 4))
163             enc = TWO_BYTE_LOHI;
164           else {
165             printf("Unknown encoding details %s\n", details);
166             show_help();
167             exit(1);
168           }
169           if (!strncmp(details+5, "BOM", 4))
170             bom = WITH_BOM;
171         }
172         else {
173           printf ("Missing encoding details\n");
174           show_help();
175           exit(1);
176         }
177       }
178       else if (!strncmp(argv[i], "-t", 3)) {
179         i++;
180         if (i < argc) {
181           char* term = argv[i];
182           if (!strncmp(term, "CR", 3))
183             end = END_CR;
184           else if (!strncmp(term, "LF", 3))
185             end = END_LF;
186           else if (!strncmp(term, "CR_LF", 6))
187             end = END_CR_LF;
188           else if (!strncmp(term, "LF_CR", 6))
189             end = END_LF_CR;
190           else {
191             printf("Unknown terminator: %s\n", term);
192             show_help();
193             exit(1);
194           }
195         }
196         else {
197           printf ("Missing terminator\n");
198           show_help();
199           exit(1);
200         }
201       }
202       else {
203         printf ("Unrecognized option: %s\n", argv[i]);
204         show_help();
205         exit(1);
206       }
207     }
208   }
209   
210   gedcom_init();
211   setlocale(LC_ALL, "");
212   gedcom_set_message_handler(gedcom_message_handler);
213   gedcom_write_set_encoding(encoding, enc, bom);
214   gedcom_write_set_line_terminator(end);
215
216   output_open(outfilename);
217
218   if (infilename) {
219     result = gom_parse_file(infilename);
220   }
221   else {
222     result = gom_new_model();
223     if (result == 0)
224       result |= update_header(encoding);
225   }
226   if (result == 0)
227     result |= gom_write_file(gedfilename, &total_conv_fails);
228   if (result == 0 && total_conv_fails == 0) {
229     output(1, "Test succeeded\n");
230   }
231   else {
232     output(1, "Test failed: %d\n", result);
233   }
234
235   output_close();
236   return result;
237 }