home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libi18n / acptlang.c next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.8 KB  |  167 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. /*    acptlang.c    */
  19. /*  File implement Accept-Language and Accept-Charset */
  20.  
  21. #include "intlpriv.h"
  22. #include "prefapi.h"
  23.  
  24. static char *accept_language = NULL;
  25. static const char *pref_accept_language = "intl.accept_languages";
  26.  
  27. static char *accept_charset = NULL;
  28. static const char *pref_accept_charset = "intl.accept_charsets";
  29.  
  30.  
  31. PRIVATE void FilterOutHumanReadableText(char* str)
  32. {
  33.     if(NULL == str)
  34.         return;
  35. /*
  36.     Currently, we only do this on Window build because
  37.     1. I only know window have this problem
  38.     2. Macintosh should be ok since we do not store this in this
  39.         format on the Mac
  40.     3. I don't know about UNIX for now.
  41.  
  42.     We probably need to build this for all the platform since
  43.     the preference will be for xp so a window pref could be 
  44.         used on Mac and UNIX
  45.  
  46. */
  47. #if ((defined XP_WIN) || (defined XP_UNIX) || (defined XP_OS2))
  48.     if(strchr(str, '[') != NULL)     /* Searching [ to determinate wheather         */
  49.                                     /* This is "Window-Style" AcceptLang Pref     */
  50.     {
  51.         int state= 0;
  52.         char *in, *out;
  53.         /*
  54.             State Machine:
  55.  
  56.                     State 0      State 1        State 2
  57.             Hit [   Goto State 1 Copy in (Err!) Emit [,] Goto State 1
  58.             Hit ]   Ignore in    Goto State 2   ignore In
  59.             Else    Ignore in    Copy in        ignore In        
  60.  
  61.             Sample data:
  62.             in:     AAA BBB [abb-a], CC DD [ee-ff]
  63.             state:  0000000001111112222222221111112
  64.             action:          abb-a         ,ee-ff  \0
  65.             Out:     abb-a,ee-ff
  66.     
  67.  
  68.         */
  69.         for(state=0, in=out=str; *in!=0; in++)
  70.         {
  71.             switch(state)
  72.             {
  73.                 case 1:                    /* Between [ and ] */
  74.                     if(*in == ']')        /* hit ] */
  75.                         state = 2;        /* Change state to 2 */
  76.                     else
  77.                         *out++ = *in;    /* Copy text between [ and ] */
  78.                     break;
  79.                 case 2:                    /* Before hit not-first [ */ 
  80.                     if(*in == '[')        /* hit the not-first [ */
  81.                     {
  82.                         state = 1;        /* Change state to 1 */
  83.                         *out++ = ',';    /* Need to copy , */
  84.                     }
  85.                     break;
  86.                 case 0:                    /* Before hit first [ */
  87.                 default:
  88.                     if(*in == '[')        /* hit first [ */
  89.                         state = 1;        /* Change state to 1 */
  90.                     break;
  91.             }
  92.             
  93.         }
  94.         *out = '\0';                    /* NULL terminate output */
  95.     }
  96. #endif
  97. }
  98.  
  99. /* callback routine invoked by prefapi when the pref value changes */
  100. /* According to the comment in mime2fun.c 
  101.    Win16 build fails if PR_CALLBACK is declared as static
  102.    So I change it to MODULE_PRIVATE
  103. */
  104.  
  105. MODULE_PRIVATE int PR_CALLBACK intl_SetAcceptLanguage(const char * newpref, void * data)
  106. {
  107.     if (accept_language) {
  108.         XP_FREE(accept_language);
  109.         accept_language = NULL;
  110.     }
  111.     
  112.     PREF_CopyCharPref(pref_accept_language, &accept_language);
  113.     FilterOutHumanReadableText(accept_language);
  114.     return PREF_NOERROR;
  115. }
  116.  
  117. /* INTL_GetAcceptLanguage()                        */
  118. /* return the AcceptLanguage from XP Preference */
  119. /* this should be a C style NULL terminated string */
  120. PUBLIC char* INTL_GetAcceptLanguage()
  121. {
  122.     if (accept_language == NULL)
  123.     {
  124.         PREF_CopyCharPref(pref_accept_language, &accept_language);
  125.         
  126.         if (accept_language)
  127.             PREF_RegisterCallback(pref_accept_language, intl_SetAcceptLanguage, NULL);
  128.     }
  129.     FilterOutHumanReadableText(accept_language);
  130.     
  131.     return accept_language;
  132. }
  133.  
  134. /* callback routine invoked by prefapi when the pref value changes */
  135. /* According to the comment in mime2fun.c 
  136.    Win16 build fails if PR_CALLBACK is declared as static
  137.    So I change it to MODULE_PRIVATE 
  138. */
  139. MODULE_PRIVATE int PR_CALLBACK intl_SetAcceptCharset(const char * newpref, void * data)
  140. {
  141.     if (accept_charset) {
  142.         XP_FREE(accept_charset);
  143.         accept_charset = NULL;
  144.     }
  145.     
  146.     PREF_CopyCharPref(pref_accept_charset, &accept_charset);
  147.     FilterOutHumanReadableText(accept_charset);
  148.     return PREF_NOERROR;
  149. }
  150.  
  151. /* INTL_GetAcceptCharset()                        */
  152. /* return the AcceptCharset from XP Preference */
  153. /* this should be a C style NULL terminated string */
  154. PUBLIC char* INTL_GetAcceptCharset()
  155. {
  156.     if (accept_charset == NULL)
  157.     {
  158.         PREF_CopyCharPref(pref_accept_charset, &accept_charset);
  159.         
  160.         if (accept_charset)
  161.             PREF_RegisterCallback(pref_accept_charset, intl_SetAcceptCharset, NULL);
  162.     }
  163.     
  164.     return accept_charset;
  165. }
  166.  
  167.