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 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34 
35 #include <stddef.h>
36 #include <string.h>
37 
38 #ifdef HAVE_STRINGS_H
39 #include <strings.h>
40 #endif
41 
42 #include "mbfl_encoding.h"
43 #include "mbfl_language.h"
44 
45 #include "nls/nls_ja.h"
46 #include "nls/nls_kr.h"
47 #include "nls/nls_zh.h"
48 #include "nls/nls_uni.h"
49 #include "nls/nls_de.h"
50 #include "nls/nls_ru.h"
51 #include "nls/nls_ua.h"
52 #include "nls/nls_en.h"
53 #include "nls/nls_hy.h"
54 #include "nls/nls_tr.h"
55 #include "nls/nls_neutral.h"
56 
57 #ifndef HAVE_STRCASECMP
58 #ifdef HAVE_STRICMP
59 #define strcasecmp stricmp
60 #endif
61 #endif
62 
63 static const mbfl_language *mbfl_language_ptr_table[] = {
64 	&mbfl_language_uni,
65 	&mbfl_language_japanese,
66 	&mbfl_language_korean,
67 	&mbfl_language_simplified_chinese,
68 	&mbfl_language_traditional_chinese,
69 	&mbfl_language_english,
70 	&mbfl_language_german,
71 	&mbfl_language_russian,
72 	&mbfl_language_ukrainian,
73 	&mbfl_language_armenian,
74 	&mbfl_language_turkish,
75 	&mbfl_language_neutral,
76 	NULL
77 };
78 
79 /* language resolver */
80 const mbfl_language *
mbfl_name2language(const char * name)81 mbfl_name2language(const char *name)
82 {
83 	const mbfl_language *language;
84 	int i, j;
85 
86 	if (name == NULL) {
87 		return NULL;
88 	}
89 
90 	i = 0;
91 	while ((language = mbfl_language_ptr_table[i++]) != NULL){
92 		if (strcasecmp(language->name, name) == 0) {
93 			return language;
94 		}
95 	}
96 
97 	i = 0;
98 	while ((language = mbfl_language_ptr_table[i++]) != NULL){
99 		if (strcasecmp(language->short_name, name) == 0) {
100 			return language;
101 		}
102 	}
103 
104 	/* serch aliases */
105 	i = 0;
106 	while ((language = mbfl_language_ptr_table[i++]) != NULL) {
107 		if (language->aliases != NULL) {
108 			j = 0;
109 			while ((*language->aliases)[j] != NULL) {
110 				if (strcasecmp((*language->aliases)[j], name) == 0) {
111 					return language;
112 				}
113 				j++;
114 			}
115 		}
116 	}
117 
118 	return NULL;
119 }
120 
121 const mbfl_language *
mbfl_no2language(enum mbfl_no_language no_language)122 mbfl_no2language(enum mbfl_no_language no_language)
123 {
124 	const mbfl_language *language;
125 	int i;
126 
127 	i = 0;
128 	while ((language = mbfl_language_ptr_table[i++]) != NULL){
129 		if (language->no_language == no_language) {
130 			return language;
131 		}
132 	}
133 
134 	return NULL;
135 }
136 
137 enum mbfl_no_language
mbfl_name2no_language(const char * name)138 mbfl_name2no_language(const char *name)
139 {
140 	const mbfl_language *language;
141 
142 	language = mbfl_name2language(name);
143 	if (language == NULL) {
144 		return mbfl_no_language_invalid;
145 	} else {
146 		return language->no_language;
147 	}
148 }
149 
150 const char *
mbfl_no_language2name(enum mbfl_no_language no_language)151 mbfl_no_language2name(enum mbfl_no_language no_language)
152 {
153 	const mbfl_language *language;
154 
155 	language = mbfl_no2language(no_language);
156 	if (language == NULL) {
157 		return "";
158 	} else {
159 		return language->name;
160 	}
161 }
162