Skip to content

Commit

Permalink
Merge pull request #64 from opencor/issue61
Browse files Browse the repository at this point in the history
Support for buffer contents
  • Loading branch information
fbergmann authored Jun 4, 2024
2 parents e185a98 + b5bfa68 commit a8a4bba
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 13 deletions.
25 changes: 21 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -356,8 +358,16 @@ 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()

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)
Expand Down Expand Up @@ -480,6 +490,9 @@ 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/)
include_directories(BEFORE ${CMAKE_CURRENT_BINARY_DIR}/src/)

Expand Down Expand Up @@ -621,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}
Expand Down
84 changes: 77 additions & 7 deletions src/combine/combinearchive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <zipper/unzipper.h>

#include <fstream>
#include <iterator>
#include <sstream>
#include <cstdio>

Expand Down Expand Up @@ -51,7 +52,7 @@ CombineArchive::initializeFromDirectory(const std::string &directory)
}

bool
CombineArchive::initializeFromArchive(
CombineArchive::initializeFromUnzipper(
zipper::Unzipper* pUnzipper,
bool skipOmex/*=false*/)
{
Expand Down Expand Up @@ -153,12 +154,35 @@ 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);
return initializeFromUnzipper(mpUnzipper, skipOmex);
}

bool
CombineArchive::initializeFromBuffer(
const std::vector<unsigned char>& pBuffer,
bool skipOmex /*= false*/)
{
cleanUp();

try
{
auto buffer = const_cast<std::vector<unsigned char> &>(pBuffer);

mpUnzipper = new Unzipper(buffer);
}
catch (const std::exception&)
{
// something went wrong
cleanUp();
return false;
}

return initializeFromUnzipper(mpUnzipper, skipOmex);
}

bool CombineArchive::cleanUp()
Expand Down Expand Up @@ -287,9 +311,8 @@ bool CombineArchive::writeToFile(const std::string &fileName)
return true;
}

bool
CombineArchive::getStream(const std::string &name,
std::ifstream &stream)
std::map<std::string, std::string>::iterator
CombineArchive::mapIterator(const std::string &name)
{
std::map<std::string, std::string>::iterator it = mMap.find(name);
if (it == mMap.end())
Expand All @@ -301,9 +324,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)
Expand Down Expand Up @@ -504,6 +537,23 @@ CombineArchive::addFile(std::istream &stream,
return addFile(tempFilename, targetName, format, isMaster);
}

bool
CombineArchive::addFileFromBuffer(const std::vector<unsigned char> &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<unsigned char>(out));

return addFile(tempFilename, targetName, format, isMaster);
}

int
CombineArchive::addMetadata(const std::string &targetName,
const OmexDescription &description)
Expand Down Expand Up @@ -553,3 +603,23 @@ CombineArchive::extractEntryToString(const std::string& name)
extractEntryToStream(name, stream);
return stream.str();
}

std::vector<unsigned char>
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<unsigned char> res;
mpUnzipper->extractEntryToMemory(filename, res);

return res;
}
52 changes: 50 additions & 2 deletions src/combine/combinearchive.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned char>& buffer,
const std::string& targetName,
const std::string& format,
bool isMaster = false);

/**
* Adds the given metadata to the list.
*
Expand Down Expand Up @@ -165,6 +182,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<unsigned char> extractEntryToBuffer(const std::string& name);

/**
* extracts all entries in this archive into the given directory.
*
Expand Down Expand Up @@ -210,10 +236,23 @@ 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);


/**
* 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 initializeFromBuffer(const std::vector<unsigned char>& pBuffer,
bool skipOmex=false);

/**
* @return the manifest
Expand Down Expand Up @@ -389,6 +428,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<std::string, std::string>::iterator mapIterator(const std::string &name);
};

LIBCOMBINE_CPP_NAMESPACE_END
Expand Down

0 comments on commit a8a4bba

Please sign in to comment.