9f78ff2add25eb0ff3d1254d0270fd4945de057b
[project/luci.git] / libs / lmo / src / lmo_po2lmo.c
1 /*
2 * lmo - Lua Machine Objects - PO to LMO conversion tool
3 *
4 * Copyright (C) 2009 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 "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 dest[pos-off] = src[pos];
56 esc = 0;
57 }
58 else if( src[pos] == '\\' )
59 {
60 off++;
61 esc = 1;
62
63 }
64 else if( src[pos] != '"' )
65 {
66 dest[pos-off] = src[pos];
67 }
68 else
69 {
70 dest[pos-off] = '\0';
71 break;
72 }
73 }
74 }
75
76 return (off > -1) ? strlen(dest) : -1;
77 }
78
79 int main(int argc, char *argv[])
80 {
81 char line[4096];
82 char key[4096];
83 char val[4096];
84 char tmp[4096];
85 int state = 0;
86 int offset = 0;
87 int length = 0;
88
89 FILE *in;
90 FILE *out;
91
92 lmo_entry_t *head = NULL;
93 lmo_entry_t *entry = NULL;
94
95 if( (argc != 3) || ((in = fopen(argv[1], "r")) == NULL) || ((out = fopen(argv[2], "w")) == NULL) )
96 usage(argv[0]);
97
98 memset(line, 0, sizeof(key));
99 memset(key, 0, sizeof(val));
100 memset(val, 0, sizeof(val));
101
102 while( (NULL != fgets(line, sizeof(line), in)) || (state >= 2 && feof(in)) )
103 {
104 if( state == 0 && strstr(line, "msgid \"") == line )
105 {
106 switch(extract_string(line, key, sizeof(key)))
107 {
108 case -1:
109 die("Syntax error in msgid");
110 case 0:
111 continue;
112 default:
113 state = 1;
114 }
115 }
116 else if( state == 1 && strstr(line, "msgstr \"") == line )
117 {
118 switch(extract_string(line, val, sizeof(val)))
119 {
120 case -1:
121 die("Syntax error in msgstr");
122 case 0:
123 state = 2;
124 break;
125 default:
126 state = 3;
127 }
128 }
129 else if( state == 2 )
130 {
131 switch(extract_string(line, tmp, sizeof(tmp)))
132 {
133 case -1:
134 state = 3;
135 break;
136 default:
137 strcat(val, tmp);
138 }
139 }
140 else if( state == 3 )
141 {
142 if( strlen(key) > 0 && strlen(val) > 0 )
143 {
144 if( (entry = (lmo_entry_t *) malloc(sizeof(lmo_entry_t))) != NULL )
145 {
146 memset(entry, 0, sizeof(entry));
147 length = strlen(val) + ((4 - (strlen(val) % 4)) % 4);
148
149 entry->key_id = htonl(sfh_hash(key, strlen(key)));
150 entry->val_id = htonl(sfh_hash(val, strlen(val)));
151 entry->offset = htonl(offset);
152 entry->length = htonl(strlen(val));
153
154 print(val, length, 1, out);
155 offset += length;
156
157 entry->next = head;
158 head = entry;
159 }
160 else
161 {
162 die("Out of memory");
163 }
164 }
165
166 state = 0;
167 memset(key, 0, sizeof(key));
168 memset(val, 0, sizeof(val));
169 }
170
171 memset(line, 0, sizeof(line));
172 }
173
174 entry = head;
175 while( entry != NULL )
176 {
177 print(&entry->key_id, sizeof(uint32_t), 1, out);
178 print(&entry->val_id, sizeof(uint32_t), 1, out);
179 print(&entry->offset, sizeof(uint32_t), 1, out);
180 print(&entry->length, sizeof(uint32_t), 1, out);
181 entry = entry->next;
182 }
183
184 if( offset > 0 )
185 {
186 offset = htonl(offset);
187 print(&offset, sizeof(uint32_t), 1, out);
188 fsync(fileno(out));
189 fclose(out);
190 }
191 else
192 {
193 fclose(out);
194 unlink(argv[2]);
195 }
196
197 fclose(in);
198 return(0);
199 }