From b58b14f09d514f754a84ccb00872b2e303a8724b Mon Sep 17 00:00:00 2001 From: Alan Garny Date: Fri, 24 May 2024 13:56:50 +1200 Subject: [PATCH 1/7] CMake: reference zlib and zipper in the same way as libSBML. --- CMakeLists.txt | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6ec6cab..51e4284 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -346,8 +346,10 @@ endif() set(USE_ZLIB ON) add_definitions( -DUSE_ZLIB ) -if (ZLIB_LIBRARY) - set(LIBCOMBINE_LIBS ${LIBCOMBINE_LIBS} ZLIB::ZLIB) +if (ZLIB_LIBRARY_NAME) + set(LIBCOMBINE_LIBS ${LIBCOMBINE_LIBS} ${ZLIB_LIBRARY_NAME}) +elseif (ZLIB_LIBRARY) + set(LIBCOMBINE_LIBS ${LIBCOMBINE_LIBS} ${ZLIB_LIBRARY}) endif() if (LIBSBML_LIBRARY_NAME) @@ -356,8 +358,10 @@ elseif (LIBSBML_LIBRARY) set(LIBCOMBINE_LIBS ${LIBCOMBINE_LIBS} ${LIBSBML_LIBRARY}) endif() -if (ZIPPER_LIBRARY) - set(LIBCOMBINE_LIBS ${LIBCOMBINE_LIBS} ZIPPER::ZIPPER) +if (ZIPPER_LIBRARY_NAME) + set(LIBCOMBINE_LIBS ${LIBCOMBINE_LIBS} ${ZIPPER_LIBRARY_NAME}) +elseif (ZIPPER_LIBRARY) + set(LIBCOMBINE_LIBS ${LIBCOMBINE_LIBS} ${ZIPPER_LIBRARY}) endif() set(USING_INTEL FALSE) @@ -480,6 +484,8 @@ include_directories(${EXTRA_INCLUDE_DIRS}) endif() include_directories(${LIBSBML_INCLUDE_DIR}) +include_directories(${ZLIB_INCLUDE_DIR}) +include_directories(${ZIPPER_INCLUDE_DIR}) include_directories(BEFORE ${CMAKE_CURRENT_SOURCE_DIR}/src/) include_directories(BEFORE ${CMAKE_CURRENT_BINARY_DIR}/src/) From adea20374568782b5a1f570417efd0b1688cdbe9 Mon Sep 17 00:00:00 2001 From: Alan Garny Date: Fri, 24 May 2024 14:31:27 +1200 Subject: [PATCH 2/7] CMake: allow to specify an XML library. If we don't then we get some undefined symbols XML-related methods (e.g., _xmlCreatePushParserCtxt). --- CMakeLists.txt | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 51e4284..1b48785 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -364,6 +364,12 @@ elseif (ZIPPER_LIBRARY) set(LIBCOMBINE_LIBS ${LIBCOMBINE_LIBS} ${ZIPPER_LIBRARY}) endif() +if (XML_LIBRARY_NAME) + set(LIBCOMBINE_LIBS ${LIBCOMBINE_LIBS} ${XML_LIBRARY_NAME}) +elseif (XML_LIBRARY) + set(LIBCOMBINE_LIBS ${LIBCOMBINE_LIBS} ${XML_LIBRARY}) +endif() + set(USING_INTEL FALSE) if (WIN32 AND CMAKE_C_COMPILER AND CMAKE_C_COMPILER MATCHES ".*icl.*$") message(STATUS "Detected Intel Compiler") @@ -484,6 +490,7 @@ include_directories(${EXTRA_INCLUDE_DIRS}) endif() include_directories(${LIBSBML_INCLUDE_DIR}) +include_directories(${XML_INCLUDE_DIR}) include_directories(${ZLIB_INCLUDE_DIR}) include_directories(${ZIPPER_INCLUDE_DIR}) include_directories(BEFORE ${CMAKE_CURRENT_SOURCE_DIR}/src/) @@ -627,6 +634,10 @@ libCombine version ${LIBCOMBINE_VERSION} SBML include dir = ${LIBSBML_INCLUDE_DIR} libSBML compression support = ${LIBSBML_HAS_MINIZIP} + XML library configuration: + XML library = ${XML_LIBRARY} + XML include dir = ${XML_INCLUDE_DIR} + Zlib library configuration: Zlib library = ${ZLIB_LIBRARY} Zlib include dir = ${ZLIB_INCLUDE_DIR} From 55157b612faaea3081ea6a453fa2690df8370892 Mon Sep 17 00:00:00 2001 From: Alan Garny Date: Wed, 29 May 2024 18:58:28 +1200 Subject: [PATCH 3/7] Initialise a COMBINE archive from a buffer. --- src/combine/combinearchive.cpp | 23 ++++++++++++++++++++++- src/combine/combinearchive.h | 13 +++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/combine/combinearchive.cpp b/src/combine/combinearchive.cpp index e090b42..7a2486a 100644 --- a/src/combine/combinearchive.cpp +++ b/src/combine/combinearchive.cpp @@ -153,7 +153,28 @@ CombineArchive::initializeFromArchive( } catch (const std::exception&) { - // invalid COMBINE archive, it should always have a manifest + // something went wrong + cleanUp(); + return false; + } + + return initializeFromArchive(mpUnzipper, skipOmex); +} + +bool +CombineArchive::initializeFromArchive( + std::vector& pBuffer, + bool skipOmex /*= false*/) +{ + cleanUp(); + + try + { + mpUnzipper = new Unzipper(pBuffer); + } + catch (const std::exception&) + { + // something went wrong cleanUp(); return false; } diff --git a/src/combine/combinearchive.h b/src/combine/combinearchive.h index 8291b9c..db42218 100644 --- a/src/combine/combinearchive.h +++ b/src/combine/combinearchive.h @@ -214,6 +214,19 @@ class LIBCOMBINE_EXTERN CombineArchive bool skipOmex=false); + /** + * initializes this instance from a buffer + * + * @param buffer the buffer + * @param skipOmex optional flag indicating whether meta data processing + * should be skipped or not (default). The metadata processing, removes + * annotations in the restricted 2014 subset of the OMEX Metadata and + * adds them to the convenience classes. + * + * @return boolean indicating success or failure + */ + bool initializeFromArchive(std::vector& pBuffer, + bool skipOmex=false); /** * @return the manifest From f51320e9f341063153dba26db2b19660a8cfd98a Mon Sep 17 00:00:00 2001 From: Alan Garny Date: Tue, 4 Jun 2024 09:46:43 +1200 Subject: [PATCH 4/7] Renamed initializeFromArchive() with an unzipper to initializeFromUnzipper(). --- src/combine/combinearchive.cpp | 6 +++--- src/combine/combinearchive.h | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/combine/combinearchive.cpp b/src/combine/combinearchive.cpp index 7a2486a..bafbd2c 100644 --- a/src/combine/combinearchive.cpp +++ b/src/combine/combinearchive.cpp @@ -51,7 +51,7 @@ CombineArchive::initializeFromDirectory(const std::string &directory) } bool -CombineArchive::initializeFromArchive( +CombineArchive::initializeFromUnzipper( zipper::Unzipper* pUnzipper, bool skipOmex/*=false*/) { @@ -158,7 +158,7 @@ CombineArchive::initializeFromArchive( return false; } - return initializeFromArchive(mpUnzipper, skipOmex); + return initializeFromUnzipper(mpUnzipper, skipOmex); } bool @@ -179,7 +179,7 @@ CombineArchive::initializeFromArchive( return false; } - return initializeFromArchive(mpUnzipper, skipOmex); + return initializeFromUnzipper(mpUnzipper, skipOmex); } bool CombineArchive::cleanUp() diff --git a/src/combine/combinearchive.h b/src/combine/combinearchive.h index db42218..bec2903 100644 --- a/src/combine/combinearchive.h +++ b/src/combine/combinearchive.h @@ -210,8 +210,8 @@ class LIBCOMBINE_EXTERN CombineArchive * * @return boolean indicating success or failure */ - bool initializeFromArchive(zipper::Unzipper* pUnzipper, - bool skipOmex=false); + bool initializeFromUnzipper(zipper::Unzipper* pUnzipper, + bool skipOmex=false); /** From 2fd6d7b0f4b35629eb9a9cc9da9e9519d0aad1c4 Mon Sep 17 00:00:00 2001 From: Alan Garny Date: Tue, 4 Jun 2024 09:48:29 +1200 Subject: [PATCH 5/7] Renamed initializeFromArchive() with a buffer to initializeFromBuffer(). Also pass a constant buffer reference rather than a buffer reference (which is needed by the unzipper). --- src/combine/combinearchive.cpp | 8 +++++--- src/combine/combinearchive.h | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/combine/combinearchive.cpp b/src/combine/combinearchive.cpp index bafbd2c..773b314 100644 --- a/src/combine/combinearchive.cpp +++ b/src/combine/combinearchive.cpp @@ -162,15 +162,17 @@ CombineArchive::initializeFromArchive( } bool -CombineArchive::initializeFromArchive( - std::vector& pBuffer, +CombineArchive::initializeFromBuffer( + const std::vector& pBuffer, bool skipOmex /*= false*/) { cleanUp(); try { - mpUnzipper = new Unzipper(pBuffer); + auto buffer = const_cast &>(pBuffer); + + mpUnzipper = new Unzipper(buffer); } catch (const std::exception&) { diff --git a/src/combine/combinearchive.h b/src/combine/combinearchive.h index bec2903..fffccde 100644 --- a/src/combine/combinearchive.h +++ b/src/combine/combinearchive.h @@ -225,8 +225,8 @@ class LIBCOMBINE_EXTERN CombineArchive * * @return boolean indicating success or failure */ - bool initializeFromArchive(std::vector& pBuffer, - bool skipOmex=false); + bool initializeFromBuffer(const std::vector& pBuffer, + bool skipOmex=false); /** * @return the manifest From 01d319ba2d7f909a24286f932d4cac05761133d9 Mon Sep 17 00:00:00 2001 From: Alan Garny Date: Tue, 4 Jun 2024 12:27:52 +1200 Subject: [PATCH 6/7] Added extractEntryToMemory(). --- src/combine/combinearchive.cpp | 37 ++++++++++++++++++++++++++++++---- src/combine/combinearchive.h | 18 +++++++++++++++++ 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/combine/combinearchive.cpp b/src/combine/combinearchive.cpp index 773b314..b2f3808 100644 --- a/src/combine/combinearchive.cpp +++ b/src/combine/combinearchive.cpp @@ -310,9 +310,8 @@ bool CombineArchive::writeToFile(const std::string &fileName) return true; } -bool -CombineArchive::getStream(const std::string &name, - std::ifstream &stream) +std::map::iterator +CombineArchive::mapIterator(const std::string &name) { std::map::iterator it = mMap.find(name); if (it == mMap.end()) @@ -324,9 +323,19 @@ CombineArchive::getStream(const std::string &name, if (name.find("/") == 0) it = mMap.find(name.substr(1)); if (it == mMap.end()) - return false; + return mMap.end(); } } + return it; +} + +bool +CombineArchive::getStream(const std::string &name, + std::ifstream &stream) +{ + auto it = mapIterator(name); + if (it == mMap.end()) + return false; std::string filename = (*it).second; if (filename.find("unzipper://") == 0) @@ -576,3 +585,23 @@ CombineArchive::extractEntryToString(const std::string& name) extractEntryToStream(name, stream); return stream.str(); } + +std::vector +CombineArchive::extractEntryToBuffer(const std::string& name) +{ + auto it = mapIterator(name); + if (it == mMap.end()) + return {}; + + std::string filename = (*it).second; + if (filename.find("unzipper://") == 0) + filename = filename.substr(std::string("unzipper://").length()); + + if (mpUnzipper == NULL) + return {}; + + std::vector res; + mpUnzipper->extractEntryToMemory(filename, res); + + return res; +} diff --git a/src/combine/combinearchive.h b/src/combine/combinearchive.h index fffccde..dbfee93 100644 --- a/src/combine/combinearchive.h +++ b/src/combine/combinearchive.h @@ -165,6 +165,15 @@ class LIBCOMBINE_EXTERN CombineArchive */ std::string extractEntryToString(const std::string& name); + /** + * extracts the given entry and returns the contents as a buffer. + * + * @param name the entry to be extracted + * + * @return the content of the entry, or empty if not found. + */ + std::vector extractEntryToBuffer(const std::string& name); + /** * extracts all entries in this archive into the given directory. * @@ -402,6 +411,15 @@ class LIBCOMBINE_EXTERN CombineArchive * */ int addMetadataToArchive(OmexDescription& desc, zipper::Zipper* zipper); + + /** + * returns the map iterator for the given file. + * + * @param name the name that should be in the current map of files + * + * @return the map iterator + */ + std::map::iterator mapIterator(const std::string &name); }; LIBCOMBINE_CPP_NAMESPACE_END From b5bfa6808a4707d7479d5fc0d4aed0344e52a40c Mon Sep 17 00:00:00 2001 From: Alan Garny Date: Tue, 4 Jun 2024 13:07:34 +1200 Subject: [PATCH 7/7] Added addFileFromBuffer(). --- src/combine/combinearchive.cpp | 18 ++++++++++++++++++ src/combine/combinearchive.h | 17 +++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/combine/combinearchive.cpp b/src/combine/combinearchive.cpp index b2f3808..81a2dce 100644 --- a/src/combine/combinearchive.cpp +++ b/src/combine/combinearchive.cpp @@ -12,6 +12,7 @@ #include #include +#include #include #include @@ -536,6 +537,23 @@ CombineArchive::addFile(std::istream &stream, return addFile(tempFilename, targetName, format, isMaster); } +bool +CombineArchive::addFileFromBuffer(const std::vector &buffer, + const std::string &targetName, + const std::string &format, + bool isMaster) +{ + std::string tempFilename = Util::getTempFilename(); + mTempFiles.push_back(tempFilename); + + std::ofstream out(tempFilename.c_str(), std::ios::out | std::ios::binary); + + std::copy(buffer.begin(), buffer.end(), + std::ostream_iterator(out)); + + return addFile(tempFilename, targetName, format, isMaster); +} + int CombineArchive::addMetadata(const std::string &targetName, const OmexDescription &description) diff --git a/src/combine/combinearchive.h b/src/combine/combinearchive.h index dbfee93..d5d23a1 100644 --- a/src/combine/combinearchive.h +++ b/src/combine/combinearchive.h @@ -118,6 +118,23 @@ class LIBCOMBINE_EXTERN CombineArchive const std::string& targetName, const std::string& format, bool isMaster = false); + + /** + * Adds the given buffer to the archive. + * + * @param buffer the buffer of the file to be added to the archive. + * @param targetName the target name of the file in the archive + * @param format the format that this file has + * @param isMaster boolean indicating whether the file should be + * opened first if there are multiple ones. + * + * @return boolean indicating success or failure + */ + bool addFileFromBuffer(const std::vector& buffer, + const std::string& targetName, + const std::string& format, + bool isMaster = false); + /** * Adds the given metadata to the list. *