| 1 | /* |
| 2 | * test.c - general libcharset test/demo program which converts |
| 3 | * between two arbitrary charsets. |
| 4 | */ |
| 5 | |
| 6 | #include <stdio.h> |
| 7 | #include <string.h> |
| 8 | #include "charset.h" |
| 9 | |
| 10 | #define lenof(x) ( sizeof((x)) / sizeof(*(x)) ) |
| 11 | |
| 12 | int main(int argc, char **argv) |
| 13 | { |
| 14 | int srcset, dstset; |
| 15 | charset_state instate = CHARSET_INIT_STATE; |
| 16 | charset_state outstate = CHARSET_INIT_STATE; |
| 17 | char inbuf[256], outbuf[256]; |
| 18 | wchar_t midbuf[256]; |
| 19 | const char *inptr; |
| 20 | const wchar_t *midptr; |
| 21 | int rdret, inlen, midlen, inret, midret; |
| 22 | |
| 23 | if (argc != 3) { |
| 24 | fprintf(stderr, "usage: convcs <charset> <charset>\n"); |
| 25 | return 1; |
| 26 | } |
| 27 | |
| 28 | srcset = charset_from_localenc(argv[1]); |
| 29 | if (srcset == CS_NONE) { |
| 30 | fprintf(stderr, "unknown source charset '%s'\n", argv[1]); |
| 31 | return 1; |
| 32 | } |
| 33 | |
| 34 | dstset = charset_from_localenc(argv[2]); |
| 35 | if (dstset == CS_NONE) { |
| 36 | fprintf(stderr, "unknown destination charset '%s'\n", argv[2]); |
| 37 | return 1; |
| 38 | } |
| 39 | |
| 40 | while (1) { |
| 41 | |
| 42 | rdret = fread(inbuf, 1, sizeof(inbuf), stdin); |
| 43 | |
| 44 | if (rdret <= 0) |
| 45 | break; /* EOF */ |
| 46 | |
| 47 | inlen = rdret; |
| 48 | inptr = inbuf; |
| 49 | while ( (inret = charset_to_unicode(&inptr, &inlen, midbuf, |
| 50 | lenof(midbuf), srcset, |
| 51 | &instate, NULL, 0)) > 0) { |
| 52 | midlen = inret; |
| 53 | midptr = midbuf; |
| 54 | while ( (midret = charset_from_unicode(&midptr, &midlen, outbuf, |
| 55 | lenof(outbuf), dstset, |
| 56 | &outstate, NULL)) > 0) { |
| 57 | fwrite(outbuf, 1, midret, stdout); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /* |
| 63 | * Reset encoding state. |
| 64 | */ |
| 65 | while ( (midret = charset_from_unicode(NULL, NULL, outbuf, |
| 66 | lenof(outbuf), dstset, |
| 67 | &outstate, NULL)) > 0) { |
| 68 | fwrite(outbuf, 1, midret, stdout); |
| 69 | } |
| 70 | |
| 71 | return 0; |
| 72 | } |