Created
February 5, 2010 01:53
-
-
Save atr000/295398 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* A character filter with Unicode support | |
* Copyright (C) 2009 | |
* Andreas Harnack (ah8 at freenet dot de) | |
* This software is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* You should have received a copy of the GNU General Public License along | |
* with this library; see the file COPYING. If not, write to the Free | |
* Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
* USA. | |
* As a special exception, you may use this file as part of a free software | |
* library without restriction. Specifically, if other files instantiate | |
* templates or use macros or inline functions from this file, or you compile | |
* this file and link it with other files to produce an executable, this | |
* file does not by itself cause the resulting executable to be covered by | |
* the GNU General Public License. This exception does not however | |
* invalidate any other reasons why the executable file might be covered by | |
* the GNU General Public License. | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <locale.h> | |
#include <wchar.h> | |
extern char *charcode[256]; | |
extern char *lookup(wint_t ch); | |
int text2codes(char *charcode[], int warn, FILE *in, FILE *out) | |
{ | |
wint_t ch; | |
while ( (ch = getwc(in)) != WEOF ) { | |
char *code = (ch&~0xff) ? lookup(ch) : charcode[ch&0xff]; | |
if ( ! code ) | |
putc(ch, out); | |
else if ( *code ) | |
fputs(code, out); | |
else if ( warn ) | |
fprintf(stderr, "ignored charcode %02x\n", ch); | |
} | |
return ferror(in); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
int rc = 0, warn = 0; | |
char *prog; | |
for(prog = *(argv++); *argv!=NULL && **argv=='-'; argv++) { | |
if((*((*argv)+1))=='\0') | |
break; // stdin | |
else if(strcmp(*argv, "-i") == 0) | |
warn = 1; | |
else if(strcmp(*argv, "-br") == 0) | |
charcode['\n'] = 0; | |
} | |
if ( ! setlocale(LC_CTYPE, "") ) | |
fprintf(stderr, "locale not specified"); | |
if( *argv == NULL ) | |
rc = text2codes(charcode, warn, stdin, stdout); | |
else for(; rc == 0 && *argv != NULL; argv++) { | |
FILE *fp; | |
if ( strcmp(*argv, "-") == 0 ) | |
rc = text2codes(charcode, warn, stdin, stdout); | |
else if( (fp=fopen(*argv, "r")) != NULL ) { | |
rc = text2codes(charcode, warn, fp, stdout); | |
fclose(fp); | |
} | |
else { | |
fprintf(stderr, "%s: can't open %s\n", prog, *argv); | |
exit(2); | |
} | |
} | |
if ( rc ) | |
perror("ERROR"); | |
return rc; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment