aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan200101 <sentrycraft123@gmail.com>2021-01-02 03:01:43 +0100
committerJan200101 <sentrycraft123@gmail.com>2021-03-26 18:53:25 +0100
commit9e558ba6d93da7ce3b9c5b57d53237ee695e7b54 (patch)
treea6a6d7be5480d124f39049c670d8c5651ef2bcec
parente3db069e103960a93b33ee5f0de57bb0594c6096 (diff)
downloadpolecat-9e558ba6d93da7ce3b9c5b57d53237ee695e7b54.tar.gz
polecat-9e558ba6d93da7ce3b9c5b57d53237ee695e7b54.zip
improve parser logic and math reuse
-rw-r--r--src/lutris.c29
1 files changed, 17 insertions, 12 deletions
diff --git a/src/lutris.c b/src/lutris.c
index bdecaff..fe5c08c 100644
--- a/src/lutris.c
+++ b/src/lutris.c
@@ -557,8 +557,11 @@ size_t parseVar(char** pvar, struct list_t* variables, size_t variable_count)
char* tail, *end;
char* buf, *value;
+ size_t offset;
+ size_t keylen, backlen;
+
size_t varcount = 0;
- size_t offset = 0;
+
while (*head != '\0')
{
@@ -570,9 +573,7 @@ size_t parseVar(char** pvar, struct list_t* variables, size_t variable_count)
end = strlen(var) + var;
- offset = head - var;
-
- int bufend = (head-var);
+ offset = (size_t)(head-var);
// find variable key
tail = ++head;
@@ -582,14 +583,18 @@ size_t parseVar(char** pvar, struct list_t* variables, size_t variable_count)
++tail;
}
- if ((tail-head) > 0)
+ // setup variables for reuse
+ keylen = (size_t)(tail-head);
+ backlen = (size_t)(end-tail);
+
+ if (keylen > 0)
{
// resolve variable key and store it
- buf = malloc(tail-head);
- strncpy(buf, head, tail-head);
+ buf = malloc(keylen);
+ strncpy(buf, head, keylen);
for (size_t i = 0; i < variable_count; ++i)
{
- if (strncmp(variables[i].key, buf, tail-head+1) == 0)
+ if (strncmp(variables[i].key, buf, keylen+1) == 0)
{
switch (variables[i].type)
{
@@ -614,15 +619,15 @@ size_t parseVar(char** pvar, struct list_t* variables, size_t variable_count)
if (!value) continue;
// copy everything from variable key end to end of string
- buf = malloc(end-tail+1);
- strncpy(buf, tail, end-tail+1);
+ buf = malloc(backlen+1);
+ strncpy(buf, tail, backlen+1);
- size_t varsize = (head-var) + strlen(value) + (end-tail) + 1;
+ size_t varsize = offset + strlen(value) + backlen + 1;
var = realloc(var, varsize);
// end of the string up until the variable
// we cannot fetch this after the realloc because it points to completly different memory making pointer math impossible
- var[bufend] = '\0';
+ var[offset] = '\0';
strncat(var, value, varsize - strlen(var) - 1);
strncat(var, buf, varsize - strlen(var) - 1);
// cleanup