home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libi18n / unicode / unitbl.c < prev   
Encoding:
C/C++ Source or Header  |  1998-04-08  |  5.2 KB  |  195 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. #include "prlog.h"
  19. #include "prtypes.h"
  20. #include "libi18n.h"
  21. #include "csid.h"
  22.  
  23. #define MAXUTABLENAME    16
  24. typedef struct tblrsrcinfo {
  25.     char    name[MAXUTABLENAME];
  26.     uint16    refcount;
  27.     HGLOBAL hTbl;
  28. } tblrsrcinfo;
  29.  
  30. typedef struct utablename {
  31.     uint16        csid;
  32.     tblrsrcinfo    frominfo;
  33.     tblrsrcinfo toinfo;
  34.  
  35. } utablename;
  36.  
  37. PR_PUBLIC_API(void*)     UNICODE_LOADUCS2TABLE(uint16 csid, int from);
  38. PR_PUBLIC_API(void)    UNICODE_UNLOADUCS2TABLE(uint16 csid, void *utblPtr, int from);
  39.  
  40. tblrsrcinfo*     unicode_FindUTableName(uint16 csid, int from);
  41.  
  42. HINSTANCE _unicode_hInstance;
  43.  
  44. #ifdef _WIN32
  45. BOOL WINAPI DllMain (HINSTANCE hDLL, DWORD dwReason, LPVOID lpReserved)
  46. {
  47.   switch (dwReason)
  48.   {
  49.     case DLL_PROCESS_ATTACH:
  50.       _unicode_hInstance = hDLL;
  51.       break;
  52.  
  53.     case DLL_THREAD_ATTACH:
  54.       break;
  55.  
  56.     case DLL_THREAD_DETACH:
  57.       break;
  58.  
  59.     case DLL_PROCESS_DETACH:
  60.       _unicode_hInstance = NULL;
  61.       break;
  62.   }
  63.  
  64.     return TRUE;
  65. }
  66.  
  67. #else  /* ! _WIN32 */
  68.  
  69. int CALLBACK LibMain( HINSTANCE hInst, WORD wDataSeg, 
  70.                       WORD cbHeapSize, LPSTR lpszCmdLine )
  71. {
  72.     _unicode_hInstance = hInst;
  73.     return TRUE;
  74. }
  75.  
  76. BOOL CALLBACK _export _loadds WEP(BOOL fSystemExit)
  77. {
  78.     _unicode_hInstance = NULL;
  79.     return TRUE;
  80. }
  81. #endif /* ! _WIN32 */
  82.  
  83.  
  84. utablename utablenametbl[] =
  85. {
  86.     /* Special Note, for Windows, we use cp1252 for CS_LATIN1 */
  87.     {CS_ASCII,        {"CP1252.UF",    0,    NULL},        {"CP1252.UT",    0,    NULL}},
  88.     {CS_LATIN1,        {"CP1252.UF",    0,    NULL},        {"CP1252.UT",    0,    NULL}},
  89.     {CS_SJIS,        {"SJIS.UF",        0,    NULL},        {"SJIS.UT",        0,    NULL}},
  90.     {CS_BIG5,        {"BIG5.UF",        0,    NULL},        {"BIG5.UT",        0,    NULL}},
  91.     {CS_GB_8BIT,    {"GB2312.UF",    0,    NULL},        {"GB2312.UT",    0,    NULL}},
  92.     {CS_KSC_8BIT,    {"U20KSC.UF",    0,    NULL},        {"U20KSC.UT",    0,    NULL}},
  93.     {CS_CP_1251,    {"CP1251.UF",    0,    NULL},        {"CP1251.UT",    0,    NULL}},
  94.     {CS_CP_1250,        {"CP1250.UF",    0,    NULL},        {"CP1250.UT",    0,    NULL}},
  95.     {CS_CP_1253,        {"CP1253.UF",    0,    NULL},        {"CP1253.UT",    0,    NULL}},
  96.     {CS_8859_9,        {"CP1254.UF",    0,    NULL},        {"CP1254.UT",    0,    NULL}},
  97.     {CS_SYMBOL,        {"MACSYMBO.UF",    0,    NULL},        {"MACSYMBO.UT",    0,    NULL}},
  98.     {CS_DINGBATS,    {"MACDINGB.UF",    0,    NULL},        {"MACDINGB.UT",    0,    NULL}},
  99.     {CS_DEFAULT,    {"",            0,    NULL},        {"",            0,    NULL}}
  100. };
  101. static tblrsrcinfo* unicode_FindUTableName(uint16 csid, int from)
  102. {
  103.     int i;
  104.     for(i=0; utablenametbl[i].csid != CS_DEFAULT; i++)
  105.     {
  106.         if(utablenametbl[i].csid == csid)
  107.             return from ? &(utablenametbl[i].frominfo) 
  108.                         : &(utablenametbl[i].toinfo);
  109.     }
  110. #ifdef _DEBUG
  111.     OutputDebugString("unicode_FindUTableName: Cannot find table information");
  112. #endif /* _DEBUG */
  113.  
  114.     return NULL;
  115. }
  116. PR_PUBLIC_API(void *) UNICODE_LOADUCS2TABLE(uint16 csid, int from)
  117. {
  118.     HRSRC   hrsrc;
  119.     HGLOBAL hRes;
  120.     void *table;
  121.     tblrsrcinfo* tbl = unicode_FindUTableName(csid, from);
  122.     /*    Cannot find this csid */
  123.     if(tbl == NULL)
  124.         return (NULL);
  125.     /*  Find a loaded table */
  126.     if(tbl->refcount > 0)
  127.     {
  128.         tbl->refcount++;
  129.         return ((void*)LockResource(tbl->hTbl));
  130.     }
  131.     /*  Find a unloaded table */
  132.     hrsrc = FindResource(_unicode_hInstance, tbl->name, RT_RCDATA);
  133.     if(!hrsrc) 
  134.     {
  135.         /* cannot find that RCDATA resource */
  136. #ifdef _DEBUG
  137.         OutputDebugString("UNICODE_LoadUCS2Table cannot find table resource");
  138. #endif
  139.         return (NULL);
  140.     }
  141.     hRes = LoadResource(_unicode_hInstance,hrsrc);
  142.     if(!hRes) 
  143.     {
  144.         /* cannot find that RCDATA resource */
  145. #ifdef _DEBUG
  146.         OutputDebugString("UNICODE_LoadUCS2Table cannot load table resource");
  147. #endif
  148.         return (NULL);
  149.     }
  150.     table = (void*)    LockResource(hRes);
  151.     if(!table)
  152.     {
  153.         /* cannot find that RCDATA resource */
  154. #ifdef _DEBUG
  155.         OutputDebugString("UNICODE_LoadUCS2Table cannot lock table resource");
  156. #endif
  157.         return (NULL);
  158.     }
  159.     tbl->refcount++;
  160.     tbl->hTbl = hRes;
  161.     return(table);
  162. }
  163. PR_PUBLIC_API(void)    UNICODE_UNLOADUCS2TABLE(uint16 csid, void *utblPtr, int from)
  164. {
  165.     tblrsrcinfo* tbl = unicode_FindUTableName(csid, from);
  166.     /*    Cannot find this csid */
  167.     if(tbl == NULL)
  168.     {
  169. #ifdef _DEBUG
  170.         OutputDebugString("unicode_UnloadUCS2Table don't know how to deal with this csid");
  171. #endif
  172.         return;
  173.     }
  174.     /*  Find a loaded table */
  175.     if(tbl->refcount == 0)
  176.     {
  177. #ifdef _DEBUG
  178.         OutputDebugString("unicode_UnloadUCS2Table try to unload an unloaded table");
  179. #endif
  180.         tbl->hTbl = NULL;
  181.         return;
  182.     }
  183. #ifndef _WIN32
  184.     /*  UnlockResource to decrease the internal reference count */
  185.     UnlockResource(tbl->hTbl);
  186. #endif
  187.     tbl->refcount--;
  188.     if(tbl->refcount <= 0)
  189.     {
  190.         FreeResource(tbl->hTbl);
  191.         tbl->hTbl = NULL;
  192.         tbl->refcount = 0;
  193.     }
  194. }
  195.