1 /*
2  * "streamable kanji code filter and converter"
3  * Copyright (c) 1998-2002 HappySize, Inc. All rights reserved.
4  *
5  * LICENSE NOTICES
6  *
7  * This file is part of "streamable kanji code filter and converter",
8  * which is distributed under the terms of GNU Lesser General Public
9  * License (version 2) as published by the Free Software Foundation.
10  *
11  * This software is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with "streamable kanji code filter and converter";
18  * if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19  * Suite 330, Boston, MA  02111-1307  USA
20  *
21  * The author of this file:
22  *
23  */
24 /*
25  * The source code included in this files was separated from mbfilter.c
26  * by Moriyoshi Koizumi <moriyoshi@php.net> on 20 Dec 2002. The file
27  * mbfilter.c is included in this package .
28  *
29  */
30 
31 #include "libmbfl/config.h"
32 
33 #include <stddef.h>
34 #include <string.h>
35 
36 #ifdef HAVE_STRINGS_H
37 #include <strings.h>
38 #endif
39 
40 #include "mbfl_encoding.h"
41 #include "mbfl_language.h"
42 
43 #include "nls/nls_ja.h"
44 #include "nls/nls_kr.h"
45 #include "nls/nls_zh.h"
46 #include "nls/nls_uni.h"
47 #include "nls/nls_de.h"
48 #include "nls/nls_ru.h"
49 #include "nls/nls_ua.h"
50 #include "nls/nls_en.h"
51 #include "nls/nls_hy.h"
52 #include "nls/nls_tr.h"
53 #include "nls/nls_neutral.h"
54 
55 #ifndef HAVE_STRCASECMP
56 #ifdef HAVE_STRICMP
57 #define strcasecmp stricmp
58 #endif
59 #endif
60 
61 static const mbfl_language *mbfl_language_ptr_table[] = {
62 	&mbfl_language_uni,
63 	&mbfl_language_japanese,
64 	&mbfl_language_korean,
65 	&mbfl_language_simplified_chinese,
66 	&mbfl_language_traditional_chinese,
67 	&mbfl_language_english,
68 	&mbfl_language_german,
69 	&mbfl_language_russian,
70 	&mbfl_language_ukrainian,
71 	&mbfl_language_armenian,
72 	&mbfl_language_turkish,
73 	&mbfl_language_neutral,
74 	NULL
75 };
76 
77 /* language resolver */
78 const mbfl_language *
mbfl_name2language(const char * name)79 mbfl_name2language(const char *name)
80 {
81 	const mbfl_language *language;
82 	int i, j;
83 
84 	if (name == NULL) {
85 		return NULL;
86 	}
87 
88 	i = 0;
89 	while ((language = mbfl_language_ptr_table[i++]) != NULL){
90 		if (strcasecmp(language->name, name) == 0) {
91 			return language;
92 		}
93 	}
94 
95 	i = 0;
96 	while ((language = mbfl_language_ptr_table[i++]) != NULL){
97 		if (strcasecmp(language->short_name, name) == 0) {
98 			return language;
99 		}
100 	}
101 
102 	/* search aliases */
103 	i = 0;
104 	while ((language = mbfl_language_ptr_table[i++]) != NULL) {
105 		if (language->aliases != NULL) {
106 			j = 0;
107 			while (language->aliases[j]) {
108 				if (strcasecmp(language->aliases[j], name) == 0) {
109 					return language;
110 				}
111 				j++;
112 			}
113 		}
114 	}
115 
116 	return NULL;
117 }
118 
119 const mbfl_language *
mbfl_no2language(enum mbfl_no_language no_language)120 mbfl_no2language(enum mbfl_no_language no_language)
121 {
122 	const mbfl_language *language;
123 	int i;
124 
125 	i = 0;
126 	while ((language = mbfl_language_ptr_table[i++]) != NULL){
127 		if (language->no_language == no_language) {
128 			return language;
129 		}
130 	}
131 
132 	return NULL;
133 }
134 
135 enum mbfl_no_language
mbfl_name2no_language(const char * name)136 mbfl_name2no_language(const char *name)
137 {
138 	const mbfl_language *language;
139 
140 	language = mbfl_name2language(name);
141 	if (language == NULL) {
142 		return mbfl_no_language_invalid;
143 	} else {
144 		return language->no_language;
145 	}
146 }
147 
148 const char *
mbfl_no_language2name(enum mbfl_no_language no_language)149 mbfl_no_language2name(enum mbfl_no_language no_language)
150 {
151 	const mbfl_language *language;
152 
153 	language = mbfl_no2language(no_language);
154 	if (language == NULL) {
155 		return "";
156 	} else {
157 		return language->name;
158 	}
159 }
160