Complete.Org: Mailing Lists: Archives: freeciv-dev: May 2004:
[Freeciv-Dev] (PR#8693) population_to_text
Home

[Freeciv-Dev] (PR#8693) population_to_text

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#8693) population_to_text
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 6 May 2004 14:20:02 -0700
Reply-to: rt@xxxxxxxxxxx

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

Population_to_text() shouldn't be in utility/.

However moving it to common/ presents some small difficulties since it 
calls general_int_to_text, which is not a public function nor is it 
pretty enough to be made one.

So this patch moves population_to_text() into common/game.c. 
general_int_to_text is renamed as big_int_to_text (this name is better 
but not really good).  More importantly its parameters are renamed as 
mantissa and exponent (since we're talking about scientific notation 
here).  It's parameters are made unsigned (since it must have unsigned 
values anyway).  And a gaping buffer underrun possibility is fixed.

See also PR#8615.

jason

Index: common/game.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/game.c,v
retrieving revision 1.178
diff -u -r1.178 game.c
--- common/game.c       10 Apr 2004 23:05:51 -0000      1.178
+++ common/game.c       6 May 2004 21:15:55 -0000
@@ -597,3 +597,12 @@
 #undef name_strlcpy
 
 }
+
+/***************************************************************
+...
+***************************************************************/
+const char *population_to_text(int thousand_citizen)
+{
+  return big_int_to_text(thousand_citizen, 3);
+}
+
Index: common/game.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/game.h,v
retrieving revision 1.135
diff -u -r1.135 game.h
--- common/game.h       24 Apr 2004 17:32:47 -0000      1.135
+++ common/game.h       6 May 2004 21:15:55 -0000
@@ -278,6 +278,8 @@
 bool is_valid_player_id(int player_id);
 int get_num_human_and_ai_players(void);
 
+const char *population_to_text(int thousand_citizen);
+
 extern struct civ_game game;
 extern bool is_server;
 
Index: utility/shared.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/utility/shared.c,v
retrieving revision 1.110
diff -u -r1.110 shared.c
--- utility/shared.c    2 May 2004 11:42:24 -0000       1.110
+++ utility/shared.c    6 May 2004 21:15:55 -0000
@@ -277,44 +277,48 @@
 /***************************************************************
   Returns a statically allocated string containing a nicely-formatted
   version of the given number according to the user's locale.  (Only
-  works for numbers >= zero.) The actually number used for the
-  formatting is: nr*10^decade_exponent
+  works for numbers >= zero.)  The number is given in scientific notation
+  as mantissa * 10^exponent.
 ***************************************************************/
-static const char *general_int_to_text(int nr, int decade_exponent)
+const char *big_int_to_text(unsigned int mantissa, unsigned int exponent)
 {
   static char buf[64]; /* Note that we'll be filling this in right to left. */
   char *grp = grouping;
   char *ptr;
-  int cnt;
+  unsigned int cnt = 0;
 
-  assert(nr >= 0);
-  assert(decade_exponent >= 0);
+#if 0 /* Not needed while the values are unsigned. */
+  assert(mantissa >= 0);
+  assert(exponent >= 0);
+#endif
 
-  if (nr == 0) {
+  if (mantissa == 0) {
     return "0";
   }
 
   ptr = &buf[sizeof(buf)];
   *(--ptr) = '\0';
 
-  cnt = 0;
-  while (nr != 0 && decade_exponent >= 0) {
+  while (mantissa != 0 && exponent >= 0) {
     int dig;
 
-    assert(ptr > buf);
+    if (ptr <= buf + grouping_sep_len) {
+      assert(ptr > buf + grouping_sep_len);
+      return ptr;
+    }
 
-    if (decade_exponent > 0) {
+    if (exponent > 0) {
       dig = 0;
-      decade_exponent--;
+      exponent--;
     } else {
-      dig = nr % 10;
-      nr /= 10;
+      dig = mantissa % 10;
+      mantissa /= 10;
     }
 
     *(--ptr) = '0' + dig;
 
     cnt++;
-    if (nr != 0 && cnt == *grp) {
+    if (mantissa != 0 && cnt == *grp) {
       /* Reached count of digits in group: insert separator and reset count. */
       cnt = 0;
       if (*grp == CHAR_MAX) {
@@ -339,17 +343,9 @@
 /***************************************************************
 ...
 ***************************************************************/
-const char *int_to_text(int nr)
-{
-  return general_int_to_text(nr, 0);
-}
-
-/***************************************************************
-...
-***************************************************************/
-const char *population_to_text(int thousand_citizen)
+const char *int_to_text(unsigned int number)
 {
-  return general_int_to_text(thousand_citizen, 3);
+  return big_int_to_text(number, 0);
 }
 
 /***************************************************************
Index: utility/shared.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/utility/shared.h,v
retrieving revision 1.125
diff -u -r1.125 shared.h
--- utility/shared.h    2 May 2004 11:42:24 -0000       1.125
+++ utility/shared.h    6 May 2004 21:15:56 -0000
@@ -170,8 +170,9 @@
 bool is_option(const char *option_name,char *option);
 int get_tokens(const char *str, char **tokens, size_t num_tokens,
               const char *delimiterset);
-const char *int_to_text(int nr);
-const char *population_to_text(int thousand_citizen);
+
+const char *big_int_to_text(unsigned int mantissa, unsigned int exponent);
+const char *int_to_text(unsigned int number);
 
 bool is_sane_name(const char *name);
 const char *textyear(int year);

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#8693) population_to_text, Jason Short <=