Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Redistribution and use in source and binary forms, with or without
  3.  * modification, are permitted provided that the following conditions
  4.  * are met:
  5.  * 1. Redistributions of source code must retain the above copyright
  6.  *    notice, this list of conditions and the following disclaimer.
  7.  * 2. Redistributions in binary form must reproduce the above copyright
  8.  *    notice, this list of conditions and the following disclaimer in the
  9.  *    documentation and/or other materials provided with the distribution.
  10.  *
  11.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  12.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  13.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  14.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  15.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  16.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  17.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  18.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  19.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  20.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  21.  * SUCH DAMAGE.
  22.  */
  23.  
  24. #include <limits.h>
  25. #include <string.h>
  26. #include "lctype.h"
  27. #include "ldpart.h"
  28. #include "setlocale.h"
  29.  
  30. #define LCCTYPE_SIZE (sizeof(struct lc_ctype_T) / sizeof(char *))
  31.  
  32. static char     numone[] = { '\1', '\0'};
  33.  
  34. static const struct lc_ctype_T _C_ctype_locale = {
  35.         "ASCII",                        /* codeset */
  36.         numone                          /* mb_cur_max */
  37. #ifdef __HAVE_LOCALE_INFO_EXTENDED__
  38.         ,
  39.         { "0", "1", "2", "3", "4",      /* outdigits */
  40.           "5", "6", "7", "8", "9" },
  41.         { L"0", L"1", L"2", L"3", L"4", /* woutdigits */
  42.           L"5", L"6", L"7", L"8", L"9" }
  43. #endif
  44. };
  45.  
  46. static struct lc_ctype_T _ctype_locale;
  47. static int      _ctype_using_locale;
  48. #ifdef __HAVE_LOCALE_INFO_EXTENDED__
  49. static char     *_ctype_locale_buf;
  50. #else
  51. /* Max encoding_len + NUL byte + 1 byte mb_cur_max plus trailing NUL byte */
  52. #define _CTYPE_BUF_SIZE (ENCODING_LEN + 3)
  53. static char _ctype_locale_buf[_CTYPE_BUF_SIZE];
  54. #endif
  55.  
  56. int
  57. __ctype_load_locale(const char *name, void *f_wctomb, const char *charset,
  58.                     int mb_cur_max)
  59. {
  60.         int ret;
  61.  
  62. #ifdef __CYGWIN__
  63.         extern int __set_lc_ctype_from_win (const char *,
  64.                                             const struct lc_ctype_T *,
  65.                                             struct lc_ctype_T *, char **,
  66.                                             void *, const char *, int);
  67.         int old_ctype_using_locale = _ctype_using_locale;
  68.         _ctype_using_locale = 0;
  69.         ret = __set_lc_ctype_from_win (name, &_C_ctype_locale, &_ctype_locale,
  70.                                        &_ctype_locale_buf, f_wctomb, charset,
  71.                                        mb_cur_max);
  72.         /* ret == -1: error, ret == 0: C/POSIX, ret > 0: valid */
  73.         if (ret < 0)
  74.           _ctype_using_locale = old_ctype_using_locale;
  75.         else
  76.           {
  77.             _ctype_using_locale = ret;
  78.             ret = 0;
  79.           }
  80. #elif !defined (__HAVE_LOCALE_INFO_EXTENDED__)
  81.         if (!strcmp (name, "C"))
  82.           _ctype_using_locale = 0;
  83.         else
  84.           {
  85.             _ctype_locale.codeset = strcpy (_ctype_locale_buf, charset);
  86.             char *mbc = _ctype_locale_buf + _CTYPE_BUF_SIZE - 2;
  87.             mbc[0] = mb_cur_max;
  88.             mbc[1] = '\0';
  89.             _ctype_locale.mb_cur_max = mbc;
  90.             _ctype_using_locale = 1;
  91.           }
  92.         ret = 0;
  93. #else
  94.         ret = __part_load_locale(name, &_ctype_using_locale,
  95.                 _ctype_locale_buf, "LC_CTYPE",
  96.                 LCCTYPE_SIZE, LCCTYPE_SIZE,
  97.                 (const char **)&_ctype_locale);
  98.         if (ret == 0 && _ctype_using_locale)
  99.                 _ctype_locale.grouping =
  100.                         __fix_locale_grouping_str(_ctype_locale.grouping);
  101. #endif
  102.         return ret;
  103. }
  104.  
  105. struct lc_ctype_T *
  106. __get_current_ctype_locale(void) {
  107.  
  108.         return (_ctype_using_locale
  109.                 ? &_ctype_locale
  110.                 : (struct lc_ctype_T *)&_C_ctype_locale);
  111. }
  112.