make dstring_adjust `grow' in increments of 1024. this should speed up

multiple appends nicely.
also create davsprintf and dasprintf to /append/ the printed strings to the
dstring rather than overwriting it as is done with dvsprintf and dsprintf.
This commit is contained in:
Bill Currie 2002-10-15 19:52:46 +00:00
parent 1f0b6e84a5
commit 68b41f9b44
2 changed files with 45 additions and 9 deletions

View file

@ -62,5 +62,7 @@ void dstring_clearstr (dstring_t *dstr);
int dvsprintf (dstring_t *dstr, const char *fmt, va_list args); int dvsprintf (dstring_t *dstr, const char *fmt, va_list args);
int dsprintf (dstring_t *dstr, const char *fmt, ...) __attribute__((format(printf,2,3))); int dsprintf (dstring_t *dstr, const char *fmt, ...) __attribute__((format(printf,2,3)));
int davsprintf (dstring_t *dstr, const char *fmt, va_list args);
int dasprintf (dstring_t *dstr, const char *fmt, ...) __attribute__((format(printf,2,3)));
#endif // __dstring_h #endif // __dstring_h

View file

@ -59,10 +59,10 @@ inline void
dstring_adjust (dstring_t *dstr) dstring_adjust (dstring_t *dstr)
{ {
if (dstr->size > dstr->truesize) { if (dstr->size > dstr->truesize) {
dstr->str = realloc (dstr->str, dstr->size); dstr->truesize = (dstr->size + 1023) & ~1023;
dstr->str = realloc (dstr->str, dstr->truesize);
if (!dstr->str) if (!dstr->str)
Sys_Error ("dstring_adjust: Failed to reallocate memory."); Sys_Error ("dstring_adjust: Failed to reallocate memory.");
dstr->truesize = dstr->size;
} }
} }
@ -191,8 +191,8 @@ dstring_clearstr (dstring_t *dstr)
# define VA_COPY memcpy (a, b, sizeof (a)) # define VA_COPY memcpy (a, b, sizeof (a))
#endif #endif
int static int
dvsprintf (dstring_t *dstr, const char *fmt, va_list args) _dvsprintf (dstring_t *dstr, int offs, const char *fmt, va_list args)
{ {
int size; int size;
@ -205,22 +205,30 @@ dvsprintf (dstring_t *dstr, const char *fmt, va_list args)
dstr->size = 1024; dstr->size = 1024;
dstring_adjust (dstr); dstring_adjust (dstr);
} }
size = vsnprintf (dstr->str, dstr->truesize, fmt, args) + 1; // +1 for nul // +1 for nul
size = vsnprintf (dstr->str + offs, dstr->truesize - offs, fmt, args) + 1;
while (size <= 0 || size > dstr->truesize) { while (size <= 0 || size > dstr->truesize) {
if (size > 0) if (size > 0)
dstr->size = (size + 1023) & ~1023; // 1k multiples dstr->size = (size + offs + 1023) & ~1023; // 1k multiples
else else
dstr->size = dstr->truesize + 1024; dstr->size = dstr->truesize + 1024;
dstring_adjust (dstr); dstring_adjust (dstr);
#ifdef VA_LIST_IS_ARRAY #ifdef VA_LIST_IS_ARRAY
VA_COPY (args, tmp_args); VA_COPY (args, tmp_args);
#endif #endif
size = vsnprintf (dstr->str, dstr->truesize, fmt, args) + 1; size = vsnprintf (dstr->str + offs, dstr->truesize - offs,
fmt, args) + 1;
} }
dstr->size = size; dstr->size = size + offs;
return size - 1; return size - 1;
} }
int
dvsprintf (dstring_t *dstr, const char *fmt, va_list args)
{
return _dvsprintf (dstr, 0, fmt, args);
}
int int
dsprintf (dstring_t *dstr, const char *fmt, ...) dsprintf (dstring_t *dstr, const char *fmt, ...)
{ {
@ -228,7 +236,33 @@ dsprintf (dstring_t *dstr, const char *fmt, ...)
int ret; int ret;
va_start (args, fmt); va_start (args, fmt);
ret = dvsprintf (dstr, fmt, args); ret = _dvsprintf (dstr, 0, fmt, args);
va_end (args);
return ret;
}
int
davsprintf (dstring_t *dstr, const char *fmt, va_list args)
{
int offs = 0;
if (dstr->size)
offs = dstr->size - 1;
return _dvsprintf (dstr, offs, fmt, args);
}
int
dasprintf (dstring_t *dstr, const char *fmt, ...)
{
va_list args;
int ret;
int offs = 0;
if (dstr->size)
offs = dstr->size - 1;
va_start (args, fmt);
ret = _dvsprintf (dstr, offs, fmt, args);
va_end (args); va_end (args);
return ret; return ret;