rjb 1.4.5 → 1.4.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,218 +1,218 @@
1
- /*
2
- * Rjb - Ruby <-> Java Bridge
3
- * Copyright(c) 2004 Kuwashima
4
- *
5
- * This library is free software; you can redistribute it and/or
6
- * modify it under the terms of the GNU Lesser General Public
7
- * License as published by the Free Software Foundation; either
8
- * version 2.1 of the License, or (at your option) any later version.
9
- *
10
- * This library is distributed in the hope that it will be useful,
11
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
- * Lesser General Public License for more details.
14
- *
15
- * $Id: riconv.c 117 2010-06-04 12:16:25Z arton $
16
- */
17
-
18
- #include "ruby.h"
19
- #include "extconf.h"
20
-
21
- #if defined _WIN32 || defined __CYGWIN__
22
- #include <windows.h>
23
- #endif
24
-
25
- #if defined HAVE_NL_LANGINFO
26
- #include <langinfo.h>
27
- static const char* const NL_EUC_TABLE[] = { "EUC-JISX0213", "EUC-JP", "EUC-JP-MS" };
28
- static const char* const NL_SJIS_TABLE[] = { "SHIFT_JIS", "SHIFT_JISX0213", "WINDOWS-31J" };
29
- #endif
30
-
31
- #if defined HAVE_SETLOCALE
32
- #include <locale.h>
33
- #endif
34
- static const char* const LOCALE_EUC_TABLE[] = { "japanese", "ja_JP.eucJP", "japanese.euc", "ja_JP", "ja_JP.ujis" };
35
- static const char* const LOCALE_SJIS_TABLE[] = { "japanese.sjis", "ja_JP.SJIS" };
36
- static const char* const LOCALE_UTF8_TABLE[] = { "ja_JP.UTF-8", "ja_JP.utf8" };
37
-
38
- #include "riconv.h"
39
-
40
- static const char* const CS_EUCJP = "EUC-JP";
41
- static const char* const CS_CP932 = "CP932";
42
- static const char* const CS_SJIS = "SHIFT_JIS";
43
- static const char* const CS_UTF8 = "UTF-8";
44
-
45
-
46
- #if RJB_RUBY_VERSION_CODE < 190
47
- static VALUE objIconvJ2R;
48
- static VALUE objIconvR2J;
49
- static const char* charcode; //is this necessary?
50
- static char Kcode = '\0';
51
-
52
- static int find_table(const char* const str, const char* const table[])
53
- {
54
- int i;
55
- int size = sizeof(table) / sizeof(table[0]);
56
- for (i = 0; i < size; ++i)
57
- {
58
- if (strstr(str, table[i])) return 1;
59
- }
60
- return 0;
61
- }
62
- #endif
63
-
64
- #if RJB_RUBY_VERSION_CODE < 190
65
- static const char* get_charcode_name_by_locale(const char* const name)
66
- {
67
- if (find_table(name, LOCALE_UTF8_TABLE))
68
- return NULL;
69
- else if (find_table(name, LOCALE_EUC_TABLE))
70
- return CS_EUCJP;
71
- else if (find_table(name, LOCALE_SJIS_TABLE))
72
- return CS_SJIS;
73
- else
74
- return NULL;
75
- }
76
- /*
77
- * Get better charcode name.
78
- */
79
- static const char* get_charcode_name()
80
- {
81
- const char* result = NULL;
82
- const char* lang = NULL;
83
-
84
- switch(Kcode)
85
- {
86
- case 'E':
87
- result = CS_EUCJP;
88
- break;
89
- case 'S':
90
- #if defined _WIN32 || defined __CYGWIN__
91
- result = CS_CP932;
92
- #else
93
- result = CS_SJIS;
94
- #endif
95
- break;
96
- case 'U':
97
- //as is.
98
- break;
99
- case 'N':
100
- default:
101
- #if defined _WIN32 || defined __CYGWIN__
102
- if (932 == GetACP()) result = CS_CP932;
103
- #elif defined HAVE_NL_LANGINFO
104
- setlocale(LC_ALL, "C"); //initialize
105
- lang = nl_langinfo(CODESET);
106
- if (find_table(lang, NL_EUC_TABLE))
107
- result = CS_EUCJP;
108
- else if (find_table(lang, NL_SJIS_TABLE))
109
- result = CS_SJIS;
110
- #elif defined HAVE_SETLOCALE
111
- setlocale(LC_ALL, "C"); //initialize
112
- result = get_charcode_name_by_locale(setlocale(LC_ALL, NULL));
113
- #elif defined HAVE_GETENV
114
- if (result = get_charcode_name_by_locale(getenv("LC_ALL")))
115
- ;
116
- else if (result = get_charcode_name_by_locale(getenv("LC_CTYPE")))
117
- ;
118
- else if (result = get_charcode_name_by_locale(getenv("LANG")))
119
- ;
120
- #endif
121
- break;
122
- }
123
- return result;
124
- }
125
- #endif
126
-
127
- #if RJB_RUBY_VERSION_CODE < 190
128
- static void reinit()
129
- {
130
- charcode = get_charcode_name();
131
- if (charcode)
132
- {
133
- VALUE rb_iconv_klass = rb_const_get(rb_cObject, rb_intern("Iconv"));
134
- if (RTEST(rb_iconv_klass)) {
135
- ID sym_new = rb_intern("new");
136
- rb_gc_unregister_address(&objIconvR2J);
137
- objIconvR2J = rb_funcall(rb_iconv_klass, sym_new, 2, rb_str_new2(CS_UTF8), rb_str_new2(charcode));
138
- rb_gc_register_address(&objIconvR2J);
139
- rb_gc_unregister_address(&objIconvJ2R);
140
- objIconvJ2R = rb_funcall(rb_iconv_klass, sym_new, 2, rb_str_new2(charcode), rb_str_new2(CS_UTF8));
141
- rb_gc_register_address(&objIconvJ2R);
142
- }
143
- }
144
- else
145
- {
146
- objIconvR2J = objIconvJ2R = Qnil;
147
- }
148
- }
149
- #endif
150
-
151
- #if RJB_RUBY_VERSION_CODE < 190
152
- static void check_kcode()
153
- {
154
- VALUE rb_iconv_klass = rb_const_get(rb_cObject, rb_intern("Iconv"));
155
- VALUE kcode = rb_gv_get("$KCODE");
156
- if (RTEST(rb_iconv_klass) && TYPE(kcode) == T_STRING) {
157
- char* kcodep = StringValuePtr(kcode);
158
- char upper_kcode = toupper(*kcodep);
159
- if (Kcode != upper_kcode)
160
- {
161
- Kcode = upper_kcode;
162
- reinit();
163
- }
164
- }
165
- else
166
- {
167
- objIconvR2J = objIconvJ2R = Qnil;
168
- }
169
- }
170
- #endif
171
-
172
- VALUE exticonv_local_to_utf8(VALUE local_string)
173
- {
174
- #if RJB_RUBY_VERSION_CODE < 190
175
- check_kcode();
176
- if(RTEST(objIconvR2J))
177
- {
178
- return rb_funcall(objIconvR2J, rb_intern("iconv"), 1, local_string);
179
- }
180
- else
181
- {
182
- return local_string;
183
- }
184
- #else
185
- VALUE rb_cEncoding, encoding, sjis, eucjp, iso2022jp;
186
- rb_cEncoding = rb_const_get(rb_cObject, rb_intern("Encoding"));
187
- sjis = rb_const_get(rb_cEncoding, rb_intern("SHIFT_JIS"));
188
- eucjp = rb_const_get(rb_cEncoding, rb_intern("EUC_JP"));
189
- iso2022jp = rb_const_get(rb_cEncoding, rb_intern("ISO_2022_JP"));
190
- encoding = rb_funcall(local_string, rb_intern("encoding"), 0);
191
-
192
- if (encoding == sjis || encoding == eucjp || encoding == iso2022jp)
193
- {
194
- return rb_funcall(local_string, rb_intern("encode"), 1, rb_str_new2("utf-8"));
195
- }
196
- else
197
- {
198
- return local_string;
199
- }
200
- #endif
201
- }
202
-
203
- VALUE exticonv_utf8_to_local(VALUE utf8_string)
204
- {
205
- #if RJB_RUBY_VERSION_CODE < 190
206
- check_kcode();
207
- if(RTEST(objIconvR2J))
208
- {
209
- return rb_funcall(objIconvJ2R, rb_intern("iconv"), 1, utf8_string);
210
- }
211
- else
212
- {
213
- return utf8_string;
214
- }
215
- #else
216
- return rb_funcall(utf8_string, rb_intern("force_encoding"), 1, rb_str_new2("utf-8"));
217
- #endif
218
- }
1
+ /*
2
+ * Rjb - Ruby <-> Java Bridge
3
+ * Copyright(c) 2004 Kuwashima
4
+ *
5
+ * This library is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * This library is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * $Id: riconv.c 117 2010-06-04 12:16:25Z arton $
16
+ */
17
+
18
+ #include "ruby.h"
19
+ #include "extconf.h"
20
+
21
+ #if defined _WIN32 || defined __CYGWIN__
22
+ #include <windows.h>
23
+ #endif
24
+
25
+ #if defined HAVE_NL_LANGINFO
26
+ #include <langinfo.h>
27
+ static const char* const NL_EUC_TABLE[] = { "EUC-JISX0213", "EUC-JP", "EUC-JP-MS" };
28
+ static const char* const NL_SJIS_TABLE[] = { "SHIFT_JIS", "SHIFT_JISX0213", "WINDOWS-31J" };
29
+ #endif
30
+
31
+ #if defined HAVE_SETLOCALE
32
+ #include <locale.h>
33
+ #endif
34
+ static const char* const LOCALE_EUC_TABLE[] = { "japanese", "ja_JP.eucJP", "japanese.euc", "ja_JP", "ja_JP.ujis" };
35
+ static const char* const LOCALE_SJIS_TABLE[] = { "japanese.sjis", "ja_JP.SJIS" };
36
+ static const char* const LOCALE_UTF8_TABLE[] = { "ja_JP.UTF-8", "ja_JP.utf8" };
37
+
38
+ #include "riconv.h"
39
+
40
+ static const char* const CS_EUCJP = "EUC-JP";
41
+ static const char* const CS_CP932 = "CP932";
42
+ static const char* const CS_SJIS = "SHIFT_JIS";
43
+ static const char* const CS_UTF8 = "UTF-8";
44
+
45
+
46
+ #if RJB_RUBY_VERSION_CODE < 190
47
+ static VALUE objIconvJ2R;
48
+ static VALUE objIconvR2J;
49
+ static const char* charcode; //is this necessary?
50
+ static char Kcode = '\0';
51
+
52
+ static int find_table(const char* const str, const char* const table[])
53
+ {
54
+ int i;
55
+ int size = sizeof(table) / sizeof(table[0]);
56
+ for (i = 0; i < size; ++i)
57
+ {
58
+ if (strstr(str, table[i])) return 1;
59
+ }
60
+ return 0;
61
+ }
62
+ #endif
63
+
64
+ #if RJB_RUBY_VERSION_CODE < 190
65
+ static const char* get_charcode_name_by_locale(const char* const name)
66
+ {
67
+ if (find_table(name, LOCALE_UTF8_TABLE))
68
+ return NULL;
69
+ else if (find_table(name, LOCALE_EUC_TABLE))
70
+ return CS_EUCJP;
71
+ else if (find_table(name, LOCALE_SJIS_TABLE))
72
+ return CS_SJIS;
73
+ else
74
+ return NULL;
75
+ }
76
+ /*
77
+ * Get better charcode name.
78
+ */
79
+ static const char* get_charcode_name()
80
+ {
81
+ const char* result = NULL;
82
+ const char* lang = NULL;
83
+
84
+ switch(Kcode)
85
+ {
86
+ case 'E':
87
+ result = CS_EUCJP;
88
+ break;
89
+ case 'S':
90
+ #if defined _WIN32 || defined __CYGWIN__
91
+ result = CS_CP932;
92
+ #else
93
+ result = CS_SJIS;
94
+ #endif
95
+ break;
96
+ case 'U':
97
+ //as is.
98
+ break;
99
+ case 'N':
100
+ default:
101
+ #if defined _WIN32 || defined __CYGWIN__
102
+ if (932 == GetACP()) result = CS_CP932;
103
+ #elif defined HAVE_NL_LANGINFO
104
+ setlocale(LC_ALL, "C"); //initialize
105
+ lang = nl_langinfo(CODESET);
106
+ if (find_table(lang, NL_EUC_TABLE))
107
+ result = CS_EUCJP;
108
+ else if (find_table(lang, NL_SJIS_TABLE))
109
+ result = CS_SJIS;
110
+ #elif defined HAVE_SETLOCALE
111
+ setlocale(LC_ALL, "C"); //initialize
112
+ result = get_charcode_name_by_locale(setlocale(LC_ALL, NULL));
113
+ #elif defined HAVE_GETENV
114
+ if (result = get_charcode_name_by_locale(getenv("LC_ALL")))
115
+ ;
116
+ else if (result = get_charcode_name_by_locale(getenv("LC_CTYPE")))
117
+ ;
118
+ else if (result = get_charcode_name_by_locale(getenv("LANG")))
119
+ ;
120
+ #endif
121
+ break;
122
+ }
123
+ return result;
124
+ }
125
+ #endif
126
+
127
+ #if RJB_RUBY_VERSION_CODE < 190
128
+ static void reinit()
129
+ {
130
+ charcode = get_charcode_name();
131
+ if (charcode)
132
+ {
133
+ VALUE rb_iconv_klass = rb_const_get(rb_cObject, rb_intern("Iconv"));
134
+ if (RTEST(rb_iconv_klass)) {
135
+ ID sym_new = rb_intern("new");
136
+ rb_gc_unregister_address(&objIconvR2J);
137
+ objIconvR2J = rb_funcall(rb_iconv_klass, sym_new, 2, rb_str_new2(CS_UTF8), rb_str_new2(charcode));
138
+ rb_gc_register_address(&objIconvR2J);
139
+ rb_gc_unregister_address(&objIconvJ2R);
140
+ objIconvJ2R = rb_funcall(rb_iconv_klass, sym_new, 2, rb_str_new2(charcode), rb_str_new2(CS_UTF8));
141
+ rb_gc_register_address(&objIconvJ2R);
142
+ }
143
+ }
144
+ else
145
+ {
146
+ objIconvR2J = objIconvJ2R = Qnil;
147
+ }
148
+ }
149
+ #endif
150
+
151
+ #if RJB_RUBY_VERSION_CODE < 190
152
+ static void check_kcode()
153
+ {
154
+ VALUE rb_iconv_klass = rb_const_get(rb_cObject, rb_intern("Iconv"));
155
+ VALUE kcode = rb_gv_get("$KCODE");
156
+ if (RTEST(rb_iconv_klass) && TYPE(kcode) == T_STRING) {
157
+ char* kcodep = StringValuePtr(kcode);
158
+ char upper_kcode = toupper(*kcodep);
159
+ if (Kcode != upper_kcode)
160
+ {
161
+ Kcode = upper_kcode;
162
+ reinit();
163
+ }
164
+ }
165
+ else
166
+ {
167
+ objIconvR2J = objIconvJ2R = Qnil;
168
+ }
169
+ }
170
+ #endif
171
+
172
+ VALUE exticonv_local_to_utf8(VALUE local_string)
173
+ {
174
+ #if RJB_RUBY_VERSION_CODE < 190
175
+ check_kcode();
176
+ if(RTEST(objIconvR2J))
177
+ {
178
+ return rb_funcall(objIconvR2J, rb_intern("iconv"), 1, local_string);
179
+ }
180
+ else
181
+ {
182
+ return local_string;
183
+ }
184
+ #else
185
+ VALUE rb_cEncoding, encoding, sjis, eucjp, iso2022jp;
186
+ rb_cEncoding = rb_const_get(rb_cObject, rb_intern("Encoding"));
187
+ sjis = rb_const_get(rb_cEncoding, rb_intern("SHIFT_JIS"));
188
+ eucjp = rb_const_get(rb_cEncoding, rb_intern("EUC_JP"));
189
+ iso2022jp = rb_const_get(rb_cEncoding, rb_intern("ISO_2022_JP"));
190
+ encoding = rb_funcall(local_string, rb_intern("encoding"), 0);
191
+
192
+ if (encoding == sjis || encoding == eucjp || encoding == iso2022jp)
193
+ {
194
+ return rb_funcall(local_string, rb_intern("encode"), 1, rb_str_new2("utf-8"));
195
+ }
196
+ else
197
+ {
198
+ return local_string;
199
+ }
200
+ #endif
201
+ }
202
+
203
+ VALUE exticonv_utf8_to_local(VALUE utf8_string)
204
+ {
205
+ #if RJB_RUBY_VERSION_CODE < 190
206
+ check_kcode();
207
+ if(RTEST(objIconvR2J))
208
+ {
209
+ return rb_funcall(objIconvJ2R, rb_intern("iconv"), 1, utf8_string);
210
+ }
211
+ else
212
+ {
213
+ return utf8_string;
214
+ }
215
+ #else
216
+ return rb_funcall(utf8_string, rb_intern("force_encoding"), 1, rb_str_new2("utf-8"));
217
+ #endif
218
+ }
@@ -1,24 +1,24 @@
1
- /*
2
- * Rjb - Ruby <-> Java Bridge
3
- * Copyright(c) 2004 Kuwashima
4
- *
5
- * This library is free software; you can redistribute it and/or
6
- * modify it under the terms of the GNU Lesser General Public
7
- * License as published by the Free Software Foundation; either
8
- * version 2.1 of the License, or (at your option) any later version.
9
- *
10
- * This library is distributed in the hope that it will be useful,
11
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
- * Lesser General Public License for more details.
14
- *
15
- * $Id: riconv.h 43 2007-12-26 18:55:04Z kuwa1 $
16
- */
17
- #ifndef _RICONV_H
18
- #define _RICONV_H
19
-
20
-
21
- VALUE exticonv_local_to_utf8(VALUE);
22
- VALUE exticonv_utf8_to_local(VALUE);
23
-
24
- #endif /* _RICONV_H */
1
+ /*
2
+ * Rjb - Ruby <-> Java Bridge
3
+ * Copyright(c) 2004 Kuwashima
4
+ *
5
+ * This library is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * This library is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * $Id: riconv.h 43 2007-12-26 18:55:04Z kuwa1 $
16
+ */
17
+ #ifndef _RICONV_H
18
+ #define _RICONV_H
19
+
20
+
21
+ VALUE exticonv_local_to_utf8(VALUE);
22
+ VALUE exticonv_utf8_to_local(VALUE);
23
+
24
+ #endif /* _RICONV_H */