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

[Freeciv-Dev] Re: (PR#8720) Dynamtext

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] Re: (PR#8720) Dynamtext
From: "Raimar Falke" <i-freeciv-lists@xxxxxxxxxxxxx>
Date: Sat, 22 May 2004 00:40:18 -0700
Reply-to: rt@xxxxxxxxxxx

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

On Fri, May 21, 2004 at 11:58:26PM -0700, Jason Short wrote:

Here is a patch. It is a step toward the goal. If this is applied I
will make another one to change text.c and change the cases in
common/*

        Raimar

-- 
 email: rf13@xxxxxxxxxxxxxxxxx
  "Heuer's Law: Any feature is a bug unless it can be turned off."

Index: utility/astring.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/utility/astring.c,v
retrieving revision 1.9
diff -u -u -r1.9 astring.c
--- utility/astring.c   19 May 2004 21:14:41 -0000      1.9
+++ utility/astring.c   22 May 2004 07:38:27 -0000
@@ -43,11 +43,20 @@
 #endif
 
 #include <assert.h>
+#include <stdarg.h>
 
 #include "mem.h"
+#include "support.h"           /* my_vsnprintf, mystrlcat */
 
 #include "astring.h"
 
+/*
+ * An formatted string in vadd() has to fit into VADD_TMP_SIZE. One
+ * buffer of VADD_TMP_SIZE size will be allocated for the entire
+ * program.
+ */
+#define VADD_TMP_SIZE  (1024)
+
 /**********************************************************************
   Initialize the struct.
 ***********************************************************************/
@@ -68,6 +77,7 @@
 void astr_minsize(struct astring *astr, size_t n)
 {
   int n1;
+  bool was_null = astr->str == NULL;
   
   assert(astr != NULL);
   
@@ -80,6 +90,9 @@
   n1 = (3*(astr->n_alloc+10)) / 2;
   astr->n_alloc = (n > n1) ? n : n1;
   astr->str = (char *)fc_realloc(astr->str, astr->n_alloc);
+  if (was_null) {
+    astr_clear(astr);
+  }
 }
 
 /**********************************************************************
@@ -98,3 +111,47 @@
   }
   *astr = zero_astr;
 }
+
+/****************************************************************************
+  Add the text to the string.
+****************************************************************************/
+static void vadd(struct astring *astr, const char *format, va_list ap)
+{
+  size_t new_len;
+  static char buf[VADD_TMP_SIZE];
+
+  if (my_vsnprintf(buf, sizeof(buf), format, ap) == -1) {
+    die("Formatted string bigger than %d", sizeof(buf));
+  }
+
+  /* Avoid calling strlen with NULL. */
+  astr_minsize(astr, 1);
+
+  new_len = strlen(astr->str) + strlen(buf) + 1;
+
+  astr_minsize(astr, new_len);
+  mystrlcat(astr->str, buf, astr->n_alloc);
+}
+
+/****************************************************************************
+  Add the text to the string.
+****************************************************************************/
+void astr_add(struct astring *astr, const char *format, ...)
+{
+  va_list args;
+
+  va_start(args, format);
+  vadd(astr, format, args);
+  va_end(args);
+}
+
+/**********************************************************************
+  Sets the content to the empty string.
+***********************************************************************/
+void astr_clear(struct astring *astr)
+{
+  assert(astr != NULL);
+
+  astr_minsize(astr, 1);
+  astr->str[0] = '\0';
+}
Index: utility/astring.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/utility/astring.h,v
retrieving revision 1.9
diff -u -u -r1.9 astring.h
--- utility/astring.h   19 May 2004 21:14:41 -0000      1.9
+++ utility/astring.h   22 May 2004 07:38:27 -0000
@@ -38,5 +38,8 @@
 void astr_init(struct astring *astr);
 void astr_minsize(struct astring *astr, size_t n);
 void astr_free(struct astring *astr);
+void astr_clear(struct astring *astr);
+void astr_add(struct astring *astr, const char *format, ...)
+fc__attribute((format(printf, 2, 3)));
 
 #endif  /* FC__ASTRING_H */

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