home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / evbl0627.zip / everblue_20010627.zip / x11 / Xlc_SetLocale.c < prev    next >
C/C++ Source or Header  |  1999-11-02  |  7KB  |  237 lines

  1. /* $TOG: SetLocale.c /main/37 1997/06/02 17:27:45 kaleb $ */
  2.  
  3. /*
  4.  * Copyright 1990, 1991 by OMRON Corporation, NTT Software Corporation,
  5.  *                      and Nippon Telegraph and Telephone Corporation
  6.  *
  7.  * Permission to use, copy, modify, distribute, and sell this software and its
  8.  * documentation for any purpose is hereby granted without fee, provided that
  9.  * the above copyright notice appear in all copies and that both that
  10.  * copyright notice and this permission notice appear in supporting
  11.  * documentation, and that the names of OMRON, NTT Software, and NTT
  12.  * not be used in advertising or publicity pertaining to distribution of the
  13.  * software without specific, written prior permission. OMRON, NTT Software,
  14.  * and NTT make no representations about the suitability of this
  15.  * software for any purpose.  It is provided "as is" without express or
  16.  * implied warranty.
  17.  *
  18.  * OMRON, NTT SOFTWARE, AND NTT, DISCLAIM ALL WARRANTIES WITH REGARD
  19.  * TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  20.  * AND FITNESS, IN NO EVENT SHALL OMRON, NTT SOFTWARE, OR NTT, BE
  21.  * LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 
  22.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  23.  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  24.  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  25.  *
  26.  *    Authors: Li Yuhong        OMRON Corporation
  27.  *         Tetsuya Kato        NTT Software Corporation
  28.  *         Hiroshi Kuribayashi    OMRON Corporation
  29.  *   
  30.  */
  31. /*
  32.  
  33. Copyright (c) 1987  X Consortium
  34.  
  35. Permission is hereby granted, free of charge, to any person obtaining
  36. a copy of this software and associated documentation files (the
  37. "Software"), to deal in the Software without restriction, including
  38. without limitation the rights to use, copy, modify, merge, publish,
  39. distribute, sublicense, and/or sell copies of the Software, and to
  40. permit persons to whom the Software is furnished to do so, subject to
  41. the following conditions:
  42.  
  43. The above copyright notice and this permission notice shall be included
  44. in all copies or substantial portions of the Software.
  45.  
  46. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  47. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  48. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  49. IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
  50. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  51. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  52. OTHER DEALINGS IN THE SOFTWARE.
  53.  
  54. Except as contained in this notice, the name of the X Consortium shall
  55. not be used in advertising or otherwise to promote the sale, use or
  56. other dealings in this Software without prior written authorization
  57. from the X Consortium.
  58.  
  59. */
  60. /* $XFree86: xc/lib/X11/SetLocale.c,v 3.4.2.4 1998/05/26 02:23:54 dawes Exp $ */
  61.  
  62. #include "Xlib_private.h"
  63. #include "Xlcint.h"
  64. #include <X11/Xlocale.h>
  65. #include <X11/Xos.h>
  66.  
  67. #define MAXLOCALE    64    /* buffer size of locale name */
  68.  
  69. #ifdef X_LOCALE
  70.  
  71. /* alternative setlocale() for when the OS does not provide one */
  72.  
  73. #ifdef X_NOT_STDC_ENV
  74. extern char *getenv();
  75. #endif
  76.  
  77. #if NeedFunctionPrototypes
  78. char *
  79. _Xsetlocale(
  80.     int          category,
  81.     _Xconst char *name
  82. )
  83. #else
  84. char *
  85. _Xsetlocale(category, name)
  86.     int        category;
  87.     char       *name;
  88. #endif
  89. {
  90.     DBUG_ENTER("_Xsetlocale")
  91.     static char *xsl_name;
  92.     char *old_name;
  93.     XrmMethods methods;
  94.     XPointer state;
  95.  
  96.     if (category != LC_CTYPE && category != LC_ALL)
  97.     DBUG_RETURN(NULL);
  98.     if (!name) {
  99.     if (xsl_name)
  100.         DBUG_RETURN(xsl_name);
  101.     DBUG_RETURN("C");
  102.     }
  103.     if (!*name)
  104.     name = getenv("LC_CTYPE");
  105.     if (!name || !*name)
  106.     name = getenv("LANG");
  107.     if (name && strlen(name) >= MAXLOCALE)
  108.     name = NULL;
  109.     if (!name || !*name || !_XOpenLC(name))
  110.     name = "C";
  111.     old_name = xsl_name;
  112.     xsl_name = (char *)name;
  113.     methods = _XrmInitParseInfo(&state);
  114.     xsl_name = old_name;
  115.     if (!methods)
  116.     DBUG_RETURN(NULL);
  117.     name = (*methods->lcname)(state);
  118.     xsl_name = Xmalloc(strlen(name) + 1);
  119.     if (!xsl_name) {
  120.     xsl_name = old_name;
  121.     (*methods->destroy)(state);
  122.     DBUG_RETURN(NULL);
  123.     }
  124.     strcpy(xsl_name, name);
  125.     if (old_name)
  126.     Xfree(old_name);
  127.     (*methods->destroy)(state);
  128.     DBUG_RETURN(xsl_name);
  129. }
  130.  
  131. #else /* X_LOCALE */
  132.  
  133. /*
  134.  * _XlcMapOSLocaleName is an implementation dependent routine that derives
  135.  * the LC_CTYPE locale name as used in the sample implementation from that
  136.  * returned by setlocale.
  137.  * Should match the code in Xt ExtractLocaleName.
  138.  */
  139.  
  140. char *
  141. _XlcMapOSLocaleName(osname, siname)
  142.     char *osname;
  143.     char *siname;
  144. {
  145.     DBUG_ENTER("_XlcMapOSLocaleName")
  146. #if defined(hpux) || defined(CSRG_BASED) || defined(sun) || defined(SVR4) || defined(sgi) || defined(__osf__) || defined(AIXV3) || defined(ultrix) || defined(WIN32) || defined(__EMX__)
  147. #ifdef hpux
  148. #ifndef _LastCategory
  149. /* HPUX 9 and earlier */
  150. #define SKIPCOUNT 2
  151. #define STARTCHAR ':'
  152. #define ENDCHAR ';'
  153. #else
  154. /* HPUX 10 */
  155. #define ENDCHAR ' '
  156. #endif
  157. #else
  158. #ifdef ultrix
  159. #define SKIPCOUNT 2
  160. #define STARTCHAR '\001'
  161. #define ENDCHAR '\001'
  162. #else
  163. #if defined(WIN32) || defined(__EMX__)
  164. #define SKIPCOUNT 1
  165. #define STARTCHAR '='
  166. #define ENDCHAR ';'
  167. #define WHITEFILL
  168. #else
  169. #if defined(__osf__) || defined(AIXV3)
  170. #define STARTCHAR ' '
  171. #define ENDCHAR ' '
  172. #else
  173. #if !defined(sun) || defined(SVR4)
  174. #define STARTCHAR '/'
  175. #endif
  176. #define ENDCHAR '/'
  177. #endif
  178. #endif
  179. #endif
  180. #endif
  181.  
  182.     char           *start;
  183.     char           *end;
  184.     int             len;
  185. #ifdef SKIPCOUNT
  186.     int            n;
  187. #endif
  188.  
  189.     start = osname;
  190. #ifdef SKIPCOUNT
  191.     for (n = SKIPCOUNT;
  192.      --n >= 0 && start && (start = strchr (start, STARTCHAR));
  193.      start++)
  194.     ;
  195.     if (!start)
  196.     start = osname;
  197. #endif
  198. #ifdef STARTCHAR
  199.     if (start && (start = strchr (start, STARTCHAR))) {
  200.     start++;
  201. #endif
  202.     if (end = strchr (start, ENDCHAR)) {
  203.         len = end - start;
  204.         if (len >= MAXLOCALE)
  205.         len = MAXLOCALE - 1;
  206.         strncpy(siname, start, len);
  207.         *(siname + len) = '\0';
  208. #ifdef WHITEFILL
  209.         for (start = siname; start = strchr(start, ' '); )
  210.         *start++ = '-';
  211. #endif
  212.         DBUG_RETURN(siname);
  213. #ifdef STARTCHAR
  214.     }
  215. #endif
  216.     }
  217. #ifdef WHITEFILL
  218.     if (strchr(osname, ' ')) {
  219.     len = strlen(osname);
  220.     if (len >= MAXLOCALE - 1)
  221.         len = MAXLOCALE - 1;
  222.     strncpy(siname, osname, len);
  223.     *(siname + len) = '\0';
  224.     for (start = siname; start = strchr(start, ' '); )
  225.         *start++ = '-';
  226.     DBUG_RETURN(siname);
  227.     }
  228. #endif
  229. #undef STARTCHAR
  230. #undef ENDCHAR
  231. #undef WHITEFILL
  232. #endif
  233.     DBUG_RETURN(osname);
  234. }
  235.  
  236. #endif  /* X_LOCALE */
  237.