1 | /* |
1 | /* |
2 | rewrite.c - Part of psiconv, a PSION 5 file formats converter |
2 | rewrite.c - Part of psiconv, a PSION 5 file formats converter |
3 | Copyright (c) 1999 Frodo Looijaard <frodol@dds.nl> |
3 | Copyright (c) 1999-2014 Frodo Looijaard <frodo@frodo.looijaard.name> |
4 | |
4 | |
5 | This program is free software; you can redistribute it and/or modify |
5 | This program is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published by |
6 | it under the terms of the GNU General Public License as published by |
7 | the Free Software Foundation; either version 2 of the License, or |
7 | the Free Software Foundation; either version 2 of the License, or |
8 | (at your option) any later version. |
8 | (at your option) any later version. |
… | |
… | |
17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
18 | */ |
18 | */ |
19 | |
19 | |
20 | #include <psiconv/parse.h> |
20 | #include <psiconv/parse.h> |
21 | #include <psiconv/generate.h> |
21 | #include <psiconv/generate.h> |
|
|
22 | #include <psiconv/configuration.h> |
22 | |
23 | |
|
|
24 | #include <stdlib.h> |
23 | #include <stdio.h> |
25 | #include <stdio.h> |
24 | |
26 | |
25 | int main(int argc, char *argv[]) |
27 | int main(int argc, char *argv[]) |
26 | { |
28 | { |
27 | FILE *fp; |
29 | FILE *fp; |
28 | psiconv_buffer buf; |
30 | psiconv_buffer buf; |
29 | psiconv_file psionfile; |
31 | psiconv_file psionfile; |
|
|
32 | psiconv_config config; |
|
|
33 | |
|
|
34 | /* psiconv_verbosity = PSICONV_VERB_DEBUG; */ |
30 | |
35 | |
31 | if (argc < 3) { |
36 | if (argc < 3) { |
32 | fprintf(stderr,"Not enough arguments\n"); |
37 | fprintf(stderr,"Not enough arguments\n"); |
|
|
38 | fprintf(stderr,"Syntax: INFILE OUTFILE\n"); |
33 | exit(1); |
39 | exit(1); |
34 | } |
40 | } |
|
|
41 | |
|
|
42 | config = psiconv_config_default(); |
|
|
43 | psiconv_config_read(NULL,&config); |
|
|
44 | |
35 | if (!(fp = fopen(argv[1],"r"))) { |
45 | if (!(fp = fopen(argv[1],"r"))) { |
36 | perror("Can't open file"); |
46 | perror("Can't open file"); |
37 | exit(1); |
47 | exit(1); |
38 | } |
48 | } |
39 | if (!(buf=psiconv_buffer_new(0))) { |
49 | if (!(buf=psiconv_buffer_new())) { |
40 | perror("Can't allocate buf"); |
50 | perror("Can't allocate buf"); |
41 | exit(1); |
51 | exit(1); |
42 | } |
52 | } |
43 | if ((psiconv_buffer_fread_all(buf,fp))) { |
53 | if ((psiconv_buffer_fread_all(buf,fp))) { |
44 | perror("Can't fread file"); |
54 | perror("Can't fread file"); |
45 | exit(1); |
55 | exit(1); |
46 | } |
56 | } |
47 | fclose(fp); |
57 | fclose(fp); |
48 | if ((psiconv_parse(buf,&psionfile))) { |
58 | if ((psiconv_parse(config,buf,&psionfile))) { |
49 | fprintf(stderr,"Parse error\n"); |
59 | fprintf(stderr,"Parse error\n"); |
50 | exit(1); |
60 | exit(1); |
51 | } |
61 | } |
|
|
62 | |
52 | psiconv_buffer_free(buf); |
63 | psiconv_buffer_free(buf); |
53 | buf = NULL; |
64 | buf = NULL; |
54 | if (psiconv_write(&buf,psionfile)) { |
65 | if (psiconv_write(config,&buf,psionfile)) { |
55 | fprintf(stderr,"Parse error\n"); |
66 | fprintf(stderr,"Generate error\n"); |
56 | exit(1); |
67 | exit(1); |
57 | } |
68 | } |
|
|
69 | psiconv_free_file(psionfile); |
58 | if (!(fp = fopen(argv[2],"w"))) { |
70 | if (!(fp = fopen(argv[2],"w"))) { |
59 | perror("Can't open file"); |
71 | perror("Can't open file"); |
60 | exit(1); |
72 | exit(1); |
61 | } |
73 | } |
62 | if ((psiconv_buffer_fwrite_all(buf,fp))) { |
74 | if ((psiconv_buffer_fwrite_all(buf,fp))) { |
63 | perror("Can't fwrite file"); |
75 | perror("Can't fwrite file"); |
64 | exit(1); |
76 | exit(1); |
65 | } |
77 | } |
|
|
78 | fclose(fp); |
|
|
79 | psiconv_buffer_free(buf); |
|
|
80 | psiconv_config_free(config); |
66 | exit(0); |
81 | exit(0); |
67 | } |
82 | } |