2 * lmo - Lua Machine Objects - PO to LMO conversion tool
4 * Copyright (C) 2009-2011 Jo-Philipp Wich <xm@subsignal.org>
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 static void die(const char *msg)
23 fprintf(stderr, "Error: %s\n", msg);
27 static void usage(const char *name)
29 fprintf(stderr, "Usage: %s input.po output.lmo\n", name);
33 static void print(const void *ptr, size_t size, size_t nmemb, FILE *stream)
35 if( fwrite(ptr, size, nmemb, stream) == 0 )
36 die("Failed to write stdout");
39 static int extract_string(const char *src, char *dest, int len)
45 for( pos = 0; (pos < strlen(src)) && (pos < len); pos++ )
47 if( (off == -1) && (src[pos] == '"') )
55 dest[pos-off] = src[pos];
58 else if( src[pos] == '\\' )
63 else if( src[pos] != '"' )
65 dest[pos-off] = src[pos];
75 return (off > -1) ? strlen(dest) : -1;
78 int main(int argc, char *argv[])
87 uint32_t key_id, val_id;
92 lmo_entry_t *head = NULL;
93 lmo_entry_t *entry = NULL;
95 if( (argc != 3) || ((in = fopen(argv[1], "r")) == NULL) || ((out = fopen(argv[2], "w")) == NULL) )
98 memset(line, 0, sizeof(key));
99 memset(key, 0, sizeof(val));
100 memset(val, 0, sizeof(val));
102 while( (NULL != fgets(line, sizeof(line), in)) || (state >= 2 && feof(in)) )
104 if( state == 0 && strstr(line, "msgid \"") == line )
106 switch(extract_string(line, key, sizeof(key)))
109 die("Syntax error in msgid");
117 else if( state == 1 || state == 2 )
119 if( strstr(line, "msgstr \"") == line || state == 2 )
121 switch(extract_string(line, val, sizeof(val)))
132 switch(extract_string(line, tmp, sizeof(tmp)))
142 else if( state == 3 )
144 switch(extract_string(line, tmp, sizeof(tmp)))
156 if( strlen(key) > 0 && strlen(val) > 0 )
158 key_id = sfh_hash(key, strlen(key));
159 val_id = sfh_hash(val, strlen(val));
161 if( key_id != val_id )
163 if( (entry = (lmo_entry_t *) malloc(sizeof(lmo_entry_t))) != NULL )
165 memset(entry, 0, sizeof(entry));
166 length = strlen(val) + ((4 - (strlen(val) % 4)) % 4);
168 entry->key_id = htonl(key_id);
169 entry->val_id = htonl(val_id);
170 entry->offset = htonl(offset);
171 entry->length = htonl(strlen(val));
173 print(val, length, 1, out);
181 die("Out of memory");
187 memset(key, 0, sizeof(key));
188 memset(val, 0, sizeof(val));
191 memset(line, 0, sizeof(line));
195 while( entry != NULL )
197 print(&entry->key_id, sizeof(uint32_t), 1, out);
198 print(&entry->val_id, sizeof(uint32_t), 1, out);
199 print(&entry->offset, sizeof(uint32_t), 1, out);
200 print(&entry->length, sizeof(uint32_t), 1, out);
206 offset = htonl(offset);
207 print(&offset, sizeof(uint32_t), 1, out);