Complete.Org: Mailing Lists: Archives: freeciv-dev: May 2004:
[Freeciv-Dev] (PR#8664) gettext and const char *'s
Home

[Freeciv-Dev] (PR#8664) gettext and const char *'s

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#8664) gettext and const char *'s
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 3 May 2004 21:56:58 -0700
Reply-to: rt@xxxxxxxxxxx

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

We should probably treat every gettext-returned string as a const char * 
  rather than a char *.  (This may or may not relate to PR#8661.)

The attached -test patch changes fcintl.h to force a const char * for 
these strings.  (This is not intended to be applied to cvs, just as a 
testing tool.)

The second patch attached makes the necessary changes so that common/ 
conforms to this restriction.  I also changed get_activity_text to take 
an enum activity_type, as it should.

jason

? test.diff
Index: utility/fcintl.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/utility/fcintl.h,v
retrieving revision 1.9
diff -u -r1.9 fcintl.h
--- utility/fcintl.h    4 Apr 2003 15:47:49 -0000       1.9
+++ utility/fcintl.h    4 May 2004 04:11:10 -0000
@@ -25,10 +25,10 @@
 #include <locale.h>
 #endif
 
-#define _(String) gettext(String)
+#define _(String) (const char *)gettext(String)
 #define N_(String) String
-#define Q_(String) skip_intl_qualifier_prefix(gettext(String))
-#define PL_(String1, String2, n) ngettext((String1), (String2), (n))
+#define Q_(String) (conast char *)skip_intl_qualifier_prefix(gettext(String))
+#define PL_(String1, String2, n) (const char *)ngettext((String1), (String2), 
(n))
 
 #else
 
Index: common/city.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/city.c,v
retrieving revision 1.206
diff -u -r1.206 city.c
--- common/city.c       22 Apr 2004 22:58:28 -0000      1.206
+++ common/city.c       4 May 2004 04:55:26 -0000
@@ -256,7 +256,7 @@
 const char *get_impr_name_ex(struct city *pcity, Impr_Type_id id)
 {
   static char buffer[256];
-  char *state = NULL;
+  const char *state = NULL;
 
   if (pcity) {
     switch (pcity->improvements[id]) {
Index: common/unit.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/unit.c,v
retrieving revision 1.206
diff -u -r1.206 unit.c
--- common/unit.c       29 Apr 2004 19:59:21 -0000      1.206
+++ common/unit.c       4 May 2004 04:55:26 -0000
@@ -566,31 +566,51 @@
 /**************************************************************************
 Return name of activity in static buffer
 **************************************************************************/
-char* get_activity_text (int activity)
+const char *get_activity_text(enum unit_activity activity)
 {
-  char *text;
-
+  /* The switch statement has just the activities listed with no "default"
+   * handling.  This enables the compiler to detect missing entries
+   * automatically, and still handles everything correctly. */
   switch (activity) {
-  case ACTIVITY_IDLE:          text = _("Idle"); break;
-  case ACTIVITY_POLLUTION:     text = _("Pollution"); break;
-  case ACTIVITY_ROAD:          text = _("Road"); break;
-  case ACTIVITY_MINE:          text = _("Mine"); break;
-  case ACTIVITY_IRRIGATE:      text = _("Irrigation"); break;
-  case ACTIVITY_FORTIFYING:    text = _("Fortifying"); break;
-  case ACTIVITY_FORTIFIED:     text = _("Fortified"); break;
-  case ACTIVITY_FORTRESS:      text = _("Fortress"); break;
-  case ACTIVITY_SENTRY:                text = _("Sentry"); break;
-  case ACTIVITY_RAILROAD:      text = _("Railroad"); break;
-  case ACTIVITY_PILLAGE:       text = _("Pillage"); break;
-  case ACTIVITY_GOTO:          text = _("Goto"); break;
-  case ACTIVITY_EXPLORE:       text = _("Explore"); break;
-  case ACTIVITY_TRANSFORM:     text = _("Transform"); break;
-  case ACTIVITY_AIRBASE:       text = _("Airbase"); break;
-  case ACTIVITY_FALLOUT:       text = _("Fallout"); break;
-  default:                     text = _("Unknown"); break;
+  case ACTIVITY_IDLE:
+    return _("Idle");
+  case ACTIVITY_POLLUTION:
+    return _("Pollution");
+  case ACTIVITY_ROAD:
+    return _("Road");
+  case ACTIVITY_MINE:
+    return _("Mine");
+  case ACTIVITY_IRRIGATE:
+    return _("Irrigation");
+  case ACTIVITY_FORTIFYING:
+    return _("Fortifying");
+  case ACTIVITY_FORTIFIED:
+    return _("Fortified");
+  case ACTIVITY_FORTRESS:
+    return _("Fortress");
+  case ACTIVITY_SENTRY:
+    return _("Sentry");
+  case ACTIVITY_RAILROAD:
+    return _("Railroad");
+  case ACTIVITY_PILLAGE:
+    return _("Pillage");
+  case ACTIVITY_GOTO:
+    return _("Goto");
+  case ACTIVITY_EXPLORE:
+    return _("Explore");
+  case ACTIVITY_TRANSFORM:
+    return _("Transform");
+  case ACTIVITY_AIRBASE:
+    return _("Airbase");
+  case ACTIVITY_FALLOUT:
+    return _("Fallout");
+  case ACTIVITY_UNKNOWN:
+  case ACTIVITY_PATROL_UNUSED:
+  case ACTIVITY_LAST:
+    break;
   }
 
-  return text;
+  return _("Unknown");
 }
 
 /****************************************************************************
@@ -1010,7 +1030,7 @@
 const char *unit_activity_text(struct unit *punit)
 {
   static char text[64];
-  char *moves_str;
+  const char *moves_str;
    
   switch(punit->activity) {
    case ACTIVITY_IDLE:
Index: common/unit.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/unit.h,v
retrieving revision 1.116
diff -u -r1.116 unit.h
--- common/unit.h       29 Apr 2004 19:59:21 -0000      1.116
+++ common/unit.h       4 May 2004 04:55:26 -0000
@@ -256,7 +256,7 @@
 bool can_unit_bombard(struct unit *punit);
 bool can_unit_change_homecity(struct unit *punit);
 bool can_unit_do_connect(struct unit *punit, enum unit_activity activity);
-char* get_activity_text (int activity);
+const char *get_activity_text(enum unit_activity activity);
 bool can_unit_continue_current_activity(struct unit *punit);
 bool can_unit_do_activity(struct unit *punit, enum unit_activity activity);
 bool can_unit_do_activity_targeted(struct unit *punit,
Index: common/unittype.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/unittype.c,v
retrieving revision 1.32
diff -u -r1.32 unittype.c
--- common/unittype.c   14 Apr 2004 11:38:45 -0000      1.32
+++ common/unittype.c   4 May 2004 04:55:26 -0000
@@ -300,13 +300,14 @@
       strcat(astr.str,unitname);
 
       if(count==1) {
-       char *and_str = _(" and ");
+       const char *and_str = _(" and ");
+
         astr_minsize(&astr,astr.n+strlen(and_str));
         strcat(astr.str, and_str);
       }
       else {
         if(count != 0) {
-         char *and_comma = Q_("?and:, ");
+         const char *and_comma = Q_("?and:, ");
 
          astr_minsize(&astr, astr.n + strlen(and_comma));
          strcat(astr.str, and_comma);

[Prev in Thread] Current Thread [Next in Thread]