*
* @param[in] data A pointer to a memory location where the content of the picture file is stored.
* @param[in] size The size in bytes of the memory occupied by the @p data.
+ * @param[in] copy Decides whether the data should be copied into the local buffer.
*
* @retval Result::Success When succeed.
* @retval Result::InvalidArguments In case no data are provided or the @p size is zero or less.
*
* @note: This api supports only SVG format
*/
- Result load(const char* data, uint32_t size) noexcept;
+ Result load(const char* data, uint32_t size, bool copy = false) noexcept;
/**
* @brief Resize the picture content with the given width and height.
* \param[in] paint Tvg_Paint pointer
* \param[in] data raw data pointer
* \param[in] size of data
+* \param[in] copy Decides whether the data should be copied into the local buffer
*
* \return Tvg_Result return value
* \retval TVG_RESULT_SUCCESS: if ok.
*
* \warning Please do not use it, this API is not official one. It can be modified in the next version.
*/
-TVG_EXPORT Tvg_Result tvg_picture_load_data(Tvg_Paint* paint, const char *data, uint32_t size);
+TVG_EXPORT Tvg_Result tvg_picture_load_data(Tvg_Paint* paint, const char *data, uint32_t size, bool copy);
/*!
}
-TVG_EXPORT Tvg_Result tvg_picture_load_data(Tvg_Paint* paint, const char *data, uint32_t size)
+TVG_EXPORT Tvg_Result tvg_picture_load_data(Tvg_Paint* paint, const char *data, uint32_t size, bool copy)
{
if (!paint) return TVG_RESULT_INVALID_ARGUMENT;
- return (Tvg_Result) reinterpret_cast<Picture*>(paint)->load(data, size);
+ return (Tvg_Result) reinterpret_cast<Picture*>(paint)->load(data, size, copy);
}
virtual ~Loader() {}
virtual bool open(const string& path) { /* Not supported */ return false; };
- virtual bool open(const char* data, uint32_t size) { /* Not supported */ return false; };
+ virtual bool open(const char* data, uint32_t size, bool copy) { /* Not supported */ return false; };
virtual bool open(const uint32_t* data, uint32_t w, uint32_t h, bool copy) { /* Not supported */ return false; };
virtual bool read() = 0;
virtual bool close() = 0;
}
-shared_ptr<Loader> LoaderMgr::loader(const char* data, uint32_t size)
+shared_ptr<Loader> LoaderMgr::loader(const char* data, uint32_t size, bool copy)
{
for (int i = 0; i < static_cast<int>(FileType::Unknown); i++) {
auto loader = _find(static_cast<FileType>(i));
if (loader) {
- if (loader->open(data, size)) return shared_ptr<Loader>(loader);
+ if (loader->open(data, size, copy)) return shared_ptr<Loader>(loader);
else delete(loader);
}
}
static bool init();
static bool term();
static shared_ptr<Loader> loader(const string& path);
- static shared_ptr<Loader> loader(const char* data, uint32_t size);
+ static shared_ptr<Loader> loader(const char* data, uint32_t size, bool copy);
static shared_ptr<Loader> loader(const uint32_t* data, uint32_t w, uint32_t h, bool copy);
};
}
-Result Picture::load(const char* data, uint32_t size) noexcept
+Result Picture::load(const char* data, uint32_t size, bool copy /*=false*/) noexcept
{
if (!data || size <= 0) return Result::InvalidArguments;
- return pImpl->load(data, size);
+ return pImpl->load(data, size, copy);
}
return Result::Success;
}
- Result load(const char* data, uint32_t size)
+ Result load(const char* data, uint32_t size, bool copy)
{
if (loader) loader->close();
- loader = LoaderMgr::loader(data, size);
+ loader = LoaderMgr::loader(data, size, copy);
if (!loader) return Result::NonSupport;
if (!loader->read()) return Result::Unknown;
w = loader->w;
}
+void SvgLoader::clearBuffer()
+{
+ free(buffer);
+ size = 0;
+ buffer = nullptr;
+ content = nullptr;
+}
+
+
/************************************************************************/
/* External Class Implementation */
/************************************************************************/
}
-bool SvgLoader::open(const char* data, uint32_t size)
+bool SvgLoader::open(const char* data, uint32_t size, bool copy)
{
- //TODO: verify memory leak if open() is called multiple times.
+ clearBuffer();
+ if (copy) {
+ buffer = (char*)malloc(size);
+ if (!buffer) return false;
+ memcpy(buffer, data, size);
+ this->content = buffer;
+ } else {
+ this->content = data;
+ }
- this->content = data;
this->size = size;
return header();
bool SvgLoader::open(const string& path)
{
//TODO: verify memory leak if open() is called multiple times.
+ clearBuffer();
ifstream f;
f.open(path);
loaderData.doc = nullptr;
loaderData.stack.reset();
+ clearBuffer();
+
return true;
}
{
public:
string filePath;
+ char* buffer = nullptr;
const char* content = nullptr;
uint32_t size = 0;
using Loader::open;
bool open(const string& path) override;
- bool open(const char* data, uint32_t size) override;
+ bool open(const char* data, uint32_t size, bool copy) override;
bool header();
bool read() override;
void run(unsigned tid) override;
unique_ptr<Scene> scene() override;
+
+private:
+ void clearBuffer();
};
*/
#include <fstream>
+#include <memory.h>
#include "tvgLoaderMgr.h"
#include "tvgTvgLoader.h"
#include "tvgTvgLoadParser.h"
return tvgValidateData(pointer, size);
}
-bool TvgLoader::open(const char *data, uint32_t size)
+bool TvgLoader::open(const char *data, uint32_t size, bool copy)
{
- //TODO: verify memory leak if open() is called multiple times.
+ if (copy) {
+ if (buffer) clearBuffer();
+ buffer = (char*)malloc(size);
+ if (!buffer) return false;
+ memcpy(buffer, data, size);
+ this->pointer = buffer;
+ } else {
+ this->pointer = data;
+ }
- this->pointer = data;
this->size = size;
return tvgValidateData(pointer, size);
using Loader::open;
bool open(const string &path) override;
- bool open(const char *data, uint32_t size) override;
+ bool open(const char *data, uint32_t size, bool copy) override;
bool read() override;
bool close() override;