diff options
Diffstat (limited to 'src/cc4group.c')
| -rw-r--r-- | src/cc4group.c | 88 |
1 files changed, 50 insertions, 38 deletions
diff --git a/src/cc4group.c b/src/cc4group.c index 2b5a6b4..b3653b7 100644 --- a/src/cc4group.c +++ b/src/cc4group.c @@ -128,6 +128,7 @@ static const C4GroupEntryData* cc4group_getEntryByPath(CC4Group* const this, con static const C4GroupEntryData* cc4group_getDirectoryByPath(CC4Group* const this, const char* const entryPath, bool allowRoot); static const C4GroupEntryData* cc4group_getFileByPath(CC4Group* const this, const char* const entryPath); static const C4GroupEntryData* cc4group_getFileOrDirectoryByPath(CC4Group* const this, const char* const entryPath, bool allowRoot); +static GroupEntryList* cc4group_getChildren(CC4Group* const this, const C4GroupEntryData* entry); static char* cc4group_strerrorFormatter(int32_t const code, const char* const method, const char* const causer, void* const data) { @@ -990,14 +991,14 @@ static void cc4group_init(CC4Group* const this) this->error.lastFormattedMessage = NULL; } -static void cc4group_setMakerRecursively(const C4GroupEntryData* groupEntry, const char* const maker) +static void cc4group_setMakerRecursively(CC4Group* const this, const C4GroupEntryData* groupEntry, const char* const maker) { - ForeachGroupEntry(groupEntry->children) + ForeachGroupEntry(cc4group_getChildren(this, groupEntry)) { if(entry->value.core.Directory) { C4GroupHeader_setMaker(entry->value.header, maker); - cc4group_setMakerRecursively(&entry->value, maker); + cc4group_setMakerRecursively(this, &entry->value, maker); } } } @@ -1017,19 +1018,19 @@ static bool cc4group_setMaker(CC4Group* const this, const char* const maker, con C4GroupHeader_setMaker(entry->header, maker); if(recursive) { - cc4group_setMakerRecursively(entry, maker); + cc4group_setMakerRecursively(this, entry, maker); } return true; } -static void cc4group_setOfficialRecursively(const C4GroupEntryData* groupEntry, bool const official) +static void cc4group_setOfficialRecursively(CC4Group* const this, const C4GroupEntryData* groupEntry, bool const official) { - ForeachGroupEntry(groupEntry->children) + ForeachGroupEntry(cc4group_getChildren(this, groupEntry)) { if(entry->value.core.Directory) { C4GroupHeader_setOfficial(entry->value.header, official); - cc4group_setOfficialRecursively(&entry->value, official); + cc4group_setOfficialRecursively(this, &entry->value, official); } } } @@ -1048,20 +1049,20 @@ static bool cc4group_setOfficial(CC4Group* const this, bool const official, cons C4GroupHeader_setOfficial(entry->header, official); if(recursive) { - cc4group_setOfficialRecursively(entry, official); + cc4group_setOfficialRecursively(this, entry, official); } return true; } -static void cc4group_setCreationRecursively(const C4GroupEntryData* groupEntry, int32_t const creation) +static void cc4group_setCreationRecursively(CC4Group* const this, const C4GroupEntryData* groupEntry, int32_t const creation) { - ForeachGroupEntry(groupEntry->children) + ForeachGroupEntry(cc4group_getChildren(this, groupEntry)) { entry->value.core.Modified = creation; if(entry->value.core.Directory) { C4GroupHeader_setCreation(entry->value.header, creation); - cc4group_setCreationRecursively(&entry->value, creation); + cc4group_setCreationRecursively(this, &entry->value, creation); } } } @@ -1084,7 +1085,7 @@ static bool cc4group_setCreation(CC4Group* const this, int32_t const creation, c C4GroupHeader_setCreation(entry->header, creation); if(recursive) { - cc4group_setCreationRecursively(entry, creation); + cc4group_setCreationRecursively(this, entry, creation); } } return true; @@ -1253,7 +1254,7 @@ static bool cc4group_extractChildren(CC4Group* const this, const C4GroupEntryDat goto ret; } - ForeachGroupEntry(root->children) + ForeachGroupEntry(cc4group_getChildren(this, root)) { if(!cc4group_extractChildren(this, &entry->value, tmpPath)) { @@ -1328,9 +1329,8 @@ static const C4GroupEntryData* cc4group_getEntryByPath(CC4Group* const this, con break; } - // TODO: lazy bool found = false; - ForeachGroupEntry(currentParent->children) + ForeachGroupEntry(cc4group_getChildren(this, currentParent)) { if(strcmp(entry->value.core.FileName, part) == 0) { @@ -1446,6 +1446,14 @@ static const uint8_t* cc4group_getOnlyEntryData(CC4Group* const this, const C4Gr return entry->data; } +static GroupEntryList* cc4group_getChildren(CC4Group* const this, const C4GroupEntryData* entry) +{ + // TODO: lazy + (void)this; + + return entry->children; +} + static bool cc4group_getEntryData(CC4Group* const this, const char* const entryPath, const void** const data, size_t* size) { assert(this); @@ -1502,14 +1510,15 @@ static const char* cc4group_getErrorCauser(const CC4Group* const this) return this->error.causer; } -static size_t cc4group_calculateEntrySizes(C4GroupEntryData* const entryData) +static size_t cc4group_calculateEntrySizes(CC4Group* const this, C4GroupEntryData* const entryData) { if(entryData->core.Directory) { - size_t sum = sizeof(C4GroupHeader) + GroupEntryListSize(entryData->children) * sizeof(C4GroupEntryCore); - ForeachGroupEntry(entryData->children) + GroupEntryList* children = cc4group_getChildren(this, entryData); + size_t sum = sizeof(C4GroupHeader) + GroupEntryListSize(children) * sizeof(C4GroupEntryCore); + ForeachGroupEntry(children) { - sum += cc4group_calculateEntrySizes(&entry->value); + sum += cc4group_calculateEntrySizes(this, &entry->value); } return entryData->core.Size = sum; @@ -1525,7 +1534,7 @@ static void cc4group_calculateEntryCRC(CC4Group* const this, C4GroupEntryData* c if(groupEntry->core.Directory) { uint32_t crc = 0; - ForeachGroupEntry(groupEntry->children) + ForeachGroupEntry(cc4group_getChildren(this, groupEntry)) { cc4group_calculateEntryCRC(this, &entry->value); crc ^= entry->value.core.CRC; @@ -1643,7 +1652,8 @@ static bool cc4group_deflateToCallback(WriteCallback* const callback, const void static bool cc4group_writeEntries(CC4Group* const this, C4GroupEntryData* const entryData, WriteCallback* const callback) { C4GroupHeader header = *(C4GroupHeader*)cc4group_getOnlyEntryData(this, entryData); - header.Entries = GroupEntryListSize(entryData->children); + GroupEntryList* children = cc4group_getChildren(this, entryData); + header.Entries = GroupEntryListSize(children); header.Ver1 = 1; header.Ver2 = 2; @@ -1662,7 +1672,7 @@ static bool cc4group_writeEntries(CC4Group* const this, C4GroupEntryData* const size_t offset = 0; - ForeachGroupEntry(entryData->children) + ForeachGroupEntry(children) { entry->value.core.Offset = offset; entry->value.core.Packed = 1; @@ -1676,7 +1686,7 @@ static bool cc4group_writeEntries(CC4Group* const this, C4GroupEntryData* const offset += entry->value.core.Size; } - ForeachGroupEntry(entryData->children) + ForeachGroupEntry(children) { if(entry->value.core.Directory) { @@ -1756,7 +1766,7 @@ static bool cc4group_saveWithWriteCallback(CC4Group* const this, CC4Group_WriteC bool success = false; - cc4group_calculateEntrySizes(&this->root); + cc4group_calculateEntrySizes(this, &this->root); cc4group_calculateEntryCRC(this, &this->root); if(!cc4group_writeEntries(this, &this->root, &callback)) { @@ -1928,8 +1938,8 @@ static bool cc4group_getEntryInfos(CC4Group* const this, const char* const path, node = &this->root; } - - *size = GroupEntryListSize(node->children); + GroupEntryList* children = cc4group_getChildren(this, node); + *size = GroupEntryListSize(children); CC4Group_EntryInfo* myInfos = malloc(*size * sizeof(CC4Group_EntryInfo)); if(myInfos == NULL) { @@ -1938,7 +1948,7 @@ static bool cc4group_getEntryInfos(CC4Group* const this, const char* const path, } *infos = myInfos; - ForeachGroupEntry(node->children) + ForeachGroupEntry(children) { cc4group_getEntryInfoForEntry(&entry->value, myInfos++); } @@ -1995,7 +2005,7 @@ static bool cc4group_getParentAndChildEntries(CC4Group* const this, const char* *parentEntry = parent; GroupEntryListEntry* child = NULL; - ForeachGroupEntry(parent->children) + ForeachGroupEntry(cc4group_getChildren(this, parent)) { if(strcmp(entry->value.core.FileName, entryName) == 0) { @@ -2036,23 +2046,25 @@ static bool cc4group_deleteEntry(CC4Group* const this, const char* const path, b return false; } - deleteChildren(deleteEntry->value.children); + deleteChildren(cc4group_getChildren(this, &deleteEntry->value)); } - GroupEntryListRemove(parent->children, deleteEntry); - parent->header->Entries = GroupEntryListSize(parent->children); + GroupEntryList* parentChildren = cc4group_getChildren(this, parent); + GroupEntryListRemove(parentChildren, deleteEntry); + parent->header->Entries = GroupEntryListSize(parentChildren); return true; } -static C4GroupEntryData* cc4group_addEntryToDirectory(C4GroupEntryData* const directory, const C4GroupEntryData* const entry) +static C4GroupEntryData* cc4group_addEntryToDirectory(CC4Group* const this, C4GroupEntryData* const directory, const C4GroupEntryData* const entry) { assert(directory->core.Directory); // TODO? Implement engine-optimized order sorting here? - C4GroupEntryData* newEntry = &GroupEntryListAppend(directory->children, *entry)->value; + GroupEntryList* directoryChildren = cc4group_getChildren(this, directory); + C4GroupEntryData* newEntry = &GroupEntryListAppend(directoryChildren, *entry)->value; newEntry->parent = directory; - directory->header->Entries = GroupEntryListSize(directory->children); + directory->header->Entries = GroupEntryListSize(directoryChildren); return newEntry; } @@ -2101,8 +2113,8 @@ static bool cc4group_renameEntry(CC4Group* const this, const char* const oldPath } C4GroupEntryCore_setFileName(&entry->value.core, targetName); - cc4group_addEntryToDirectory(newParent, &entry->value); - GroupEntryListRemove(oldParent->children, entry); + cc4group_addEntryToDirectory(this, newParent, &entry->value); + GroupEntryListRemove(cc4group_getChildren(this, oldParent), entry); free(targetPath); return true; @@ -2150,7 +2162,7 @@ static C4GroupEntryData* cc4group_createEntry(CC4Group* const this, const char* free(targetPath); - return cc4group_addEntryToDirectory(parent, &entry); + return cc4group_addEntryToDirectory(this, parent, &entry); } static bool cc4group_createDirectory(CC4Group* const this, const char* const path) @@ -2281,7 +2293,7 @@ static CC4Group* cc4group_openAsChild(CC4Group* const this, const char* const pa child->root.data = entry->data; child->root.core.Directory = 1; child->root.core.FileName[0] = '\0'; - child->root.children = entry->children; + child->root.children = cc4group_getChildren(this, entry); child->parent = this; ++this->referenceCounter; |
