[Freeciv-Dev] Re: (PR#8720) Dynamtext
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
<URL: http://rt.freeciv.org/Ticket/Display.html?id=8720 >
On Tue, May 25, 2004 at 06:26:18AM -0700, Jason Short wrote:
>
> <URL: http://rt.freeciv.org/Ticket/Display.html?id=8720 >
>
> Raimar Falke wrote:
>
> > How do you decrease the string size? There is no function for this.
> >
> > How can you get an astring where (astr->str==null) is different from
> > (astr->n==0)? It is IMHO not possible.
>
> astr_minsize(0)
IMHO is in the sequence
astr_init(x);
astr_minsize(x,0);
the second line a nop.
> >>See <MARK>. WHy is this buffer static? This seems totally unnecessary.
> >
> > To decrease the number of malloc/free calls.
>
> But if you remove the "static" this array will be on the stack.
Yes.
Raimar
--
email: rf13@xxxxxxxxxxxxxxxxx
"We've all heard that a million monkeys banging on a million typewriters
will eventually reproduce the entire works of Shakespeare.
Now, thanks to the Internet, we know this is not true."
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 28 May 2004 12:04:25 -0000
@@ -43,8 +43,10 @@
#endif
#include <assert.h>
+#include <stdarg.h>
#include "mem.h"
+#include "support.h" /* my_vsnprintf, mystrlcat */
#include "astring.h"
@@ -68,6 +70,7 @@
void astr_minsize(struct astring *astr, size_t n)
{
int n1;
+ bool was_null = astr->str == NULL;
assert(astr != NULL);
@@ -80,6 +83,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 +104,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;
+ char buf[1024];
+
+ if (my_vsnprintf(buf, sizeof(buf), format, ap) == -1) {
+ die("Formatted string bigger than %d bytes", 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 28 May 2004 12:04:25 -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 */
- [Freeciv-Dev] Re: (PR#8720) Dynamtext, (continued)
- [Freeciv-Dev] Re: (PR#8720) Dynamtext, Mike Kaufman, 2004/05/12
- [Freeciv-Dev] Re: (PR#8720) Dynamtext, Jason Short, 2004/05/12
- [Freeciv-Dev] Re: (PR#8720) Dynamtext, Raimar Falke, 2004/05/13
- [Freeciv-Dev] (PR#8720) Dynamtext, Jason Short, 2004/05/22
- [Freeciv-Dev] Re: (PR#8720) Dynamtext, Raimar Falke, 2004/05/22
- [Freeciv-Dev] Re: (PR#8720) Dynamtext, Jason Short, 2004/05/22
- [Freeciv-Dev] Re: (PR#8720) Dynamtext, Raimar Falke, 2004/05/22
- [Freeciv-Dev] Re: (PR#8720) Dynamtext, Jason Short, 2004/05/23
- [Freeciv-Dev] Re: (PR#8720) Dynamtext, Raimar Falke, 2004/05/25
- [Freeciv-Dev] Re: (PR#8720) Dynamtext, Jason Short, 2004/05/25
- [Freeciv-Dev] Re: (PR#8720) Dynamtext,
Raimar Falke <=
- [Freeciv-Dev] Re: (PR#8720) Dynamtext, Jason Short, 2004/05/28
|
|