ftp.nice.ch/pub/next/connectivity/news/NewsBase.3.02.s.tar.gz#/NewsBase302.source/NNTP/kanjiconv.c

This is kanjiconv.c in view mode; [Download] [Up]

/* kconv.c -  Japanese code converter
   between two of EUC, JIS, and Shift-JIS code.
   written by K.Handa  89.5.25 */
/*
 * modified by K.Miyai, ISR 1991.Dec.24 
 */
#include <streams/streams.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>

int	i1, i2;
int	c1, c2;

int   kanjiconv();
int  j2e_conv();
int  e2j_conv();
int  e2j_conv_adj();
int  e2e_adj();

#define ESC_CODE 033
#define TOK1 '$'
#define TOK2 'B'
#define TOA1 '('
#define TOA2 'J'

#define		MAXLEN_LINE	500
/* 507 is supposed to be a maximum for nntp, but doesn't work */

/* #define TEST */
#ifdef TEST
int
main(int argc, char **argv)
{
    char 	conv_func[32];
    int         conv_flag;
    NXStream 	*in, *out;
    int		stdInFileDesc, stdOutFileDesc;

    /* input stream */
    if (argc ==1 || argc > 3) {
        fprintf(stderr, "Usage: kanjiconv -j/-e/-c/-a [filename]\n");
    } else if (argc == 2) {
	stdInFileDesc = fileno(stdin);
	in = NXOpenFile(stdInFileDesc, NX_READONLY);
    } else if ((in = NXMapFile((char *)*(argv+2), NX_READONLY)) == NULL) {
	fprintf(stderr,"can not open file %s\n", *(argv+2));
	return (-1);
    }
    /* output stream */
    stdOutFileDesc = fileno(stdout);
    out = NXOpenFile(stdOutFileDesc, NX_WRITEONLY);

    /* call kanji converter */
    ++argv;
    if ((char)**argv != '-') {
      fprintf(stderr," invalid option\n");
    }
    switch ((char)*(*argv+1)) {
    case 'e':
      kanjiconv("jis_to_euc", in, out);
    case 'j':
      kanjiconv("euc_to_jis", in, out);
    case 'c':
      kanjiconv("euc_to_jis_adjustline", in, out);
    default:
      kanjiconv("euc_to_euc_adjustline", in, out);
    }

    NXClose(in); NXClose(out);
    return(0);
}
#endif TEST /*TEST*/

int
kanjiconv (char *conv_option, NXStream *fin, NXStream *fout)
{
    if (strcmp(conv_option,"jis_to_euc") == 0)
	j2e_conv(fin, fout);
    else if (strcmp(conv_option,"euc_to_jis") == 0)
	e2j_conv(fin, fout);
    else if (strcmp(conv_option,"euc_to_jis_adjustline") == 0)
	e2j_conv_adj(fin, fout);
    else if (strcmp(conv_option,"euc_to_euc_adjustline") == 0)
	e2e_adj(fin, fout);
    else
	return (-1);
    
    return 0;
}

int
j2e_conv (NXStream *fin, NXStream *fout)
{
    int kanji = 0;

    while ((char)(i1 = NXGetc(fin)) != EOF) {
	if (kanji) {
	    if (i1 == ESC_CODE) {
		if ((i1 = NXGetc(fin)) == TOA1) {
		    NXGetc(fin);
		    kanji = 0;
		} else {
		    NXPutc(fout,ESC_CODE);
		    NXUngetc(fin);
		}
	    } else if (0x21 <= i1 && i1 <= 0x7e) {
		c1 = i1 | 0x80;
		c2 = NXGetc(fin) | 0x80;
		NXPutc(fout, c1); NXPutc(fout, c2);
	    } else
		NXPutc(fout, i1);
	} else {
	    if (i1 == ESC_CODE) {
		if ((i1 = NXGetc(fin)) == TOK1) {
		    NXGetc(fin);
		    kanji = 1;
		} else {
		    NXPutc(fout, ESC_CODE);
		    NXUngetc(fin);
		}
	    } else
		NXPutc(fout, i1);
	}
    }
    return (0);
}
	
