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
19 unsigned int wnext(uint8_t *&bp)
21 unsigned int ch = *bp++;
23 static const unsigned char byts[] = {
24 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5,
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
33 ch = i<0 ? ch-ofs[n] : '?';
38 int wnext(uint8_t *&bp, unsigned int ch)
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;
51 //csv = comma seperated value file
53 static bool is_sep(int ch) { return ch == SEP; }
55 static bool is_opnr(int ch)
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;
65 // converts libreoffice csv to string (with quotes attached)
67 static void xlat1(uint8_t *&in, uint8_t *out)
69 uint8_t *ibp = in, *obp = out;
71 if( (ch=wnext(in)) == '\"' ) {
72 bool is_nested = in[0] == '\"' && in[1] == '\"';
73 while( (ch=wnext(in)) != 0 ) {
76 unsigned nch = wnext(in);
77 if( nch != '\"' ) { in = bp; break; }
81 if( is_nested && ch == '"' ) {
82 while( out > obp && *(out-1) == ' ' ) --out;
87 while( (ch=wnext(in)) && !is_sep(ch) ) wnext(out,ch);
92 static inline unsigned gch(uint8_t *&in) {
93 unsigned ch = wnext(in);
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;
108 // converts string (with opn/cls attached) to c string
109 static void xlat2(uint8_t *in, uint8_t *out)
112 unsigned lch = 0, ch = gch(in);
114 if( !is_opnr(ch) ) wnext(out, ch);
115 while( (ch=gch(in)) != 0 ) {
119 if( lch && is_opnr(lch) ) out = obp;
124 // converts c++ string to c string text
125 static void xlat3(const char *cp, uint8_t *out)
129 uint8_t *bp = (uint8_t*)cp;
130 while( (ch=wnext(bp)) != 0 ) {
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;
149 // converts c++ string to csv string text
150 static void xlat4(const char *cp, uint8_t *out)
154 uint8_t *bp = (uint8_t*)cp;
155 while( (ch=wnext(bp)) != 0 ) {
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;
174 // parses input to c++ string
175 static string xlat(uint8_t *&in)
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);
182 class tstring : public string {
185 tstring(const char *sp, bool k) { string::assign(sp); ok = k; }
188 typedef map<string,tstring> Trans;
191 static inline bool prefix_is(uint8_t *bp, const char *cp)
193 return !strncmp((const char *)bp, cp, strlen(cp));
195 static inline uint8_t *bgets(uint8_t *bp, int len, FILE *fp)
197 uint8_t *ret = (uint8_t*)fgets((char*)bp, len, fp);
199 int len = strlen((char *)bp);
200 if( len > 0 && bp[len-1] == '\n' ) bp[len-1] = 0;
204 static inline int bputs(uint8_t *bp, FILE *fp)
207 fputs((const char*)bp, fp);
211 static inline int bput(uint8_t *bp, FILE *fp)
214 fputs((const char*)bp, fp);
219 static inline bool is_nlin(unsigned ch, uint8_t *bp)
221 return ch == ' ' && bp[0] == '\\' && bp[1] == ' ' && ( bp[2] == 'n' || bp[2] == 'N' );
224 static inline bool is_ccln(unsigned ch, uint8_t *bp)
226 return ch == ' ' && bp[0] == ':' && bp[1] == ':' && bp[2] == ' ';
229 static inline bool is_quot(unsigned ch, uint8_t *bp)
231 return ch == ' ' && bp[0] == '\\' && bp[1] == ' ' && bp[2] == '"';
234 static inline bool is_colon(unsigned ch)
239 static inline bool is_per(unsigned ch)
241 if( ch == '%' ) return true;
242 if( ch == 0xff05 ) return true;
246 static unsigned fmt_flds = 0;
248 enum { fmt_flg=1, fmt_wid=2, fmt_prc=4, fmt_len=8, fmt_cnv=16, };
250 static int is_flags(uint8_t *fp)
252 if( (fmt_flds & fmt_flg) != 0 ) return 0;
253 if( !strchr("#0-+ I", *fp) ) return 0;
258 static int is_width(uint8_t *fp)
260 if( (fmt_flds & fmt_wid) != 0 ) return 0;
261 if( *fp != '*' && *fp < '0' && *fp > '9' ) return 0;
264 bool argno = *fp++ == '*';
265 while( *fp >= '0' && *fp <= '9' ) ++fp;
266 if( argno && *fp++ != '$' ) return 1;
270 static int is_prec(uint8_t *fp)
272 if( (fmt_flds & fmt_prc) != 0 ) return 0;
273 if( *fp != '.' ) return 0;
275 if( *fp != '*' && *fp < '0' && *fp > '9' ) return 0;
277 bool argno = *fp++ == '*';
278 while( *fp >= '0' && *fp <= '9' ) ++fp;
279 if( argno && *fp++ != '$' ) return 1;
283 static int is_len(uint8_t *fp)
285 if( (fmt_flds & fmt_len) != 0 ) return 0;
286 if( !strchr("hlLqjzt", *fp) ) return 0;
288 if( fp[0] == 'h' && fp[1] == 'h' ) return 2;
289 if( fp[0] == 'l' && fp[1] == 'l' ) return 2;
293 static int is_conv(uint8_t *fp)
295 if( !strchr("diouxXeEfFgGaAscCSpnm", *fp) ) return 0;
300 static inline int fmt_spec(uint8_t *fp)
303 if( is_per(*fp) ) return 1;
306 while( !is_conv(fp) ) {
308 if( !(len=is_flags(fp)) && !(len=is_width(fp)) &&
309 !(len=is_prec(fp)) && !(len=is_len(fp)) ) return 0;
315 static bool chkfmt(int no, uint8_t *ap, uint8_t *bp, uint8_t *cp)
318 uint8_t *asp = ap, *bsp = bp;
321 unsigned bpr = 0, bch = wnext(bp);
322 for( ; bch!=0; bch=wnext(bp) ) {
323 if( is_opnr(bch) ) ++n;
326 // trim solitary opnrs on ends b
327 if( n != 1 || !is_opnr(bpr) ) bep = bp;
328 bp = bsp; bch = wnext(bp);
329 if( n == 1 && is_opnr(bch) ) bch = wnext(bp);
331 unsigned apr = 0, ach = wnext(ap);
333 while( ach != 0 && bch != 0 ) {
335 while( ach != 0 && !is_per(ach) ) {
336 apr = ach; ach = wnext(ap);
339 while( bch != 0 && !is_per(bch) ) {
340 if( is_nlin(bch, bp) ) {
343 else if( is_ccln(bch, bp) ) {
344 wnext(cp, bch=':'); bp += 3;
346 else if( is_quot(bch, bp) ) {
349 else if( is_colon(bch) ) {
352 wnext(cp,bch); bpr = bch;
353 bch = bp >= bep ? 0 : wnext(bp);
355 if( !ach || !bch ) break;
356 // if % on a and % on b and is fmt_spec
357 if( is_per(ach) && is_per(bch) && (n=fmt_spec(ap)) > 0 ) {
358 if( apr != bpr ) wnext(cp,apr);
359 wnext(cp,ach); apr = ach; ach = wnext(ap);
360 // copy format data from a
361 while( ach != 0 && --n >= 0 ) {
362 wnext(cp, ach); apr = ach; ach = wnext(ap);
364 bpr = bch; bch = bp >= bep ? 0 : wnext(bp);
365 // skip format data from b (ignore case)
366 while( bch != 0 && ((bpr ^ apr) & ~('a'-'A')) ) {
368 bch = bp >= bep ? 0 : wnext(bp);
370 // hit eol and didn't find end of spec on b
371 if( !bch && !((bpr ^ apr) & ~('a'-'A')) ) {
372 fprintf(stderr, "line %d: missed spec: %s\n", no, (char*)asp);
377 fprintf(stderr, "line %d: missed fmt: %s\n", no, (char*)asp);
379 apr = ach; ach = wnext(ap);
380 bpr = bch; bch = bp >= bep ? 0 : wnext(bp);
385 wnext(cp, bch); bpr = bch;
386 bch = bp >= bep ? 0 : wnext(bp);
388 if( apr == '\n' && bpr != '\n' ) wnext(cp,'\n');
393 void load(FILE *afp, FILE *bfp)
398 while( bgets(inp, sizeof(inp), afp) ) {
401 string key = xlat(bp);
403 if( !bgets(inp, sizeof(inp), bfp) ) {
404 fprintf(stderr,"xlat file ended early\n");
409 else if( !is_sep(*bp++) ) {
410 fprintf(stderr, "missing sep at line %d: %s", no, inp);
413 string txt = xlat(bp);
414 const char *val = (const char *)bp;
416 bool ok = chkfmt(no, (uint8_t*)key.c_str(), (uint8_t*)txt.c_str(), str);
417 val = (const char*)str;
418 Trans::iterator it = trans.lower_bound(key);
419 if( it == trans.end() || it->first.compare(key) )
420 trans.insert(it, Trans::value_type(key, tstring(val, ok)));
422 (string)it->second = val;
428 void scan_po(FILE *ifp, FILE *ofp)
431 uint8_t ibfr[MX_STR], tbfr[MX_STR];
433 while( bgets(ibfr, sizeof(ibfr), ifp) ) {
434 if( !prefix_is(ibfr, "msgid ") ) {
435 bputs(ibfr, ofp); ++no;
438 uint8_t str[MX_STR]; xlat2(&ibfr[6], str);
439 string key((const char*)str);
440 if( !bgets(tbfr, sizeof(tbfr), ifp) ) {
441 fprintf(stderr, "file truncated line %d: %s", no, ibfr);
444 bputs(ibfr, ofp); ++no;
446 while( tbfr[0] == '"' ) {
447 bputs(tbfr, ofp); ++no;
448 xlat2(&tbfr[0], str); key.append((const char*)str);
449 if( !bgets(tbfr, sizeof(tbfr), ifp) ) {
450 fprintf(stderr, "file truncated line %d: %s", no, ibfr);
454 if( !prefix_is(tbfr, "msgstr ") ) {
455 fprintf(stderr, "file truncated line %d: %s", no, ibfr);
460 if( !key.size() ) continue;
461 xlat3(key.c_str(), str);
462 printf("%s\n", (char *)str);
466 Trans::iterator it = trans.lower_bound(key);
467 if( it == trans.end() || it->first.compare(key) ) {
468 fprintf(stderr, "no trans line %d: %s\n", no, ibfr);
469 xlat3(key.c_str(), &tbfr[7]);
470 bputs(tbfr, ofp); ++no;
471 bputs((uint8_t*)"#msgstr \"\"", ofp); ++no;
473 else if( !it->second.ok ) {
474 fprintf(stderr, "bad fmt line %d: %s\n", no, ibfr);
475 xlat3(it->first.c_str(), &tbfr[7]);
476 bputs(tbfr, ofp); ++no;
477 xlat3(it->second.c_str(), str);
478 bput((uint8_t*)"#msgstr ", ofp);
479 bputs(str, ofp); ++no;
482 xlat3(it->second.c_str(), &tbfr[7]);
483 bputs(tbfr, ofp); ++no;
486 if( ifp != stdin ) fclose(ifp);
489 void list_po(FILE *ifp, FILE *ofp)
492 uint8_t ibfr[MX_STR], tbfr[MX_STR];
494 while( bgets(ibfr, sizeof(ibfr), ifp) ) {
496 if( !prefix_is(ibfr, "msgid ") ) continue;
497 uint8_t str[MX_STR]; xlat2(&ibfr[6], str);
498 string key((const char*)str);
499 if( !bgets(tbfr, sizeof(tbfr), ifp) ) {
500 fprintf(stderr, "file truncated line %d: %s", no, ibfr);
505 while( tbfr[0] == '"' ) {
506 xlat2(&tbfr[0], str); key.append((const char*)str);
507 if( !bgets(tbfr, sizeof(tbfr), ifp) ) {
508 fprintf(stderr, "file truncated line %d: %s", no, ibfr);
513 if( !prefix_is(tbfr, "msgstr ") ) {
514 fprintf(stderr, "file truncated line %d: %s", no, ibfr);
518 xlat2(&ibfr[7], str);
519 string txt((const char*)str);
520 if( !bgets(tbfr, sizeof(tbfr), ifp) ) {
521 fprintf(stderr, "file truncated line %d: %s", no, ibfr);
526 while( tbfr[0] == '"' ) {
527 xlat2(&tbfr[0], str); txt.append((const char*)str);
528 if( !bgets(tbfr, sizeof(tbfr), ifp) ) {
529 fprintf(stderr, "file truncated line %d: %s", no, ibfr);
534 if( !txt.size() ) continue;
535 if( !key.compare(txt) ) continue;
536 xlat4(key.c_str(), str);
537 fprintf(ofp, "%s,", (char *)str);
538 xlat4(txt.c_str(), str);
539 fprintf(ofp, "%s\n", (char *)str);
543 static void usage(const char *av0)
545 printf("test csv %s csv < data.csv\n",av0);
546 printf("test po %s po < data.po\n",av0);
547 printf("get strings %s key < xgettext.po\n",av0);
548 printf("gen xlation %s xlat < xgettext.po xlat.csv\n",av0);
549 printf("gen xlation %s xlat < xgettext.po text,xlat ...\n",av0);
553 int main(int ac, char **av)
555 if( ac == 1 ) usage(av[0]);
557 if( !strcmp(av[1],"csv") ) { // test csv
559 for( Trans::iterator it = trans.begin(); it!=trans.end(); ++it ) {
560 uint8_t str[MX_STR]; xlat3(it->second.c_str(), str);
561 printf("key = \"%s\", val = %s\n", it->first.c_str(), (char *)str);
566 if( !strcmp(av[1],"po") ) { // test po
567 list_po(stdin, stdout);
571 if( !strcmp(av[1],"key") ) {
576 if( ac < 3 ) usage(av[0]);
578 FILE *ifp = !strcmp(av[2],"-") ? stdin : fopen(av[2], "r");
579 if( !ifp ) { perror(av[2]); exit(1); }
581 // if( ac < 4 ) usage(av[0]);
583 if( strcmp(av[1],"xlat") ) {
584 fprintf(stderr,"unkn cmd: %s\n", av[1]);
588 for( int i=3; i<ac; ++i ) { // create trans mapping
590 strncpy(fn, av[i], sizeof(fn));
593 // look for <filename> or <filename>,<filename>
594 while( k<(int)sizeof(fn) && fn[k]!=0 && fn[k]!=',' ) ++k;
595 if( k<(int)sizeof(fn) && fn[k]==',' ) {
597 bfp = fopen(&fn[k], "r");
598 if( !bfp ) { perror(&fn[k]); exit(1); }
600 FILE *afp = fopen(&fn[0], "r");
601 if( !afp ) { perror(&fn[0]); exit(1); }
604 if( bfp ) fclose(bfp);
607 scan_po(ifp, stdout);