root/tools/makelang.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. main
  2. lang_load_from_mem
  3. load_from_file
  4. skip_space
  5. next_token
  6. load_conditions
  7. is_conditional

   1 #include <stdio.h>
   2 #include <stdlib.h>
   3 #include <unistd.h>
   4 #include <math.h>
   5 
   6 #include <string.h>
   7 #include <fcntl.h>
   8 #include <sys/stat.h>
   9 
  10 #include "../core/gui_lang.h"
  11 
  12 char **lang_conditions;
  13 void load_conditions(char *file);
  14 char* is_conditional(int id);
  15 char* load_from_file(const char* filename);
  16 void lang_load_from_mem(char *buf);
  17 
  18 int num_lines=0;
  19 char** langfile = 0;
  20 
  21 
  22 ////////////////////////////////////////////////////////////////////////////////////////////////
  23 //
  24 // @tsv - Utility to create preparsed gui_lang.c (from concatenation gui_lang.c with .lng file)
  25 //
  26 // USAGE:   makelang path_to_gui_lang.h path_to_english.lng [path_to_secondary.lng] > lang_str.h
  27 //
  28 ///////////////////////////////////////////////////////////////////////////////////////////////
  29 int main( int argc, char **argv )
  30 {
  31         int i;
  32 
  33         if ( argc < 3 )
  34         {
  35                 printf("#error No arguments for language file parser. lang_str.h creation was failed\n");
  36                 exit(-1);
  37         }
  38 
  39         num_lines=0;
  40 
  41         load_conditions(argv[1]);
  42 
  43         char* file1 = load_from_file(argv[2]);
  44         char* file2 = 0;
  45         if ( argc > 3 )
  46           file2 = load_from_file(argv[3]);
  47 
  48         // Do calculation num of lang strings
  49         lang_load_from_mem ( file1 );
  50         lang_load_from_mem ( file2 );
  51 
  52         if ( num_lines <= 0 )
  53         {
  54                 printf("#error No strings found by language file parser. lang_str.h creation was failed\n");
  55                 exit(-1);
  56         }
  57 
  58         // Create array
  59         langfile = malloc( sizeof(char*)*(num_lines+1) );
  60         for ( i=0; i<=num_lines; i++ )
  61           { langfile[i]=0; }
  62 
  63         // Load lang files
  64         lang_load_from_mem ( file1 );
  65         lang_load_from_mem ( file2 );
  66 
  67 
  68         char* lng_filename = (argc>2 && file2) ? argv[2] : argv[1];
  69 
  70         int num_empty_lines=0;
  71         char buf[500];
  72 
  73         printf("//Auto generated file. Do not edit the contents of this file.\n//Update the CHDK/LANG/*.lng files to make changes.\n//Generated from %s\n\n",lng_filename);
  74 
  75     // Extract the language name
  76     char *s = strrchr(lng_filename,'/');
  77     if (s == 0) s = lng_filename; else s++;
  78     strcpy(buf,s);
  79     s = strrchr(buf,'.');
  80     if (s) *s = 0;
  81 
  82         printf("static char* gui_lang_default = \n");
  83         
  84         for ( i=1; i<=num_lines; i++ )
  85         {
  86                 if ( !langfile[i])
  87                 {
  88                         printf("/*%3d*/ \"\\0\"\n", i);
  89                         num_empty_lines++;
  90                 }
  91                 else
  92                 {
  93                   {
  94                         // Do shielding of \ symbol
  95                         const char* from = langfile[i];
  96                         char* to = buf;
  97                         
  98                         while(*from)
  99                         {
 100                                 *to=*from; to++;
 101                                 if (*from=='\\' && from[1]!='"' && from[1]!='n')
 102                                  { *to=*from; to++;}
 103                                 from++;
 104                         }
 105                         *to=0;
 106                   }
 107                   char *iscond = is_conditional(i);
 108                   if (iscond)
 109                   {
 110                       if (iscond[0] == '!')
 111                           printf("#ifndef %s\n/*%3d*/ \"%s\"\n#endif\n        \"\\0\"\n", iscond+1, i, buf);
 112                       else
 113                           printf("#ifdef %s\n/*%3d*/ \"%s\"\n#endif\n        \"\\0\"\n", iscond, i, buf);
 114                   }
 115                   else
 116                   {
 117                       printf("/*%3d*/ \"%s\\0\"\n",i,buf);
 118                   }
 119                 }
 120         }
 121 
 122     printf(";\n\n");
 123         
 124     if ( num_empty_lines > 50 )
 125           printf("#warning Too many empty lines are detected. Please ensure that .lng files have serial numbers\n\n");
 126 
 127     printf("//Sanity check of GUI_LANG_ITEMS\n");
 128     printf("#if (GUI_LANG_ITEMS != %d)\n",num_lines);
 129     printf("#error GUI_LANG_ITEMS value have to be %d. Please fix at gui_lang.h\n", num_lines);
 130     printf("#endif\n");
 131         
 132     return 0;
 133 }
 134 
 135 // Parser language file
 136 // Language strings are collected in langfile
 137 //------------------------------------------------------
 138 void lang_load_from_mem(char *buf) 
 139 {
 140     char *p, *s, *e;
 141     int i;
 142     
 143     if ( !buf )
 144      return;
 145 
 146     e = buf-1;
 147     while(e) 
 148         {
 149         p = e+1;
 150         while (*p && (*p=='\r' || *p=='\n')) ++p; //skip empty lines
 151         i = strtol(p, &e, 0/*autodetect base oct-dec-hex*/);    // convert "*p" to long "i" and return pointer beyond to e
 152 
 153         if ( !langfile ) {
 154            if ( i >= num_lines ) {
 155               num_lines = i;
 156            }
 157            e = strpbrk(p, "\r\n");              //find eol
 158            continue;
 159         }
 160         
 161         if (e!=p) {
 162             p = e;
 163             e = strpbrk(p, "\r\n");             //break string with zero on \r|\n
 164             if (e) *e=0;
 165                         
 166             while (*p && *p!='\"') ++p; // cut string from "" if it exists
 167             if (*p) ++p;
 168             s = p; 
 169             while (*p && (*p!='\"' || *(p-1)=='\\')) ++p;
 170             *p=0;
 171                 
 172             langfile[i]=s;              // add string
 173         }
 174         else {
 175             //skip invalid line
 176             e = strpbrk(p, "\r\n");
 177             if (e) *e=0;
 178         }
 179     }
 180 }
 181 
 182 //-------------------------------------------------------------------
 183 
 184 char* load_from_file(const char *filename) 
 185 {
 186     int f, size;
 187     static struct stat st;
 188     char *buf = 0;
 189 
 190     f = open(filename, O_RDONLY, 0777);
 191     if (f>=0) {
 192         size = (stat((char*)filename, &st)==0)?st.st_size:0;
 193         if (size) {
 194             buf = malloc(size+1);
 195             if (buf) {
 196                 size = read(f, buf, size);
 197                 buf[size]=0;
 198             }
 199         }
 200         close(f);
 201     }
 202     return buf;
 203 }
 204 
 205 //-------------------------------------------------------------------
 206 // Conditional entries
 207 char* skip_space(char *p)
 208 {
 209     while (*p && ((*p == ' ') || (*p == '\t'))) p++;
 210     return p;
 211 }
 212 
 213 char* next_token(char *p, char* buf)
 214 {
 215     *buf = 0;
 216     p = skip_space(p);
 217     if ((*p == '\r') || (*p == '\n')) return 0;
 218     while (*p && (*p != ' ') && (*p != '\t') && (*p != '\r') && (*p != '\n')) *buf++ = *p++;
 219     *buf = 0;
 220     return p;
 221 }
 222 
 223 void load_conditions(char *file)
 224 {
 225     lang_conditions = 0;
 226 
 227     char *buf = load_from_file(file);
 228 
 229     if (!buf)
 230         return;
 231 
 232     lang_conditions = malloc(sizeof(char*) * (GUI_LANG_ITEMS+1));
 233     memset(lang_conditions, 0, sizeof(char*) * (GUI_LANG_ITEMS+1));
 234 
 235     char *p, *e;
 236     char name[500];
 237     char index[20];
 238     char cond[200];
 239     int i;
 240 
 241     e = buf;
 242     while (e && *e)
 243     {
 244         p = e;
 245         if (strncmp(p, "#define", 7) == 0)
 246         {
 247             p = next_token(p+7, name);
 248             if (p)
 249             {
 250                 p = next_token(p, index);
 251                 if (p)
 252                 {
 253                     p = next_token(p, cond);
 254                     if (p && (strcmp(cond, "//CONDITIONAL:") == 0))
 255                     {
 256                         p = next_token(p, cond);
 257                         if (p)
 258                         {
 259                             i = atoi(index);
 260                             lang_conditions[i] = malloc(strlen(cond)+1);
 261                             strcpy(lang_conditions[i], cond);
 262                         }
 263                     }
 264                 }
 265             }
 266         }
 267         e = strpbrk(e, "\n");
 268         if (e) e++;
 269     }
 270 
 271     free(buf);
 272 }
 273 
 274 char* is_conditional(int id)
 275 {
 276     if (lang_conditions && (id>=1) && (id<=GUI_LANG_ITEMS) && lang_conditions[id])
 277     {
 278         return lang_conditions[id];
 279     }
 280     return 0;
 281 }

/* [<][>][^][v][top][bottom][index][help] */