home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libi18n / csnamefn.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.9 KB  |  200 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. /*    csnamefn.c    */
  19.  
  20. #include "intlpriv.h"
  21.  
  22. extern csname2id_t csname2id_tbl[];
  23.  
  24. /* for XP_GetString() */
  25. #include "xpgetstr.h"
  26. extern int XP_DOCINFO_1;
  27. extern int XP_DOCINFO_2;
  28. extern int XP_DOCINFO_3;
  29. extern int XP_DOCINFO_4;
  30.  
  31. PUBLIC int16
  32. INTL_CharSetNameToID(char    *charset)
  33. {
  34.     csname2id_t    *csn2idp;
  35.     int16            csid;
  36.  
  37.             /* Parse the URL charset string for the charset ID.
  38.              * If no MIME Content-Type charset pararm., default.
  39.              * HTML specifies ASCII, but let user override cuz
  40.              * of prior conventions (i.e. Japan).
  41.              */
  42.     csn2idp = csname2id_tbl;
  43.     csid = csn2idp->cs_id;        /* 1st entry is default codeset ID    */
  44.  
  45.     if (charset != NULL) {        /* Linear search for charset string */
  46.         while (*(csn2idp->cs_name) != '\0') {
  47.              if (strcasecomp(charset, (char *)csn2idp->cs_name) == 0) {
  48.                  return(csn2idp->cs_id);
  49.              }
  50.              csn2idp++;
  51.         }
  52.      }
  53.     return(csn2idp->cs_id);        /* last entry is CS_UNKNOWN    */
  54. }
  55. PUBLIC unsigned char *INTL_CsidToCharsetNamePt(int16 csid)
  56. {
  57.     csname2id_t    *csn2idp;
  58.  
  59.     csid &= ~CS_AUTO;
  60.     csn2idp = &csname2id_tbl[1];    /* First one is reserved, skip it. */
  61.     csid &= 0xff;
  62.  
  63.     /* Linear search for charset string */
  64.     while (*(csn2idp->cs_name) != '\0') {
  65.         if ((csn2idp->cs_id & 0xff) == csid)
  66.             return csn2idp->cs_name;
  67.          csn2idp++;
  68.     }
  69.     return (unsigned char *)"";
  70. }
  71.  
  72. PUBLIC unsigned char *INTL_CsidToJavaCharsetNamePt(int16 csid)
  73. {
  74.     csname2id_t    *csn2idp;
  75.  
  76.     csn2idp = &csname2id_tbl[1];    /* First one is reserved, skip it. */
  77.     csid &= 0xff;
  78.  
  79.     /* Linear search for charset string */
  80.     while (*(csn2idp->cs_name) != '\0') {
  81.         if ((csn2idp->cs_id & 0xff) == csid)
  82.             return csn2idp->java_name;
  83.          csn2idp++;
  84.     }
  85.     return (unsigned char *)"";
  86. }
  87.  
  88.  
  89. PUBLIC void 
  90. INTL_CharSetIDToName(int16 csid, char  *charset)
  91. {
  92.     if (charset) {    
  93.         strcpy(charset,(char *)INTL_CsidToCharsetNamePt(csid));
  94.     }
  95. }
  96.  
  97. PUBLIC void 
  98. INTL_CharSetIDToJavaName(int16 csid, char  *charset)
  99. {
  100.     if (charset) {    
  101.         strcpy(charset,(char *)INTL_CsidToJavaCharsetNamePt(csid));
  102.     }
  103. }
  104.  
  105. PUBLIC const char* PR_CALLBACK 
  106. INTL_CharSetIDToJavaCharSetName(int16 csid) {
  107.     return (char *)INTL_CsidToJavaCharsetNamePt(csid);
  108. }
  109.  
  110. PUBLIC char *
  111. INTL_CharSetDocInfo(iDocumentContext context)
  112. {
  113.     INTL_CharSetInfo c = LO_GetDocumentCharacterSetInfo(context);
  114.     register int16            doc_csid = INTL_GetCSIDocCSID(c);
  115.     register csname2id_t    *csn2idp;
  116.     char                    *s = NULL;
  117.     int                        detected = 0;
  118.  
  119.     if (doc_csid == CS_DEFAULT) {
  120.         doc_csid = INTL_DefaultDocCharSetID(context) & ~CS_AUTO;    /* Get CSID from prefs    */
  121.     } else if (doc_csid & CS_AUTO) {
  122.         doc_csid &= ~CS_AUTO;                /* mask off bit for name lookup */
  123.         detected = 1;
  124.     } else {
  125.         StrAllocCopy(s, INTL_GetCSIMimeCharset(c));    /* string from MIME header */
  126.  
  127.         if (doc_csid == CS_UNKNOWN)
  128.                 StrAllocCat(s, XP_GetString(XP_DOCINFO_1));
  129.         return(s);
  130.     }
  131.             /* Look up name for default & autodetected CSIDs        */
  132. #if defined(XP_WIN) || defined(XP_OS2)
  133.     csn2idp = &csname2id_tbl[1] ;    /* skip first default one    */
  134.     for (; *(csn2idp->cs_name) != '\0'; csn2idp++)
  135. #else
  136.     for (csn2idp = csname2id_tbl; *(csn2idp->cs_name) != '\0'; csn2idp++)
  137. #endif
  138.     {
  139.          if (doc_csid == csn2idp->cs_id) {
  140.             StrAllocCopy(s, (char *)csn2idp->cs_name);
  141.             if (detected)
  142.                 StrAllocCat(s, XP_GetString(XP_DOCINFO_2));
  143.             else
  144.                 StrAllocCat(s, XP_GetString(XP_DOCINFO_3));
  145.             return(s);
  146.          }
  147.     }
  148.     StrAllocCopy(s, INTL_GetCSIMimeCharset(c));    /* string from MIME header */
  149.     StrAllocCat(s, XP_GetString(XP_DOCINFO_4));
  150.     return (s);
  151. }
  152.  
  153.  
  154.  
  155.  
  156. #if defined(XP_WIN) || defined(XP_OS2)
  157.  
  158.  
  159. /*
  160.  This routine will change the default URL charset to
  161.  newCharset, BTW newCharset is passed from UI.
  162. */
  163. void
  164. FE_ChangeURLCharset(const char *charset)
  165. {
  166.     csname2id_t    *csn2idp;
  167.     char            *cp;
  168.  
  169.     if (charset == NULL)
  170.         return;
  171.  
  172.     csn2idp = csname2id_tbl;
  173.  
  174.     cp = (char *)charset;
  175.     if (cp)
  176.         while (*cp != '\0') {
  177.             *cp = tolower(*cp);
  178.             cp++;
  179.         }
  180.  
  181.     while (*(csn2idp->cs_name) != '\0') {
  182.          if (strcasecomp(charset, (char *)csn2idp->cs_name) == 0) {
  183.                 INTL_ChangeDefaultCharSetID(csn2idp->cs_id);
  184.                  return;
  185.              }
  186.              csn2idp++;
  187.         }
  188. }
  189.  
  190. void
  191. INTL_ChangeDefaultCharSetID(int16 csid)
  192. {
  193.     csname2id_tbl[0].cs_id = csid;
  194. }
  195.  
  196. #endif /* XP_WIN */
  197.  
  198.  
  199.  
  200.