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