update cin.po, goog xlat update xx.po
[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   unsigned lch = gch(in), rch = 1, ch = 0;
112   if( lch ) {
113     if( is_opnr(lch) ) {
114       for( uint8_t *ip=in; (ch=gch(ip))!=0; rch=ch );
115       if( lch == rch ) { lch = gch(in);  rch = 0; }
116     }
117     while( (ch=gch(in)) != 0 ) { wnext(out, lch);  lch = ch; }
118     if( rch ) wnext(out, lch);
119   }
120   *out = 0;
121 }
122
123 int brkput = 0;
124
125 // converts c++ string to c string text
126 static void xlat3(const char *cp, uint8_t *out)
127 {
128   wnext(out, '\"');
129   unsigned ch;
130   uint8_t *bp = (uint8_t*)cp;
131   while( (ch=wnext(bp)) != 0 ) {
132     switch( ch ) {
133     case '"':   ch = '\"'; break;
134     case '\a':  ch = 'a';  break;
135     case '\b':  ch = 'b';  break;
136     case '\f':  ch = 'f';  break;
137     case '\n':  ch = 'n';  break;
138     case '\r':  ch = 'r';  break;
139     case '\t':  ch = 't';  break;
140     case '\v':  ch = 'v';  break;
141     default: wnext(out,ch);  continue;
142     }
143     wnext(out,'\\');
144     wnext(out, ch);
145     if( brkput && ch == 'n' && *bp ) {
146       wnext(out, '\"');
147       wnext(out, '\n');
148       wnext(out, '\"');
149     }
150   }
151   wnext(out, '\"');
152   wnext(out, 0);
153 }
154
155 // converts c++ string to csv string text
156 static void xlat4(const char *cp, uint8_t *out)
157 {
158   wnext(out, '\"');
159   unsigned ch;
160   uint8_t *bp = (uint8_t*)cp;
161   while( (ch=wnext(bp)) != 0 ) {
162     switch( ch ) {
163     case '\a':  ch = 'a';  break;
164     case '\b':  ch = 'b';  break;
165     case '\f':  ch = 'f';  break;
166     case '\n':  ch = 'n';  break;
167     case '\r':  ch = 'r';  break;
168     case '\t':  ch = 't';  break;
169     case '\v':  ch = 'v';  break;
170     case '\"': wnext(out,ch); // fall thru
171     default: wnext(out,ch);  continue;
172     }
173     wnext(out,'\\');
174     wnext(out,ch);
175   }
176   wnext(out, '\"');
177   wnext(out, 0);
178 }
179
180 // parses input to c++ string
181 static string xlat(uint8_t *&in)
182 {
183   uint8_t bfr[MX_STR];  bfr[0] = 0;  xlat1(in, bfr);
184   uint8_t str[MX_STR];  str[0] = 0;  xlat2(bfr, str);
185   return string((const char*)str);
186 }
187
188 class tstring : public string {
189 public:
190   bool ok;
191   tstring(const char *sp, bool k) { string::assign(sp); ok = k; }
192 };
193
194 typedef map<string,tstring> Trans;
195 static Trans trans;
196
197 static inline bool prefix_is(uint8_t *bp, const char *cp)
198 {
199   return !strncmp((const char *)bp, cp, strlen(cp));
200 }
201 static inline uint8_t *bgets(uint8_t *bp, int len, FILE *fp)
202 {
203   uint8_t *ret = (uint8_t*)fgets((char*)bp, len, fp);
204   if( ret ) {
205     int len = strlen((char *)bp);
206     if( len > 0 && bp[len-1] == '\n' ) bp[len-1] = 0;
207   }
208   return ret;
209 }
210 static inline int bputs(uint8_t *bp, FILE *fp)
211 {
212   if( !fp ) return 0;
213   fputs((const char*)bp, fp);
214   fputc('\n',fp);
215   int n = 1;
216   while( *bp ) if( *bp++ == '\n' ) ++n;
217   return n;
218 }
219 static inline int bput(uint8_t *bp, FILE *fp)
220 {
221   if( !fp ) return 0;
222   fputs((const char*)bp, fp);
223   return 1;
224 }
225
226 static bool goog = false;
227 static bool nocmts = false;
228
229 static inline bool is_nlin(unsigned ch, uint8_t *bp)
230 {
231   return ch == ' ' && bp[0] == '\\' && bp[1] == ' ' && ( bp[2] == 'n' || bp[2] == 'N' );
232 }
233
234 static inline bool is_ccln(unsigned ch, uint8_t *bp)
235 {
236   return ch == ' ' && bp[0] == ':' && bp[1] == ':' && bp[2] == ' ';
237 }
238
239 static inline bool is_quot(unsigned ch, uint8_t *bp)
240 {
241   return ch == ' ' && bp[0] == '\\' && bp[1] == ' ' && bp[2] == '"';
242 }
243
244 static inline bool is_colon(unsigned ch)
245 {
246   return ch == 0xff1a;
247 }
248
249 static inline bool is_per(unsigned ch)
250 {
251   if( ch == '%' ) return true;
252   if( ch == 0xff05 ) return true;
253   return false;
254 }
255
256 static unsigned fmt_flds = 0;
257
258 enum { fmt_flg=1, fmt_wid=2, fmt_prc=4, fmt_len=8, fmt_cnv=16, };
259
260 static int is_flags(uint8_t *fp)
261 {
262   if( (fmt_flds & fmt_flg) != 0 ) return 0;
263   if( !strchr("#0-+ I", *fp) ) return 0;
264   fmt_flds |= fmt_flg;
265   return 1;
266 }
267
268 static int is_width(uint8_t *fp)
269 {
270   if( (fmt_flds & fmt_wid) != 0 ) return 0;
271   if( *fp != '*' && *fp < '0' && *fp > '9' ) return 0;
272   fmt_flds |= fmt_wid;
273   uint8_t *bp = fp;
274   bool argno = *fp++ == '*';
275   while( *fp >= '0' && *fp <= '9' ) ++fp;
276   if( argno && *fp++ != '$' ) return 1;
277   return fp - bp;
278 }
279
280 static int is_prec(uint8_t *fp)
281 {
282   if( (fmt_flds & fmt_prc) != 0 ) return 0;
283   if( *fp != '.' ) return 0;
284   fmt_flds |= fmt_prc;
285   if( *fp != '*' && *fp < '0' && *fp > '9' ) return 0;
286   uint8_t *bp = fp;
287   bool argno = *fp++ == '*';
288   while( *fp >= '0' && *fp <= '9' ) ++fp;
289   if( argno && *fp++ != '$' ) return 1;
290   return fp - bp;
291 }
292
293 static int is_len(uint8_t *fp)
294 {
295   if( (fmt_flds & fmt_len) != 0 ) return 0;
296   if( !strchr("hlLqjzt", *fp) ) return 0;
297   fmt_flds |= fmt_len;
298   if( fp[0] == 'h' && fp[1] == 'h' ) return 2;
299   if( fp[0] == 'l' && fp[1] == 'l' ) return 2;
300   return 1;
301 }
302
303 static int is_conv(uint8_t *fp)
304 {
305   if( !strchr("diouxXeEfFgGaAscCSpnm", *fp) ) return 0;
306   return 1;
307 }
308
309
310 static inline int fmt_spec(uint8_t *fp)
311 {
312   if( !*fp ) return 0;
313   if( is_per(*fp) ) return 1;
314   fmt_flds = 0;
315   uint8_t *bp = fp;
316   while( !is_conv(fp) ) {
317     int len;
318     if( !(len=is_flags(fp)) && !(len=is_width(fp)) &&
319         !(len=is_prec(fp))  && !(len=is_len(fp)) ) return 0;
320     fp += len;
321   }
322   return fp - bp + 1;
323 }
324
325 static bool chkfmt(int no, uint8_t *ap, uint8_t *bp, uint8_t *cp)
326 {
327   bool ret = true;
328   uint8_t *asp = ap, *bsp = bp;
329   int n = 0;
330   uint8_t *bep = bp;
331   unsigned bpr = 0, bch = wnext(bp);
332   for( ; bch!=0; bch=wnext(bp) ) {
333     if( goog && is_opnr(bch) ) ++n;
334     bep = bp;  bpr = bch;
335   }
336
337   // trim solitary opnrs on ends b
338   if( goog && ( n != 1 || !is_opnr(bpr) ) ) bep = bp;
339   bp = bsp;  bch = wnext(bp);
340   if( goog && ( n == 1 && is_opnr(bch) ) ) bch = wnext(bp);
341
342   unsigned apr = 0, ach = wnext(ap);
343   apr = bpr = 0;
344   while( ach != 0 && bch != 0 ) {
345     // move to % on a
346     while( ach != 0 && !is_per(ach) ) {
347       apr = ach;  ach = wnext(ap);
348     }
349     // move to % on b
350     while( bch != 0 && !is_per(bch) ) {
351       if( goog ) { // google xlat recoginizers
352         if( is_nlin(bch, bp) ) {
353           bch = '\n';  bp += 3;
354         }
355         else if( is_ccln(bch, bp) ) {
356           wnext(cp, bch=':');  bp += 3;
357         }
358         else if( is_quot(bch, bp) ) {
359           bch = '\"';  bp += 3;
360         }
361         else if( is_colon(bch) ) {
362           bch = ':';
363         }
364       }
365       wnext(cp,bch);  bpr = bch;
366       bch = bp >= bep ? 0 : wnext(bp);
367     }
368     if( !ach || !bch ) break;
369     // if % on a and % on b and is fmt_spec
370     if( is_per(ach) && is_per(bch) && (n=fmt_spec(ap)) > 0 ) {
371       if( apr && apr != bpr ) wnext(cp,apr);
372       wnext(cp,ach);  apr = ach;  ach = wnext(ap);
373       // copy format data from a
374       while( ach != 0 && --n >= 0 ) {
375         wnext(cp, ach);  apr = ach;  ach = wnext(ap);
376       }
377       bpr = bch;  bch = bp >= bep ? 0 : wnext(bp);
378       if( apr == '%' && bch == '%' ) {
379         // copy %% format data from b
380         bpr = bch;
381         bch = bp >= bep ? 0 : wnext(bp);
382       }
383       else {
384         // skip format data from b (ignore case)
385         while( bch != 0 && ((bpr ^ apr) & ~('a'-'A')) ) {
386           bpr = bch;
387           bch = bp >= bep ? 0 : wnext(bp);
388         }
389         // hit eol and didn't find end of spec on b
390         if( !bch && ((bpr ^ apr) & ~('a'-'A')) != 0 ) {
391           fprintf(stderr, "line %d: missed spec: %s\n", no, (char*)asp);
392           ret = false;
393         }
394       }
395     }
396     else {
397       fprintf(stderr, "line %d: missed fmt: %s\n", no, (char*)asp);
398       wnext(cp, bch);
399       apr = ach;  ach = wnext(ap);
400       bpr = bch;  bch = bp >= bep ? 0 : wnext(bp);
401       ret = false;
402     }
403   }
404   while( bch != 0 ) {
405     wnext(cp, bch);  bpr = bch;
406     bch = bp >= bep ? 0 : wnext(bp);
407   }
408   if( apr == '\n' && bpr != '\n' ) wnext(cp,'\n');
409   wnext(cp, 0);
410   return ret;
411 }
412
413 void load(FILE *afp, FILE *bfp)
414 {
415   int no = 0;
416   int ins = 0, rep = 0;
417   uint8_t inp[MX_STR];
418
419   while( bgets(inp, sizeof(inp), afp) ) {
420     ++no;
421     uint8_t *bp = inp;
422     string key = xlat(bp);
423     if( bfp ) {
424       if( !bgets(inp, sizeof(inp), bfp) ) {
425         fprintf(stderr,"xlat file ended early\n");
426         exit(1);
427       }
428       bp = inp;
429     }
430     else if( !is_sep(*bp++) ) {
431       fprintf(stderr, "missing sep at line %d: %s", no, inp);
432       exit(1);
433     }
434     string txt = xlat(bp);
435     const char *val = (const char *)bp;
436     uint8_t str[MX_STR];
437     bool ok = chkfmt(no, (uint8_t*)key.c_str(), (uint8_t*)txt.c_str(), str);
438       val = (const char*)str;
439     Trans::iterator it = trans.lower_bound(key);
440     if( it == trans.end() || it->first.compare(key) ) {
441       trans.insert(it, Trans::value_type(key, tstring(val, ok)));
442       ++ins;
443     }
444     else {
445       it->second.assign(val);
446       it->second.ok = ok;
447       ++rep;
448     }
449   }
450   fprintf(stderr,"***     ins %d, rep %d\n", ins, rep);
451 }
452
453 void scan_po(FILE *ifp, FILE *ofp)
454 {
455   int no = 0;
456   uint8_t ibfr[MX_STR], tbfr[MX_STR];
457
458   while( bgets(ibfr, sizeof(ibfr), ifp) ) {
459     if( !prefix_is(ibfr, "msgid ") ) {
460       if( nocmts && ibfr[0] == '#' ) continue;
461       no += bputs(ibfr, ofp);
462       continue;
463     }
464     uint8_t str[MX_STR]; xlat2(&ibfr[6], str);
465     string key((const char*)str);
466     if( !bgets(tbfr, sizeof(tbfr), ifp) ) {
467       fprintf(stderr, "file truncated line %d: %s", no, ibfr);
468       exit(1);
469     }
470     no += bputs(ibfr, ofp);
471    
472     while( tbfr[0] == '"' ) {
473       no += bputs(tbfr, ofp);
474       xlat2(&tbfr[0], str);  key.append((const char*)str);
475       if( !bgets(tbfr, sizeof(tbfr), ifp) ) {
476         fprintf(stderr, "file truncated line %d: %s", no, ibfr);
477         exit(1);
478       }
479     }
480     if( !prefix_is(tbfr, "msgstr ") ) {
481       fprintf(stderr, "file truncated line %d: %s", no, ibfr);
482       exit(1);
483     }
484
485     if( !ofp ) {
486       if( !key.size() ) continue;
487       xlat3(key.c_str(), str);
488       printf("%s\n", (char *)str);
489       continue;
490     }
491
492     Trans::iterator it = trans.lower_bound(key);
493     if( it == trans.end() || it->first.compare(key) ) {
494       fprintf(stderr, "no trans line %d: %s\n", no, ibfr);
495       xlat3(key.c_str(), &tbfr[7]);
496       no += bputs(tbfr, ofp);
497       no += bputs((uint8_t*)"#msgstr \"\"", ofp);
498     }
499     else if( 0 && !it->second.ok ) {
500       fprintf(stderr, "bad fmt line %d: %s\n", no, ibfr);
501       xlat3(it->first.c_str(), &tbfr[7]);
502       no += bputs(tbfr, ofp);
503       xlat3(it->second.c_str(), str);
504       bput((uint8_t*)"#msgstr ", ofp);
505       no += bputs(str, ofp);
506     }
507     else {
508       xlat3(it->second.c_str(), &tbfr[7]);
509       no += bputs(tbfr, ofp);
510     }
511   }
512   if( ifp != stdin ) fclose(ifp);
513 }
514
515 void list_po(FILE *ifp, FILE *ofp, int xeqx = 0, int nnul = 0)
516 {
517   int no = 0;
518   int dup = 0, nul = 0;
519   uint8_t ibfr[MX_STR], tbfr[MX_STR];
520
521   while( bgets(ibfr, sizeof(ibfr), ifp) ) {
522     ++no;
523     if( !prefix_is(ibfr, "msgid ") ) continue;
524     uint8_t str[MX_STR]; xlat2(&ibfr[6], str);
525     string key((const char*)str);
526     if( !bgets(tbfr, sizeof(tbfr), ifp) ) {
527       fprintf(stderr, "file truncated line %d: %s", no, ibfr);
528       exit(1);
529     }
530     ++no;
531    
532     while( tbfr[0] == '"' ) {
533       xlat2(&tbfr[0], str);  key.append((const char*)str);
534       if( !bgets(tbfr, sizeof(tbfr), ifp) ) {
535         fprintf(stderr, "file truncated line %d: %s", no, ibfr);
536         exit(1);
537       }
538       ++no;
539     }
540     if( !prefix_is(tbfr, "msgstr ") ) {
541       fprintf(stderr, "file truncated line %d: %s", no, ibfr);
542       exit(1);
543     }
544
545     xlat2(&tbfr[7], str);
546     string txt((const char*)str);
547    
548     while( bgets(tbfr, sizeof(tbfr), ifp) && tbfr[0] == '"' ) {
549       xlat2(&tbfr[0], str);  txt.append((const char*)str);
550       ++no;
551     }
552     if( nnul && !txt.size() ) {
553       ++nul;
554       if( nnul > 0 ) continue;
555     }
556     else if( xeqx && !key.compare(txt) ) {
557        ++dup;
558        if( xeqx > 0 ) continue;
559     }
560     else if( nnul < 0 || xeqx < 0 ) continue;
561     xlat4(key.c_str(), str);
562     fprintf(ofp, "%s,", (char *)str);
563     xlat4(txt.c_str(), str);
564     fprintf(ofp, "%s\n", (char *)str);
565   }
566   fprintf(stderr, "*** dup %d, nul %d\n", dup, nul);
567 }
568
569 static void usage(const char *av0)
570 {
571   printf("list csv    %s csv < data.csv > data.po\n",av0);
572   printf("list po     %s po < data.po > data.csv\n",av0);
573   printf("list po     %s dups < data.po\n",av0);
574   printf("list po     %s nodups < data.po\n",av0);
575   printf("get strings %s key  < xgettext.po\n",av0);
576   printf("gen xlation %s xlat   xgettext.po xlat.csv\n",av0);
577   printf("gen xlation %s xlat - text,xlat ... < xgettext.po\n",av0);
578   exit(1);
579 }
580
581 int main(int ac, char **av)
582 {
583   if( ac == 1 ) usage(av[0]);
584
585   // if to rework google xlat output
586   if( getenv("GOOG") ) goog = true;
587   if( getenv("NOCMTS") ) nocmts = true;
588
589   if( !strcmp(av[1],"csv") ) {  // test csv
590     load(stdin, 0);
591     for( Trans::iterator it = trans.begin(); it!=trans.end(); ++it ) {
592       uint8_t str1[MX_STR];  xlat3(it->first.c_str(), str1);
593       printf("msgid %s\n", (char *)str1);
594       uint8_t str2[MX_STR];  xlat3(it->second.c_str(), str2);
595       printf("msgstr %s\n\n", (char *)str2);
596     }
597     return 0;
598   }
599
600   if( !strcmp(av[1],"dups") ) {  // test po
601     list_po(stdin, stdout, -1, -1);
602     return 0;
603   }
604
605   if( !strcmp(av[1],"nodups") ) {  // test po
606     list_po(stdin, stdout, 1, 1);
607     return 0;
608   }
609
610   if( !strcmp(av[1],"po") ) {  // test po
611     list_po(stdin, stdout);
612     return 0;
613   }
614
615   if( !strcmp(av[1],"key") ) {
616     scan_po(stdin, 0);
617     return 0;
618   }
619
620   if( ac < 3 ) usage(av[0]);
621
622   FILE *ifp = !strcmp(av[2],"-") ? stdin : fopen(av[2], "r");
623   if( !ifp ) { perror(av[2]);  exit(1); }
624  
625 //  if( ac < 4 ) usage(av[0]);
626
627   if( strcmp(av[1],"xlat") ) {
628     fprintf(stderr,"unkn cmd: %s\n", av[1]);
629     return 1;
630   }
631
632   brkput = 1;
633   for( int i=3; i<ac; ++i ) {  // create trans mapping
634     fprintf(stderr,"*** load %s\n", av[i]);
635     char fn[MX_STR*2];
636     strncpy(fn, av[i], sizeof(fn));
637     int k = 0;
638     FILE *bfp = 0;
639     // look for <filename> or <filename>,<filename>
640     while( k<(int)sizeof(fn) && fn[k]!=0 && fn[k]!=',' ) ++k;
641     if( k<(int)sizeof(fn) && fn[k]==',' ) {
642       fn[k++] = 0;
643       bfp = fopen(&fn[k], "r");
644       if( !bfp ) { perror(&fn[k]);  exit(1); }
645     }
646     FILE *afp = fopen(&fn[0], "r");
647     if( !afp ) { perror(&fn[0]);  exit(1); }
648     load(afp, bfp);
649     fclose(afp);
650     if( bfp ) fclose(bfp);
651   }
652
653   scan_po(ifp, stdout);
654   return 0;
655 }
656