added translation statistics file
[slovak-l10n] / ukeyboard / vkb_compiler.c
1 /*
2  *  Copyright (c) 2007-2008 Jiri Benc <jbenc@upir.cz>
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 version 2 as
6  *  published by the Free Software Foundation.
7  *
8  *  This program is distributed in the hope that it will be useful,
9  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *  GNU General Public License for more details.
12  *
13  *  You should have received a copy of the GNU General Public License
14  *  along with this program; if not, write to the Free Software
15  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
16  */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include "vkb_compiler.h"
21
22 struct priv {
23         FILE *fin;
24         FILE *fout;
25         char *fin_name;
26         char *fout_name;
27 };
28
29 static void error(void *p, int line, char *msg)
30 {
31         struct priv *priv = p;
32
33         if (line < 0)
34                 fprintf(stderr, "%s: Error: %s.\n", priv->fin_name, msg);
35         else
36                 fprintf(stderr, "%s:%d: Error: %s.\n", priv->fin_name, line, msg);
37 }
38
39 static int warning(void *p, int line, char *msg)
40 {
41         struct priv *priv = p;
42
43         fprintf(stderr, "%s:%d: Warning: %s.\n", priv->fin_name, line, msg);
44         return 0;
45 }
46
47 static int get_line(void *p, void *buf, size_t size)
48 {
49         struct priv *priv = p;
50
51         return fgets(buf, size, priv->fin) ? 0 : -1;
52 }
53
54 static int write_buf(void *p, void *buf, size_t size)
55 {
56         struct priv *priv = p;
57
58         if (fwrite(buf, 1, size, priv->fout) != size) {
59                 fprintf(stderr, "Error writing %s.\n", priv->fout_name);
60                 return -1;
61         }
62         return 0;
63 }
64
65 static off_t tell_buf(void *p)
66 {
67         struct priv *priv = p;
68
69         return ftell(priv->fout);
70 }
71
72 static void seek_buf(void *p, off_t pos)
73 {
74         struct priv *priv = p;
75
76         fseek(priv->fout, pos, SEEK_SET);
77 }
78
79 static void init_input(struct priv *priv, char *fname)
80 {
81         priv->fin_name = fname;
82         priv->fin = fopen(fname, "r");
83         if (!priv->fin) {
84                 error(priv, -1, "Cannot open input file");
85                 exit(1);
86         }
87 }
88
89 static void init_output(struct priv *priv, char *fname)
90 {
91         priv->fout_name = fname;
92         priv->fout = fopen(fname, "w");
93         if (!priv->fout) {
94                 fprintf(stderr, "Error creating %s.\n", fname);
95                 exit(1);
96         }
97 }
98
99 static void close_input(struct priv *priv)
100 {
101         fclose(priv->fin);
102 }
103
104 static void close_output(struct priv *priv)
105 {
106         fclose(priv->fout);
107 }
108
109 static struct compiler_ops ops = {
110         .get_line = get_line,
111         .write = write_buf,
112         .tell = tell_buf,
113         .seek = seek_buf,
114         .error = error,
115         .warning = warning,
116 };
117
118 int main(int argc, char **argv)
119 {
120         struct priv priv;
121         int res;
122
123         if (argc < 3) {
124                 fprintf(stderr, "Missing parameters (source and destination filename).\n");
125                 exit(1);
126         }
127         init_input(&priv, argv[1]);
128         init_output(&priv, argv[2]);
129         res = compile(&ops, &priv);
130         close_output(&priv);
131         close_input(&priv);
132         return res ? 1 : 0;
133 }