int
e2j_conv(NXStream *fin, NXStream *fout)
{
    int kanji = 0;

    while ((char)(i1 = NXGetc(fin)) != EOF) {
	if (kanji) {
	    if (i1 & 0x80) {
		c1 = i1 & 0x7f; c2 = NXGetc(fin) & 0x7f;
		NXPutc(fout,c1); NXPutc(fout,c2);
	    } else {
		NXPutc(fout,ESC_CODE); NXPutc(fout,TOA1); NXPutc(fout,TOA2);
		NXPutc(fout,i1);
		kanji = 0;
	    }
	} else {
	    if ((i2=NXGetc(fin)) == EOF) {
		NXPutc(fout, i1);
		break;
	     } else if ((i1 & 0x80) && (i2 & 0x80)) {
		NXPutc(fout,ESC_CODE); NXPutc(fout,TOK1); NXPutc(fout,TOK2);
		c1 = i1 & 0x7f; c2 = i2 & 0x7f;
		NXPutc(fout,c1); NXPutc(fout,c2);	  
		kanji = 1;
	    } else {
		NXUngetc(fin);
		NXPutc(fout,i1);
	    }
	}
    }
    return (0);
}


int
e2j_conv_adj(NXStream *fin, NXStream *fout)
{
    int 		kanji = 0;
    unsigned int	length = 0;
    int		adj_flag = 0;
    
    while ((char)(i1 = NXGetc(fin)) != EOF) {
	if (i1 == '\n') {
	    /* reset counting */
	    length = 0;
	}
	if (length > MAXLEN_LINE) {
	    /* now i1 is next to MAXLEN_LINE */
	    if (kanji) {
		NXUngetc(fin);
		/* end this line*/
		NXPutc(fout,ESC_CODE); NXPutc(fout,TOA1); NXPutc(fout,TOA2);
		NXPutc(fout,'\r'); NXPutc(fout,'\n');
		/* start next line*/
		NXPutc(fout,ESC_CODE); NXPutc(fout,TOK1); NXPutc(fout,TOK2);
		length = 0;
	    } else {
		NXUngetc(fin);
		NXPutc(fout,'\r'); NXPutc(fout,'\n');
		length = 0;
	    }
	    adj_flag = 1;
	    continue;
	}
		
	if (kanji) {
	    if (i1 & 0x80) {
		c1 = i1 & 0x7f; c2 = NXGetc(fin) & 0x7f;
		NXPutc(fout,c1); NXPutc(fout,c2);
		length += 2;
	    } else {
		NXPutc(fout,ESC_CODE); NXPutc(fout,TOA1); NXPutc(fout,TOA2);
		NXPutc(fout,i1);
		kanji = 0;
		length += 4;
	    }
	} else {
	    if ((i2=NXGetc(fin)) == EOF) {
		NXPutc(fout, i1);
		length += 1;
		break;
	     } else if ((i1 & 0x80) && (i2 & 0x80)) {
		NXPutc(fout,ESC_CODE); NXPutc(fout,TOK1); NXPutc(fout,TOK2);
		c1 = i1 & 0x7f; c2 = i2 & 0x7f;
		NXPutc(fout,c1); NXPutc(fout,c2);	  
		kanji = 1;
		length += 5;
	    } else {
		NXUngetc(fin);
		NXPutc(fout,i1);
		length += 1;
	    }
	}
    }
    return adj_flag;
}

int
e2e_adj(NXStream *fin, NXStream *fout)
{
    unsigned int	length = 0;
    int		adj_flag = 0;

    while ((char)(i1 = NXGetc(fin)) != EOF) {
	if (i1 == '\n') {
	    /* reset counting */
	    length = 0;
	}
	if (length > MAXLEN_LINE) {
	    /* now i1 is next to MAXLEN_LINE */
	    NXUngetc(fin);
	    /* end this line*/
	    NXPutc(fout,'\r'); NXPutc(fout,'\n');
	    length = 0;
	    adj_flag = 1;
	    continue;
	}
	if (i1 & 0x80) {
	    if ((i2=NXGetc(fin)) && 0x80) {
		/* i1:i2 is kanji, so put out the pair */
		NXPutc(fout,i1); NXPutc(fout,i2);
	    } else {
		NXUngetc(i2);
		NXPutc(fout,i1);
	    }
	} else {
	    NXPutc(fout,i1);
	}
    }
    return (adj_flag);
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.