cdf8b0638ed966a9c9e338b9d8069a0f7e918975
[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("  -w <gedfile>  File to write gedcom to (def. %s)\n", WRITE_GEDCOM);
55   printf("  -e <encoding> Encoding (UNICODE, ASCII, ANSEL, ...: see gedcom.enc)\n");
56   printf("  -u <unicode_enc> Encoding details for Unicode\n");
57   printf("        <unicode_enc> can be: HILO, LOHI, HILO_BOM, LOHI_BOM\n");
58   printf("  -t <terminator>  Line terminator\n");
59   printf("        <terminator> can be CR, LF, CR_LF, LF_CR\n");
60 }
61
62 int update_header(char* encoding)
63 {
64   struct header* head = NULL;
65   char* value;
66   char* long_note = "This note is for testing the continuation stuff\n"
67     "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"
68     "And now we have an at character: @, which should be doubled";
69
70   head = gom_get_header();
71   if (head == NULL)
72     return 1;
73   else {
74     /*
75     value = gom_set_string(&head->charset.name, encoding);
76     if (value == NULL || strcmp(value, encoding))
77       return 1;
78     else
79       return 0;
80     */
81     value = gom_set_string(&head->note, long_note);
82     if (value == NULL || strcmp(value, long_note))
83       return 1;
84     else
85       return 0;
86   }
87 }
88
89 int main(int argc, char* argv[])
90 {
91   int result;
92   int total_conv_fails = 0;
93   char* outfilename = NULL;
94   char* gedfilename = WRITE_GEDCOM;
95   char* encoding    = "ASCII";
96   Encoding enc      = ONE_BYTE;
97   Enc_bom bom       = WITHOUT_BOM;
98   Enc_line_end end  = END_LF;
99   
100   if (argc > 1) {
101     int i;
102     for (i=1; i<argc; i++) {
103       if (!strncmp(argv[i], "-h", 3)) {
104         show_help();
105         exit(1);
106       }
107       else if (!strncmp(argv[i], "-q", 3)) {
108         output_set_quiet(1);
109       }
110       else if (!strncmp(argv[i], "-o", 3)) {
111         i++;
112         if (i < argc) {
113           outfilename = argv[i];
114         }
115         else {
116           printf ("Missing output file name\n");
117           show_help();
118           exit(1);
119         }
120       }
121       else if (!strncmp(argv[i], "-w", 3)) {
122         i++;
123         if (i < argc) {
124           gedfilename = argv[i];
125         }
126         else {
127           printf ("Missing output file name\n");
128           show_help();
129           exit(1);
130         }
131       }
132       else if (!strncmp(argv[i], "-e", 3)) {
133         i++;
134         if (i < argc) {
135           encoding = argv[i];
136         }
137         else {
138           printf ("Missing encoding\n");
139           show_help();
140           exit(1);
141         }
142       }
143       else if (!strncmp(argv[i], "-u", 3)) {
144         i++;
145         if (i < argc) {
146           char* details = argv[i];
147           if (!strncmp(details, "HILO", 4))
148             enc = TWO_BYTE_HILO;
149           else if (!strncmp(details, "LOHI", 4))
150             enc = TWO_BYTE_LOHI;
151           else {
152             printf("Unknown encoding details %s\n", details);
153             show_help();
154             exit(1);
155           }
156           if (!strncmp(details+5, "BOM", 4))
157             bom = WITH_BOM;
158         }
159         else {
160           printf ("Missing encoding details\n");
161           show_help();
162           exit(1);
163         }
164       }
165       else if (!strncmp(argv[i], "-t", 3)) {
166         i++;
167         if (i < argc) {
168           char* term = argv[i];
169           if (!strncmp(term, "CR", 3))
170             end = END_CR;
171           else if (!strncmp(term, "LF", 3))
172             end = END_LF;
173           else if (!strncmp(term, "CR_LF", 6))
174             end = END_CR_LF;
175           else if (!strncmp(term, "LF_CR", 6))
176             end = END_LF_CR;
177           else {
178             printf("Unknown terminator: %s\n", term);
179             show_help();
180             exit(1);
181           }
182         }
183         else {
184           printf ("Missing terminator\n");
185           show_help();
186           exit(1);
187         }
188       }
189       else {
190         printf ("Unrecognized option: %s\n", argv[i]);
191         show_help();
192         exit(1);
193       }
194     }
195   }
196   
197   gedcom_init();
198   setlocale(LC_ALL, "");
199   gedcom_set_message_handler(gedcom_message_handler);
200   gedcom_write_set_encoding(encoding, enc, bom);
201   gedcom_write_set_line_terminator(end);
202
203   output_open(outfilename);
204   
205   result = gom_new_model();
206   if (result == 0)
207     result |= update_header(encoding);
208   if (result == 0)
209     result |= gom_write_file(gedfilename, &total_conv_fails);
210   if (result == 0 && total_conv_fails == 0) {
211     output(1, "Test succeeded\n");
212   }
213   else {
214     output(1, "Test failed: %d\n", result);
215   }
216
217   output_close();
218   return result;
219 }