Add DTD for ptbxml format
[jelmer/ptabtools.git] / ptb2ptb.c
1 /*
2         (c) 2004: Jelmer Vernooij <jelmer@samba.org>
3
4         This program is free software; you can redistribute it and/or modify
5         it under the terms of the GNU General Public License as published by
6         the Free Software Foundation; either version 2 of the License, or
7         (at your option) any later version.
8
9         This program is distributed in the hope that it will be useful,
10         but WITHOUT ANY WARRANTY; without even the implied warranty of
11         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12         GNU General Public License for more details.
13
14         You should have received a copy of the GNU General Public License
15         along with this program; if not, write to the Free Software
16         Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <stdio.h>
20 #include <errno.h>
21 #include <popt.h>
22 #include <string.h>
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #ifdef HAVE_STDINT_H
29 #  include <stdint.h>
30 #endif
31
32 #include "ptb.h"
33
34
35 int main(int argc, const char **argv) 
36 {
37         struct ptbf *ret;
38         int debugging = 0;
39         int version = 0;
40         int quiet = 0;
41         const char *input;
42         int c;
43         char *output = NULL;
44         poptContext pc;
45         struct poptOption options[] = {
46                 POPT_AUTOHELP
47                 {"version", 'v', POPT_ARG_NONE, &version, 'v', "Show version information" },
48                 POPT_TABLEEND
49         };
50
51         pc = poptGetContext(argv[0], argc, argv, options, 0);
52         poptSetOtherOptionHelp(pc, "file.ptb");
53         while((c = poptGetNextOpt(pc)) >= 0) {
54                 switch(c) {
55                 case 'v':
56                         printf("ptb2ptb Version "PACKAGE_VERSION"\n");
57                         printf("(C) 2004-2005 Jelmer Vernooij <jelmer@samba.org>\n");
58                         exit(0);
59                         break;
60                 }
61         }
62                         
63         ptb_set_debug(debugging);
64         
65         if(!poptPeekArg(pc)) {
66                 poptPrintUsage(pc, stderr, 0);
67                 return -1;
68         }
69         input = poptGetArg(pc);
70         
71         if (!quiet) fprintf(stderr, "Parsing %s... \n", input);
72                                         
73         ret = ptb_read_file(input);
74         
75         if(!ret) {
76                 perror("Read error: ");
77                 return -1;
78         } 
79
80         if(!output) {
81                 int baselength = strlen(input);
82                 if (!strcmp(input + strlen(input) - 4, ".ptb")) {
83                         baselength -= 4;
84                 }
85                 output = malloc(baselength + 9);
86                 strncpy(output, input, baselength);
87                 strcpy(output + baselength, ".ptb.2");
88         }
89
90         if (!quiet) fprintf(stderr, "Generating new powertab file in %s...\n", output);
91
92         if (ptb_write_file(output, ret) == -1) {
93                 fprintf(stderr, "Error while generating PowerTab file\n");
94         }
95
96         free(output);
97         
98         return (ret?0:1);
99 }