Complete.Org: Mailing Lists: Archives: freeciv-dev: November 2004:
[Freeciv-Dev] (PR#11169) unicode "support" in fciconv
Home

[Freeciv-Dev] (PR#11169) unicode "support" in fciconv

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [Freeciv-Dev] (PR#11169) unicode "support" in fciconv
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 23 Nov 2004 16:28:45 -0800
Reply-to: rt@xxxxxxxxxxx

<URL: http://rt.freeciv.org/Ticket/Display.html?id=11169 >

This patch adds some unicode code to fciconv.

I was originally going to use this to fix create_centered_string. 
However instead I think create_centered_string should just be deleted. 
Perhaps it will instead be useful inside ftwl's text-rendering engine, 
or in wordwrap_string, or somewhere else.

jason

Index: utility/fciconv.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/utility/fciconv.c,v
retrieving revision 1.10
diff -u -r1.10 fciconv.c
--- utility/fciconv.c   13 Nov 2004 09:16:59 -0000      1.10
+++ utility/fciconv.c   23 Nov 2004 23:59:32 -0000
@@ -177,15 +177,21 @@
 
   Don't use this function if you can avoid it.  Use one of the
   xxx_to_yyy_string functions.
+
+  Note: from_len gives the size of the "from" text, in bytes.  "from" may
+  be use 32-bit characters in which case
+  from_len == 4 * (wstrlen(from) + 1).  However when converting with
+  unicode do not use the malloc option (make sure to pass a buffer in).
 ***************************************************************************/
-char *convert_string(const char *text,
-                    const char *from,
-                    const char *to,
-                    char *buf, size_t bufsz)
+static char *convert_string(const char *text,
+                           size_t text_sz,
+                           const char *from,
+                           const char *to,
+                           char *buf, size_t bufsz)
 {
 #ifdef HAVE_ICONV
   iconv_t cd = iconv_open(to, from);
-  size_t from_len = strlen(text) + 1, to_len;
+  size_t to_len;
   bool alloc = (buf == NULL);
 
   assert(is_init && from != NULL && to != NULL);
@@ -205,13 +211,13 @@
   }
 
   if (alloc) {
-    to_len = from_len;
+    to_len = text_sz;
   } else {
     to_len = bufsz;
   }
 
   do {
-    size_t flen = from_len, tlen = to_len, res;
+    size_t flen = text_sz, tlen = to_len, res;
     const char *mytext = text;
     char *myresult;
 
@@ -274,7 +280,7 @@
 #define CONV_FUNC_MALLOC(src, dst)                                          \
 char *src ## _to_ ## dst ## _string_malloc(const char *text)                \
 {                                                                           \
-  return convert_string(text, (src ## _encoding),                          \
+  return convert_string(text, strlen(text) + 1, (src ## _encoding),        \
                        (dst ## _encoding), NULL, 0);                       \
 }
 
@@ -282,7 +288,7 @@
 char *src ## _to_ ## dst ## _string_buffer(const char *text,                \
                                           char *buf, size_t bufsz)         \
 {                                                                           \
-  return convert_string(text, (src ## _encoding),                          \
+  return convert_string(text, strlen(text) + 1, (src ## _encoding),        \
                         (dst ## _encoding), buf, bufsz);                    \
 }
 
@@ -304,6 +310,42 @@
 static CONV_FUNC_STATIC(internal, local)
 
 /***************************************************************************
+  Convert from unicode into the internal encoding.
+***************************************************************************/
+char *unicode_to_internal_string_static(const fc_unicode *text)
+{
+  return convert_string((const char *)text,
+                       (unicode_strlen(text) + 1) * sizeof(*text),
+                       internal_encoding, "UCS-4",
+                       convert_buffer, sizeof(convert_buffer));
+}
+
+/***************************************************************************
+  Convert from the internal encoding into unicode.
+***************************************************************************/
+fc_unicode *internal_to_unicode_string_static(const char *text)
+{
+  return (fc_unicode *)convert_string(text, strlen(text) + 1,
+                                     internal_encoding, "UCS-4",
+                                     convert_buffer,
+                                     sizeof(convert_buffer));
+}
+
+/***************************************************************************
+  Return the length in characters of a unicode (UCS-4) string.
+***************************************************************************/
+size_t unicode_strlen(const fc_unicode *text)
+{
+  size_t i;
+
+  for (i = 0; text[i] != '\0'; i++) {
+    /* nothing */
+  }
+
+  return i;
+}
+
+/***************************************************************************
   Do a fprintf from the internal charset into the local charset.
 ***************************************************************************/
 void fc_fprintf(FILE *stream, const char *format, ...)
Index: utility/fciconv.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/utility/fciconv.h,v
retrieving revision 1.7
diff -u -r1.7 fciconv.h
--- utility/fciconv.h   13 Nov 2004 09:16:59 -0000      1.7
+++ utility/fciconv.h   23 Nov 2004 23:59:32 -0000
@@ -30,13 +30,14 @@
 char *internal_to_local_string_malloc(const char *text);
 char *local_to_internal_string_malloc(const char *text);
 
+typedef unsigned int fc_unicode;
+char *unicode_to_internal_string_static(const fc_unicode *text);
+fc_unicode *internal_to_unicode_string_static(const char *text);
+
+size_t unicode_strlen(const fc_unicode *text);
+
 #define fc_printf(...) fc_fprintf(stdout, __VA_ARGS__)
 void fc_fprintf(FILE *stream, const char *format, ...)
       fc__attribute((format (printf, 2, 3)));
 
-char *convert_string(const char *text,
-                    const char *from,
-                    const char *to,
-                    char *buf, size_t bufsz);
-
 #endif /* FC__FCICONV_H */
Index: utility/shared.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/utility/shared.c,v
retrieving revision 1.120
diff -u -r1.120 shared.c
--- utility/shared.c    13 Nov 2004 09:26:52 -0000      1.120
+++ utility/shared.c    23 Nov 2004 23:59:33 -0000
@@ -81,21 +81,23 @@
   FIXME: This is only used in the Xaw client, and so probably does
   not belong in common.
 ***************************************************************/
-char *create_centered_string(const char *s)
+char *create_centered_string(const char *text)
 {
+  fc_unicode *s = internal_to_unicode_string_static(text);
+
   /* Points to the part of the source that we're looking at. */
-  const char *cp;
+  fc_unicode *cp;
 
   /* Points to the beginning of the line in the source that we're
    * looking at. */
-  const char *cp0;
+  fc_unicode *cp0;
 
   /* Points to the result. */
-  char *r;
+  fc_unicode *r;
 
   /* Points to the part of the result that we're filling in right
      now. */
-  char *rn;
+  fc_unicode *rn;
 
   int i;
 
@@ -116,7 +118,7 @@
   if(maxlen<curlen)
     maxlen=curlen;
   
-  r=rn=fc_malloc(nlines*(maxlen+1));
+  r=rn=s;
   
   curlen=0;
   for(cp0=cp=s; *cp != '\0'; cp++) {
@@ -125,7 +127,7 @@
     else {
       for(i=0; i<(maxlen-curlen)/2; i++)
        *rn++=' ';
-      memcpy(rn, cp0, curlen);
+      memcpy(rn, cp0, curlen * sizeof(fc_unicode));
       rn+=curlen;
       *rn++='\n';
       curlen=0;

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#11169) unicode "support" in fciconv, Jason Short <=