Convert UtilStringConcat() into a varargs function.

This allows us to concatenate an arbitrary amount of strings without
having to maintain a bunch of pointers or leak memory when nesting calls.
This commit is contained in:
Jordan Bancino 2023-01-07 03:17:06 +00:00
parent 0c807d0f22
commit 2ce09f8632
4 changed files with 50 additions and 42 deletions

View file

@ -17,7 +17,8 @@ Milestone: v0.2.0
[ ] Document UserInteractiveAuth (move docs from Matrix)
[ ] Document MemoryHexDump
[ ] Move String functions to a new String.h?
[ ] Make UtilStringConcat use varargs
[~] Make UtilStringConcat use varargs
[ ] Update documentation
[x] Look into seeding random strings (possibly create Random.h?)
[~] User registration
[x] Username validation

View file

@ -200,7 +200,7 @@ DbHashKey(Array * args)
for (i = 0; i < ArraySize(args); i++)
{
char *tmp = UtilStringConcat(str, ArrayGet(args, i));
char *tmp = UtilStringConcat(2, str, ArrayGet(args, i));
Free(str);
str = tmp;
@ -213,19 +213,17 @@ static char *
DbDirName(Db * db, Array * args)
{
size_t i;
char *str = UtilStringConcat(db->dir, "/");
char *str = UtilStringConcat(2, db->dir, "/");
for (i = 0; i < ArraySize(args) - 1; i++)
{
char *tmp, *tmp2;
char *tmp;
tmp = UtilStringConcat(str, ArrayGet(args, i));
tmp2 = UtilStringConcat(tmp, "/");
tmp = UtilStringConcat(3, str, ArrayGet(args, i), "/");
Free(str);
Free(tmp);
str = tmp2;
str = tmp;
}
return str;
@ -235,11 +233,11 @@ static char *
DbFileName(Db * db, Array * args)
{
size_t i;
char *str = UtilStringConcat(db->dir, "/");
char *str = UtilStringConcat(2, db->dir, "/");
for (i = 0; i < ArraySize(args); i++)
{
char *tmp, *tmp2;
char *tmp;
char *arg = UtilStringDuplicate(ArrayGet(args, i));
size_t j = 0;
@ -260,15 +258,13 @@ DbFileName(Db * db, Array * args)
j++;
}
tmp = UtilStringConcat(str, arg);
tmp2 = UtilStringConcat(tmp,
tmp = UtilStringConcat(3, str, arg,
(i < ArraySize(args) - 1) ? "/" : ".json");
Free(arg);
Free(str);
Free(tmp);
str = tmp2;
str = tmp;
}
return str;

View file

@ -27,6 +27,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
@ -211,43 +212,53 @@ UtilStringDuplicate(char *inStr)
}
char *
UtilStringConcat(char *str1, char *str2)
UtilStringConcat(size_t nStr,...)
{
char *ret;
size_t str1Len, str2Len;
va_list argp;
char *str;
char *strp;
size_t strLen = 0;
size_t i;
str1Len = str1 ? strlen(str1) : 0;
str2Len = str2 ? strlen(str2) : 0;
va_start(argp, nStr);
for (i = 0; i < nStr; i++)
{
char *argStr = va_arg(argp, char *);
ret = Malloc(str1Len + str2Len + 1);
if (argStr)
{
strLen += strlen(argStr);
}
}
va_end(argp);
if (!ret)
{
return NULL;
}
str = Malloc(strLen + 1);
strp = str;
if (str1 && str2)
va_start(argp, nStr);
for (i = 0; i < nStr; i++)
{
strcpy(ret, str1);
strcpy(ret + str1Len, str2);
/* Manually copy chars instead of using strcopy() so we don't
* have to call strlen() on the strings again, and we aren't
* writing useless null chars. */
char *argStr = va_arg(argp, char *);
if (argStr)
{
while (*argStr)
{
*strp = *argStr;
strp++;
argStr++;
}
else
{
if (str1)
{
strcpy(ret, str1);
}
else if (str2)
{
strcpy(ret, str2);
}
else
{
strcpy(ret, "");
}
}
return ret;
va_end(argp);
str[strLen] = '\0';
return str;
}
int

View file

@ -45,7 +45,7 @@ extern char *
UtilStringDuplicate(char *);
extern char *
UtilStringConcat(char *, char *);
UtilStringConcat(size_t,...);
extern int
UtilSleepMillis(long);