2002-03-03 03:36:27 +00:00
|
|
|
/*
|
|
|
|
dstring.c
|
|
|
|
|
|
|
|
dynamic string buffer functions
|
|
|
|
|
|
|
|
Copyright (C) 2002 Brian Koropoff.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2003-01-14 20:18:29 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2003-01-15 15:31:36 +00:00
|
|
|
static __attribute__ ((unused)) const char rcsid[] =
|
|
|
|
"$Id$";
|
|
|
|
|
2002-03-03 03:36:27 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "QF/sys.h"
|
|
|
|
#include "QF/dstring.h"
|
|
|
|
|
2002-03-08 23:11:42 +00:00
|
|
|
#include "compat.h"
|
|
|
|
|
2002-03-21 21:56:22 +00:00
|
|
|
dstring_t *
|
2002-03-05 22:36:04 +00:00
|
|
|
dstring_new (void)
|
|
|
|
{
|
2002-03-21 21:56:22 +00:00
|
|
|
dstring_t *new;
|
|
|
|
|
|
|
|
new = calloc (1, sizeof (dstring_t));
|
|
|
|
|
2002-03-03 03:36:27 +00:00
|
|
|
if (!new)
|
2002-05-14 06:12:29 +00:00
|
|
|
Sys_Error ("dstring_new: Failed to allocate memory.");
|
2002-03-03 03:36:27 +00:00
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2002-03-05 22:36:04 +00:00
|
|
|
void
|
|
|
|
dstring_delete (dstring_t *dstr)
|
|
|
|
{
|
2002-03-03 03:36:27 +00:00
|
|
|
if (dstr->str)
|
|
|
|
free (dstr->str);
|
|
|
|
free (dstr);
|
|
|
|
}
|
2002-03-21 21:56:22 +00:00
|
|
|
|
2002-07-29 19:52:19 +00:00
|
|
|
inline void
|
2002-03-05 22:36:04 +00:00
|
|
|
dstring_adjust (dstring_t *dstr)
|
|
|
|
{
|
2002-03-03 03:36:27 +00:00
|
|
|
if (dstr->size > dstr->truesize) {
|
2002-10-15 19:52:46 +00:00
|
|
|
dstr->truesize = (dstr->size + 1023) & ~1023;
|
|
|
|
dstr->str = realloc (dstr->str, dstr->truesize);
|
2002-03-03 03:36:27 +00:00
|
|
|
if (!dstr->str)
|
2002-05-14 06:12:29 +00:00
|
|
|
Sys_Error ("dstring_adjust: Failed to reallocate memory.");
|
2002-03-03 03:36:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-08 23:05:39 +00:00
|
|
|
void
|
|
|
|
dstring_copy (dstring_t *dstr, const char *data, unsigned int len)
|
|
|
|
{
|
|
|
|
dstr->size = len;
|
|
|
|
dstring_adjust (dstr);
|
|
|
|
memcpy (dstr->str, data, len);
|
|
|
|
}
|
|
|
|
|
2002-03-05 22:36:04 +00:00
|
|
|
void
|
|
|
|
dstring_append (dstring_t *dstr, const char *data, unsigned int len)
|
|
|
|
{
|
2002-03-21 21:56:22 +00:00
|
|
|
unsigned int ins = dstr->size; // Save insertion point
|
|
|
|
|
2002-03-03 03:36:27 +00:00
|
|
|
dstr->size += len;
|
|
|
|
dstring_adjust (dstr);
|
|
|
|
memcpy (dstr->str + ins, data, len);
|
|
|
|
}
|
|
|
|
|
2002-03-05 22:36:04 +00:00
|
|
|
void
|
2002-08-03 06:04:00 +00:00
|
|
|
dstring_insert (dstring_t *dstr, unsigned int pos, const char *data,
|
|
|
|
unsigned int len)
|
2002-03-05 22:36:04 +00:00
|
|
|
{
|
2002-03-03 03:36:27 +00:00
|
|
|
unsigned int oldsize = dstr->size;
|
2002-03-21 21:56:22 +00:00
|
|
|
|
2002-03-03 03:36:27 +00:00
|
|
|
dstr->size += len;
|
|
|
|
dstring_adjust (dstr);
|
2002-03-21 21:56:22 +00:00
|
|
|
memmove (dstr->str + pos + len, dstr->str + pos, oldsize - pos);
|
|
|
|
memcpy (dstr->str + pos, data, len);
|
2002-03-03 03:36:27 +00:00
|
|
|
}
|
|
|
|
|
2002-03-05 22:36:04 +00:00
|
|
|
void
|
|
|
|
dstring_snip (dstring_t *dstr, unsigned int pos, unsigned int len)
|
|
|
|
{
|
2002-03-21 21:56:22 +00:00
|
|
|
memmove (dstr->str + pos, dstr->str + pos + len, dstr->size - pos - len);
|
2002-03-03 03:36:27 +00:00
|
|
|
dstr->size -= len;
|
|
|
|
dstring_adjust (dstr);
|
|
|
|
}
|
|
|
|
|
2002-03-05 22:36:04 +00:00
|
|
|
void
|
|
|
|
dstring_clear (dstring_t *dstr)
|
|
|
|
{
|
2002-03-03 03:36:27 +00:00
|
|
|
dstr->size = 0;
|
|
|
|
dstring_adjust (dstr);
|
|
|
|
}
|
|
|
|
|
2002-04-19 22:54:27 +00:00
|
|
|
void
|
2002-08-03 06:04:00 +00:00
|
|
|
dstring_replace (dstring_t *dstr, unsigned int pos, unsigned int rlen,
|
|
|
|
const char *data, unsigned int len)
|
2002-04-19 22:54:27 +00:00
|
|
|
{
|
2002-04-23 07:47:24 +00:00
|
|
|
unsigned int oldsize = dstr->size;
|
2002-04-19 22:54:27 +00:00
|
|
|
if (rlen < len) {
|
|
|
|
dstr->size += len - rlen;
|
|
|
|
dstring_adjust (dstr);
|
|
|
|
memmove (dstr->str + pos + len, dstr->str + pos + rlen,
|
2002-04-23 07:47:24 +00:00
|
|
|
oldsize - (pos + rlen));
|
2002-04-19 22:54:27 +00:00
|
|
|
} else if (rlen > len) {
|
|
|
|
memmove (dstr->str + pos + len, dstr->str + pos + rlen,
|
2002-04-23 07:47:24 +00:00
|
|
|
oldsize - (pos + rlen));
|
2002-04-19 22:54:27 +00:00
|
|
|
dstr->size -= rlen - len;
|
|
|
|
dstring_adjust (dstr);
|
|
|
|
}
|
|
|
|
memcpy (dstr->str + pos, data, len);
|
|
|
|
}
|
|
|
|
|
2003-04-13 05:48:28 +00:00
|
|
|
char *
|
|
|
|
dstring_freeze (dstring_t *dstr)
|
|
|
|
{
|
|
|
|
char *str = realloc (dstr->str, dstr->size);
|
|
|
|
free (dstr);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2002-03-21 21:56:22 +00:00
|
|
|
dstring_t *
|
2002-03-05 22:36:04 +00:00
|
|
|
dstring_newstr (void)
|
|
|
|
{
|
2002-03-21 21:56:22 +00:00
|
|
|
dstring_t *new;
|
|
|
|
|
|
|
|
new = calloc (1, sizeof (dstring_t));
|
|
|
|
|
2002-03-03 03:36:27 +00:00
|
|
|
if (!new)
|
2002-05-14 06:12:29 +00:00
|
|
|
Sys_Error ("dstring_newstr: Failed to allocate memory.");
|
2002-03-03 03:36:27 +00:00
|
|
|
new->size = 1;
|
2002-03-21 21:56:22 +00:00
|
|
|
dstring_adjust (new);
|
2002-03-03 03:36:27 +00:00
|
|
|
new->str[0] = 0;
|
|
|
|
return new;
|
|
|
|
}
|
2002-03-05 22:36:04 +00:00
|
|
|
|
2003-05-08 23:05:39 +00:00
|
|
|
void
|
|
|
|
dstring_copystr (dstring_t *dstr, const char *str)
|
|
|
|
{
|
|
|
|
dstr->size = strlen (str) + 1;
|
|
|
|
dstring_adjust (dstr);
|
|
|
|
strcpy (dstr->str, str);
|
|
|
|
}
|
|
|
|
|
2002-03-05 22:36:04 +00:00
|
|
|
void
|
2002-03-21 21:56:22 +00:00
|
|
|
dstring_appendstr (dstring_t *dstr, const char *str)
|
|
|
|
{
|
|
|
|
dstr->size += strlen (str);
|
|
|
|
dstring_adjust (dstr);
|
|
|
|
strcat (dstr->str, str);
|
2002-03-03 03:36:27 +00:00
|
|
|
}
|
|
|
|
|
2002-03-21 21:16:26 +00:00
|
|
|
void
|
2002-03-21 21:56:22 +00:00
|
|
|
dstring_appendsubstr (dstring_t *dstr, const char *str, unsigned int len)
|
|
|
|
{
|
|
|
|
unsigned int l = strlen (str);
|
|
|
|
|
|
|
|
if (len > l)
|
|
|
|
len = l;
|
|
|
|
dstr->size += len;
|
|
|
|
dstring_adjust (dstr);
|
|
|
|
strncat (dstr->str, str, len);
|
2002-03-21 21:16:26 +00:00
|
|
|
}
|
|
|
|
|
2002-03-05 22:36:04 +00:00
|
|
|
void
|
2002-08-03 06:04:00 +00:00
|
|
|
dstring_insertstr (dstring_t *dstr, unsigned int pos, const char *str)
|
2002-03-05 22:36:04 +00:00
|
|
|
{
|
2002-03-21 21:53:52 +00:00
|
|
|
// Don't insert strlen + 1 to achieve concatenation
|
2002-08-03 06:04:00 +00:00
|
|
|
dstring_insert (dstr, pos, str, strlen (str));
|
2002-03-03 03:36:27 +00:00
|
|
|
}
|
|
|
|
|
2002-03-21 21:53:52 +00:00
|
|
|
void
|
2002-08-03 06:04:00 +00:00
|
|
|
dstring_insertsubstr (dstring_t *dstr, unsigned int pos, const char *str,
|
2002-03-21 21:53:52 +00:00
|
|
|
unsigned int len)
|
|
|
|
{
|
|
|
|
unsigned int l = strlen (str);
|
2002-03-21 21:56:22 +00:00
|
|
|
|
2002-03-21 21:53:52 +00:00
|
|
|
if (len > l)
|
|
|
|
len = l;
|
2002-08-03 06:04:00 +00:00
|
|
|
dstring_insert (dstr, pos, str, len);
|
2002-03-21 21:53:52 +00:00
|
|
|
}
|
|
|
|
|
2002-03-05 22:36:04 +00:00
|
|
|
void
|
|
|
|
dstring_clearstr (dstring_t *dstr)
|
|
|
|
{
|
2002-03-03 03:36:27 +00:00
|
|
|
dstr->size = 1;
|
|
|
|
dstring_adjust (dstr);
|
|
|
|
dstr->str[0] = 0;
|
|
|
|
}
|
2002-03-08 23:11:42 +00:00
|
|
|
|
2002-04-15 22:17:02 +00:00
|
|
|
#if defined (HAVE_VA_COPY)
|
|
|
|
# define VA_COPY(a,b) va_copy (a, b)
|
|
|
|
#elif defined (HAVE__VA_COPY)
|
|
|
|
# define VA_COPY(a,b) __va_copy (a, b)
|
|
|
|
#else
|
|
|
|
# define VA_COPY memcpy (a, b, sizeof (a))
|
|
|
|
#endif
|
|
|
|
|
2002-10-15 19:52:46 +00:00
|
|
|
static int
|
|
|
|
_dvsprintf (dstring_t *dstr, int offs, const char *fmt, va_list args)
|
2002-03-08 23:11:42 +00:00
|
|
|
{
|
2002-12-17 22:35:14 +00:00
|
|
|
int size;
|
2002-03-08 23:11:42 +00:00
|
|
|
|
2002-04-15 22:17:02 +00:00
|
|
|
#ifdef VA_LIST_IS_ARRAY
|
|
|
|
va_list tmp_args;
|
|
|
|
VA_COPY (tmp_args, args);
|
|
|
|
#endif
|
|
|
|
|
2002-12-15 07:01:50 +00:00
|
|
|
if (!dstr->truesize)
|
|
|
|
dstring_clearstr (dstr); // Make it a string
|
2002-12-17 22:35:14 +00:00
|
|
|
// Some vsnprintf implementations return -1 on truncation
|
|
|
|
while ((size = vsnprintf (dstr->str + offs, dstr->truesize - offs, fmt, args)) == -1) {
|
|
|
|
dstr->size = dstr->truesize + 1024;
|
2002-03-08 23:11:42 +00:00
|
|
|
dstring_adjust (dstr);
|
2002-04-15 22:17:02 +00:00
|
|
|
#ifdef VA_LIST_IS_ARRAY
|
|
|
|
VA_COPY (args, tmp_args);
|
|
|
|
#endif
|
2002-12-17 22:35:14 +00:00
|
|
|
}
|
|
|
|
dstr->size = size + offs + 1;
|
|
|
|
// "Proper" implementations return the required size
|
|
|
|
if (dstr->size > dstr->truesize) {
|
|
|
|
dstring_adjust (dstr);
|
|
|
|
#ifdef VA_LIST_IS_ARRAY
|
|
|
|
VA_COPY (args, tmp_args);
|
|
|
|
#endif
|
|
|
|
vsnprintf (dstr->str + offs, dstr->truesize - offs, fmt, args);
|
2002-03-08 23:11:42 +00:00
|
|
|
}
|
2002-12-15 07:01:50 +00:00
|
|
|
return size;
|
2002-03-08 23:11:42 +00:00
|
|
|
}
|
|
|
|
|
2002-10-15 19:52:46 +00:00
|
|
|
int
|
|
|
|
dvsprintf (dstring_t *dstr, const char *fmt, va_list args)
|
|
|
|
{
|
|
|
|
return _dvsprintf (dstr, 0, fmt, args);
|
|
|
|
}
|
|
|
|
|
2002-03-08 23:11:42 +00:00
|
|
|
int
|
|
|
|
dsprintf (dstring_t *dstr, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
va_start (args, fmt);
|
2002-10-15 19:52:46 +00:00
|
|
|
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);
|
2002-03-08 23:11:42 +00:00
|
|
|
va_end (args);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|