Use an own 'UniChar' rather than 'wchar_t', which may not be large enough, depending on the platform.

Also split off UNICODE functionality from strlib.h into unicode.h.



git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@3375 8092fc87-c524-0410-9efc-e669fe64eaf9
This commit is contained in:
Meep-Eep
2009-11-30 20:09:57 +00:00
parent 5e4b5c3c5f
commit a6a7b843d0
12 changed files with 76 additions and 95 deletions
+4
View File
@@ -1568,6 +1568,10 @@ SOURCE=..\..\src\libs\uio.h
# End Source File
# Begin Source File
SOURCE=..\..\src\libs\unicode.h
# End Source File
# Begin Source File
SOURCE=..\..\src\libs\vidlib.h
# End Source File
# End Group
+7 -7
View File
@@ -21,7 +21,7 @@
#include "libs/log.h"
extern void FixContextFontEffect (void);
static inline TFB_Char *getCharFrame (FONT_DESC *fontPtr, wchar_t ch);
static inline TFB_Char *getCharFrame (FONT_DESC *fontPtr, UniChar ch);
FONT
@@ -119,7 +119,7 @@ TextRect (TEXT *lpText, RECT *pRect, BYTE *pdelta)
{
COORD top_y, bot_y;
SIZE width;
wchar_t next_ch;
UniChar next_ch;
const unsigned char *pStr;
COUNT num_chars;
@@ -150,7 +150,7 @@ TextRect (TEXT *lpText, RECT *pRect, BYTE *pdelta)
}
while (num_chars--)
{
wchar_t ch;
UniChar ch;
SIZE last_width;
TFB_Char *charFrame;
@@ -230,7 +230,7 @@ _text_blt (RECT *pClipRect, PRIMITIVE *PrimPtr)
FONT FontPtr;
COUNT num_chars;
wchar_t next_ch;
UniChar next_ch;
const unsigned char *pStr;
TEXT *TextPtr;
POINT origin;
@@ -257,7 +257,7 @@ _text_blt (RECT *pClipRect, PRIMITIVE *PrimPtr)
num_chars = 0;
while (num_chars--)
{
wchar_t ch;
UniChar ch;
TFB_Char* fontChar;
ch = next_ch;
@@ -295,9 +295,9 @@ _text_blt (RECT *pClipRect, PRIMITIVE *PrimPtr)
}
static inline TFB_Char *
getCharFrame (FONT_DESC *fontPtr, wchar_t ch)
getCharFrame (FONT_DESC *fontPtr, UniChar ch)
{
wchar_t pageStart = ch & CHARACTER_PAGE_MASK;
UniChar pageStart = ch & CHARACTER_PAGE_MASK;
size_t charIndex;
FONT_PAGE *page = fontPtr->fontPages;
+2 -2
View File
@@ -26,9 +26,9 @@
typedef struct FontPage
{
struct FontPage *next;
wchar_t pageStart;
UniChar pageStart;
#define CHARACTER_PAGE_MASK 0xff800
wchar_t firstChar;
UniChar firstChar;
size_t numChars;
TFB_Char *charDesc;
} FONT_PAGE;
+1 -1
View File
@@ -581,7 +581,7 @@ _ReleaseCelData (void *handle)
typedef struct BuildCharDesc {
SDL_Surface *surface;
wchar_t index;
UniChar index;
} BuildCharDesc;
int
+3 -2
View File
@@ -22,6 +22,7 @@
#include <stddef.h>
#include "libs/compiler.h"
#include "libs/uio.h"
#include "libs/unicode.h"
extern BOOLEAN AnyButtonPress (BOOLEAN DetectSpecial);
@@ -43,8 +44,8 @@ extern volatile int GameActive;
void EnterCharacterMode (void);
void ExitCharacterMode (void);
wchar_t GetNextCharacter (void);
wchar_t GetLastCharacter (void);
UniChar GetNextCharacter (void);
UniChar GetLastCharacter (void);
/* Interrogating the current key configuration */
+6 -6
View File
@@ -33,8 +33,8 @@
#define KBDBUFSIZE (1 << 8)
static int kbdhead=0, kbdtail=0;
static wchar_t kbdbuf[KBDBUFSIZE];
static wchar_t lastchar;
static UniChar kbdbuf[KBDBUFSIZE];
static UniChar lastchar;
static int num_keys = 0;
static int *kbdstate = NULL;
// Holds all SDL keys +1 for holding invalid values
@@ -291,10 +291,10 @@ ExitCharacterMode (void)
lastchar = 0;
}
wchar_t
UniChar
GetNextCharacter (void)
{
wchar_t result;
UniChar result;
if (kbdhead == kbdtail)
return 0;
result = kbdbuf[kbdhead];
@@ -302,7 +302,7 @@ GetNextCharacter (void)
return result;
}
wchar_t
UniChar
GetLastCharacter (void)
{
return lastchar;
@@ -353,7 +353,7 @@ ProcessInputEvent (const SDL_Event *Event)
if (Event->type == SDL_KEYDOWN || Event->type == SDL_KEYUP)
{ // process character input event, if any
SDLKey k = Event->key.keysym.sym;
wchar_t map_key = Event->key.keysym.unicode;
UniChar map_key = Event->key.keysym.unicode;
if (k < 0 || k > num_keys)
k = num_keys; // for unknown keys
+35 -33
View File
@@ -36,9 +36,9 @@ resyncUTF8(const unsigned char **ptr) {
// Returns 0 if the encoding is bad. This can be distinguished from the
// '\0' character by checking whether **ptr == '\0' before calling this
// function.
wchar_t
UniChar
getCharFromString(const unsigned char **ptr) {
wchar_t result;
UniChar result;
if (**ptr < 0x80) {
// 0xxxxxxx, regular ASCII
@@ -126,7 +126,7 @@ err:
return 0;
}
wchar_t
UniChar
getCharFromStringN(const unsigned char **ptr, const unsigned char *end) {
size_t numBytes;
@@ -167,7 +167,7 @@ getLineFromString(const unsigned char *start, const unsigned char **end,
const unsigned char **startNext) {
const unsigned char *ptr = start;
const unsigned char *lastPtr;
wchar_t ch;
UniChar ch;
// Search for the first newline.
for (;;) {
@@ -215,7 +215,7 @@ getLineFromString(const unsigned char *start, const unsigned char **end,
size_t
utf8StringCount(const unsigned char *start) {
size_t count = 0;
wchar_t ch;
UniChar ch;
for (;;) {
ch = getCharFromString(&start);
@@ -228,7 +228,7 @@ utf8StringCount(const unsigned char *start) {
size_t
utf8StringCountN(const unsigned char *start, const unsigned char *end) {
size_t count = 0;
wchar_t ch;
UniChar ch;
for (;;) {
ch = getCharFromStringN(&start, end);
@@ -238,11 +238,11 @@ utf8StringCountN(const unsigned char *start, const unsigned char *end) {
}
}
// Locates a wide char (ch) in a UTF-8 string (pStr)
// Locates a unicode character (ch) in a UTF-8 string (pStr)
// returns the char positions when found
// -1 when not found
int
utf8StringPos (const unsigned char *pStr, wchar_t ch)
utf8StringPos (const unsigned char *pStr, UniChar ch)
{
int pos;
@@ -278,9 +278,9 @@ int
utf8StringCompare (const unsigned char *str1, const unsigned char *str2)
{
#if 0
// wchar_t comparing version
wchar_t ch1;
wchar_t ch2;
// UniChar comparing version
UniChar ch1;
UniChar ch2;
for (;;)
{
@@ -321,7 +321,7 @@ utf8StringCompare (const unsigned char *str1, const unsigned char *str2)
unsigned char *
skipUTF8Chars(const unsigned char *ptr, size_t num) {
wchar_t ch;
UniChar ch;
const unsigned char *oldPtr;
while (num--) {
@@ -333,16 +333,16 @@ skipUTF8Chars(const unsigned char *ptr, size_t num) {
return (unsigned char *) ptr;
}
// Decodes a UTF-8 string (start) into a wide char string (wstr)
// Decodes a UTF-8 string (start) into a unicode character string (wstr)
// returns number of chars decoded and stored, not counting 0-term
// any chars that do not fit are truncated
// wide string term 0 is always appended, unless the destination
// buffer is 0 chars long
size_t
getWideFromStringN(wchar_t *wstr, size_t maxcount,
getUniCharFromStringN(UniChar *wstr, size_t maxcount,
const unsigned char *start, const unsigned char *end)
{
wchar_t *next;
UniChar *next;
if (maxcount == 0)
return 0;
@@ -366,9 +366,10 @@ getWideFromStringN(wchar_t *wstr, size_t maxcount,
// the only difference is that the source string (start) length is
// calculated by searching for 0-term
size_t
getWideFromString(wchar_t *wstr, size_t maxcount, const unsigned char *start)
getUniCharFromString(UniChar *wstr, size_t maxcount,
const unsigned char *start)
{
wchar_t *next;
UniChar *next;
if (maxcount == 0)
return 0;
@@ -394,12 +395,12 @@ getWideFromString(wchar_t *wstr, size_t maxcount, const unsigned char *start)
// <0 : negative of bytes needed if buffer too small
// string term '\0' is *not* appended or counted
int
getStringFromChar(unsigned char *ptr, size_t size, wchar_t ch)
getStringFromChar(unsigned char *ptr, size_t size, UniChar ch)
{
int i;
static const struct range_def
{
wchar_t lim;
UniChar lim;
int marker;
int mask;
}
@@ -420,8 +421,8 @@ getStringFromChar(unsigned char *ptr, size_t size, wchar_t ch)
;
if (def->mask == 0)
{ // invalid or unsupported char
log_add(log_Warning, "Warning: Invalid or unsupported wide char (%lu)",
(unsigned long)ch);
log_add(log_Warning, "Warning: Invalid or unsupported unicode "
"char (%lu)", (unsigned long) ch);
return 0;
}
@@ -454,7 +455,7 @@ getStringFromChar(unsigned char *ptr, size_t size, wchar_t ch)
// buffer is 0 bytes long
size_t
getStringFromWideN(unsigned char *ptr, size_t size,
const wchar_t *wstr, size_t count)
const UniChar *wstr, size_t count)
{
unsigned char *next;
int used;
@@ -487,9 +488,9 @@ getStringFromWideN(unsigned char *ptr, size_t size,
// the only difference is that the source string (wstr) length is
// calculated by searching for 0-term
size_t
getStringFromWide(unsigned char *ptr, size_t size, const wchar_t *wstr)
getStringFromWide(unsigned char *ptr, size_t size, const UniChar *wstr)
{
const wchar_t *end;
const UniChar *end;
for (end = wstr; *end != 0; ++end)
;
@@ -498,7 +499,7 @@ getStringFromWide(unsigned char *ptr, size_t size, const wchar_t *wstr)
}
int
isWideGraphChar(wchar_t ch)
UniChar_isGraph(UniChar ch)
{ // this is not technically sufficient, but close enough for us
// we'll consider all non-control (CO and C1) chars in 'graph' class
// except for the "Private Use Area" (0xE000 - 0xF8FF)
@@ -512,23 +513,24 @@ isWideGraphChar(wchar_t ch)
}
int
isWidePrintChar(wchar_t ch)
UniChar_isPrint(UniChar ch)
{ // this is not technically sufficient, but close enough for us
// chars in 'print' class are 'graph' + 'space' classes
// the only space we currently have defined is 0x20
return (ch == 0x20) || isWideGraphChar(ch);
return (ch == 0x20) || UniChar_isGraph(ch);
}
wchar_t
toWideUpper(wchar_t ch)
UniChar
UniChar_toUpper(UniChar ch)
{ // this is a very basic Latin-1 implementation
// just to get things going
return (ch < 0x100) ? toupper(ch) : ch;
return (ch < 0x100) ? (UniChar) toupper((int) ch) : ch;
}
wchar_t
toWideLower(wchar_t ch)
UniChar
UniChar_toLower(UniChar ch)
{ // this is a very basic Latin-1 implementation
// just to get things going
return (ch < 0x100) ? tolower(ch) : ch;
return (ch < 0x100) ? (UniChar) tolower((int) ch) : ch;
}
+6 -32
View File
@@ -22,6 +22,7 @@
#include "libs/compiler.h"
#include "port.h"
#include "libs/uio.h"
#include "libs/unicode.h"
#include <stddef.h>
@@ -57,42 +58,15 @@ extern STRINGPTR GetStringTimeStamp (STRING String);
extern BOOLEAN GetStringContents (STRING String, STRINGPTR StringBuf,
BOOLEAN AppendSpace);
wchar_t getCharFromString(const unsigned char **ptr);
wchar_t getCharFromStringN(const unsigned char **ptr,
const unsigned char *end);
unsigned char *getLineFromString(const unsigned char *start,
const unsigned char **end, const unsigned char **startNext);
size_t utf8StringCount(const unsigned char *start);
size_t utf8StringCountN(const unsigned char *start,
const unsigned char *end);
int utf8StringPos (const unsigned char *pStr, wchar_t ch);
unsigned char *utf8StringCopy (unsigned char *dst, size_t size,
const unsigned char *src);
int utf8StringCompare (const unsigned char *str1, const unsigned char *str2);
unsigned char *skipUTF8Chars(const unsigned char *ptr, size_t num);
size_t getWideFromString(wchar_t *wstr, size_t maxcount,
const unsigned char *start);
size_t getWideFromStringN(wchar_t *wstr, size_t maxcount,
const unsigned char *start, const unsigned char *end);
int getStringFromChar(unsigned char *ptr, size_t size, wchar_t ch);
size_t getStringFromWideN(unsigned char *ptr, size_t size,
const wchar_t *wstr, size_t count);
size_t getStringFromWide(unsigned char *ptr, size_t size,
const wchar_t *wstr);
int isWideGraphChar(wchar_t ch);
int isWidePrintChar(wchar_t ch);
wchar_t toWideUpper(wchar_t ch);
wchar_t toWideLower(wchar_t ch);
#define WCHAR_DEGREE_SIGN 0x00b0
#define UNICHAR_DEGREE_SIGN 0x00b0
#define STR_DEGREE_SIGN "\xC2\xB0"
#define WCHAR_INFINITY_SIGN 0x221e
#define UNICHAR_INFINITY_SIGN 0x221e
#define STR_INFINITY_SIGN "\xE2\x88\x9E"
#define WCHAR_EARTH_SIGN 0x2641
#define UNICHAR_EARTH_SIGN 0x2641
#define STR_EARTH_SIGN "\xE2\x99\x81"
#define WCHAR_MIDDLE_DOT 0x00b7
#define UNICHAR_MIDDLE_DOT 0x00b7
#define STR_MIDDLE_DOT "\xC2\xB7"
#define WCHAR_BULLET 0x2022
#define UNICHAR_BULLET 0x2022
#define STR_BULLET "\xE2\x80\xA2"
#endif /* _STRLIB_H */
+1 -1
View File
@@ -323,7 +323,7 @@ getLineWithinWidth(TEXT *pText, const unsigned char **startNext,
COUNT oldCount;
const unsigned char *ptr;
const unsigned char *wordStart;
wchar_t ch;
UniChar ch;
COUNT charCount;
//GetContextClipRect (&rect);
+2 -2
View File
@@ -123,7 +123,7 @@ JoyCharToLower (joy_char_t *outch, const joy_char_t *ch,
BOOLEAN
DoTextEntry (TEXTENTRY_STATE *pTES)
{
wchar_t ch;
UniChar ch;
UNICODE *pStr;
UNICODE *CacheInsPt;
int CacheCursorPos;
@@ -209,7 +209,7 @@ DoTextEntry (TEXTENTRY_STATE *pTES)
pTES->JoystickMode = FALSE;
chsize = getStringFromChar (chbuf, sizeof (chbuf), ch);
if (isWidePrintChar (ch) && chsize > 0)
if (UniChar_isPrint (ch) && chsize > 0)
{
if (pStr + len - pTES->BaseStr + chsize < pTES->MaxSize)
{ // insert character, when fits
+6 -6
View File
@@ -968,10 +968,10 @@ FindNextStarIndex (STAR_SEARCH_STATE *pSS, int from, BOOLEAN WithinClust)
for (c = 0, sptr = pSS->Cluster, dptr = ClusterName;
c < pSS->ClusterLen; ++c)
{
wchar_t sc = getCharFromString (&sptr);
wchar_t dc = getCharFromString (&dptr);
UniChar sc = getCharFromString (&sptr);
UniChar dc = getCharFromString (&dptr);
if (toWideUpper (sc) != toWideUpper (dc))
if (UniChar_toUpper (sc) != UniChar_toUpper (dc))
break;
}
@@ -1010,10 +1010,10 @@ FindNextStarIndex (STAR_SEARCH_STATE *pSS, int from, BOOLEAN WithinClust)
for (c = 0, sptr = pSS->Prefix, dptr = FullName;
c < pSS->PrefixLen; ++c)
{
wchar_t sc = getCharFromString (&sptr);
wchar_t dc = getCharFromString (&dptr);
UniChar sc = getCharFromString (&sptr);
UniChar dc = getCharFromString (&dptr);
if (toWideUpper (sc) != toWideUpper (dc))
if (UniChar_toUpper (sc) != UniChar_toUpper (dc))
break;
}
+3 -3
View File
@@ -89,7 +89,7 @@ MakeReport (SOUND ReadOutSounds, UNICODE *pStr, COUNT StrLen)
BYTE ButtonState;
int end_page_len;
UNICODE end_page_buf[200];
wchar_t last_c;
UniChar last_c;
COUNT row_cells;
BOOLEAN Sleepy;
RECT r;
@@ -142,11 +142,11 @@ MakeReport (SOUND ReadOutSounds, UNICODE *pStr, COUNT StrLen)
{
COUNT word_chars;
const UNICODE *pStr;
wchar_t c;
UniChar c;
pStr = t.pStr;
pNextStr = t.pStr;
while (isWideGraphChar (getCharFromString (&pNextStr)))
while (UniChar_isGraph (getCharFromString (&pNextStr)))
pStr = pNextStr;
word_chars = utf8StringCountN (t.pStr, pStr);