summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarkus Mittendrein <git@maxmitti.tk>2020-04-23 15:31:04 +0200
committerMarkus Mittendrein <git@maxmitti.tk>2020-04-23 15:33:44 +0200
commit2288b901243d626b106fb45293d39d32006b04aa (patch)
tree28f8225ade4154d4d1fd9c2228191ab5bed991b3
parent048d5f65e487ae1def7f5abe62a773f8bd990c13 (diff)
downloadcc4group-2288b901243d626b106fb45293d39d32006b04aa.tar.gz
cc4group-2288b901243d626b106fb45293d39d32006b04aa.zip
Deduplicate getEntryByPath and getParentAndChildEntries code
-rw-r--r--src/cc4group.c106
1 files changed, 43 insertions, 63 deletions
diff --git a/src/cc4group.c b/src/cc4group.c
index f9bbbd0..cb37064 100644
--- a/src/cc4group.c
+++ b/src/cc4group.c
@@ -1915,25 +1915,14 @@ static bool cc4group_extractAll(CC4Group* const this, const char* const targetPa
// sets *error to true if any error is set
-static const C4GroupEntryData* cc4group_getEntryByPath(CC4Group* const this, const char* const entryPath, bool const allowRoot, bool* const error, const C4GroupEntryData* const inParent)
+static GroupEntryListEntry* cc4group_getChildListEntryByPath(CC4Group* const this, const char* const entryPath, bool* const error, const C4GroupEntryData* const inParent)
{
assert(this);
assert(this->root.children);
+ assert(entryPath != NULL && *entryPath != '\0');
const C4GroupEntryData* currentParent = inParent ? inParent : &this->root;
- if(entryPath == NULL || *entryPath == '\0')
- {
- if(allowRoot)
- {
- return currentParent;
- }
- else
- {
- SET_MESSAGE_ERROR("The root directory is not allowed for this");
- *error = true;
- return NULL;
- }
- }
+ GroupEntryListEntry* listEntry = NULL;
char* path = strdup(entryPath);
@@ -1954,7 +1943,7 @@ static const C4GroupEntryData* cc4group_getEntryByPath(CC4Group* const this, con
}
else if(!currentParent->core.Directory)
{
- currentParent = NULL;
+ listEntry = NULL;
break;
}
@@ -1962,7 +1951,7 @@ static const C4GroupEntryData* cc4group_getEntryByPath(CC4Group* const this, con
if(!children)
{
*error = true;
- currentParent = NULL;
+ listEntry = NULL;
goto ret;
}
@@ -1972,6 +1961,7 @@ static const C4GroupEntryData* cc4group_getEntryByPath(CC4Group* const this, con
if(strcmp(entry->value.core.FileName, part) == 0)
{
currentParent = &entry->value;
+ listEntry = entry;
found = true;
break;
}
@@ -1979,7 +1969,7 @@ static const C4GroupEntryData* cc4group_getEntryByPath(CC4Group* const this, con
if(!found)
{
- currentParent = NULL;
+ listEntry = NULL;
break;
}
}
@@ -1987,7 +1977,36 @@ static const C4GroupEntryData* cc4group_getEntryByPath(CC4Group* const this, con
ret:
free(path);
- return currentParent;
+ return listEntry;
+}
+
+// sets *error to true if any error is set
+static const C4GroupEntryData* cc4group_getEntryByPath(CC4Group* const this, const char* const entryPath, bool const allowRoot, bool* const error, const C4GroupEntryData* const inParent)
+{
+ assert(this);
+ assert(this->root.children);
+
+ if(entryPath == NULL || *entryPath == '\0')
+ {
+ if(allowRoot)
+ {
+ return inParent ? inParent : &this->root;
+ }
+ else
+ {
+ SET_MESSAGE_ERROR("The root directory is not allowed for this");
+ *error = true;
+ return NULL;
+ }
+ }
+
+ GroupEntryListEntry* listEntry = cc4group_getChildListEntryByPath(this, entryPath, error, inParent);
+ if(*error || listEntry == NULL)
+ {
+ return NULL;
+ }
+
+ return &listEntry->value;
}
// sets error
@@ -2899,60 +2918,21 @@ static char* cc4group_splitParentAndChildPaths(char* const combinedPath, const c
static bool cc4group_getParentAndChildEntries(CC4Group* const this, const char* const path, const C4GroupEntryData** parentEntry, GroupEntryListEntry** const childEntry)
{
- const C4GroupEntryData* parent;
-
- if(path == NULL || *path == '\0')
- {
- SET_MESSAGE_ERROR("The path must contain at least one parent folder separated with /");
- return false;
- }
-
- char* myPath = strdup(path);
-
- if(myPath == NULL)
- {
- SET_ERRNO_ERROR("strdup: duplicating the path");
- return false;
- }
-
- const char* parentPath;
- char* entryName = cc4group_splitParentAndChildPaths(myPath, &parentPath);
-
- parent = cc4group_getDirectoryByPath(this, parentPath, true, NULL);
-
- if(parent == NULL)
- {
- free(myPath);
- return false;
- }
- *parentEntry = parent;
-
-
- GroupEntryList* children = cc4group_getChildren(this, parent);
- if(children == NULL)
+ bool error = false;
+ GroupEntryListEntry* listEntry = cc4group_getChildListEntryByPath(this, path, &error, NULL);
+ if(error)
{
- free(myPath);
return false;
}
- GroupEntryListEntry* child = NULL;
- ForeachGroupEntry(children)
- {
- if(strcmp(entry->value.core.FileName, entryName) == 0)
- {
- child = entry;
- break;
- }
- }
- free(myPath);
-
- if(child == NULL)
+ if(listEntry == NULL)
{
SET_MESSAGE_ERROR("The desired file was not found in the group file");
return false;
}
- *childEntry = child;
+ *childEntry = listEntry;
+ *parentEntry = listEntry->value.parent;
return true;
}