summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarkus Mittendrein <git@maxmitti.tk>2020-04-02 02:59:52 +0200
committerMarkus Mittendrein <git@maxmitti.tk>2020-04-02 02:59:52 +0200
commit327b9ad4985a1939826746c98b44957dd4ca59ce (patch)
tree953424b793bb0a701eb4bdbc548e976114718839
parent43b04d71d80d7bbc610eef1bce838cee4cf1631a (diff)
downloadcc4group-327b9ad4985a1939826746c98b44957dd4ca59ce.tar.gz
cc4group-327b9ad4985a1939826746c98b44957dd4ca59ce.zip
Add cc4group.extractSingleRename
-rw-r--r--src/cc4group.c32
-rw-r--r--src/cc4group.h7
-rw-r--r--src/cppc4group.cpp4
-rw-r--r--src/cppc4group.hpp4
4 files changed, 36 insertions, 11 deletions
diff --git a/src/cc4group.c b/src/cc4group.c
index 92dd784..c3454f8 100644
--- a/src/cc4group.c
+++ b/src/cc4group.c
@@ -1783,7 +1783,7 @@ static bool cc4group_extractEntry(CC4Group* const this, const C4GroupEntryData*
return success;
}
-static bool cc4group_extractChildren(CC4Group* const this, const C4GroupEntryData* const root, const char* const targetPath)
+static bool cc4group_extractChildren(CC4Group* const this, const C4GroupEntryData* const root, const char* const targetPath, bool const rename)
{
assert(root);
@@ -1797,11 +1797,14 @@ static bool cc4group_extractChildren(CC4Group* const this, const C4GroupEntryDat
}
strcpy(tmpPath, targetPath);
- if(*targetPath != '\0')
+ if(!rename)
{
- strcat(tmpPath, "/");
+ if(*targetPath != '\0')
+ {
+ strcat(tmpPath, "/");
+ }
+ strcat(tmpPath, root->core.FileName);
}
- strcat(tmpPath, root->core.FileName);
bool success = true;
@@ -1823,7 +1826,7 @@ static bool cc4group_extractChildren(CC4Group* const this, const C4GroupEntryDat
ForeachGroupEntry(children)
{
- if(!cc4group_extractChildren(this, &entry->value, tmpPath))
+ if(!cc4group_extractChildren(this, &entry->value, tmpPath, false))
{
success = false;
goto ret;
@@ -1854,7 +1857,7 @@ static bool cc4group_extractAll(CC4Group* const this, const char* const targetPa
{
assert(this);
- return cc4group_extractChildren(this, &this->root, targetPath);
+ return cc4group_extractChildren(this, &this->root, targetPath, false);
}
@@ -2017,7 +2020,21 @@ static bool cc4group_extractSingle(CC4Group* const this, const char* const entry
return false;
}
- return cc4group_extractChildren(this, entry, targetPath);
+ return cc4group_extractChildren(this, entry, targetPath, false);
+}
+
+static bool cc4group_extractSingleRename(CC4Group* const this, const char* const entryPath, const char* const targetPath)
+{
+ assert(this);
+ assert(targetPath);
+
+ const C4GroupEntryData* entry = cc4group_getFileOrDirectoryByPath(this, entryPath, true, NULL);
+ if(entry == NULL)
+ {
+ return false;
+ }
+
+ return cc4group_extractChildren(this, entry, targetPath, true);
}
static void* cc4group_mmappedFileManagementStart(void* const data, size_t const size, void* const arg)
@@ -3327,6 +3344,7 @@ CC4Group_API cc4group = {
.extractAll = cc4group_extractAll,
.extractSingle = cc4group_extractSingle,
+ .extractSingleRename = cc4group_extractSingleRename,
.getEntryInfo = cc4group_getEntryInfo,
diff --git a/src/cc4group.h b/src/cc4group.h
index d81e84a..a7d2dcc 100644
--- a/src/cc4group.h
+++ b/src/cc4group.h
@@ -305,9 +305,14 @@ typedef struct {
bool (*extractAll)(CC4Group* const this, const char* const targetPath);
// extracts only a single file or a sub directory of the group denoted by the entryPath to the targetPath
- // the containing directory of the targetPath must exist, but the final targetPath must not exist. otherwise an error will be generated
+ // the targetPath must exist and be a directory. the entry/ies will be extracted inside the targetPath
bool (*extractSingle)(CC4Group* const this, const char* const entryPath, const char* const targetPath);
+ // extracts only a single file or a sub directory of the group denoted by the entryPath to the targetPath
+ // the containing directory of the targetPath must exist, but the final targetPath must not exist. otherwise an error will be generated
+ // the entry denoted by entryPath will be renamed according to the targetPath
+ bool (*extractSingleRename)(CC4Group* const this, const char* const entryPath, const char* const targetPath);
+
// retrieval of metadata about the stored files and directories
// stores all metadata known about the entry denoted by path into the CC4Group_EntryInfo struct pointed to by info, similar to stat
diff --git a/src/cppc4group.cpp b/src/cppc4group.cpp
index 99d5e41..ea24f3b 100644
--- a/src/cppc4group.cpp
+++ b/src/cppc4group.cpp
@@ -264,9 +264,9 @@ bool CppC4Group::extractAll(const std::string& path)
return cc4group.extractAll(p->g, path.c_str());
}
-bool CppC4Group::extractSingle(const std::string& entryPath, const std::string& targetPath)
+bool CppC4Group::extractSingle(const std::string& entryPath, const std::string& targetPath, const bool rename)
{
- return cc4group.extractSingle(p->g, entryPath.c_str(), targetPath.c_str());
+ return (rename ? cc4group.extractSingleRename : cc4group.extractSingle)(p->g, entryPath.c_str(), targetPath.c_str());
}
std::optional<CppC4Group::Data> CppC4Group::getEntryData(const std::string& path)
diff --git a/src/cppc4group.hpp b/src/cppc4group.hpp
index 363fc91..0ca9eb1 100644
--- a/src/cppc4group.hpp
+++ b/src/cppc4group.hpp
@@ -160,7 +160,9 @@ public:
bool saveWithWriteCallback(const WriteCallback callback, void* const arg = nullptr, size_t bufferSize = 0);
bool extractAll(const std::string& path);
- bool extractSingle(const std::string& entryPath, const std::string& targetPath);
+
+ // as with save, maps either to cc4group.extractSingle or cc4group.extractSingleRename
+ bool extractSingle(const std::string& entryPath, const std::string& targetPath, const bool rename = true);
std::optional<Data> getEntryData(const std::string& path);
std::string getErrorMessage();