From 4dcfb134c3ae43f4f1325228a86137df00e27693 Mon Sep 17 00:00:00 2001 From: Markus Mittendrein Date: Sun, 19 Aug 2018 13:28:44 +0200 Subject: Refactor some calls to cc4group_getEntryByPath into several common functions --- src/cc4group.c | 139 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 76 insertions(+), 63 deletions(-) (limited to 'src/cc4group.c') diff --git a/src/cc4group.c b/src/cc4group.c index 5109b0a..8fd57d5 100644 --- a/src/cc4group.c +++ b/src/cc4group.c @@ -38,7 +38,7 @@ static bool cc4group_extractAll(CC4Group* const this, const char* const targetPa static bool cc4group_extractSingle(CC4Group* const this, const char* const entryPath, const char* const targetPath); // the group owns the data pointed to. the pointer is valid until the group destructor is called -static bool cc4group_getEntryData(const CC4Group* const this, const char* const entryPath, const void** const data, size_t* size); +static bool cc4group_getEntryData(CC4Group* const this, const char* const entryPath, const void** const data, size_t* size); #define C4GroupMagic1 0x1e #define C4GroupMagic2 0x8c @@ -91,6 +91,9 @@ struct CC4Group_t { }; static const C4GroupEntryData* cc4group_getEntryByPath(const CC4Group* const this, const char* const entryPath); +static const C4GroupEntryData* cc4group_getDirectoryByPath(CC4Group* const this, const char* const entryPath); +static const C4GroupEntryData* cc4group_getFileByPath(CC4Group* const this, const char* const entryPath); +static const C4GroupEntryData* cc4group_getFileOrDirectoryByPath(CC4Group* const this, const char* const entryPath); static char* cc4group_strerrorFormatter(int32_t const code, const char* const method, const char* const causer, void* const data) { @@ -606,16 +609,10 @@ static bool cc4group_setMaker(CC4Group* const this, const char* const maker, con } else { - entry = cc4group_getEntryByPath(this, path); + entry = cc4group_getDirectoryByPath(this, path); if(entry == NULL) { - SET_MESSAGE_ERROR("The desired target directory does not exist"); - return false; - } - else if(!entry->core.Directory) - { - SET_MESSAGE_ERROR("The desired target is not a directory"); return false; } } @@ -651,16 +648,10 @@ static bool cc4group_setOfficial(CC4Group* const this, bool const official, cons } else { - entry = cc4group_getEntryByPath(this, path); + entry = cc4group_getDirectoryByPath(this, path); if(entry == NULL) { - SET_MESSAGE_ERROR("The desired target directory does not exist"); - return false; - } - else if(!entry->core.Directory) - { - SET_MESSAGE_ERROR("The desired target is not a directory"); return false; } } @@ -697,11 +688,10 @@ static bool cc4group_setCreation(CC4Group* const this, int32_t const creation, c } else { - entry = (C4GroupEntryData*)cc4group_getEntryByPath(this, path); + entry = (C4GroupEntryData*)cc4group_getFileOrDirectoryByPath(this, path); if(entry == NULL) { - SET_MESSAGE_ERROR("The desired target file or directory does not exist"); return false; } } @@ -780,11 +770,10 @@ static bool cc4group_openExisting(CC4Group* const this, const char* const path) { this->realRoot = this->root; - const C4GroupEntryData* subRoot = cc4group_getEntryByPath(this, this->subPath); + const C4GroupEntryData* subRoot = cc4group_getDirectoryByPath(this, this->subPath); - if(subRoot == NULL || !subRoot->core.Directory) + if(subRoot == NULL) { - SET_MESSAGE_ERROR("The desired subgroup does not exist as a child in the mother group"); return false; } @@ -902,6 +891,8 @@ static bool cc4group_extractAll(CC4Group* const this, const char* const targetPa return cc4group_extractChildren(this, &this->root, targetPath); } + +// does not set any error static const C4GroupEntryData* cc4group_getEntryByPath(const CC4Group* const this, const char* const entryPath) { assert(this); @@ -953,31 +944,84 @@ static const C4GroupEntryData* cc4group_getEntryByPath(const CC4Group* const thi return currentParent; } +// sets error +static const C4GroupEntryData* cc4group_getDirectoryByPath(CC4Group* const this, const char* const entryPath) +{ + // asserts are in cc4group_getEntryByPath + const C4GroupEntryData* entry = cc4group_getEntryByPath(this, entryPath); + if(entry == NULL) + { + SET_MESSAGE_ERROR("The dirctory was not found in the group file"); + return NULL; + } + + if(!entry->core.Directory) + { + SET_MESSAGE_ERROR("The specified path is a file, not a directory"); + return NULL; + } + + return entry; +} + +// sets error +static const C4GroupEntryData* cc4group_getFileByPath(CC4Group* const this, const char* const entryPath) +{ + // asserts are in cc4group_getEntryByPath + const C4GroupEntryData* entry = cc4group_getEntryByPath(this, entryPath); + if(entry == NULL) + { + SET_MESSAGE_ERROR("The file was not found in the group file"); + return NULL; + } + + if(entry->core.Directory) + { + SET_MESSAGE_ERROR("The specified path is a directory, not a file"); + return NULL; + } + + return entry; +} + +// sets error +static const C4GroupEntryData* cc4group_getFileOrDirectoryByPath(CC4Group* const this, const char* const entryPath) +{ + // asserts are in cc4group_getEntryByPath + const C4GroupEntryData* entry = cc4group_getEntryByPath(this, entryPath); + if(entry == NULL) + { + SET_MESSAGE_ERROR("The file or directory was not found in the group file"); + return NULL; + } + + return entry; +} + static bool cc4group_extractSingle(CC4Group* const this, const char* const entryPath, const char* const targetPath) { assert(this); assert(entryPath); assert(targetPath); - const C4GroupEntryData* entry = cc4group_getEntryByPath(this, entryPath); + const C4GroupEntryData* entry = cc4group_getFileOrDirectoryByPath(this, entryPath); if(entry == NULL) { - SET_MESSAGE_ERROR("The desired file was not found in the group file"); return false; } return cc4group_extractChildren(this, entry, targetPath); } -static bool cc4group_getEntryData(const CC4Group* const this, const char* const entryPath, const void** const data, size_t* size) +static bool cc4group_getEntryData(CC4Group* const this, const char* const entryPath, const void** const data, size_t* size) { assert(this); assert(entryPath); assert(data); assert(size); - const C4GroupEntryData* entry = cc4group_getEntryByPath(this, entryPath); - if(entry == NULL || entry->core.Directory) + const C4GroupEntryData* entry = cc4group_getFileByPath(this, entryPath); + if(entry == NULL) { return false; } @@ -1242,10 +1286,9 @@ static bool cc4group_getEntryInfo(CC4Group* const this, const char* const path, assert(path); assert(info); - const C4GroupEntryData* entry = cc4group_getEntryByPath(this, path); + const C4GroupEntryData* entry = cc4group_getFileOrDirectoryByPath(this, path); if(entry == NULL) { - SET_MESSAGE_ERROR("The desired file was not found in the group file"); return false; } @@ -1263,16 +1306,9 @@ static bool cc4group_getEntryInfos(CC4Group* const this, const char* const path, if(path != NULL && *path != '\0') { - const C4GroupEntryData* entry = cc4group_getEntryByPath(this, path); + const C4GroupEntryData* entry = cc4group_getDirectoryByPath(this, path); if(entry == NULL) { - SET_MESSAGE_ERROR("The desired subgroup was not found in the group file"); - return false; - } - - if(!entry->core.Directory) - { - SET_MESSAGE_ERROR("The desired subgroup is a file instead of a group"); return false; } @@ -1327,11 +1363,10 @@ static bool cc4group_getParentAndChildEntries(CC4Group* const this, const char* const char* parentPath; char* entryName = cc4group_splitParentAndChildPaths(myPath, &parentPath); - parent = cc4group_getEntryByPath(this, parentPath); + parent = cc4group_getDirectoryByPath(this, parentPath); if(parent == NULL) { - SET_MESSAGE_ERROR("The containing directory of the desired file was not found in the group file"); free(myPath); return false; } @@ -1425,10 +1460,9 @@ static bool cc4group_renameEntry(CC4Group* const this, const char* const oldPath const char* targetDirectory; char* targetName = cc4group_splitParentAndChildPaths(targetPath, &targetDirectory); - C4GroupEntryData* newParent = (C4GroupEntryData*)cc4group_getEntryByPath(this, targetDirectory); + C4GroupEntryData* newParent = (C4GroupEntryData*)cc4group_getDirectoryByPath(this, targetDirectory); if(newParent == NULL) { - SET_MESSAGE_ERROR("The desired target directory does not exist"); free(targetPath); return false; } @@ -1453,17 +1487,9 @@ static C4GroupEntryData* cc4group_createEntry(CC4Group* const this, const char* const char* targetDirectory; char* targetName = cc4group_splitParentAndChildPaths(targetPath, &targetDirectory); - C4GroupEntryData* parent = (C4GroupEntryData*)cc4group_getEntryByPath(this, targetDirectory); + C4GroupEntryData* parent = (C4GroupEntryData*)cc4group_getDirectoryByPath(this, targetDirectory); if(parent == NULL) { - SET_MESSAGE_ERROR("The desired containing target directory does not exist"); - free(targetPath); - return NULL; - } - - if(!parent->core.Directory) - { - SET_MESSAGE_ERROR("The desired containing target directory is a file"); free(targetPath); return NULL; } @@ -1532,16 +1558,9 @@ static bool cc4group_setEntryData(CC4Group* const this, const char* const entryP assert(this); assert(entryPath); - C4GroupEntryData* entry = (C4GroupEntryData*)cc4group_getEntryByPath(this, entryPath); + C4GroupEntryData* entry = (C4GroupEntryData*)cc4group_getFileByPath(this, entryPath); if(entry == NULL) { - SET_MESSAGE_ERROR("The desired target file does not exist"); - return false; - } - - if(entry->core.Directory) - { - SET_MESSAGE_ERROR("The desired target file is a directory"); return false; } @@ -1577,16 +1596,10 @@ static bool cc4group_setExecutable(CC4Group* const this, bool const executable, } else { - entry = (C4GroupEntryData*)cc4group_getEntryByPath(this, path); + entry = (C4GroupEntryData*)cc4group_getFileByPath(this, path); if(entry == NULL) { - SET_MESSAGE_ERROR("The desired target file does not exist"); - return false; - } - else if(entry->core.Directory) - { - SET_MESSAGE_ERROR("The desired target is not a file"); return false; } } -- cgit v1.2.3-54-g00ecf