Index: client/gui-gtk-2.0/gui_main.c =================================================================== RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/gui_main.c,v retrieving revision 1.5 diff -u -r1.5 gui_main.c --- client/gui-gtk-2.0/gui_main.c 2002/04/05 05:56:44 1.5 +++ client/gui-gtk-2.0/gui_main.c 2002/05/14 19:24:26 @@ -722,6 +722,44 @@ } /************************************************************************** +... +**************************************************************************/ +static unsigned char *put_conv(unsigned char *dst, const char *src) +{ + gsize len; + gchar *out = g_locale_from_utf8(src, -1, NULL, &len, NULL); + + memcpy(dst, out, len); + dst[len] = '\0'; + g_free(out); + + return dst + len + 1; +} + +/************************************************************************** + Returns FALSE if the destination isn't large enough or the source was + bad. +**************************************************************************/ +static bool iget_conv(char *dst, size_t ndst, const unsigned char *src, + size_t nsrc) +{ + gsize len; /* length to copy, not including null */ + gchar *out = g_locale_to_utf8(src, nsrc, NULL, &len, NULL); + bool ret = TRUE; + + if (ndst > 0 && len >= ndst) { + ret = FALSE; + len = ndst - 1; + } + + memcpy(dst, out, len); + dst[len] = '\0'; + g_free(out); + + return ret; +} + +/************************************************************************** called from main(), is what it's named. **************************************************************************/ void ui_main(int argc, char **argv) @@ -730,14 +768,12 @@ GtkStyle *has_resources; PangoLanguage *lang; - parse_options(argc, argv); + /* set networking string conversion callbacks */ + put_conv_set_callback(put_conv); + iget_conv_set_callback(iget_conv); - /* tell GTK+ library which locale */ + parse_options(argc, argv); -#ifdef HAVE_LOCALE_H - setlocale(LC_CTYPE, "en_US.UTF-8"); - gtk_disable_setlocale(); -#endif /* GTK withdraw gtk options. Process GTK arguments */ gtk_init(&argc, &argv); Index: common/packets.c =================================================================== RCS file: /home/freeciv/CVS/freeciv/common/packets.c,v retrieving revision 1.205 diff -u -r1.205 packets.c --- common/packets.c 2002/04/25 14:09:37 1.205 +++ common/packets.c 2002/05/14 19:24:29 @@ -1159,14 +1159,70 @@ /************************************************************************** ... **************************************************************************/ +static unsigned char *put_conv(unsigned char *dst, const char *src) +{ + size_t len = strlen(src) + 1; + + memcpy(dst, src, len); + return dst + len; +} + +/************************************************************************** +... +**************************************************************************/ +static PUT_CONV_FUN put_conv_callback = put_conv; + +/************************************************************************** +... +**************************************************************************/ +void put_set_callback(PUT_CONV_FUN fun) +{ + put_conv_callback = fun; +} + +/************************************************************************** +... +**************************************************************************/ unsigned char *put_string(unsigned char *buffer, const char *mystring) +{ + return (*put_conv_callback) (buffer, mystring); +} + +/************************************************************************** + Returns FALSE if the destination isn't large enough or the source was + bad. +**************************************************************************/ +static bool iget_conv(char *dst, size_t ndst, const unsigned char *src, + size_t nsrc) { - int len = strlen(mystring) + 1; - memcpy(buffer, mystring, len); - return buffer+len; + size_t len = nsrc; /* length to copy, not including null */ + bool ret = TRUE; + + if (ndst > 0 && len >= ndst) { + ret = FALSE; + len = ndst - 1; + } + + memcpy(dst, src, len); + dst[len] = '\0'; + + return ret; } /************************************************************************** +... +**************************************************************************/ +static IGET_CONV_FUN iget_conv_callback = iget_conv; + +/************************************************************************** +... +**************************************************************************/ +void iget_conv_set_callback(IGET_CONV_FUN fun) +{ + iget_conv_callback = fun; +} + +/************************************************************************** Like old get_string, but using a pack_iter, and navail is the memory available in mystring. If string in packet is too long, a truncated string is written into mystring (and set piter->bad_string). @@ -1179,7 +1235,6 @@ { unsigned char *c; int ps_len; /* length in packet, not including null */ - int len; /* length to copy, not including null */ assert(piter != NULL); assert((navail>0) || (mystring==NULL)); @@ -1202,15 +1257,12 @@ } else { ps_len = c - piter->ptr; } - len = ps_len; - if (mystring && navail > 0 && ps_len >= navail) { + + if (mystring + && !(*iget_conv_callback) (mystring, navail, piter->ptr, ps_len)) { piter->bad_string = TRUE; - len = navail-1; } - if (mystring) { - memcpy(mystring, piter->ptr, len); - mystring[len] = '\0'; - } + if (!piter->short_packet) { piter->ptr += (ps_len+1); /* past terminator */ } Index: common/packets.h =================================================================== RCS file: /home/freeciv/CVS/freeciv/common/packets.h,v retrieving revision 1.115 diff -u -r1.115 packets.h --- common/packets.h 2002/05/14 18:53:34 1.115 +++ common/packets.h 2002/05/14 19:24:30 @@ -895,6 +895,14 @@ Nation_Type_id nations_used[MAX_NUM_PLAYERS]; }; +/* network string conversion */ +typedef unsigned char *(*PUT_CONV_FUN) (unsigned char *dst, const char *src); +void put_set_callback(PUT_CONV_FUN fun); + +typedef bool(*IGET_CONV_FUN) (char *dst, size_t ndst, + const unsigned char *src, size_t nsrc); +void iget_conv_set_callback(IGET_CONV_FUN fun); + /* These two are non-static for meta.c; others are now static --dwp */ unsigned char *put_uint16(unsigned char *buffer, int val); unsigned char *put_string(unsigned char *buffer, const char *mystring);