luci-0.11: merge outstanding trunk changes
[project/luci.git] / libs / web / src / po2lmo.c
1 /*
2 * lmo - Lua Machine Objects - PO to LMO conversion tool
3 *
4 * Copyright (C) 2009-2012 Jo-Philipp Wich <xm@subsignal.org>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #include "template_lmo.h"
20
21 static void die(const char *msg)
22 {
23 fprintf(stderr, "Error: %s\n", msg);
24 exit(1);
25 }
26
27 static void usage(const char *name)
28 {
29 fprintf(stderr, "Usage: %s input.po output.lmo\n", name);
30 exit(1);
31 }
32
33 static void print(const void *ptr, size_t size, size_t nmemb, FILE *stream)
34 {
35 if( fwrite(ptr, size, nmemb, stream) == 0 )
36 die("Failed to write stdout");
37 }
38
39 static int extract_string(const char *src, char *dest, int len)
40 {
41 int pos = 0;
42 int esc = 0;
43 int off = -1;
44
45 for( pos = 0; (pos < strlen(src)) && (pos < len); pos++ )
46 {
47 if( (off == -1) && (src[pos] == '"') )
48 {
49 off = pos + 1;
50 }
51 else if( off >= 0 )
52 {
53 if( esc == 1 )
54 {
55 switch (src[pos])
56 {
57 case '"':
58 case '\\':
59 off++;
60 break;
61 }
62 dest[pos-off] = src[pos];
63 esc = 0;
64 }
65 else if( src[pos] == '\\' )
66 {
67 dest[pos-off] = src[pos];
68 esc = 1;
69 }
70 else if( src[pos] != '"' )
71 {
72 dest[pos-off] = src[pos];
73 }
74 else
75 {
76 dest[pos-off] = '\0';
77 break;
78 }
79 }
80 }
81
82 return (off > -1) ? strlen(dest) : -1;
83 }
84
85 static int cmp_index(const void *a, const void *b)
86 {
87 uint32_t x = ntohl(((const lmo_entry_t *)a)->key_id);
88 uint32_t y = ntohl(((const lmo_entry_t *)b)->key_id);
89
90 if (x < y)
91 return -1;
92 else if (x > y)
93 return 1;
94
95 return 0;
96 }
97
98 static void print_index(void *array, int n, FILE *out)
99 {
100 lmo_entry_t *e;
101
102 qsort(array, n, sizeof(*e), cmp_index);
103
104 for (e = array; n > 0; n--, e++)
105 {
106 print(&e->key_id, sizeof(uint32_t), 1, out);
107 print(&e->val_id, sizeof(uint32_t), 1, out);
108 print(&e->offset, sizeof(uint32_t), 1, out);
109 print(&e->length, sizeof(uint32_t), 1, out);
110 }
111 }
112
113 int main(int argc, char *argv[])
114 {
115 char line[4096];
116 char key[4096];
117 char val[4096];
118 char tmp[4096];
119 int state = 0;
120 int offset = 0;
121 int length = 0;
122 int n_entries = 0;
123 void *array = NULL;
124 lmo_entry_t *entry = NULL;
125 uint32_t key_id, val_id;
126
127 FILE *in;
128 FILE *out;
129
130 if( (argc != 3) || ((in = fopen(argv[1], "r")) == NULL) || ((out = fopen(argv[2], "w")) == NULL) )
131 usage(argv[0]);
132
133 memset(line, 0, sizeof(key));
134 memset(key, 0, sizeof(val));
135 memset(val, 0, sizeof(val));
136
137 while( (NULL != fgets(line, sizeof(line), in)) || (state >= 2 && feof(in)) )
138 {
139 if( state == 0 && strstr(line, "msgid \"") == line )
140 {
141 switch(extract_string(line, key, sizeof(key)))
142 {
143 case -1:
144 die("Syntax error in msgid");
145 case 0:
146 state = 1;
147 break;
148 default:
149 state = 2;
150 }
151 }
152 else if( state == 1 || state == 2 )
153 {
154 if( strstr(line, "msgstr \"") == line || state == 2 )
155 {
156 switch(extract_string(line, val, sizeof(val)))
157 {
158 case -1:
159 state = 4;
160 break;
161 default:
162 state = 3;
163 }
164 }
165 else
166 {
167 switch(extract_string(line, tmp, sizeof(tmp)))
168 {
169 case -1:
170 state = 2;
171 break;
172 default:
173 strcat(key, tmp);
174 }
175 }
176 }
177 else if( state == 3 )
178 {
179 switch(extract_string(line, tmp, sizeof(tmp)))
180 {
181 case -1:
182 state = 4;
183 break;
184 default:
185 strcat(val, tmp);
186 }
187 }
188
189 if( state == 4 )
190 {
191 if( strlen(key) > 0 && strlen(val) > 0 )
192 {
193 key_id = sfh_hash(key, strlen(key));
194 val_id = sfh_hash(val, strlen(val));
195
196 if( key_id != val_id )
197 {
198 n_entries++;
199 array = realloc(array, n_entries * sizeof(lmo_entry_t));
200 entry = (lmo_entry_t *)array + n_entries - 1;
201
202 if (!array)
203 die("Out of memory");
204
205 entry->key_id = htonl(key_id);
206 entry->val_id = htonl(val_id);
207 entry->offset = htonl(offset);
208 entry->length = htonl(strlen(val));
209
210 length = strlen(val) + ((4 - (strlen(val) % 4)) % 4);
211
212 print(val, length, 1, out);
213 offset += length;
214 }
215 }
216
217 state = 0;
218 memset(key, 0, sizeof(key));
219 memset(val, 0, sizeof(val));
220 }
221
222 memset(line, 0, sizeof(line));
223 }
224
225 print_index(array, n_entries, out);
226
227 if( offset > 0 )
228 {
229 offset = htonl(offset);
230 print(&offset, sizeof(uint32_t), 1, out);
231 fsync(fileno(out));
232 fclose(out);
233 }
234 else
235 {
236 fclose(out);
237 unlink(argv[2]);
238 }
239
240 fclose(in);
241 return(0);
242 }