update po/cin.po and *.po to 5.1 baseline
[goodguy/history.git] / cinelerra-5.1 / po / xlat.C
1 #include <stdio.h>
2 #include <stdint.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <unistd.h>
6
7 #include <string>
8 #include <map>
9
10 #define MX_STR 8192
11
12 // test csv    ./a.out csv < data.csv
13 // test po     ./a.out  po < data.po
14 // get strings ./a.out key  < xgettext.po
15 // gen xlation ./a.out xlat < xgettext.po xlat.csv
16 // gen xlation ./a.out xlat < xgettext.po text,xlat ...
17 // gen xupdate ./a.out xlat < xgettext.po xlat.csv newer.csv ... newest.csv
18
19 unsigned int wnext(uint8_t *&bp)
20 {
21   unsigned int ch = *bp++;
22   if( ch >= 0x80 ) {
23     static const unsigned char byts[] = {
24       1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5,
25     };
26     int i = ch - 0xc0;
27     int n = i<0 ? 0 : byts[i/4];
28     for( i=n; --i>=0 && *bp>=0x80; ch+=*bp++ ) ch <<= 6;
29     static const unsigned int ofs[6] = {
30       0x00000000U, 0x00003080U, 0x000E2080U,
31       0x03C82080U, 0xFA082080U, 0x82082080U
32     };
33     ch = i<0 ?  ch-ofs[n] : '?';
34   }
35   return ch;
36 }
37
38 int wnext(uint8_t *&bp, unsigned int ch)
39 {
40   if( ch < 0x00000080 ) { *bp++ = ch;  return 1; }
41   int n = ch < 0x00000800 ? 2 : ch < 0x00010000 ? 3 :
42           ch < 0x00200000 ? 4 : ch < 0x04000000 ? 5 : 6;
43   int m = (0xff00 >> n), i = n-1;
44   *bp++ = (ch>>(6*i)) | m;
45   while( --i >= 0 ) *bp++ = ((ch>>(6*i)) & 0x3f) | 0x80;
46   return n;
47 }
48
49 using namespace std;
50
51 //csv = comma seperated value file
52 #define SEP ','
53 static bool is_sep(int ch) { return ch == SEP; }
54
55 static bool is_opnr(int ch)
56 {
57   if( ch ==  '\"'  ) return true;
58   if( ch == 0xab ) return true;
59   if( ch == 0xbb ) return true;
60   if( ch == 0x300c ) return true;
61   if( ch == 0x300d ) return true;
62   return 0;
63 }
64
65 // converts libreoffice csv to string (with quotes attached)
66 //  quote marks only
67 static void xlat1(uint8_t *&in, uint8_t *out)
68 {
69   uint8_t *ibp = in, *obp = out;
70   unsigned ch;
71   if( (ch=wnext(in)) == '\"' ) { 
72     bool is_nested = in[0] == '\"' && in[1] == '\"';
73     while( (ch=wnext(in)) != 0 ) {
74       if( ch == '\"' ) {
75         uint8_t *bp = in;
76         unsigned nch = wnext(in);
77         if( nch != '\"' ) { in = bp;  break; }
78       }
79       wnext(out, ch);
80     }
81     if( is_nested && ch == '"' ) {
82       while( out > obp && *(out-1) == ' ' ) --out;
83     }
84   }
85   if( ch != '"' ) {
86     in = ibp;  out = obp;
87     while( (ch=wnext(in)) && !is_sep(ch) ) wnext(out,ch);
88   }
89   *out = 0;
90 }
91
92 static inline unsigned gch(uint8_t *&in) {
93   unsigned ch = wnext(in);
94   if( ch == '\\' ) {
95     switch( (ch=*in++) ) {
96     case 'a':  ch = '\a';  break;
97     case 'b':  ch = '\b';  break;
98     case 'f':  ch = '\f';  break;
99     case 'n':  ch = '\n';  break;
100     case 'r':  ch = '\r';  break;
101     case 't':  ch = '\t';  break;
102     case 'v':  ch = '\v';  break;
103     }
104   }
105   return ch;
106 }
107
108 // converts string (with opn/cls attached) to c string
109 static void xlat2(uint8_t *in, uint8_t *out)
110 {
111   uint8_t *obp = out;
112   unsigned lch = 0, ch = gch(in);
113   if( ch ) {
114     if( !is_opnr(ch) ) wnext(out, ch);
115     while( (ch=gch(in)) != 0 ) {
116       lch = ch;  obp = out;
117       wnext(out, ch);
118     }
119     if( lch && is_opnr(lch) ) out = obp;
120   }
121   *out = 0;
122 }
123
124 // converts c++ string to c string text
125 static void xlat3(const char *cp, uint8_t *out)
126 {
127   wnext(out, '\"');
128   unsigned ch;
129   uint8_t *bp = (uint8_t*)cp;
130   while( (ch=wnext(bp)) != 0 ) {
131     switch( ch ) {
132     case '"':   ch = '\"'; break;
133     case '\a':  ch = 'a';  break;
134     case '\b':  ch = 'b';  break;
135     case '\f':  ch = 'f';  break;
136     case '\n':  ch = 'n';  break;
137     case '\r':  ch = 'r';  break;
138     case '\t':  ch = 't';  break;
139     case '\v':  ch = 'v';  break;
140     default: wnext(out,ch);  continue;
141     }
142     wnext(out,'\\');
143     wnext(out, ch);
144     if( ch == 'n' && *bp ) {
145       wnext(out, '\"');
146       wnext(out, '\n');
147       wnext(out, '\"');
148     }
149   }
150   wnext(out, '\"');
151   wnext(out, 0);
152 }
153
154 // converts c++ string to csv string text
155 static void xlat4(const char *cp, uint8_t *out)
156 {
157   wnext(out, '\"');
158   unsigned ch;
159   uint8_t *bp = (uint8_t*)cp;
160   while( (ch=wnext(bp)) != 0 ) {
161     switch( ch ) {
162     case '\a':  ch = 'a';  break;
163     case '\b':  ch = 'b';  break;
164     case '\f':  ch = 'f';  break;
165     case '\n':  ch = 'n';  break;
166     case '\r':  ch = 'r';  break;
167     case '\t':  ch = 't';  break;
168     case '\v':  ch = 'v';  break;
169     case '\"': wnext(out,ch); // fall thru
170     default: wnext(out,ch);  continue;
171     }
172     wnext(out,'\\');
173     wnext(out,ch);
174   }
175   wnext(out, '\"');
176   wnext(out, 0);
177 }
178
179 // parses input to c++ string
180 static string xlat(uint8_t *&in)
181 {
182   uint8_t bfr[MX_STR];  bfr[0] = 0;  xlat1(in, bfr);
183   uint8_t str[MX_STR];  str[0] = 0;  xlat2(bfr, str);
184   return string((const char*)str);
185 }
186
187 class tstring : public string {
188 public:
189   bool ok;
190   tstring(const char *sp, bool k) { string::assign(sp); ok = k; }
191 };
192
193 typedef map<string,tstring> Trans;
194 static Trans trans;
195
196 static inline bool prefix_is(uint8_t *bp, const char *cp)
197 {
198   return !strncmp((const char *)bp, cp, strlen(cp));
199 }
200 static inline uint8_t *bgets(uint8_t *bp, int len, FILE *fp)
201 {
202   uint8_t *ret = (uint8_t*)fgets((char*)bp, len, fp);
203   if( ret ) {
204     int len = strlen((char *)bp);
205     if( len > 0 && bp[len-1] == '\n' ) bp[len-1] = 0;
206   }
207   return ret;
208 }
209 static inline int bputs(uint8_t *bp, FILE *fp)
210 {
211   if( !fp ) return 0;
212   fputs((const char*)bp, fp);
213   fputc('\n',fp);
214   return 1;
215 }
216 static inline int bput(uint8_t *bp, FILE *fp)
217 {
218   if( !fp ) return 0;
219   fputs((const char*)bp, fp);
220   return 1;
221 }
222
223 static bool goog = false;
224 static bool nocmts = false;
225
226 static inline bool is_nlin(unsigned ch, uint8_t *bp)
227 {
228   return ch == ' ' && bp[0] == '\\' && bp[1] == ' ' && ( bp[2] == 'n' || bp[2] == 'N' );
229 }
230
231 static inline bool is_ccln(unsigned ch, uint8_t *bp)
232 {
233   return ch == ' ' && bp[0] == ':' && bp[1] == ':' && bp[2] == ' ';
234 }
235
236 static inline bool is_quot(unsigned ch, uint8_t *bp)
237 {
238   return ch == ' ' && bp[0] == '\\' && bp[1] == ' ' && bp[2] == '"';
239 }
240
241 static inline bool is_colon(unsigned ch)
242 {
243   return ch == 0xff1a;
244 }
245
246 static inline bool is_per(unsigned ch)
247 {
248   if( ch == '%' ) return true;
249   if( ch == 0xff05 ) return true;
250   return false;
251 }
252
253 static unsigned fmt_flds = 0;
254
255 enum { fmt_flg=1, fmt_wid=2, fmt_prc=4, fmt_len=8, fmt_cnv=16, };
256
257 static int is_flags(uint8_t *fp)
258 {
259   if( (fmt_flds & fmt_flg) != 0 ) return 0;
260   if( !strchr("#0-+ I", *fp) ) return 0;
261   fmt_flds |= fmt_flg;
262   return 1;
263 }
264
265 static int is_width(uint8_t *fp)
266 {
267   if( (fmt_flds & fmt_wid) != 0 ) return 0;
268   if( *fp != '*' && *fp < '0' && *fp > '9' ) return 0;
269   fmt_flds |= fmt_wid;
270   uint8_t *bp = fp;
271   bool argno = *fp++ == '*';
272   while( *fp >= '0' && *fp <= '9' ) ++fp;
273   if( argno && *fp++ != '$' ) return 1;
274   return fp - bp;
275 }
276
277 static int is_prec(uint8_t *fp)
278 {
279   if( (fmt_flds & fmt_prc) != 0 ) return 0;
280   if( *fp != '.' ) return 0;
281   fmt_flds |= fmt_prc;
282   if( *fp != '*' && *fp < '0' && *fp > '9' ) return 0;
283   uint8_t *bp = fp;
284   bool argno = *fp++ == '*';
285   while( *fp >= '0' && *fp <= '9' ) ++fp;
286   if( argno && *fp++ != '$' ) return 1;
287   return fp - bp;
288 }
289
290 static int is_len(uint8_t *fp)
291 {
292   if( (fmt_flds & fmt_len) != 0 ) return 0;
293   if( !strchr("hlLqjzt", *fp) ) return 0;
294   fmt_flds |= fmt_len;
295   if( fp[0] == 'h' && fp[1] == 'h' ) return 2;
296   if( fp[0] == 'l' && fp[1] == 'l' ) return 2;
297   return 1;
298 }
299
300 static int is_conv(uint8_t *fp)
301 {
302   if( !strchr("diouxXeEfFgGaAscCSpnm", *fp) ) return 0;
303   return 1;
304 }
305
306
307 static inline int fmt_spec(uint8_t *fp)
308 {
309   if( !*fp ) return 0;
310   if( is_per(*fp) ) return 1;
311   fmt_flds = 0;
312   uint8_t *bp = fp;
313   while( !is_conv(fp) ) {
314     int len;
315     if( !(len=is_flags(fp)) && !(len=is_width(fp)) &&
316         !(len=is_prec(fp))  && !(len=is_len(fp)) ) return 0;
317     fp += len;
318   }
319   return fp - bp + 1;
320 }
321
322 static bool chkfmt(int no, uint8_t *ap, uint8_t *bp, uint8_t *cp)
323 {
324   bool ret = true;
325   uint8_t *asp = ap, *bsp = bp;
326   int n = 0;
327   uint8_t *bep = bp;
328   unsigned bpr = 0, bch = wnext(bp);
329   for( ; bch!=0; bch=wnext(bp) ) {
330     if( goog && is_opnr(bch) ) ++n;
331     bep = bp;  bpr = bch;
332   }
333
334   // trim solitary opnrs on ends b
335   if( goog && ( n != 1 || !is_opnr(bpr) ) ) bep = bp;
336   bp = bsp;  bch = wnext(bp);
337   if( goog && ( n == 1 && is_opnr(bch) ) ) bch = wnext(bp);
338
339   unsigned apr = 0, ach = wnext(ap);
340   apr = bpr = 0;
341   while( ach != 0 && bch != 0 ) {
342     // move to % on a
343     while( ach != 0 && !is_per(ach) ) {
344       apr = ach;  ach = wnext(ap);
345     }
346     // move to % on b
347     while( bch != 0 && !is_per(bch) ) {
348       if( goog ) { // google xlat recoginizers
349         if( is_nlin(bch, bp) ) {
350           bch = '\n';  bp += 3;
351         }
352         else if( is_ccln(bch, bp) ) {
353           wnext(cp, bch=':');  bp += 3;
354         }
355         else if( is_quot(bch, bp) ) {
356           bch = '\"';  bp += 3;
357         }
358         else if( is_colon(bch) ) {
359           bch = ':';
360         }
361       }
362       wnext(cp,bch);  bpr = bch;
363       bch = bp >= bep ? 0 : wnext(bp);
364     }
365     if( !ach || !bch ) break;
366     // if % on a and % on b and is fmt_spec
367     if( is_per(ach) && is_per(bch) && (n=fmt_spec(ap)) > 0 ) {
368       if( apr && apr != bpr ) wnext(cp,apr);
369       wnext(cp,ach);  apr = ach;  ach = wnext(ap);
370       // copy format data from a
371       while( ach != 0 && --n >= 0 ) {
372         wnext(cp, ach);  apr = ach;  ach = wnext(ap);
373       }
374       bpr = bch;  bch = bp >= bep ? 0 : wnext(bp);
375       if( apr == '%' && bch == '%' ) {
376         // copy %% format data from b
377         bpr = bch;
378         bch = bp >= bep ? 0 : wnext(bp);
379       }
380       else {
381         // skip format data from b (ignore case)
382         while( bch != 0 && ((bpr ^ apr) & ~('a'-'A')) ) {
383           bpr = bch;
384           bch = bp >= bep ? 0 : wnext(bp);
385         }
386         // hit eol and didn't find end of spec on b
387         if( !bch && ((bpr ^ apr) & ~('a'-'A')) != 0 ) {
388           fprintf(stderr, "line %d: missed spec: %s\n", no, (char*)asp);
389           ret = false;
390         }
391       }
392     }
393     else {
394       fprintf(stderr, "line %d: missed fmt: %s\n", no, (char*)asp);
395       wnext(cp, bch);
396       apr = ach;  ach = wnext(ap);
397       bpr = bch;  bch = bp >= bep ? 0 : wnext(bp);
398       ret = false;
399     }
400   }
401   while( bch != 0 ) {
402     wnext(cp, bch);  bpr = bch;
403     bch = bp >= bep ? 0 : wnext(bp);
404   }
405   if( apr == '\n' && bpr != '\n' ) wnext(cp,'\n');
406   wnext(cp, 0);
407   return ret;
408 }
409
410 void load(FILE *afp, FILE *bfp)
411 {
412   int no = 0;
413   int ins = 0, rep = 0;
414   uint8_t inp[MX_STR];
415
416   while( bgets(inp, sizeof(inp), afp) ) {
417     ++no;
418     uint8_t *bp = inp;
419     string key = xlat(bp);
420     if( bfp ) {
421       if( !bgets(inp, sizeof(inp), bfp) ) {
422         fprintf(stderr,"xlat file ended early\n");
423         exit(1);
424       }
425       bp = inp;
426     }
427     else if( !is_sep(*bp++) ) {
428       fprintf(stderr, "missing sep at line %d: %s", no, inp);
429       exit(1);
430     }
431     string txt = xlat(bp);
432     const char *val = (const char *)bp;
433     uint8_t str[MX_STR];
434     bool ok = chkfmt(no, (uint8_t*)key.c_str(), (uint8_t*)txt.c_str(), str);
435       val = (const char*)str;
436     Trans::iterator it = trans.lower_bound(key);
437     if( it == trans.end() || it->first.compare(key) ) {
438       trans.insert(it, Trans::value_type(key, tstring(val, ok)));
439       ++ins;
440     }
441     else {
442       it->second.assign(val);
443       it->second.ok = ok;
444       ++rep;
445     }
446   }
447   fprintf(stderr,"***     ins %d, rep %d\n", ins, rep);
448 }
449
450 void scan_po(FILE *ifp, FILE *ofp)
451 {
452   int no = 0;
453   uint8_t ibfr[MX_STR], tbfr[MX_STR];
454
455   while( bgets(ibfr, sizeof(ibfr), ifp) ) {
456     if( !prefix_is(ibfr, "msgid ") ) {
457       if( nocmts && ibfr[0] == '#' ) continue;
458       bputs(ibfr, ofp);  ++no;
459       continue;
460     }
461     uint8_t str[MX_STR]; xlat2(&ibfr[6], str);
462     string key((const char*)str);
463     if( !bgets(tbfr, sizeof(tbfr), ifp) ) {
464       fprintf(stderr, "file truncated line %d: %s", no, ibfr);
465       exit(1);
466     }
467     bputs(ibfr, ofp);  ++no;
468    
469     while( tbfr[0] == '"' ) {
470       bputs(tbfr, ofp);  ++no;
471       xlat2(&tbfr[0], str);  key.append((const char*)str);
472       if( !bgets(tbfr, sizeof(tbfr), ifp) ) {
473         fprintf(stderr, "file truncated line %d: %s", no, ibfr);
474         exit(1);
475       }
476     }
477     if( !prefix_is(tbfr, "msgstr ") ) {
478       fprintf(stderr, "file truncated line %d: %s", no, ibfr);
479       exit(1);
480     }
481
482     if( !ofp ) {
483       if( !key.size() ) continue;
484       xlat3(key.c_str(), str);
485       printf("%s\n", (char *)str);
486       continue;
487     }
488
489     Trans::iterator it = trans.lower_bound(key);
490     if( it == trans.end() || it->first.compare(key) ) {
491       fprintf(stderr, "no trans line %d: %s\n", no, ibfr);
492       xlat3(key.c_str(), &tbfr[7]);
493       bputs(tbfr, ofp);  ++no;
494       bputs((uint8_t*)"#msgstr \"\"", ofp); ++no;
495     }
496     else if( !it->second.ok ) {
497       fprintf(stderr, "bad fmt line %d: %s\n", no, ibfr);
498       xlat3(it->first.c_str(), &tbfr[7]);
499       bputs(tbfr, ofp);  ++no;
500       xlat3(it->second.c_str(), str);
501       bput((uint8_t*)"#msgstr ", ofp);
502       bputs(str, ofp);  ++no;
503     }
504     else {
505       xlat3(it->second.c_str(), &tbfr[7]);
506       bputs(tbfr, ofp);  ++no;
507     }
508   }
509   if( ifp != stdin ) fclose(ifp);
510 }
511
512 void list_po(FILE *ifp, FILE *ofp)
513 {
514   int no = 0;
515   int dup = 0, nul = 0;
516   uint8_t ibfr[MX_STR], tbfr[MX_STR];
517
518   while( bgets(ibfr, sizeof(ibfr), ifp) ) {
519     ++no;
520     if( !prefix_is(ibfr, "msgid ") ) continue;
521     uint8_t str[MX_STR]; xlat2(&ibfr[6], str);
522     string key((const char*)str);
523     if( !bgets(tbfr, sizeof(tbfr), ifp) ) {
524       fprintf(stderr, "file truncated line %d: %s", no, ibfr);
525       exit(1);
526     }
527     ++no;
528    
529     while( tbfr[0] == '"' ) {
530       xlat2(&tbfr[0], str);  key.append((const char*)str);
531       if( !bgets(tbfr, sizeof(tbfr), ifp) ) {
532         fprintf(stderr, "file truncated line %d: %s", no, ibfr);
533         exit(1);
534       }
535       ++no;
536     }
537     if( !prefix_is(tbfr, "msgstr ") ) {
538       fprintf(stderr, "file truncated line %d: %s", no, ibfr);
539       exit(1);
540     }
541
542     xlat2(&tbfr[7], str);
543     string txt((const char*)str);
544    
545     while( bgets(tbfr, sizeof(tbfr), ifp) && tbfr[0] == '"' ) {
546       xlat2(&tbfr[0], str);  txt.append((const char*)str);
547       ++no;
548     }
549     if( !txt.size() ) { ++nul; continue; }
550     if( !key.compare(txt) ) { ++dup; continue; }
551     xlat4(key.c_str(), str);
552     fprintf(ofp, "%s,", (char *)str);
553     xlat4(txt.c_str(), str);
554     fprintf(ofp, "%s\n", (char *)str);
555   }
556   fprintf(stderr, "*** dup %d, nul %d\n", dup, nul);
557 }
558
559 static void usage(const char *av0)
560 {
561   printf("test csv    %s  csv < data.csv\n",av0);
562   printf("test po     %s   po < data.po\n",av0);
563   printf("get strings %s  key < xgettext.po\n",av0);
564   printf("gen xlation %s xlat   xgettext.po xlat.csv\n",av0);
565   printf("gen xlation %s xlat - text,xlat ... < xgettext.po\n",av0);
566   exit(1);
567 }
568
569 int main(int ac, char **av)
570 {
571   if( ac == 1 ) usage(av[0]);
572
573   // if to rework google xlat output
574   if( getenv("GOOG") ) goog = true;
575   if( getenv("NOCMTS") ) nocmts = true;
576
577   if( !strcmp(av[1],"csv") ) {  // test csv
578     load(stdin, 0);
579     for( Trans::iterator it = trans.begin(); it!=trans.end(); ++it ) {
580       uint8_t str[MX_STR];  xlat3(it->second.c_str(), str);
581       printf("key = \"%s\", val = %s\n", it->first.c_str(), (char *)str);
582     }
583     return 0;
584   }
585
586   if( !strcmp(av[1],"po") ) {  // test po
587     list_po(stdin, stdout);
588     return 0;
589   }
590
591   if( !strcmp(av[1],"key") ) {
592     scan_po(stdin, 0);
593     return 0;
594   }
595
596   if( ac < 3 ) usage(av[0]);
597
598   FILE *ifp = !strcmp(av[2],"-") ? stdin : fopen(av[2], "r");
599   if( !ifp ) { perror(av[2]);  exit(1); }
600  
601 //  if( ac < 4 ) usage(av[0]);
602
603   if( strcmp(av[1],"xlat") ) {
604     fprintf(stderr,"unkn cmd: %s\n", av[1]);
605     return 1;
606   }
607
608   for( int i=3; i<ac; ++i ) {  // create trans mapping
609     fprintf(stderr,"*** load %s\n", av[i]);
610     char fn[MX_STR*2];
611     strncpy(fn, av[i], sizeof(fn));
612     int k = 0;
613     FILE *bfp = 0;
614     // look for <filename> or <filename>,<filename>
615     while( k<(int)sizeof(fn) && fn[k]!=0 && fn[k]!=',' ) ++k;
616     if( k<(int)sizeof(fn) && fn[k]==',' ) {
617       fn[k++] = 0;
618       bfp = fopen(&fn[k], "r");
619       if( !bfp ) { perror(&fn[k]);  exit(1); }
620     }
621     FILE *afp = fopen(&fn[0], "r");
622     if( !afp ) { perror(&fn[0]);  exit(1); }
623     load(afp, bfp);
624     fclose(afp);
625     if( bfp ) fclose(bfp);
626   }
627
628   scan_po(ifp, stdout);
629   return 0;
630 }
631