首页 | 数据结构 | 文件列表 | 数据字段 | 全局定义

utils.c

浏览该文件的文档。
00001 
00023 // START OF FILE
00024 /*****************************************************************************/
00025 #include "stdlib.h"             // strtol
00026 #include "limits.h"
00027 #include "string.h"             // strlen
00028 
00029 #ifdef _WIN32
00030 #include "win32dep.h"
00031 #endif
00032 
00033 #include "debug.h"              // gaim_debug
00034 #include "utils.h"
00035 #include "char_conv.h"          // qq_to_utf8
00036 #include "prefs.h"              // gaim_prefs_get_string
00037 
00038 #define QQ_NAME_FORMAT    "qq-%d"
00039 
00040 #ifndef g_str_has_prefix
00041 gint g_str_has_prefix(const gchar *str, const gchar *prefix)
00042 {
00043         gint len = strlen(prefix);
00044         return !strncmp(str, prefix, len);
00045 }
00046 #endif
00047 
00048 /*****************************************************************************/
00049 gchar *get_name_by_index_str(gchar ** array, const gchar * index_str, gint amount) {
00050         gint index;
00051 
00052         index = atoi(index_str);
00053         if (index < 0 || index >= amount)
00054                 index = 0;
00055 
00056         return array[index];
00057 }                               // get_name_by_index_str
00058 
00059 /*****************************************************************************/
00060 gchar *get_index_str_by_name(gchar ** array, const gchar * name, gint amount) {
00061         gint index;
00062 
00063         for (index = 0; index <= amount; index++)
00064                 if (g_ascii_strcasecmp(array[index], name) == 0)
00065                         break;
00066 
00067         if (index >= amount)
00068                 index = 0;      // meaning no match
00069         return g_strdup_printf("%d", index);
00070 }                               // get_index_str_by_name
00071 
00072 /*****************************************************************************/
00073 gint qq_string_to_dec_value(const gchar * str)
00074 {
00075         g_return_val_if_fail(str != NULL, 0);
00076         return strtol(str, NULL, 10);
00077 }                               // _qq_string_to_dec_value
00078 
00079 /*****************************************************************************/
00080 // split the given data(len) with delimit,
00081 // check the number of field matches the expected_fields (<=0 means all)
00082 // return gchar* array (needs to be freed by g_strfreev later), or NULL
00083 gchar **split_data(guint8 * data, gint len, const gchar * delimit, gint expected_fields) {
00084 
00085         guint8 *input;
00086         gchar **segments;
00087         gint i, j;
00088 
00089         g_return_val_if_fail(data != NULL && len != 0 && delimit != 0, NULL);
00090 
00091         // as the last field would be string, but data is not ended with 0x00
00092         // we have to duplicate the data and append a 0x00 at the end
00093         input = g_newa(guint8, len + 1);
00094         g_memmove(input, data, len);
00095         input[len] = 0x00;
00096 
00097         segments = g_strsplit(input, delimit, 0);
00098         if (expected_fields <= 0)
00099                 return segments;
00100 
00101         for (i = 0; segments[i] != NULL; i++) {;
00102         }
00103         if (i < expected_fields) {      // not enough fields
00104                 gaim_debug(GAIM_DEBUG_ERROR, "QQ",
00105                            "Invalid data, expect %d fields, found only %d, discard\n", expected_fields, i);
00106                 g_strfreev(segments);
00107                 return NULL;
00108         } else if (i > expected_fields) {       // more fields, OK
00109                 gaim_debug(GAIM_DEBUG_WARNING, "QQ",
00110                            "Dangerous data, expect %d fields, found %d, return all\n", expected_fields, i);
00111                 segments[expected_fields] = NULL;
00112                 // free up those not used
00113                 for (j = expected_fields + 1; j < i; j++)
00114                         g_free(segments[i]);
00115         }                       // if i
00116 
00117         return segments;
00118 }                               // split_data
00119 
00120 /*****************************************************************************/
00121 // given a four-byte ip data, convert it into a human readable ip string
00122 // the return needs to be freed
00123 gchar *gen_ip_str(guint8 * ip)
00124 {
00125         return g_strdup_printf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
00126 }                               // gen_ip_str
00127 
00128 /*****************************************************************************/
00129 // return the QQ icon file name
00130 // the return needs to be freed
00131 gchar *get_icon_name(gint set, gint suffix)
00132 {
00133         return g_strdup_printf("qq_%d-%d", set, suffix);
00134 }                               // get_icon_name
00135 
00136 /*****************************************************************************/
00137 // convert a QQ UID to a unique name of GAIM
00138 // the return needs to be freed
00139 gchar *uid_to_gaim_name(guint32 uid)
00140 {
00141         return g_strdup_printf(QQ_NAME_FORMAT, uid);
00142 }                               // uid_to_gaim_name
00143 
00144 /*****************************************************************************/
00145 // convert GAIM name to original QQ UID
00146 guint32 gaim_name_to_uid(const gchar * name)
00147 {
00148         gchar *p;
00149 
00150         g_return_val_if_fail(g_str_has_prefix(name, QQ_NAME_PREFIX), 0);
00151 
00152         p = g_strrstr(name, QQ_NAME_PREFIX);
00153         // atoi is not thread-safe and also not async-cancel safe
00154         // atoi is deprecated by strtol() and should not be used in new code
00155         return (p == NULL) ? 0 : strtol(p + strlen(QQ_NAME_PREFIX), NULL, 10);
00156 }                               // gaim_name_to_uid
00157 
00158 /*****************************************************************************/
00159 // convert QQ icon index into its pixbuf
00160 GdkPixbuf *get_face_gdkpixbuf(guint8 index)
00161 {
00162         gint set, suffix;
00163         gchar *image_name, *file_name;
00164         GdkPixbuf *pixbuf;
00165         const gchar *datadir;
00166 
00167         set = (index / 3) + 1;
00168         suffix = (index % 3) + 1;
00169 
00170         image_name = g_strdup_printf("%s.png", get_icon_name(set, suffix));
00171         // we need to configure DATADIR in Makefile.am
00172         // st = -DDATADIR=\"$(datadir)\"
00173         datadir = gaim_prefs_get_string("/plugins/prpl/qq/datadir");
00174         if (datadir == NULL || strlen(datadir) == 0)
00175                 file_name = g_build_filename(datadir, "pixmaps", "gaim", "status", "default", image_name, NULL);
00176         else
00177                 file_name = g_build_filename(DATADIR, "pixmaps", "gaim", "status", "default", image_name, NULL);
00178 
00179         pixbuf = gdk_pixbuf_new_from_file(file_name, NULL);
00180 
00181         g_free(image_name);
00182         g_free(file_name);
00183 
00184         return pixbuf;
00185 }                               // get_face_gdkpixbuf
00186 
00187 /*****************************************************************************/
00188 // try to dump the data as GBK
00189 void try_dump_as_gbk(guint8 * data, gint len)
00190 {
00191         gint i;
00192         guint8 *incoming;
00193         gchar *msg_utf8;
00194 
00195         incoming = g_newa(guint8, len + 1);
00196         g_memmove(incoming, data, len);
00197         incoming[len] = 0x00;
00198         // GBK code: 
00199         // Single-byte ASCII:      0x21-0x7E
00200         // GBK first byte range:   0x81-0xFE
00201         // GBK second byte range:  0x40-0x7E and 0x80-0xFE
00202         for (i = 0; i < len; i++)
00203                 if (incoming[i] >= 0x81)
00204                         break;
00205 
00206         msg_utf8 = i < len ? qq_to_utf8(&incoming[i], QQ_CHARSET_DEFAULT) : NULL;
00207 
00208         if (msg_utf8 != NULL) {
00209                 gaim_debug(GAIM_DEBUG_WARNING, "QQ", "Try extract GB msg: %s\n", msg_utf8);
00210                 g_free(msg_utf8);
00211         }                       // msg_utf8 != NULL
00212 }                               // try_dump_gbk
00213 
00214 /*****************************************************************************/
00215 // dump a chunk of raw data into hex string
00216 // the return should be freed later
00217 gchar *hex_dump_to_str(const guint8 * buffer, gint bytes)
00218 {
00219         GString *str;
00220         gchar *ret;
00221         gint i, j, ch;
00222 
00223         str = g_string_new("");
00224         for (i = 0; i < bytes; i += 16) {
00225                 // length label
00226                 g_string_append_printf(str, "%04d: ", i);
00227 
00228                 // dump hex value
00229                 for (j = 0; j < 16; j++)
00230                         if ((i + j) < bytes)
00231                                 g_string_append_printf(str, " %02X", buffer[i + j]);
00232                         else
00233                                 g_string_append(str, "   ");
00234                 g_string_append(str, "  ");
00235 
00236                 // dump ascii value
00237                 for (j = 0; j < 16 && (i + j) < bytes; j++) {
00238                         ch = buffer[i + j] & 127;
00239                         if (ch < ' ' || ch == 127)
00240                                 g_string_append_c(str, '.');
00241                         else
00242                                 g_string_append_c(str, ch);
00243                 }               // for j
00244                 g_string_append_c(str, '\n');
00245         }                       // for i
00246 
00247         ret = str->str;
00248         // GString can be freed without freeing it character data
00249         g_string_free(str, FALSE);
00250 
00251         return ret;
00252 }                               // hex_dump_to_str
00253 
00254 /*****************************************************************************/
00255 // ENF OF FILE

Generated at Mon May 8 15:41:24 2006 for OpenQ by  doxygen 1.4.4