/* Last edited on 2024-12-25 10:17:34 by stolfi */ /* Reads a GB-encoded Chinese text, insertes spaces around each Chinese character (a pair of bytes in 0xA0 .. 0xFF). */ #include #include #include #define LOCHA (0x80) #define HICHA (0xFF) #define LOCHB (0x21) #define HICHB (0xFF) #define SPACE (0x20) void doputchar(int c); void error(char *msg); static int nerrors = 0; static int nread = 0; int main(int argc, char **argv) { int q, r, s; q = 0; while(1) { r = getchar(); if (r == EOF) { fclose(stdout); exit(0); } else { nread++; if ((r < LOCHA) || (r > HICHA)) { doputchar(r); q = r; } else { s = getchar(); if (s == EOF) { doputchar(r); q = r; error("unexpected EOF in Chinese character"); } else { nread++; if ((s < LOCHB) || (s > HICHB)) { doputchar(r); doputchar(s); q = s; error("incomplete Chinese character"); } else { if (q != SPACE) { doputchar(SPACE); } doputchar(r); doputchar(s); doputchar(SPACE); q = SPACE; } } } } } } void doputchar(int c) { int res = putchar(c); if (res != c) { error("write error"); } } #define MAXERRORS (10) void error(char *msg) { fprintf(stdout, "«ERR»"); if (nerrors < MAXERRORS) { fprintf(stderr, "byte %d: %s\n", nread, msg); } else if (nerrors == MAXERRORS) { fprintf(stderr, "(more...)\n"); } nerrors++; }