home *** CD-ROM | disk | FTP | other *** search
- /*==================================================================
- File: Win32ZString.h
-
- Contains: Windows-specific parsing for ZStrings.
-
- Written by: Nalini Prakash
-
- Copyright: 2000-2001 Connectix Corporation
-
- This source has been placed into the public domain by
- Connectix Corporation. You have the right to modify,
- distribute or use this code without any legal limitations
- or finanicial/licensing requirements. Connectix is not
- liable for any problems that result from the use of this
- code.
-
- If you have comments, feedback, questions, or would like
- to submit bug fixes or updates to this code, please email
- opensource@connectix.com.
- ==================================================================*/
-
- #include "Win32ZString.h"
- #include "ZStringDictionary.h"
-
-
- char * Win32ZString::sDictionaryData = NULL;
-
-
- /*------------------------------------------------------------------
- Initialize [static]
-
- This static method instantiates the singleton classes
- for tracking ZStrings.
- ------------------------------------------------------------------*/
-
- void
- Win32ZString::Initialize()
- {
- new ZStringDictionary();
- new Win32ZStringParser();
- }
-
-
- /*------------------------------------------------------------------
- TearDown [static]
- ------------------------------------------------------------------*/
-
- void
- Win32ZString::TearDown()
- {
- delete &ZStringDictionary::GetZStringDictionary();
- delete &ZStringParser::GetZStringParser();
- }
-
-
- /*------------------------------------------------------------------
- GetTagReplacement
- ------------------------------------------------------------------*/
-
- Z_UInt16
- Win32ZStringParser::GetTagReplacement(
- ZStringTagID inTagID,
- char * outReplacement)
- {
- const char * ellipse = "...";
- const char * trademark = "(tm)";
- const char * registeredTrademark = "(r)";
- const char * copyright = "(c)";
- const char * mdash = "-";
- const char * carriageReturn = "\r\n";
-
- const char * replacementStr = NULL;
- Boolean specialChar = ((inTagID == kZTag_hellip) || (inTagID == kZTag_trade) ||
- (inTagID == kZTag_copy) || (inTagID == kZTag_reg) ||
- (inTagID == kZTag_mdash) || (inTagID == kZTag_br));
-
- // By default, all tags are one character.
- UInt16 tagSize = 1;
-
- if (outReplacement != NULL)
- {
- static const char sTagReplacements[] = {
- '\0', // kTagIDInvalid
- '\xC6', // kZTag_AElig
- '\xC1', // kZTag_Aacute
- '\xC2', // kZTag_Acirc
- '\xC0', // kZTag_Agrave
- '\xC5', // kZTag_Aring
- '\xC3', // kZTag_Atilde
- '\xC4', // kZTag_Auml
- '\xC7', // kZTag_Ccedil
- '\xC9', // kZTag_Eacute
- '\xCA', // kZTag_Ecirc
- '\xC8', // kZTag_Egrave
- '\xCB', // kZTag_Euml
- '\xCD', // kZTag_Iacute
- '\xCE', // kZTag_Icirc
- '\xCC', // kZTag_Igrave
- '\xCF', // kZTag_Iuml
- '\xD1', // kZTag_Ntilde
- '\xD3', // kZTag_Oacute
- '\xD4', // kZTag_Ocirc
- '\xD2', // kZTag_Ograve
- '\xD8', // kZTag_Oslash
- '\xD5', // kZTag_Otilde
- '\xD6', // kZTag_Ouml
- '\xDA', // kZTag_Uacute
- '\xDB', // kZTag_Ucirc
- '\xD9', // kZTag_Ugrave
- '\xDC', // kZTag_Uuml
- '\xE1', // kZTag_aacute
- '\xE2', // kZTag_acirc
- '\xE6', // kZTag_aelig
- '\xE0', // kZTag_agrave
- '\x26', // kZTag_amp
- '\xE5', // kZTag_aring
- '\xE3', // kZTag_atilde
- '\xE4', // kZTag_auml
- '\x84', // kZTag_bdquo
- '\xB7', // kZTag_bull
- '\xE7', // kZTag_ccedil
- '\xA2', // kZTag_cent
- '\xA9', // kZTag_copy
- '\xE9', // kZTag_eacute
- '\xEA', // kZTag_ecirc
- '\xE8', // kZTag_egrave
- '\xEB', // kZTag_euml
- '\x3E', // kZTag_gt
- '\x85', // kZTag_hellip
- '\xED', // kZTag_iacute
- '\xEE', // kZTag_icirc
- '\xA1', // kZTag_iexcl
- '\xEC', // kZTag_igrave
- '\xBF', // kZTag_iquest
- '\xEF', // kZTag_iuml
- '"', //'\x93', // kZTag_ldquo
- '\'', //'\x91', // kZTag_lsquo
- '\x3C', // kZTag_lt
- '\x97', // kZTag_mdash
- '\xB5', // kZTag_micro
- '\xA0', // kZTag_nbsp
- '\x96', // kZTag_ndash
- '\xF1', // kZTag_ntilde
- '\xF3', // kZTag_oacute
- '\xF4', // kZTag_ocirc
- '\xF2', // kZTag_ograve
- '\xF8', // kZTag_oslash
- '\xF5', // kZTag_otilde
- '\xF6', // kZTag_ouml
- '\xB6', // kZTag_para
- '\xE3', // kZTag_pi !!!
- '\xA3', // kZTag_pound
- '"', //'\x94', // kZTag_rdquo
- '\xAE', // kZTag_reg
- '\x00', // kZTag_replace
- '\'', //'\x92', // kZTag_rsquo
- '\x82', // kZTag_sbquo
- '\xDF', // kZTag_szlig
- '\x99', // kZTag_trade
- '\xFA', // kZTag_uacute
- '\xFB', // kZTag_ucirc
- '\xF9', // kZTag_ugrave
- '\xFC', // kZTag_uuml
- '\xA5', // kZTag_yen
- '\xFF', // kZTag_yuml
- '\x0D' // kZTag_br
- };
-
- check(inTagID > kZTag_Invalid && inTagID < sizeof(sTagReplacements)/sizeof(char));
- check(inTagID != kZTag_replace);
-
- // fill in the equivalent
- *outReplacement = sTagReplacements[inTagID];
-
- }
-
- switch (inTagID)
- {
- case kZTag_hellip:
- replacementStr = ellipse;
- break;
-
- case kZTag_trade:
- replacementStr = trademark;
- break;
-
- case kZTag_copy:
- replacementStr = copyright;
- break;
-
- case kZTag_reg:
- replacementStr = registeredTrademark;
- break;
-
- case kZTag_mdash:
- replacementStr = mdash;
- break;
-
- case kZTag_br:
- replacementStr = carriageReturn;
- break;
-
- default:
- break;
- }
-
- if (specialChar)
- {
- if (outReplacement)
- strcpy(outReplacement, replacementStr);
- tagSize = strlen(replacementStr);
- }
-
- return tagSize;
- }
-
-
- /*------------------------------------------------------------------
- LoadOverrideDictionary
- ------------------------------------------------------------------*/
-
- Z_Boolean
- Win32ZString::LoadOverrideDictionary(
- UInt16 inOverrideID)
- {
- HRSRC resource;
- HGLOBAL resHandle;
- void * resData;
- UInt32 resSize;
-
- // find the resource
- resource = ::FindResource(NULL, (LPCTSTR)inOverrideID, "DICT");
- if (resource == NULL)
- return false;
-
- // get the size
- resSize = ::SizeofResource(NULL, resource);
- if (resSize == 0)
- goto CantSizeResource;
-
- // load the resource
- resHandle = ::LoadResource(NULL, resource);
- if (resHandle == NULL)
- goto CantLoadResource;
-
- // get the data
- resData = ::LockResource(resHandle);
- if (resData == NULL)
- goto CantLockResource;
-
- // delete any old dictionary
- delete[] sDictionaryData;
-
- // allocate memory
- sDictionaryData = new char[resSize + 2];
-
- if (sDictionaryData == NULL)
- goto CantAllocateMemory;
- ::memset(sDictionaryData, 0, resSize + 2);
- ::memcpy(sDictionaryData, resData, resSize);
-
- // populate the dictionary
- ZString::PopulateDictionary(sDictionaryData);
- return true;
-
- CantAllocateMemory:
- CantLockResource:
- CantLoadResource:
- CantSizeResource:
- return false;
- }
-
-
- /*------------------------------------------------------------------
- LoadOverrideDictionary
- ------------------------------------------------------------------*/
-
- Z_Boolean
- Win32ZString::LoadOverrideDictionary(
- const char * inFilename)
- {
- FILE * file;
- UInt32 fileSize;
-
- // open the file
- file = ::fopen(inFilename, "rb");
- if (file == NULL)
- return false;
-
- // determine the size
- ::fseek(file, 0, SEEK_END);
- fileSize = ::ftell(file);
- ::fseek(file, 0, SEEK_SET);
-
- // delete any old dictionary
- delete[] sDictionaryData;
-
- // allocate memory
- sDictionaryData = new char[fileSize + 2];
-
- if (sDictionaryData == NULL)
- goto CantAllocateMemory;
- ::memset(sDictionaryData, 0, fileSize + 2);
- ::fread(sDictionaryData, 1, fileSize, file);
-
- // populate the dictionary
- ZString::PopulateDictionary(sDictionaryData);
- return true;
-
- CantAllocateMemory:
- return false;
- }
-
-
- /*------------------------------------------------------------------
- CalcOverrideDictionaryID
- ------------------------------------------------------------------*/
-
- Z_UInt16
- Win32ZString::CalcOverrideDictionaryID()
- {
- Z_UInt16 dictionaryID = kEnglishOverrideID;
-
- // Get the calling thread's current locale.
- LCID lcid = GetThreadLocale();
-
- // Get the primary language.
- WORD languageID = LANGIDFROMLCID(lcid);
- WORD primaryLanguage = PRIMARYLANGID(languageID);
-
- switch (primaryLanguage)
- {
- default:
- case LANG_ENGLISH:
- dictionaryID = kEnglishOverrideID;
- break;
-
- case LANG_FRENCH:
- dictionaryID = kFrenchOverrideID;
- break;
-
- case LANG_GERMAN:
- dictionaryID = kGermanOverrideID;
- break;
-
- case LANG_ITALIAN:
- dictionaryID = kItalianOverrideID;
- break;
-
- case LANG_SPANISH:
- dictionaryID = kSpanishOverrideID;
- break;
-
- case LANG_JAPANESE:
- dictionaryID = kJapaneseOverrideID;
- break;
-
- case LANG_PORTUGUESE:
- dictionaryID = kPortugueseOverrideID;
- break;
- }
-
- return dictionaryID;
- }
-
-
-