|
From: <set...@us...> - 2006-04-24 01:03:43
|
Revision: 1327 Author: sethdill Date: 2006-04-23 18:03:36 -0700 (Sun, 23 Apr 2006) ViewCVS: http://svn.sourceforge.net/frontierkernel/?rev=1327&view=rev Log Message: ----------- First half of changes to re-work string.convertCharset(), so that it now takes internet naems (IANA names) instead of platform-specific codes. - also, added new verb string.ischarsetavailable() to make life a little easier for the scripter. Takes one argument, the internet-friendly name of the character set whose existence is being tested (like 'utf-8'). Modified Paths: -------------- Frontier/trunk/Common/headers/strings.h Frontier/trunk/Common/resources/Mac/kernelverbs.r Frontier/trunk/Common/source/strings.c Frontier/trunk/Common/source/stringverbs.c Modified: Frontier/trunk/Common/headers/strings.h =================================================================== --- Frontier/trunk/Common/headers/strings.h 2006-04-22 21:06:49 UTC (rev 1326) +++ Frontier/trunk/Common/headers/strings.h 2006-04-24 01:03:36 UTC (rev 1327) @@ -266,9 +266,9 @@ extern boolean utf8tomacroman (Handle, Handle); /* 2006-02-25 creedon */ -extern boolean gettextencodingid (const Handle, long *); /* 2006-04-20 smd */ +extern boolean convertCharset( Handle, Handle, const bigstring, const bigstring ); -extern boolean converttextencoding (Handle, Handle, long, long); /* 2006-04-14 smd */ +extern boolean isTextEncodingAvailable( const bigstring ); /* 2006-04-23 smd */ #endif Modified: Frontier/trunk/Common/resources/Mac/kernelverbs.r =================================================================== --- Frontier/trunk/Common/resources/Mac/kernelverbs.r 2006-04-22 21:06:49 UTC (rev 1326) +++ Frontier/trunk/Common/resources/Mac/kernelverbs.r 2006-04-24 01:03:36 UTC (rev 1327) @@ -601,7 +601,8 @@ "multipleReplaceAll", "macromantoutf8", // 2006-02-25 creedon "utf8tomacroman", // 2006-02-25 creedon - "convertcharset" /* 2006-04-13 smd */ + "convertcharset", /* 2006-04-13 smd */ + "ischarsetavailable" /* 2006-04-23 smd */ } } }; Modified: Frontier/trunk/Common/source/strings.c =================================================================== --- Frontier/trunk/Common/source/strings.c 2006-04-22 21:06:49 UTC (rev 1326) +++ Frontier/trunk/Common/source/strings.c 2006-04-24 01:03:36 UTC (rev 1327) @@ -56,8 +56,19 @@ static byte bshexprefix [] = STR_hexprefix; +/* declarations */ +static boolean converttextencoding( Handle, Handle, const long, const long ); +static boolean getTextEncodingID( const bigstring, long * ); +#ifdef WIN95VERSION + static HRESULT UnicodeToAnsi(Handle, Handle, unsigned long); + static HRESULT AnsiToUnicode(Handle, Handle, unsigned long); +#endif + + +/* definitions */ + boolean equalstrings (const bigstring bs1, const bigstring bs2) { /* @@ -2341,21 +2352,15 @@ /* convert an "internet name" for an encoding into a platform-specific id for the character set */ -boolean gettextencodingid (const Handle h, long * encodingId) { +static boolean getTextEncodingID( const bigstring bsEncodingName, long * encodingId ) { #ifdef MACVERSION OSStatus err; TextEncoding oneEncoding; - bigstring ianaName; - long ct, ix = 0; - ct = gethandlesize (h); - ianaName [0] = (byte) ct; - loadfromhandle (h, &ix, ct, ianaName + 1); + err = TECGetTextEncodingFromInternetName( &oneEncoding, bsEncodingName ); - err = TECGetTextEncodingFromInternetName( &oneEncoding, ianaName ); - if ( err != noErr ) return (false); @@ -2366,8 +2371,27 @@ return (true); } +/* + determine if the specified character set is recognized by the OS + bsEncodingName is an IANA-friendly name like "utf-8" , "macintosh", or "iso-8859-1" +*/ +boolean isTextEncodingAvailable( const bigstring bsEncodingName ) +{ + long encodingId; + boolean fl; + + fl = getTextEncodingID( bsEncodingName, &encodingId ); + + if ( ! fl ) + return (false); + + if ( encodingId < 0 ) + return (false); + + return (true); +} -boolean converttextencoding (Handle h, Handle hresult, long inputcharset, long outputcharset) { +static boolean converttextencoding( Handle h, Handle hresult, const long inputcharset, const long outputcharset) { #ifdef MACVERSION /*Mac character conversion common routine*/ @@ -2378,10 +2402,6 @@ long pullBytes = 0; long lentext = gethandlesize (h); - // sethandlesize (h, lentext + 1); - - // (*h) [lentext] = '\0'; - status = TECCreateConverter (&converter, inputcharset, outputcharset); if ( status != noErr ) { TECDisposeConverter (converter); @@ -2419,15 +2439,6 @@ } } - //old code - // if (((*h) [0] == '\xFF') || ((*h) [0] == '\xEF')) /*pop byte order mark*/ - // pullfromhandle (h, 0, 3, NULL); - - /* - if (pullBytes > 0) - pullfromhandle (h, 0, pullBytes, NULL); - */ - lentext = gethandlesize (h) - pullBytes; status = TECConvertText (converter, (ConstTextPtr)(*h + pullBytes), lentext, &ctorigbytes, (TextPtr)(*hresult), sizeoutputbuffer, &ctoutputbytes); @@ -2445,10 +2456,6 @@ return (true); - // kwchange 2005-05-23 --- commented out to see if this is the missing trailing char bug - // kwchange 2005-05-27 --- ran this the last week and have no complains - //sethandlesize (hresult, gethandlesize (hresult) - 1); /*pop trailing terminator*/ - #endif /*end Mac character conversion routine(s)*/ #ifdef WIN95VERSION @@ -2517,6 +2524,34 @@ } /*converttextencoding*/ +boolean convertCharset( Handle hString, Handle hresult, const bigstring charsetIn, const bigstring charsetOut ) +{ +#ifdef MACVERSION + + TextEncoding teInputSet, teOutputSet; + + if ( ! getTextEncodingID( charsetIn, (long *) &teInputSet ) ) + { + // FIXME: generate error, encoding not available + return (false); + } + + if ( ! getTextEncodingID( charsetOut, (long *) &teOutputSet ) ) + { + // FIXME: generate error, encoding not available + return (false); + } + + return converttextencoding( hString, hresult, (long) teInputSet, (long) teOutputSet ); + +#endif + +#ifdef WIN95VERSION + +#endif +} + + boolean utf16toansi (Handle h, Handle hresult) { /* Modified: Frontier/trunk/Common/source/stringverbs.c =================================================================== --- Frontier/trunk/Common/source/stringverbs.c 2006-04-22 21:06:49 UTC (rev 1326) +++ Frontier/trunk/Common/source/stringverbs.c 2006-04-24 01:03:36 UTC (rev 1327) @@ -312,6 +312,8 @@ utf8tomacromanfunc, // 2006-02-25 creedon convertcharsetfunc, /* 2006-04-14 smd */ + + ischarsetavailablefunc, /* 2006-04-23 smd */ ctstringverbs } tystringtoken; @@ -1319,6 +1321,21 @@ } /*latintomac*/ +static boolean isCharsetAvailableVerb( hdltreenode hp1, tyvaluerecord *v ) +{ + bigstring bscharset; + + flnextparamislast = true; + + if ( !getstringvalue( hp1, 1, bscharset ) ) /* IANA names can't be longer than 40 ASCII characters, so this is safe */ + return (false); + + (*v).data.flvalue = isTextEncodingAvailable( bscharset ); + + return (true); +} + + static boolean stringfunctionvalue (short token, hdltreenode hparam1, tyvaluerecord *vreturned, bigstring bserror) { /* @@ -2302,32 +2319,39 @@ return (setheapvalue (hresult, stringvaluetype, v)); } - case convertcharsetfunc: { /* 2006-04-13 smd: convert any character set to any other character set, if the os supports it */ - + case convertcharsetfunc: /* 2006-04-13 smd: convert any character set to any other character set, if the os supports it */ + { Handle h, hresult; - long charsetIn, charsetOut; + bigstring charsetIn, charsetOut; - if (!getlongvalue (hp1, 1, &charsetIn)) + /* + the "internet names" of character sets are 40 ASCII chars or less, + so this is a safe way to get the params + */ + if ( !getstringvalue( hp1, 1, charsetIn ) ) goto error; - if (!getlongvalue (hp1, 2, &charsetOut)) + if ( !getstringvalue( hp1, 2, charsetOut ) ) goto error; flnextparamislast = true; - if (!getreadonlytextvalue (hp1, 3, &h)) + if ( !getreadonlytextvalue( hp1, 3, &h ) ) goto error; - newemptyhandle (&hresult); + newemptyhandle( &hresult ); - if (! converttextencoding (h, hresult, charsetIn, charsetOut)) + if ( !convertCharset( h, hresult, charsetIn, charsetOut ) ) goto error; - // disposehandle(h); - - return (setheapvalue (hresult, stringvaluetype, v)); - } + return ( setheapvalue( hresult, stringvaluetype, v ) ); + } + case ischarsetavailablefunc: /* 2006-04-23 smd: determine if a character set is available, based on an internet-friendly (IANA) name */ + { + return isCharsetAvailableVerb( hparam1, v ); + } + default: errornum = notimplementederror; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |