Big-5 Fundamental Functions (Big5.c)

/* Define some fundamental I/O functions for Big5 code */

#include "Big5.h"

/* Very naive version of getting a Big5 code from a FILE.
   Once a character is in the first Big5 byte range, this function
   assumes a Big5 code is present.  If your input file is definitely
   save, ie, it is definitely a plain ASCII + Big5 text file, use this
   version.

   See LOG file for change logs.
*/

Big5 fgetc5(FILE *fp) {
    Big5 code=0;
    int c;
    c = fgetc(fp);
    if (isfirst(c)) code = (c << 8) + fgetc(fp);
    else if (c == EOF)
	code = EOF;
    else
	code = c;
    return code;
}

/* Same function as fgetc5(), but not as naive.  This function checks
   both bytes, if they are both in the range of Big5 code range,
   then take as a Big5 character.  Otherwise treat them as two
   Latin-1 characters.
*/

Big5 fgetbig5(FILE *fp) {
    Big5 code=0;
    int c, d;
    c = fgetc(fp);
    if (isfirst(c)) {
        d = fgetc(fp);
	if (issecond(d))
	    code = (c << 8) + d;
	else {
	    code = c;
	    ungetc(d, fp);
	}
    }
    else if (c == EOF)
	code = EOF;
    else
	code = c;
    return code;
}
/* Put a Big5 code out to a FILE.  If it fails to write, return EOF.
   Otherwise it returns the width of the character: 1 for ASCII and
   2 for Big5.
*/

int fputc5(Big5 c, FILE *fp) {
    char x = ((c & 0xFF00) >> 8);
    if (x) {
	fputc(x, fp);
	return ((fputc((c & 0x00FF), fp) == EOF)? EOF : 2);
    }
    else
	return ((fputc((c & 0x00FF), fp) == EOF)? EOF : 1);
}

/* About the pronunciation and tone symbols */

int isphon(Big5 c) {
    int n;
    if (getfirst(c) == 0xA3) {
	if ((n=getsecond(c)) > 0x73) {
	    ((n < 0x7F) ? (n -= 0x74) : (n = n - 0xA0 + 10));
	    if (n < 37) n += 6;
	    else if (n < 42) n -= 36;
	    else n = 0;
	}
    }
    else n = 0;
    return n;
}

Big5 phon(int n) {
    Big5 c;
    if ((n < 43) && (n > 0)) {
	c = (0xA3)<<8;
	if (n < 6) c += (n+0xBA);
	else if (n < 17) c += (n+110);
	else c += (n+144);
    }
    else c = ((0xA1)<<8) | 0xBC;
    return c;
}

/* About the linear order of Big-5 codes */

int linear(Big5 c) {
    int x, y, n;
    x = getfirst(c);
    y = getsecond(c);
    if ((x >= 164) && (x <= 198)) {
        y -= ( (y > 126) ? (161 - 63) : 64 );
        n = (x - 164) * 157 + y;
    }
    else if ((x >= 201) && (x <= 249)) {
        y -= ( (y > 126) ? (161 - 63) : 64 );
        n = (x - 201) * 157 + y + 5401;
    }
    else n = -1;
    return n;
}

Big5 b5(int n) {
    int x, y;
    if ((n>=0) && (n<5401)) {
	x = n/157 + 164;
	n %= 157;
	y = (n < 63) ? (n + 64) : (n - 63 + 161);
    }
    else if (n<13060) {
	n -= 5401;
	x = n/157 + 201;
	n %= 157;
	y = (n < 63) ? (n + 64) : (n - 63 + 161);
    }
    else {
	x = 0xA1;
	y = 0xBC;
    }
    return (x<<8)+y;
}

Created: Nov 17, 1996
Last Revised: Nov 20, 1996
© Copyright 1996 Wei-Chang Shann

shann@math.ncu.edu.tw