libs/lmo: fix whitespace handling in string hashing, optimize code
[project/luci.git] / libs / lmo / src / lmo_po2lmo.c
index 9f78ff2..f6f3994 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * lmo - Lua Machine Objects - PO to LMO conversion tool
  *
- *   Copyright (C) 2009 Jo-Philipp Wich <xm@subsignal.org>
+ *   Copyright (C) 2009-2011 Jo-Philipp Wich <xm@subsignal.org>
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -52,14 +52,20 @@ static int extract_string(const char *src, char *dest, int len)
                {
                        if( esc == 1 )
                        {
+                               switch (src[pos])
+                               {
+                               case '"':
+                               case '\\':
+                                       off++;
+                                       break;
+                               }
                                dest[pos-off] = src[pos];
                                esc = 0;
                        }
                        else if( src[pos] == '\\' )
                        {
-                               off++;
+                               dest[pos-off] = src[pos];
                                esc = 1;
-                               
                        }
                        else if( src[pos] != '"' )
                        {
@@ -85,6 +91,7 @@ int main(int argc, char *argv[])
        int state  = 0;
        int offset = 0;
        int length = 0;
+       uint32_t key_id, val_id;
 
        FILE *in;
        FILE *out;
@@ -108,58 +115,78 @@ int main(int argc, char *argv[])
                                case -1:
                                        die("Syntax error in msgid");
                                case 0:
-                                       continue;
-                               default:
                                        state = 1;
+                                       break;
+                               default:
+                                       state = 2;
                        }
                }
-               else if( state == 1 && strstr(line, "msgstr \"") == line )
+               else if( state == 1 || state == 2 )
                {
-                       switch(extract_string(line, val, sizeof(val)))
+                       if( strstr(line, "msgstr \"") == line || state == 2 )
                        {
-                               case -1:
-                                       die("Syntax error in msgstr");
-                               case 0:
-                                       state = 2;
-                                       break;
-                               default:
-                                       state = 3;
+                               switch(extract_string(line, val, sizeof(val)))
+                               {
+                                       case -1:
+                                               state = 4;
+                                               break;
+                                       default:
+                                               state = 3;
+                               }
+                       }
+                       else
+                       {
+                               switch(extract_string(line, tmp, sizeof(tmp)))
+                               {
+                                       case -1:
+                                               state = 2;
+                                               break;
+                                       default:
+                                               strcat(key, tmp);
+                               }
                        }
                }
-               else if( state == 2 )
+               else if( state == 3 )
                {
                        switch(extract_string(line, tmp, sizeof(tmp)))
                        {
                                case -1:
-                                       state = 3;
+                                       state = 4;
                                        break;
                                default:
                                        strcat(val, tmp);
                        }
                }
-               else if( state == 3 )
+
+               if( state == 4 )
                {
                        if( strlen(key) > 0 && strlen(val) > 0 )
                        {
-                               if( (entry = (lmo_entry_t *) malloc(sizeof(lmo_entry_t))) != NULL )
-                               {
-                                       memset(entry, 0, sizeof(entry));
-                                       length = strlen(val) + ((4 - (strlen(val) % 4)) % 4);
+                               key_id = sfh_hash(key, strlen(key));
+                               val_id = sfh_hash(val, strlen(val));
 
-                                       entry->key_id = htonl(sfh_hash(key, strlen(key)));
-                                       entry->val_id = htonl(sfh_hash(val, strlen(val)));
-                                       entry->offset = htonl(offset);
-                                       entry->length = htonl(strlen(val));
-
-                                       print(val, length, 1, out);
-                                       offset += length;
-
-                                       entry->next = head;
-                                       head = entry;
-                               }
-                               else
+                               if( key_id != val_id )
                                {
-                                       die("Out of memory");
+                                       if( (entry = (lmo_entry_t *) malloc(sizeof(lmo_entry_t))) != NULL )
+                                       {
+                                               memset(entry, 0, sizeof(entry));
+                                               length = strlen(val) + ((4 - (strlen(val) % 4)) % 4);
+
+                                               entry->key_id = htonl(key_id);
+                                               entry->val_id = htonl(val_id);
+                                               entry->offset = htonl(offset);
+                                               entry->length = htonl(strlen(val));
+
+                                               print(val, length, 1, out);
+                                               offset += length;
+
+                                               entry->next = head;
+                                               head = entry;
+                                       }
+                                       else
+                                       {
+                                               die("Out of memory");
+                                       }
                                }
                        }