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 UserInteractiveAuth (move docs from Matrix)
[ ] Document MemoryHexDump [ ] Document MemoryHexDump
[ ] Move String functions to a new String.h? [ ] 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?) [x] Look into seeding random strings (possibly create Random.h?)
[~] User registration [~] User registration
[x] Username validation [x] Username validation

View file

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

View file

@ -27,6 +27,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h>
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#include <math.h> #include <math.h>
@ -211,43 +212,53 @@ UtilStringDuplicate(char *inStr)
} }
char * char *
UtilStringConcat(char *str1, char *str2) UtilStringConcat(size_t nStr,...)
{ {
char *ret; va_list argp;
size_t str1Len, str2Len; char *str;
char *strp;
size_t strLen = 0;
size_t i;
str1Len = str1 ? strlen(str1) : 0; va_start(argp, nStr);
str2Len = str2 ? strlen(str2) : 0; 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) str = Malloc(strLen + 1);
{ strp = str;
return NULL;
}
if (str1 && str2) va_start(argp, nStr);
for (i = 0; i < nStr; i++)
{ {
strcpy(ret, str1); /* Manually copy chars instead of using strcopy() so we don't
strcpy(ret + str1Len, str2); * 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 int

View file

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