Skip to content

Instantly share code, notes, and snippets.

@tsupo
Created May 7, 2009 20:40
Show Gist options
  • Save tsupo/108349 to your computer and use it in GitHub Desktop.
Save tsupo/108349 to your computer and use it in GitHub Desktop.
Kanji Preprocessor for C compiler
/*
* KANJI preprocessor for C compiler not supported WIDE CHAR
* written by H.Tsujimura 12 Jan 1999
* modified 20 Dec 1999
*
* kpp.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef MAXPATHLEN
#define MAXPATHLEN 256
#endif
#define iskanji1( c ) \
((((unsigned char)(c) >= 0x81) && \
((unsigned char)(c) <= 0x9F) ) || \
(((unsigned char)(c) >= 0xE0) && \
((unsigned char)(c) <= 0xFE) ) )
typedef enum { FALSE = 0, TRUE = 1 } BOOL;
void
kpp( char *fname, FILE *fp )
{
FILE *gp;
char output[MAXPATHLEN];
char buf[BUFSIZ], tmp[BUFSIZ];
char *p;
BOOL inComment = FALSE;
BOOL inQuote = FALSE;
strcpy( output, fname );
if ( ( p = strrchr( output, '.' ) ) != NULL ) {
strcpy( p + 1, "kpp" );
}
else {
strcat( output, ".kpp" );
}
if ( ( gp = fopen( output, "w" ) ) != NULL ) {
while ( ( p = fgets( buf, BUFSIZ - 1, fp ) ) != NULL ) {
redo:
while ( (*p == ' ') || (*p == '\t') )
p++;
if ( !(*p) || (*p == '\n') || ((*p == '/') && (*(p + 1) == '/')) ) {
fputs( buf, gp );
continue;
}
if ( inComment == TRUE ) {
while ( *p ) {
if ( (*p == '*') && (*(p + 1) == '/') ) {
p += 2;
inComment = FALSE;
break;
}
p++;
}
if ( inComment == FALSE )
goto redo;
while ( (*p == ' ') || (*p == '\t') )
p++;
if ( !(*p) || (*p == '\n') ) {
fputs( buf, gp );
continue;
}
}
else if ( inQuote == TRUE ) {
while ( *p ) {
if ( iskanji1(*p) ) {
p++;
if ( *p == '\\' ) {
strcpy( tmp, p + 1 );
*++p = '\\';
strcpy( p + 1, tmp );
}
}
else if ( *p == '\\' )
p++;
else if ( *p == '\"' ) {
p++;
inQuote = FALSE;
break;
}
p++;
}
while ( (*p == ' ') || (*p == '\t') )
p++;
if ( !(*p) || (*p == '\n') || ((*p == '/') && (*(p + 1) == '/')) ) {
fputs( buf, gp );
continue;
}
}
while ( *p ) {
if ( (*p == '/') && ( *(p + 1) == '*' ) ) {
inComment = TRUE;
p += 2;
goto redo;
}
if ( iskanji1(*p) ) {
p++;
if ( *p == '\\' ) {
strcpy( tmp, p + 1 );
*++p = '\\';
strcpy( p + 1, tmp );
}
}
else if ( *p == '\'' ) {
if ( *(p + 1) == '\\' )
p++;
p++;
}
else if ( *p == '\"' ) {
p++;
inQuote = TRUE;
goto redo;
}
p++;
}
fputs( buf, gp );
}
fclose( gp );
}
}
void
main( int argc, char *argv[] )
{
int i;
FILE *fp;
char fileName[MAXPATHLEN];
char buf[BUFSIZ], *p;
if ( argc <= 1 ) {
fputs( "output filename ? ", stdout );
do {
fileName[0] = '\0';
p = fgets( buf, BUFSIZ - 1, stdin );
if ( !p ) {
clearerr( stdin );
continue;
}
while ( (*p == ' ') || (*p == '\t') )
p++;
if ( *p && (*p != '\n') )
strcpy( fileName, p );
} while ( fileName[0] == '\0' );
kpp( fileName, stdin );
}
else {
for ( i = 1; i < argc; i++ ) {
if ( ( fp = fopen( argv[i], "r" ) ) != NULL ) {
kpp( argv[i], fp );
fclose( fp );
}
}
}
exit( 0 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment