-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
553 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#ifndef UC_H_ASCX | ||
#define UC_H_ASCX | ||
|
||
|
||
#include <limits.h> | ||
#include <stddef.h> | ||
|
||
|
||
#ifndef UC_WINT_IMPL | ||
#define UC_WINT_IMPL | ||
|
||
typedef signed long int UC_wint_t; | ||
|
||
#define UC_WINT_MAX LONG_MAX | ||
#define UC_WINT_MIN LONG_MIN | ||
|
||
#define UC_WEOF (-1) | ||
|
||
#endif /* UC_WINT_IMPL */ | ||
|
||
|
||
typedef enum UC_trans | ||
{ | ||
UC_TRANS_437_C0, /* w/ C0 control characters. */ | ||
UC_TRANS_437_C0_REP, /* w/ dingbats instead of C0 control characters. */ | ||
|
||
UC_TRANS_850_C0, /* w/ C0 control characters. */ | ||
UC_TRANS_850_C0_REP, /* w/ dingbats instead of C0 control characters. */ | ||
|
||
UC_TRANS_858_C0, /* w/ C0 control characters. */ | ||
UC_TRANS_858_C0_REP, /* w/ dingbats instead of C0 control characters. */ | ||
|
||
UC_TRANS_1252, | ||
|
||
UC_TRANS_8859_1, /* w/ C0 and C1 control characters. */ | ||
|
||
|
||
UC_TRANS_COUNT | ||
} | ||
UC_trans_t; | ||
|
||
|
||
extern const wchar_t UC_TRANSTABLE_437_UPPER [128]; | ||
extern const wchar_t UC_TRANSTABLE_850_UPPER [128]; | ||
extern const wchar_t UC_TRANSTABLE_IBM_C0_REP [33]; | ||
extern const wchar_t UC_TRANSTABLE_1252_UPPER [32]; | ||
extern const wchar_t UC_EURO; | ||
|
||
|
||
UC_wint_t UC_ascxtowc(int c, UC_trans_t t); | ||
|
||
size_t | ||
UC_ascxstowcs(wchar_t* dest, const unsigned char* src, size_t n, UC_trans_t t); | ||
|
||
|
||
#endif /* UC_H_ASCX */ |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
#include "ascx.h" | ||
|
||
#include <stddef.h> | ||
#include <string.h> | ||
|
||
|
||
#define UC_tablecopy(dest, src, n) memcpy(dest, src, (n) * sizeof(wchar_t)) | ||
|
||
|
||
UC_wint_t UC_ascxtowc(int c, UC_trans_t t) | ||
{ | ||
unsigned char bc; | ||
wchar_t wc; | ||
size_t r; | ||
|
||
bc = c; | ||
r = UC_ascxstowcs(&wc, &bc, 1, t); | ||
|
||
return r != (size_t)(-1) ? wc : UC_WEOF; | ||
} | ||
|
||
size_t | ||
UC_ascxstowcs(wchar_t* dest, const unsigned char* src, size_t n, UC_trans_t t) | ||
{ | ||
size_t i; | ||
wchar_t table [256]; | ||
|
||
if (!dest) | ||
{ | ||
/* no destination specified. | ||
simply return the length of the extended-ASCII string. */ | ||
return strlen(src); | ||
} | ||
|
||
if (t >= UC_TRANS_COUNT) | ||
{ | ||
/* invalid transformation type. */ | ||
return -1; | ||
} | ||
|
||
/* set up the transformation table: */ | ||
|
||
/* 1. start with an identity transform. */ | ||
|
||
for (i = 0; i < 256; i++) table[i] = i; | ||
|
||
/* 2. fill the C0 control codepoints with IBM dingbats if necessary. */ | ||
|
||
switch (t) | ||
{ | ||
case UC_TRANS_437_C0_REP: | ||
case UC_TRANS_850_C0_REP: | ||
case UC_TRANS_858_C0_REP: | ||
UC_tablecopy(&table[0x00], &UC_TRANSTABLE_IBM_C0_REP[0], 32); | ||
UC_tablecopy(&table[0x7F], &UC_TRANSTABLE_IBM_C0_REP[32], 1); | ||
break; | ||
} | ||
|
||
/* 3. fill the upper half of the codepage | ||
(ignoring the small differences). */ | ||
|
||
switch (t) | ||
{ | ||
case UC_TRANS_437_C0: | ||
case UC_TRANS_437_C0_REP: | ||
UC_tablecopy(&table[0x80], &UC_TRANSTABLE_437_UPPER[0], 128); | ||
break; | ||
|
||
case UC_TRANS_850_C0: | ||
case UC_TRANS_850_C0_REP: | ||
case UC_TRANS_858_C0: | ||
case UC_TRANS_858_C0_REP: | ||
UC_tablecopy(&table[0x80], &UC_TRANSTABLE_850_UPPER[0], 128); | ||
break; | ||
|
||
case UC_TRANS_1252: | ||
UC_tablecopy(&table[0x80], &UC_TRANSTABLE_1252_UPPER[0], 32); | ||
break; | ||
} | ||
|
||
/* 4. add the small differences (e.g. euro sign) */ | ||
|
||
switch (t) | ||
{ | ||
case UC_TRANS_858_C0: | ||
case UC_TRANS_858_C0_REP: | ||
UC_tablecopy(&table[0xD5], &UC_EURO, 1); | ||
break; | ||
} | ||
|
||
/* and we're done. | ||
ISO-8859-1 is a simple identity transform, | ||
so it skips steps 2-4. */ | ||
|
||
/* do the conversion. */ | ||
|
||
for (i = 0; i < n; i++) | ||
{ | ||
if (src[i]) | ||
{ | ||
/* regular character. */ | ||
dest[i] = table[src[i]]; | ||
} | ||
else | ||
{ | ||
/* null character (end of string). */ | ||
dest[i] = L'\0'; | ||
return i; | ||
} | ||
} | ||
|
||
return i; | ||
} |
Oops, something went wrong.