Mercurial > pidgin
comparison libpurple/protocols/qq/char_conv.c @ 30412:41906308232b
Correct me if I'm wrong, but why would the length be in UTF8, when
the string is not?
author | Elliott Sales de Andrade <qulogic@pidgin.im> |
---|---|
date | Sat, 14 Aug 2010 03:55:19 +0000 |
parents | f1437342cc0e |
children |
comparison
equal
deleted
inserted
replaced
30411:cd022bd83677 | 30412:41906308232b |
---|---|
35 | 35 |
36 #define QQ_NULL_MSG "(NULL)" /* return this if conversion fails */ | 36 #define QQ_NULL_MSG "(NULL)" /* return this if conversion fails */ |
37 | 37 |
38 /* convert a string from from_charset to to_charset, using g_convert */ | 38 /* convert a string from from_charset to to_charset, using g_convert */ |
39 /* Warning: do not return NULL */ | 39 /* Warning: do not return NULL */ |
40 static gchar *do_convert(const gchar *str, gssize len, const gchar *to_charset, const gchar *from_charset) | 40 static gchar *do_convert(const gchar *str, gssize len, guint8 *out_len, const gchar *to_charset, const gchar *from_charset) |
41 { | 41 { |
42 GError *error = NULL; | 42 GError *error = NULL; |
43 gchar *ret; | 43 gchar *ret; |
44 gsize byte_read, byte_write; | 44 gsize byte_read, byte_write; |
45 | 45 |
46 g_return_val_if_fail(str != NULL && to_charset != NULL && from_charset != NULL, g_strdup(QQ_NULL_MSG)); | 46 g_return_val_if_fail(str != NULL && to_charset != NULL && from_charset != NULL, g_strdup(QQ_NULL_MSG)); |
47 | 47 |
48 ret = g_convert(str, len, to_charset, from_charset, &byte_read, &byte_write, &error); | 48 ret = g_convert(str, len, to_charset, from_charset, &byte_read, &byte_write, &error); |
49 | 49 |
50 if (error == NULL) { | 50 if (error == NULL) { |
51 if (out_len) | |
52 *out_len = byte_write; | |
51 return ret; /* convert is OK */ | 53 return ret; /* convert is OK */ |
52 } | 54 } |
53 | 55 |
54 /* convert error */ | 56 /* convert error */ |
55 purple_debug_error("QQ_CONVERT", "%s\n", error->message); | 57 purple_debug_error("QQ_CONVERT", "%s\n", error->message); |
65 * the converted UTF-8 will be saved in ret | 67 * the converted UTF-8 will be saved in ret |
66 * Return: *ret != NULL | 68 * Return: *ret != NULL |
67 */ | 69 */ |
68 gint qq_get_vstr(gchar **ret, const gchar *from_charset, guint8 *data) | 70 gint qq_get_vstr(gchar **ret, const gchar *from_charset, guint8 *data) |
69 { | 71 { |
70 guint8 len; | 72 gssize len; |
73 guint8 out_len; | |
71 | 74 |
72 g_return_val_if_fail(data != NULL && from_charset != NULL, -1); | 75 g_return_val_if_fail(data != NULL && from_charset != NULL, -1); |
73 | 76 |
74 len = data[0]; | 77 len = data[0]; |
75 if (len == 0) { | 78 if (len == 0) { |
76 *ret = g_strdup(""); | 79 *ret = g_strdup(""); |
77 return 1; | 80 return 1; |
78 } | 81 } |
79 *ret = do_convert((gchar *) (data + 1), (gssize) len, UTF8, from_charset); | 82 *ret = do_convert((gchar *) (data + 1), len, &out_len, UTF8, from_charset); |
80 | 83 |
81 return len + 1; | 84 return out_len + 1; |
82 } | 85 } |
83 | 86 |
84 gint qq_put_vstr(guint8 *buf, const gchar *str_utf8, const gchar *to_charset) | 87 gint qq_put_vstr(guint8 *buf, const gchar *str_utf8, const gchar *to_charset) |
85 { | 88 { |
86 gchar *str; | 89 gchar *str; |
87 guint8 len; | 90 guint8 len; |
88 | 91 |
89 if (str_utf8 == NULL || (len = strlen(str_utf8)) == 0) { | 92 if (str_utf8 == NULL || str_utf8[0] == '\0') { |
90 buf[0] = 0; | 93 buf[0] = 0; |
91 return 1; | 94 return 1; |
92 } | 95 } |
93 str = do_convert(str_utf8, -1, to_charset, UTF8); | 96 str = do_convert(str_utf8, -1, &len, to_charset, UTF8); |
94 len = strlen(str_utf8); | |
95 buf[0] = len; | 97 buf[0] = len; |
96 if (len > 0) { | 98 if (len > 0) { |
97 memcpy(buf + 1, str, len); | 99 memcpy(buf + 1, str, len); |
98 } | 100 } |
99 return 1 + len; | 101 return 1 + len; |
100 } | 102 } |
101 | 103 |
102 /* Warning: do not return NULL */ | 104 /* Warning: do not return NULL */ |
103 gchar *utf8_to_qq(const gchar *str, const gchar *to_charset) | 105 gchar *utf8_to_qq(const gchar *str, const gchar *to_charset) |
104 { | 106 { |
105 return do_convert(str, -1, to_charset, UTF8); | 107 return do_convert(str, -1, NULL, to_charset, UTF8); |
106 } | 108 } |
107 | 109 |
108 /* Warning: do not return NULL */ | 110 /* Warning: do not return NULL */ |
109 gchar *qq_to_utf8(const gchar *str, const gchar *from_charset) | 111 gchar *qq_to_utf8(const gchar *str, const gchar *from_charset) |
110 { | 112 { |
111 return do_convert(str, -1, UTF8, from_charset); | 113 return do_convert(str, -1, NULL, UTF8, from_charset); |
112 } | 114 } |
113 | 115 |