libs/lmo: fix po2lmo to cope with new po format
[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 >= 3 && 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                                         state = 1;
112                                         break;
113                                 default:
114                                         state = 2;
115                         }
116                 }
117                 else if( state == 1 || state == 2 )
118                 {
119                         if( strstr(line, "msgstr \"") == line || state == 2 )
120                         {
121                                 switch(extract_string(line, val, sizeof(val)))
122                                 {
123                                         case -1:
124                                                 state = 4;
125                                                 break;
126                                         case 0:
127                                                 state = 2;
128                                                 break;
129                                         default:
130                                                 state = 3;
131                                 }
132                         }
133                         else
134                         {
135                                 switch(extract_string(line, tmp, sizeof(tmp)))
136                                 {
137                                         case -1:
138                                                 state = 4;
139                                                 break;
140                                         default:
141                                                 strcat(key, tmp);
142                                 }
143                         }
144                 }
145                 else if( state == 3 )
146                 {
147                         switch(extract_string(line, tmp, sizeof(tmp)))
148                         {
149                                 case -1:
150                                         state = 4;
151                                         break;
152                                 default:
153                                         strcat(val, tmp);
154                         }
155                 }
156                 else if( state == 4 )
157                 {
158                         if( strlen(key) > 0 && strlen(val) > 0 )
159                         {
160                                 if( (entry = (lmo_entry_t *) malloc(sizeof(lmo_entry_t))) != NULL )
161                                 {
162                                         memset(entry, 0, sizeof(entry));
163                                         length = strlen(val) + ((4 - (strlen(val) % 4)) % 4);
164
165                                         entry->key_id = htonl(sfh_hash(key, strlen(key)));
166                                         entry->val_id = htonl(sfh_hash(val, strlen(val)));
167                                         entry->offset = htonl(offset);
168                                         entry->length = htonl(strlen(val));
169
170                                         print(val, length, 1, out);
171                                         offset += length;
172
173                                         entry->next = head;
174                                         head = entry;
175                                 }
176                                 else
177                                 {
178                                         die("Out of memory");
179                                 }
180                         }
181
182                         state = 0;
183                         memset(key, 0, sizeof(key));
184                         memset(val, 0, sizeof(val));
185                 }
186
187                 memset(line, 0, sizeof(line));
188         }
189
190         entry = head;
191         while( entry != NULL )
192         {
193                 print(&entry->key_id, sizeof(uint32_t), 1, out);
194                 print(&entry->val_id, sizeof(uint32_t), 1, out);
195                 print(&entry->offset, sizeof(uint32_t), 1, out);
196                 print(&entry->length, sizeof(uint32_t), 1, out);
197                 entry = entry->next;
198         }
199
200         if( offset > 0 )
201         {
202                 offset = htonl(offset);
203                 print(&offset, sizeof(uint32_t), 1, out);
204                 fsync(fileno(out));
205                 fclose(out);
206         }
207         else
208         {
209                 fclose(out);
210                 unlink(argv[2]);
211         }
212
213         fclose(in);
214         return(0);
215 }