From 4338534c9cdacf98c310ab625a1d17988dde1949 Mon Sep 17 00:00:00 2001 From: Markus Mittendrein Date: Sat, 18 Aug 2018 23:11:02 +0200 Subject: Add possibility to leave data ownership to the caller of setEntryData and remove possibility to specify entry data with createFile --- src/cc4group.c | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-) (limited to 'src/cc4group.c') diff --git a/src/cc4group.c b/src/cc4group.c index 199dcdc..5109b0a 100644 --- a/src/cc4group.c +++ b/src/cc4group.c @@ -46,6 +46,7 @@ static bool cc4group_getEntryData(const CC4Group* const this, const char* const struct list_GroupEntryList; typedef struct C4GroupEntryData_t { C4GroupEntryCore core; + bool freeData; union { uint8_t* data; // if the entry is a directory, the beginning of the data contains the header @@ -164,7 +165,7 @@ static bool buildChildren(CC4Group* const this, C4GroupEntryData* const entry, s return false; } - C4GroupEntryData* childEntry = &GroupEntryListAppend(entry->children, (C4GroupEntryData){.core = *core, .data = childData + core->Offset, .children = NULL, .parent = entry})->value; + C4GroupEntryData* childEntry = &GroupEntryListAppend(entry->children, (C4GroupEntryData){.core = *core, .data = childData + core->Offset, .freeData = false, .children = NULL, .parent = entry})->value; if(core->Directory) { @@ -183,6 +184,11 @@ static void deleteChildren(GroupEntryList* const entries) { ForeachGroupEntry(entries) { + if(entry->value.freeData && entry->value.data != NULL) + { + free(entry->value.data); + } + if(entry->value.core.Directory) { deleteChildren(entry->value.children); @@ -561,6 +567,8 @@ static void cc4group_init(CC4Group* const this) this->path = ""; this->subPath = ""; + this->root.data = NULL; + this->root.freeData = false; this->root.parent = NULL; this->root.children = NULL; this->root.core.Directory = true; @@ -1463,6 +1471,7 @@ static C4GroupEntryData* cc4group_createEntry(CC4Group* const this, const char* C4GroupEntryData entry; entry.data = NULL; + entry.freeData = false; entry.children = GroupEntryListNew(); C4GroupEntryCore_init(&entry.core); @@ -1503,7 +1512,7 @@ static bool cc4group_createDirectory(CC4Group* const this, const char* const pat return true; } -static bool cc4group_createFile(CC4Group* const this, const char* const path, void* const data, size_t const size) +static bool cc4group_createFile(CC4Group* const this, const char* const path) { assert(this); assert(path); @@ -1515,39 +1524,43 @@ static bool cc4group_createFile(CC4Group* const this, const char* const path, vo return false; } - if(size != 0 && data != NULL) - { - entry->core.Size = size; - entry->data = data; - - AddCleanupJob(free, data); - } - return true; } -static bool cc4group_setEntryData(const CC4Group* const this, const char* const entryPath, void* const data, size_t const size) +static bool cc4group_setEntryData(CC4Group* const this, const char* const entryPath, const void* const data, size_t const size, bool const freeData) { assert(this); assert(entryPath); C4GroupEntryData* entry = (C4GroupEntryData*)cc4group_getEntryByPath(this, entryPath); - if(entry == NULL || entry->core.Directory) + if(entry == NULL) { + SET_MESSAGE_ERROR("The desired target file does not exist"); return false; } - if(data != NULL && size != 0) + if(entry->core.Directory) { - entry->data = data; - entry->core.Size = size; + SET_MESSAGE_ERROR("The desired target file is a directory"); + return false; + } - AddCleanupJob(free, data); + if(entry->freeData && entry->data != NULL) + { + free(entry->data); + } + + if(data != NULL) + { + entry->data = (void*)data; + entry->core.Size = size; + entry->freeData = freeData; } else { entry->data = NULL; entry->core.Size = 0; + entry->freeData = false; } return true; -- cgit v1.2.3-54-g00ecf