common: code refactoring

replaced internal string usage with char*
This commit is contained in:
Hermet Park 2024-11-08 14:50:14 +09:00
parent 9b9a0308c8
commit 4aaa0dafc4
23 changed files with 64 additions and 63 deletions

View file

@ -1260,10 +1260,10 @@ public:
* *
* @warning It's the user responsibility to release the @p data memory. * @warning It's the user responsibility to release the @p data memory.
* *
* @note If you are unsure about the MIME type, you can provide an empty value like @c "", and thorvg will attempt to figure it out. * @note If you are unsure about the MIME type, you can provide an empty value like @c nullptr, and thorvg will attempt to figure it out.
* @since 0.5 * @since 0.5
*/ */
Result load(const char* data, uint32_t size, const char* mimeType, const char* rpath = "", bool copy = false) noexcept; Result load(const char* data, uint32_t size, const char* mimeType, const char* rpath = nullptr, bool copy = false) noexcept;
/** /**
* @brief Resizes the picture content to the given width and height. * @brief Resizes the picture content to the given width and height.
@ -1540,7 +1540,7 @@ public:
* @warning It's the user responsibility to release the @p data memory. * @warning It's the user responsibility to release the @p data memory.
* *
* @note To unload the font data loaded using this API, pass the proper @p name and @c nullptr as @p data. * @note To unload the font data loaded using this API, pass the proper @p name and @c nullptr as @p data.
* @note If you are unsure about the MIME type, you can provide an empty value like @c "", and thorvg will attempt to figure it out. * @note If you are unsure about the MIME type, you can provide an empty value like @c nullptr, and thorvg will attempt to figure it out.
* @see Text::font(const char* name, float size, const char* style) * @see Text::font(const char* name, float size, const char* style)
* *
* @note 0.15 * @note 0.15

View file

@ -57,9 +57,9 @@ JpgLoader::~JpgLoader()
} }
bool JpgLoader::open(const string& path) bool JpgLoader::open(const char* path)
{ {
auto jpegFile = fopen(path.c_str(), "rb"); auto jpegFile = fopen(path, "rb");
if (!jpegFile) return false; if (!jpegFile) return false;
auto ret = false; auto ret = false;
@ -97,7 +97,7 @@ finalize:
} }
bool JpgLoader::open(const char* data, uint32_t size, TVG_UNUSED const string& rpath, bool copy) bool JpgLoader::open(const char* data, uint32_t size, TVG_UNUSED const char* rpath, bool copy)
{ {
int width, height, subSample, colorSpace; int width, height, subSample, colorSpace;
if (tjDecompressHeader3(jpegDecompressor, (unsigned char *) data, size, &width, &height, &subSample, &colorSpace) < 0) return false; if (tjDecompressHeader3(jpegDecompressor, (unsigned char *) data, size, &width, &height, &subSample, &colorSpace) < 0) return false;

View file

@ -34,8 +34,8 @@ public:
JpgLoader(); JpgLoader();
~JpgLoader(); ~JpgLoader();
bool open(const string& path) override; bool open(const char* path) override;
bool open(const char* data, uint32_t size, const string& rpath, bool copy) override; bool open(const char* data, uint32_t size, const char* rpath, bool copy) override;
bool read() override; bool read() override;
private: private:

View file

@ -52,11 +52,11 @@ PngLoader::~PngLoader()
} }
bool PngLoader::open(const string& path) bool PngLoader::open(const char* path)
{ {
image->opaque = NULL; image->opaque = NULL;
if (!png_image_begin_read_from_file(image, path.c_str())) return false; if (!png_image_begin_read_from_file(image, path)) return false;
w = (float)image->width; w = (float)image->width;
h = (float)image->height; h = (float)image->height;
@ -65,7 +65,7 @@ bool PngLoader::open(const string& path)
} }
bool PngLoader::open(const char* data, uint32_t size, TVG_UNUSED const string& rpath, bool copy) bool PngLoader::open(const char* data, uint32_t size, TVG_UNUSED const char* rpath, bool copy)
{ {
image->opaque = NULL; image->opaque = NULL;

View file

@ -32,8 +32,8 @@ public:
PngLoader(); PngLoader();
~PngLoader(); ~PngLoader();
bool open(const string& path) override; bool open(const char* path) override;
bool open(const char* data, uint32_t size, const string& rpath, bool copy) override; bool open(const char* data, uint32_t size, const char* rpath, bool copy) override;
bool read() override; bool read() override;
private: private:

View file

@ -66,9 +66,9 @@ WebpLoader::~WebpLoader()
} }
bool WebpLoader::open(const string& path) bool WebpLoader::open(const char* path)
{ {
auto webpFile = fopen(path.c_str(), "rb"); auto webpFile = fopen(path, "rb");
if (!webpFile) return false; if (!webpFile) return false;
auto ret = false; auto ret = false;
@ -99,7 +99,7 @@ finalize:
} }
bool WebpLoader::open(const char* data, uint32_t size, TVG_UNUSED const string& rpath, bool copy) bool WebpLoader::open(const char* data, uint32_t size, TVG_UNUSED const char* rpath, bool copy)
{ {
if (copy) { if (copy) {
this->data = (unsigned char *) malloc(size); this->data = (unsigned char *) malloc(size);

View file

@ -32,8 +32,8 @@ public:
WebpLoader(); WebpLoader();
~WebpLoader(); ~WebpLoader();
bool open(const string& path) override; bool open(const char* path) override;
bool open(const char* data, uint32_t size, const string& rpath, bool copy) override; bool open(const char* data, uint32_t size, const char* rpath, bool copy) override;
bool read() override; bool read() override;
RenderSurface* bitmap() override; RenderSurface* bitmap() override;

View file

@ -68,10 +68,10 @@ JpgLoader::~JpgLoader()
} }
bool JpgLoader::open(const string& path) bool JpgLoader::open(const char* path)
{ {
int width, height; int width, height;
decoder = jpgdHeader(path.c_str(), &width, &height); decoder = jpgdHeader(path, &width, &height);
if (!decoder) return false; if (!decoder) return false;
w = static_cast<float>(width); w = static_cast<float>(width);
@ -81,7 +81,7 @@ bool JpgLoader::open(const string& path)
} }
bool JpgLoader::open(const char* data, uint32_t size, TVG_UNUSED const string& rpath, bool copy) bool JpgLoader::open(const char* data, uint32_t size, TVG_UNUSED const char* rpath, bool copy)
{ {
if (copy) { if (copy) {
this->data = (char *) malloc(size); this->data = (char *) malloc(size);

View file

@ -41,8 +41,8 @@ public:
JpgLoader(); JpgLoader();
~JpgLoader(); ~JpgLoader();
bool open(const string& path) override; bool open(const char* path) override;
bool open(const char* data, uint32_t size, const string& rpath, bool copy) override; bool open(const char* data, uint32_t size, const char* rpath, bool copy) override;
bool read() override; bool read() override;
bool close() override; bool close() override;

View file

@ -195,7 +195,7 @@ bool LottieLoader::header()
} }
bool LottieLoader::open(const char* data, uint32_t size, const std::string& rpath, bool copy) bool LottieLoader::open(const char* data, uint32_t size, const char* rpath, bool copy)
{ {
if (copy) { if (copy) {
content = (char*)malloc(size + 1); content = (char*)malloc(size + 1);
@ -207,16 +207,16 @@ bool LottieLoader::open(const char* data, uint32_t size, const std::string& rpat
this->size = size; this->size = size;
this->copy = copy; this->copy = copy;
if (rpath.empty()) this->dirName = strdup("."); if (!rpath) this->dirName = strdup(".");
else this->dirName = strdup(rpath.c_str()); else this->dirName = strdup(rpath);
return header(); return header();
} }
bool LottieLoader::open(const string& path) bool LottieLoader::open(const char* path)
{ {
auto f = fopen(path.c_str(), "r"); auto f = fopen(path, "r");
if (!f) return false; if (!f) return false;
fseek(f, 0, SEEK_END); fseek(f, 0, SEEK_END);
@ -238,7 +238,7 @@ bool LottieLoader::open(const string& path)
fclose(f); fclose(f);
this->dirName = strDirname(path.c_str()); this->dirName = strDirname(path);
this->content = content; this->content = content;
this->copy = true; this->copy = true;

View file

@ -52,8 +52,8 @@ public:
LottieLoader(); LottieLoader();
~LottieLoader(); ~LottieLoader();
bool open(const string& path) override; bool open(const char* path) override;
bool open(const char* data, uint32_t size, const std::string& rpath, bool copy) override; bool open(const char* data, uint32_t size, const char* rpath, bool copy) override;
bool resize(Paint* paint, float w, float h) override; bool resize(Paint* paint, float w, float h) override;
bool read() override; bool read() override;
Paint* paint() override; Paint* paint() override;

View file

@ -68,9 +68,9 @@ PngLoader::~PngLoader()
} }
bool PngLoader::open(const string& path) bool PngLoader::open(const char* path)
{ {
auto pngFile = fopen(path.c_str(), "rb"); auto pngFile = fopen(path, "rb");
if (!pngFile) return false; if (!pngFile) return false;
auto ret = false; auto ret = false;
@ -105,7 +105,7 @@ finalize:
} }
bool PngLoader::open(const char* data, uint32_t size, TVG_UNUSED const string& rpath, bool copy) bool PngLoader::open(const char* data, uint32_t size, TVG_UNUSED const char* rpath, bool copy)
{ {
unsigned int width, height; unsigned int width, height;
if (lodepng_inspect(&width, &height, &state, (unsigned char*)(data), size) > 0) return false; if (lodepng_inspect(&width, &height, &state, (unsigned char*)(data), size) > 0) return false;

View file

@ -41,8 +41,8 @@ public:
PngLoader(); PngLoader();
~PngLoader(); ~PngLoader();
bool open(const string& path) override; bool open(const char* path) override;
bool open(const char* data, uint32_t size, const string& rpath, bool copy) override; bool open(const char* data, uint32_t size, const char* rpath, bool copy) override;
bool read() override; bool read() override;
RenderSurface* bitmap() override; RenderSurface* bitmap() override;

View file

@ -3869,7 +3869,7 @@ bool SvgLoader::header()
} }
bool SvgLoader::open(const char* data, uint32_t size, TVG_UNUSED const string& rpath, bool copy) bool SvgLoader::open(const char* data, uint32_t size, TVG_UNUSED const char* rpath, bool copy)
{ {
clear(); clear();
@ -3887,7 +3887,7 @@ bool SvgLoader::open(const char* data, uint32_t size, TVG_UNUSED const string& r
} }
bool SvgLoader::open(const string& path) bool SvgLoader::open(const char* path)
{ {
clear(); clear();

View file

@ -42,8 +42,8 @@ public:
SvgLoader(); SvgLoader();
~SvgLoader(); ~SvgLoader();
bool open(const string& path) override; bool open(const char* path) override;
bool open(const char* data, uint32_t size, const string& rpath, bool copy) override; bool open(const char* data, uint32_t size, const char* rpath, bool copy) override;
bool resize(Paint* paint, float w, float h) override; bool resize(Paint* paint, float w, float h) override;
bool read() override; bool read() override;
bool close() override; bool close() override;

View file

@ -85,11 +85,11 @@ static void _unmap(TtfLoader* loader)
#elif defined(__linux__) #elif defined(__linux__)
static bool _map(TtfLoader* loader, const string& path) static bool _map(TtfLoader* loader, const char* path)
{ {
auto& reader = loader->reader; auto& reader = loader->reader;
auto fd = open(path.c_str(), O_RDONLY); auto fd = open(path, O_RDONLY);
if (fd < 0) return false; if (fd < 0) return false;
struct stat info; struct stat info;
@ -116,11 +116,11 @@ static void _unmap(TtfLoader* loader)
reader.size = 0; reader.size = 0;
} }
#else #else
static bool _map(TtfLoader* loader, const string& path) static bool _map(TtfLoader* loader, const char* path)
{ {
auto& reader = loader->reader; auto& reader = loader->reader;
auto f = fopen(path.c_str(), "rb"); auto f = fopen(path, "rb");
if (!f) return false; if (!f) return false;
fseek(f, 0, SEEK_END); fseek(f, 0, SEEK_END);
@ -234,7 +234,7 @@ TtfLoader::~TtfLoader()
} }
bool TtfLoader::open(const string& path) bool TtfLoader::open(const char* path)
{ {
clear(); clear();
if (!_map(this, path)) return false; if (!_map(this, path)) return false;
@ -242,7 +242,7 @@ bool TtfLoader::open(const string& path)
} }
bool TtfLoader::open(const char* data, uint32_t size, TVG_UNUSED const string& rpath, bool copy) bool TtfLoader::open(const char* data, uint32_t size, TVG_UNUSED const char* rpath, bool copy)
{ {
reader.size = size; reader.size = size;
nomap = true; nomap = true;

View file

@ -43,8 +43,8 @@ struct TtfLoader : public FontLoader
~TtfLoader(); ~TtfLoader();
using FontLoader::open; using FontLoader::open;
bool open(const string& path) override; bool open(const char* path) override;
bool open(const char *data, uint32_t size, const string& rpath, bool copy) override; bool open(const char *data, uint32_t size, const char* rpath, bool copy) override;
bool transform(Paint* paint, float fontSize, bool italic) override; bool transform(Paint* paint, float fontSize, bool italic) override;
bool request(Shape* shape, char* text) override; bool request(Shape* shape, char* text) override;
bool read() override; bool read() override;

View file

@ -69,9 +69,9 @@ WebpLoader::~WebpLoader()
} }
bool WebpLoader::open(const string& path) bool WebpLoader::open(const char* path)
{ {
auto f = fopen(path.c_str(), "rb"); auto f = fopen(path, "rb");
if (!f) return false; if (!f) return false;
fseek(f, 0, SEEK_END); fseek(f, 0, SEEK_END);
@ -104,7 +104,7 @@ bool WebpLoader::open(const string& path)
} }
bool WebpLoader::open(const char* data, uint32_t size, TVG_UNUSED const string& rpath, bool copy) bool WebpLoader::open(const char* data, uint32_t size, TVG_UNUSED const char* rpath, bool copy)
{ {
if (copy) { if (copy) {
this->data = (uint8_t*) malloc(size); this->data = (uint8_t*) malloc(size);

View file

@ -37,8 +37,8 @@ public:
WebpLoader(); WebpLoader();
~WebpLoader(); ~WebpLoader();
bool open(const string& path) override; bool open(const char* path) override;
bool open(const char* data, uint32_t size, const string& rpath, bool copy) override; bool open(const char* data, uint32_t size, const char* rpath, bool copy) override;
bool read() override; bool read() override;
bool close() override; bool close() override;

View file

@ -48,8 +48,8 @@ struct LoadModule
if (pathcache) free(hashpath); if (pathcache) free(hashpath);
} }
virtual bool open(const string& path) { return false; } virtual bool open(const char* path) { return false; }
virtual bool open(const char* data, uint32_t size, const string& rpath, bool copy) { return false; } virtual bool open(const char* data, uint32_t size, const char* rpath, bool copy) { return false; }
virtual bool resize(Paint* paint, float w, float h) { return false; } virtual bool resize(Paint* paint, float w, float h) { return false; }
virtual void sync() {}; //finish immediately if any async update jobs. virtual void sync() {}; //finish immediately if any async update jobs.

View file

@ -33,8 +33,8 @@ class SaveModule
public: public:
virtual ~SaveModule() {} virtual ~SaveModule() {}
virtual bool save(Paint* paint, Paint* bg, const string& path, uint32_t quality) = 0; virtual bool save(Paint* paint, Paint* bg, const char* filename, uint32_t quality) = 0;
virtual bool save(Animation* animation, Paint* bg, const string& path, uint32_t quality, uint32_t fps) = 0; virtual bool save(Animation* animation, Paint* bg, const char* filename, uint32_t quality, uint32_t fps) = 0;
virtual bool close() = 0; virtual bool close() = 0;
}; };

View file

@ -113,14 +113,14 @@ bool GifSaver::close()
} }
bool GifSaver::save(TVG_UNUSED Paint* paint, TVG_UNUSED Paint* bg, TVG_UNUSED const string& path, TVG_UNUSED uint32_t quality) bool GifSaver::save(TVG_UNUSED Paint* paint, TVG_UNUSED Paint* bg, TVG_UNUSED const char* filename, TVG_UNUSED uint32_t quality)
{ {
TVGLOG("GIF_SAVER", "Paint is not supported."); TVGLOG("GIF_SAVER", "Paint is not supported.");
return false; return false;
} }
bool GifSaver::save(Animation* animation, Paint* bg, const string& path, TVG_UNUSED uint32_t quality, uint32_t fps) bool GifSaver::save(Animation* animation, Paint* bg, const char* filename, TVG_UNUSED uint32_t quality, uint32_t fps)
{ {
close(); close();
@ -138,10 +138,11 @@ bool GifSaver::save(Animation* animation, Paint* bg, const string& path, TVG_UNU
return false; return false;
} }
this->path = strdup(path.c_str()); if (!filename) return false;
if (!this->path) return false; this->path = strdup(filename);
this->animation = animation; this->animation = animation;
if (bg) this->bg = bg->duplicate(); if (bg) this->bg = bg->duplicate();
this->fps = static_cast<float>(fps); this->fps = static_cast<float>(fps);

View file

@ -44,8 +44,8 @@ private:
public: public:
~GifSaver(); ~GifSaver();
bool save(Paint* paint, Paint* bg, const string& path, uint32_t quality) override; bool save(Paint* paint, Paint* bg, const char* filename, uint32_t quality) override;
bool save(Animation* animation, Paint* bg, const string& path, uint32_t quality, uint32_t fps) override; bool save(Animation* animation, Paint* bg, const char* filename, uint32_t quality, uint32_t fps) override;
bool close() override; bool close() override;
}; };