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