Mercurial > gftp.yaz
annotate lib/charset-conv.c @ 992:9c583f570950 default tip
more character set conversion on remote file names.
author | Yoshiki Yazawa <yaz@honeyplanet.jp> |
---|---|
date | Wed, 15 Sep 2010 11:42:57 +0900 |
parents | c99b134c6185 |
children |
rev | line source |
---|---|
950 | 1 /*****************************************************************************/ |
2 /* charset-conv.c - contains functions for performing conversions between */ | |
3 /* character sets. */ | |
4 /* Copyright (C) 1998-2008 Brian Masney <masneyb@gftp.org> */ | |
5 /* */ | |
6 /* This program is free software; you can redistribute it and/or modify */ | |
7 /* it under the terms of the GNU General Public License as published by */ | |
8 /* the Free Software Foundation; either version 2 of the License, or */ | |
9 /* (at your option) any later version. */ | |
10 /* */ | |
11 /* This program 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 General Public License for more details. */ | |
15 /* */ | |
16 /* You should have received a copy of the GNU General Public License */ | |
17 /* along with this program; if not, write to the Free Software */ | |
18 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA */ | |
19 /*****************************************************************************/ | |
20 | |
21 #include "gftp.h" | |
989 | 22 #include <langinfo.h> |
950 | 23 static const char cvsid[] = "$Id: protocols.c 952 2008-01-24 23:31:26Z masneyb $"; |
24 | |
25 static /*@null@*/ char * | |
26 _gftp_get_next_charset (char **curpos) | |
27 { | |
28 char *ret, *endpos; | |
29 size_t len, retlen; | |
30 | |
31 if (**curpos == '\0') | |
32 return (NULL); | |
33 | |
34 for (; **curpos == ' ' || **curpos == '\t'; (*curpos)++); | |
35 | |
36 if ((endpos = strchr (*curpos, ',')) == NULL) | |
37 len = strlen (*curpos) - 1; /* the trailing ',' should be omitted */ | |
38 else | |
39 len = endpos - *curpos; | |
40 | |
41 for (retlen = len - 1; | |
42 (*curpos)[retlen - 1] == ' ' || (*curpos)[retlen - 1] == '\t'; | |
43 retlen--); | |
44 | |
45 retlen++; /* Needed due to the len - 1 above... */ | |
46 ret = g_malloc0 (retlen + 1); | |
47 memcpy (ret, *curpos, retlen); | |
48 | |
49 for (*curpos += len; **curpos == ','; (*curpos)++); | |
50 | |
51 return (ret); | |
52 } | |
53 | |
54 | |
55 static void | |
56 _do_show_iconv_error (const char *str, char *charset, int from_utf8, | |
57 GError * error) | |
58 { | |
59 const char *fromset, *toset; | |
60 | |
61 if (from_utf8) | |
62 { | |
63 fromset = "UTF-8"; | |
64 toset = charset; | |
65 } | |
66 else | |
67 { | |
68 fromset = charset; | |
69 toset = "UTF-8"; | |
70 } | |
71 | |
72 printf (_("Error converting string '%s' from character set %s to character set %s: %s\n"), | |
73 str, fromset, toset, error->message); | |
74 } | |
75 | |
989 | 76 |
77 char * | |
78 gftp_string_to_utf8 (gftp_request *request, const char *str, size_t *dest_len) | |
950 | 79 { |
989 | 80 gchar *remote_charset = NULL, *default_charset = NULL, *ret; |
81 GError *error = NULL; | |
988
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
82 |
989 | 83 gftp_lookup_global_option ("default_charset", &default_charset); |
84 if(!default_charset) | |
85 default_charset = "UTF-8"; | |
950 | 86 |
989 | 87 if(request) |
88 remote_charset = request->remote_charset; | |
89 if(!remote_charset) | |
90 remote_charset = default_charset; | |
950 | 91 |
989 | 92 ret = g_convert_with_fallback(str, -1, "UTF-8", remote_charset, "?", NULL, dest_len, &error); |
93 | |
94 return ret; | |
95 } | |
950 | 96 |
97 | |
989 | 98 char * |
99 gftp_string_from_utf8 (gftp_request *request, int force_local, const char *str, | |
100 size_t *dest_len) | |
101 { | |
102 gchar *remote_charset = NULL, *default_charset = NULL, *ret; | |
103 GError *error = NULL; | |
950 | 104 |
989 | 105 gftp_lookup_global_option ("default_charset", &default_charset); |
106 if(!default_charset) | |
107 default_charset = "UTF-8"; | |
950 | 108 |
989 | 109 if(request) |
110 remote_charset = request->remote_charset; | |
111 if(!remote_charset) | |
112 remote_charset = default_charset; | |
950 | 113 |
989 | 114 ret = g_convert_with_fallback(str, -1, remote_charset, "UTF-8", "?", NULL, dest_len, &error); |
950 | 115 |
989 | 116 return ret; |
950 | 117 } |
118 | |
988
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
119 |
950 | 120 char * |
989 | 121 gftp_filename_to_utf8 (gftp_request *request, const char *str, |
950 | 122 size_t *dest_len) |
123 { | |
989 | 124 char *codeset = nl_langinfo(CODESET); |
125 gchar *ret; | |
126 GError *error = NULL; | |
127 | |
128 ret = g_filename_to_utf8 (str, -1, NULL, dest_len, &error); | |
129 if(!ret) { | |
130 error = NULL; | |
131 ret = g_convert_with_fallback(str, -1, "UTF-8", codeset, "?", NULL, dest_len, &error); | |
132 } | |
133 | |
134 if(!ret) { | |
135 error = NULL; | |
136 ret = g_strdup(str); | |
137 } | |
138 | |
139 return ret; | |
950 | 140 } |
141 | |
142 | |
143 char * | |
989 | 144 gftp_filename_from_utf8 (gftp_request *request, const char *str, |
145 size_t *dest_len) | |
950 | 146 { |
989 | 147 char *codeset = nl_langinfo(CODESET); |
148 gchar *ret; | |
149 GError *error = NULL; | |
150 | |
151 ret = g_filename_from_utf8 (str, -1, NULL, dest_len, &error); | |
152 if(!ret) { | |
153 error = NULL; | |
154 ret = g_convert_with_fallback(str, -1, codeset, "UTF-8", "?", NULL, dest_len, &error); | |
155 } | |
156 | |
157 return ret; | |
950 | 158 } |
159 | |
160 | |
161 char * | |
989 | 162 gftp_remote_filename_to_utf8 (gftp_request *request, const char *str, |
988
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
163 size_t *dest_len) |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
164 { |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
165 char *remote_charset = NULL, *default_charset = NULL; |
989 | 166 gchar *ret; |
988
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
167 GError *error = NULL; |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
168 |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
169 if(request == NULL) |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
170 return (NULL); |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
171 |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
172 /* get remote_charset */ |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
173 remote_charset = request->remote_charset; |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
174 gftp_lookup_global_option ("default_charset", &default_charset); |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
175 if(!remote_charset) |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
176 remote_charset = (default_charset && *default_charset != '\0') |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
177 ? default_charset : "UTF8"; |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
178 |
989 | 179 ret = g_convert(str, -1, "UTF8", remote_charset, NULL, dest_len, &error); |
988
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
180 return ret; |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
181 } |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
182 |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
183 |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
184 char * |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
185 gftp_remote_filename_from_utf8 (gftp_request *request, const char *str, |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
186 size_t *dest_len) |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
187 { |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
188 char *remote_charset = NULL, *default_charset = NULL; |
989 | 189 gchar *ret; |
988
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
190 GError *error = NULL; |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
191 |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
192 if(request == NULL) |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
193 return (NULL); |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
194 |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
195 if(!g_utf8_validate (str, -1, NULL)) |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
196 return (NULL); |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
197 |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
198 /* get remote_charset */ |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
199 remote_charset = request->remote_charset; |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
200 gftp_lookup_global_option ("default_charset", &default_charset); |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
201 if(!remote_charset) |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
202 remote_charset = (default_charset && *default_charset != '\0') |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
203 ? default_charset : "UTF8"; |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
204 |
989 | 205 ret = g_convert(str, -1, remote_charset, "UTF8", NULL, dest_len, &error); |
988
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
206 return ret; |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
207 } |