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