Skip to content

Commit

Permalink
File: retrieve a child file using a relative path.
Browse files Browse the repository at this point in the history
The end user should know that a child file is managed under /some/path/file.contents.
  • Loading branch information
agarny committed Jul 4, 2024
1 parent caa5b4f commit e93d2e7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 55 deletions.
12 changes: 7 additions & 5 deletions src/support/combine/combinearchive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ limitations under the License.

#include "combinearchive_p.h"

#include "utils.h"

#include "libopencor/file.h"

#include <combine/combinearchive.h>
Expand All @@ -26,15 +28,15 @@ namespace libOpenCOR {

CombineArchive::Impl::Impl(const FilePtr &pFile, libcombine::CombineArchive *pArchive)
: mArchive(pArchive)
, mArchiveLocation(libOpenCOR::pathToString(libOpenCOR::stringToPath(pFile->fileName() + ".contents/")))
, mArchiveLocationSize(mArchiveLocation.size())
{
// Extract all the files contained in the COMBINE archive.

auto archiveLocation = pFile->fileName() + ".contents/";

for (int i = 0; i < mArchive->getNumEntries(); ++i) {
const auto *entry = mArchive->getEntry(i);
auto location = entry->getLocation();
auto file = File::create(archiveLocation + location);
auto file = File::create(mArchiveLocation + location);

file->setContents(mArchive->extractEntryToBuffer(location));

Expand Down Expand Up @@ -72,7 +74,7 @@ Strings CombineArchive::Impl::fileNames() const
Strings res;

for (const auto &file : mFiles) {
res.push_back(file->fileName());
res.push_back(file->fileName().substr(mArchiveLocationSize));
}

return res;
Expand All @@ -86,7 +88,7 @@ FilePtrs CombineArchive::Impl::files() const
FilePtr CombineArchive::Impl::file(const std::string &pFileName) const
{
for (const auto &file : mFiles) {
if (file->fileName() == pFileName) {
if (file->fileName() == mArchiveLocation + pFileName) {
return file;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/support/combine/combinearchive_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class CombineArchive::Impl: public Logger::Impl
{
public:
libcombine::CombineArchive *mArchive;
std::string mArchiveLocation;
size_t mArchiveLocationSize;
std::vector<FilePtr> mFiles;
FilePtr mMasterFile;

Expand Down
19 changes: 5 additions & 14 deletions tests/api/file/childtests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,17 @@ void doTestDataset(const std::string &pNumber, const std::vector<std::string> &p

EXPECT_TRUE(file->hasChildFiles());
EXPECT_EQ(file->childFileCount(), pSpecificChildFileNames.size() + 1);

auto childFileNames = file->childFileNames();

EXPECT_EQ(childFileNames.size(), pSpecificChildFileNames.size() + 1);

auto childFileNameDir = libOpenCOR::pathToString(libOpenCOR::stringToPath(file->fileName() + ".contents/"));

for (const auto &childFileName : childFileNames) {
EXPECT_TRUE(childFileName.starts_with(childFileNameDir));
}

EXPECT_EQ(file->childFileNames().size(), pSpecificChildFileNames.size() + 1);
EXPECT_EQ(file->childFiles().size(), pSpecificChildFileNames.size() + 1);

auto simulationFile = file->childFile(childFileNameDir + "simulation.json");
auto simulationFile = file->childFile("simulation.json");

EXPECT_NE(simulationFile, nullptr);

for (const auto &specificChildFileName : pSpecificChildFileNames) {
EXPECT_NE(file->childFile(childFileNameDir + specificChildFileName), nullptr);
EXPECT_NE(file->childFile(specificChildFileName), nullptr);
}

EXPECT_NE(simulationFile, nullptr);
EXPECT_EQ(file->childFile(libOpenCOR::resourcePath(libOpenCOR::UNKNOWN_FILE)), nullptr);

EXPECT_EQ(libOpenCOR::toString(simulationFile->contents()), libOpenCOR::textFileContents(libOpenCOR::resourcePath("api/file/dataset_" + pNumber + ".json")));
Expand Down
25 changes: 7 additions & 18 deletions tests/bindings/javascript/file.child.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,30 +63,19 @@ describe("File type tests", () => {

expect(file.hasChildFiles()).toBe(true);
expect(file.childFileCount()).toBe(specificChildFileNames.length + 1);

const childFileNames = file.childFileNames();

expect(childFileNames.size()).toBe(specificChildFileNames.length + 1);

const childFileNameDir = file.fileName() + ".contents/";

for (let i = 0; i < childFileNames.size(); ++i) {
const childFileName = childFileNames.get(i);

expect(childFileName.startsWith(childFileNameDir)).toBe(true);
}

expect(file.childFileNames().size()).toBe(
specificChildFileNames.length + 1,
);
expect(file.childFiles().size()).toBe(specificChildFileNames.length + 1);

const simulationFile = file.childFile(childFileNameDir + "simulation.json");
const simulationFile = file.childFile("simulation.json");

expect(simulationFile).not.toBeNull();

for (let i = 0; i < specificChildFileNames.length; ++i) {
expect(
file.childFile(childFileNameDir + specificChildFileNames[i]),
).not.toBeNull();
expect(file.childFile(specificChildFileNames[i])).not.toBeNull();
}

expect(simulationFile).not.toBeNull();
expect(file.childFile(utils.UNKNOWN_FILE)).toBeNull();

expect(simulationFile.contents()).toStrictEqual(jsonContents);
Expand Down
23 changes: 5 additions & 18 deletions tests/bindings/python/test_file_child.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,16 @@ def do_test_dataset(number, specific_child_file_names):

assert file.has_child_files == True
assert file.child_file_count == len(specific_child_file_names) + 1

child_file_names = file.child_file_names

assert len(child_file_names) == len(specific_child_file_names) + 1

child_file_name_dir = (
utils.path_to_string(utils.string_to_path(file.file_name + ".contents"))
+ os.sep
)

for child_file_name in child_file_names:
assert child_file_name.startswith(child_file_name_dir)

assert len(file.child_file_names) == len(specific_child_file_names) + 1
assert len(file.child_files) == len(specific_child_file_names) + 1

simulation_file = file.child_file(child_file_name_dir + "simulation.json")
simulation_file = file.child_file("simulation.json")

assert simulation_file is not None

for specific_child_file_name in specific_child_file_names:
assert (
file.child_file(child_file_name_dir + specific_child_file_name) is not None
)
assert file.child_file(specific_child_file_name) is not None

assert simulation_file is not None
assert file.child_file(utils.resource_path(utils.UnknownFile)) is None

assert utils.to_string(simulation_file.contents) == utils.text_file_contents(
Expand Down

0 comments on commit e93d2e7

Please sign in to comment.