1 /*
2 * guesslocale.c
3 *
4 * All rights reserved. Copyright (C) 2003 by Tomohiro KUBOTA.
5 * $Id: guesslocale.c,v 1.7 2004/01/05 08:41:22 nrt Exp $
6 */
7 /*
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23 #ifdef HAVE_SETLOCALE
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <locale.h>
28 #if defined(HAVE_LANGINFO_CODESET)
29 #include <langinfo.h>
30 #endif
31
32 #include <import.h>
33 #include <ctable.h>
34 #include <begin.h>
35 #include <guesslocale.h>
36
strcmp2(char * str1,char * str2)37 static int strcmp2( char *str1, char *str2 )
38 {
39 while(1) {
40 if( *str1 == '_' || *str1 == '-' ) {str1++; continue;}
41 if( *str2 == '_' || *str2 == '-' ) {str2++; continue;}
42 if( toupper(*str1) != toupper(*str2) ) return -1;
43 if( *str1 == 0 ) return 0;
44 str1++; str2++;
45 }
46 }
47
LocaleCodingSystem(char * language)48 public byte LocaleCodingSystem( char *language )
49 {
50 #ifdef MSDOS
51 strcpy( language, "ja_JP" );
52 return SHIFT_JIS;
53 #elif !defined(HAVE_LANGINFO_CODESET)
54 #warning "XXX There isn't nl_langinfo(CODESET) functionality."
55 #warning "XXX Using fixed value ``ja_JP'' and EUC_JAPAN..."
56 strcpy( language, "ja_JP" );
57 return EUC_JAPAN;
58 #else
59 static char lang[6];
60 char *nl;
61 static byte c = 0;
62
63 if( c ) {
64 strcpy( language, lang );
65 return c;
66 }
67
68 nl = setlocale( LC_CTYPE, "" );
69 if (nl != NULL) {
70 strncpy( lang, nl, 5 ); lang[5] = 0;
71 } else
72 lang[0] = 0;
73 nl = nl_langinfo( CODESET );
74
75 if ( !strcmp2(nl, "UTF-8") ) c = UTF_8;
76 else if ( !strcmp2(nl, "ISO-8859-1") ) c = ISO_8859_1;
77 else if ( !strcmp2(nl, "ISO-8859-2") ) c = ISO_8859_2;
78 else if ( !strcmp2(nl, "ISO-8859-3") ) c = ISO_8859_3;
79 else if ( !strcmp2(nl, "ISO-8859-4") ) c = ISO_8859_4;
80 else if ( !strcmp2(nl, "ISO-8859-5") ) c = ISO_8859_5;
81 else if ( !strcmp2(nl, "ISO-8859-6") ) c = ISO_8859_6;
82 else if ( !strcmp2(nl, "ISO-8859-7") ) c = ISO_8859_7;
83 else if ( !strcmp2(nl, "ISO-8859-8") ) c = ISO_8859_8;
84 else if ( !strcmp2(nl, "ISO-8859-9") ) c = ISO_8859_9;
85 else if ( !strcmp2(nl, "ISO-8859-10") ) c = ISO_8859_10;
86 else if ( !strcmp2(nl, "ISO-8859-11") ) c = ISO_8859_11;
87 else if ( !strcmp2(nl, "TIS-620") ) c = ISO_8859_11;
88 else if ( !strcmp2(nl, "ISO-8859-13") ) c = ISO_8859_13;
89 else if ( !strcmp2(nl, "ISO-8859-14") ) c = ISO_8859_14;
90 else if ( !strcmp2(nl, "ISO-8859-15") ) c = ISO_8859_15;
91 else if ( !strcmp2(nl, "EUC-JP") ) c = EUC_JAPAN;
92 else if ( !strcmp2(nl, "GB2312") ) c = EUC_CHINA;
93 else if ( !strcmp2(nl, "EUC-CN") ) c = EUC_CHINA;
94 else if ( !strcmp2(nl, "EUC-KR") ) c = EUC_KOREA;
95 else if ( !strcmp2(nl, "EUC-TW") ) c = EUC_TAIWAN;
96 else if ( !strcmp2(nl, "BIG5") ) c = BIG_FIVE;
97 else if ( !strcmp2(nl, "SHIFT_JIS") ) c = SHIFT_JIS;
98 else if ( !strcmp2(nl, "SJIS") ) c = SHIFT_JIS;
99 else c = ISO_8859_1;
100
101 strcpy( language, lang );
102 return c;
103 #endif
104 }
105
DetermineEUC(char * language,char defaultEuc)106 public byte DetermineEUC( char *language, char defaultEuc )
107 {
108 if( EUC_JAPAN == defaultEuc || EUC_KOREA == defaultEuc ||
109 EUC_CHINA == defaultEuc || EUC_TAIWAN == defaultEuc )
110 return defaultEuc;
111 if( !strncmp(language, "ja", 2) ) return EUC_JAPAN;
112 if( !strncmp(language, "ko", 2) ) return EUC_KOREA;
113 if( !strcmp(language, "zh_CN") ) return EUC_CHINA;
114 if( !strcmp(language, "zh_TW") ) return EUC_TAIWAN;
115 if( !strcmp(language, "zh_HK") ) return EUC_TAIWAN;
116 if( !strncmp(language, "zh", 2) ) return EUC_TAIWAN;
117 return EUC_JAPAN;
118 }
119
Determine8bit(char * language)120 public byte Determine8bit( char *language )
121 {
122 char country[3], language2[3], *cp;
123
124 cp = strchr(language, '_');
125 if (cp != NULL) {
126 strncpy(country, cp + 1, 2);
127 country[2] = 0;
128 } else
129 country[0] = 0;
130 strncpy( language2, language, 2 );
131 language2[2] = 0;
132
133 /* Euro countries, maybe increased in future */
134 if( !strcmp(country, "AT") ) return ISO_8859_15;
135 if( !strcmp(country, "BE") ) return ISO_8859_15;
136 if( !strcmp(country, "DE") ) return ISO_8859_15;
137 if( !strcmp(country, "ES") ) return ISO_8859_15;
138 if( !strcmp(country, "FI") ) return ISO_8859_15;
139 if( !strcmp(country, "FR") ) return ISO_8859_15;
140 if( !strcmp(country, "GR") ) return ISO_8859_7;
141 if( !strcmp(country, "IE") ) return ISO_8859_15;
142 if( !strcmp(country, "IT") ) return ISO_8859_15;
143 if( !strcmp(country, "LU") ) return ISO_8859_15;
144 if( !strcmp(country, "NL") ) return ISO_8859_15;
145 if( !strcmp(country, "PT") ) return ISO_8859_15;
146
147 if( !strcmp(language2, "ar") ) return ISO_8859_6;
148 if( !strcmp(language2, "bs") ) return ISO_8859_2;
149 if( !strcmp(language2, "cs") ) return ISO_8859_2;
150 if( !strcmp(language2, "cy") ) return ISO_8859_14;
151 if( !strcmp(language2, "fa") ) return ISO_8859_8;
152 if( !strcmp(language2, "hr") ) return ISO_8859_2;
153 if( !strcmp(language2, "hu") ) return ISO_8859_2;
154 if( !strcmp(language2, "iw") ) return ISO_8859_8;
155 if( !strcmp(language2, "lt") ) return ISO_8859_13;
156 if( !strcmp(language2, "lv") ) return ISO_8859_13;
157 if( !strcmp(language2, "mi") ) return ISO_8859_13;
158 if( !strcmp(language2, "mk") ) return ISO_8859_5;
159 if( !strcmp(language2, "mt") ) return ISO_8859_3;
160 if( !strcmp(language2, "pl") ) return ISO_8859_2;
161 if( !strcmp(language2, "pl") ) return ISO_8859_2;
162 if( !strcmp(language2, "ro") ) return ISO_8859_2;
163 if( !strcmp(language2, "ru") ) return ISO_8859_5; /* should be KOI8-R */
164 if( !strcmp(language2, "sk") ) return ISO_8859_2;
165 if( !strcmp(language2, "sl") ) return ISO_8859_2;
166 if( !strcmp(language2, "sr") ) return ISO_8859_2; /* ISO-8859-5? */
167 if( !strcmp(language2, "th") ) return ISO_8859_11;
168 if( !strcmp(language2, "tr") ) return ISO_8859_9;
169
170 return ISO_8859_1;
171 }
172
173 #endif /* HAVE_SETLOCALE */
174