dstring.[ch]:

add dstring_copysubstr and make all the substr functions use strnlen
	for the incoming string. also fix the append string functions to append
	the present string correctly.
cbuf.c:
	use dstring_copystr instead of dstring_clearstr/dstring_appendstr
idparse.c:
	use dstring_copysubstr instead of dstring_clearstr/dstring_insert
This commit is contained in:
Bill Currie 2003-05-26 19:14:18 +00:00
parent 9602d6c88d
commit 00abd17dd4
4 changed files with 27 additions and 18 deletions

View file

@ -54,6 +54,7 @@ char *dstring_freeze (dstring_t *dstr);
// String-specific functions
dstring_t *dstring_newstr (void);
void dstring_copystr (dstring_t *dstr, const char *str);
void dstring_copysubstr (dstring_t *dstr, const char *str, unsigned int len);
void dstring_appendstr (dstring_t *dstr, const char *str);
void dstring_appendsubstr (dstring_t *dstr, const char *str, unsigned int len);
void dstring_insertstr (dstring_t *dstr, unsigned int pos, const char *str);

View file

@ -88,8 +88,7 @@ Cbuf_ArgsAdd (cbuf_args_t *args, const char *arg)
args->args[i] = 0;
}
}
dstring_clearstr (args->argv[args->argc]);
dstring_appendstr (args->argv[args->argc], arg);
dstring_copystr (args->argv[args->argc], arg);
args->argc++;
}

View file

@ -165,24 +165,38 @@ dstring_copystr (dstring_t *dstr, const char *str)
strcpy (dstr->str, str);
}
void
dstring_copysubstr (dstring_t *dstr, const char *str, unsigned int len)
{
len = strnlen (str, len);
dstr->size = len + 1;
dstring_adjust (dstr);
strncpy (dstr->str, str, len);
dstr->str[len] = 0;
}
void
dstring_appendstr (dstring_t *dstr, const char *str)
{
dstr->size += strlen (str);
unsigned int pos = strnlen (dstr->str, dstr->size);
unsigned int len = strlen (str);
dstr->size = pos + len + 1;
dstring_adjust (dstr);
strcat (dstr->str, str);
strcpy (dstr->str + pos, str);
}
void
dstring_appendsubstr (dstring_t *dstr, const char *str, unsigned int len)
{
unsigned int l = strlen (str);
unsigned int pos = strnlen (dstr->str, dstr->size);
if (len > l)
len = l;
dstr->size += len;
len = strnlen (str, len);
dstr->size = pos + len + 1;
dstring_adjust (dstr);
strncat (dstr->str, str, len);
strncpy (dstr->str + pos, str, len);
dstr->str[pos + len] = 0;
}
void
@ -196,10 +210,8 @@ void
dstring_insertsubstr (dstring_t *dstr, unsigned int pos, const char *str,
unsigned int len)
{
unsigned int l = strlen (str);
len = strnlen (str, len);
if (len > l)
len = l;
dstring_insert (dstr, pos, str, len);
}

View file

@ -97,11 +97,8 @@ COM_Parse (const char *data)
int c;
int i;
if (!_com_token) {
if (!_com_token)
_com_token = dstring_newstr ();
} else {
dstring_clearstr (_com_token);
}
com_token = _com_token->str;
if (!data)
@ -123,7 +120,7 @@ skipwhite:
while (1) {
c = data[i++];
if (c == '"' || !c) {
dstring_insert (_com_token, 0, data, i - 1);
dstring_copysubstr (_com_token, data, i - 1);
goto done;
}
}
@ -132,7 +129,7 @@ skipwhite:
do {
i++;
} while (data[i] && !isspace ((byte) data[i]));
dstring_insert (_com_token, 0, data, i);
dstring_copysubstr (_com_token, data, i);
done:
com_token = _com_token->str;
return data + i;