From f31076a67ee3cd4453d8853ef8c2463becfb9e4d Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Mon, 7 Aug 2023 15:27:24 +0900 Subject: [PATCH] utils/str: Supplements functions. Added strDirname(), which returns the directory path name from the given string. Added str2int(), which converts from the string to the integer. These functions are used to supplement the existing string manipulation functions in the utils/str module. --- src/loaders/lottie/tvgLottieLoader.cpp | 14 ++------------ src/utils/tvgStr.cpp | 19 +++++++++++++++++++ src/utils/tvgStr.h | 8 ++++++-- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/loaders/lottie/tvgLottieLoader.cpp b/src/loaders/lottie/tvgLottieLoader.cpp index 140da341..262bb8de 100644 --- a/src/loaders/lottie/tvgLottieLoader.cpp +++ b/src/loaders/lottie/tvgLottieLoader.cpp @@ -39,16 +39,6 @@ static bool _checkDotLottie(const char *str) } -static int _str2int(const char* str, int len) -{ - int ret = 0; - for(int i = 0; i < len; ++i) { - ret = ret * 10 + (str[i] - '0'); - } - return ret; -} - - static int _str2float(const char* str, int len) { auto tmp = strDuplicate(str, len); @@ -149,7 +139,7 @@ bool LottieLoader::header() p += 4; auto e = strstr(p, ","); if (!e) e = strstr(p, "}"); - w = static_cast(_str2int(p, e - p)); + w = static_cast(str2int(p, e - p)); p = e; continue; } @@ -158,7 +148,7 @@ bool LottieLoader::header() p += 4; auto e = strstr(p, ","); if (!e) e = strstr(p, "}"); - h = static_cast(_str2int(p, e - p)); + h = static_cast(str2int(p, e - p)); p = e; continue; } diff --git a/src/utils/tvgStr.cpp b/src/utils/tvgStr.cpp index 0e6ca953..eeed4fc4 100644 --- a/src/utils/tvgStr.cpp +++ b/src/utils/tvgStr.cpp @@ -205,6 +205,15 @@ error: } +int str2int(const char* str, size_t n) +{ + int ret = 0; + for(size_t i = 0; i < n; ++i) { + ret = ret * 10 + (str[i] - '0'); + } + return ret; +} + char* strDuplicate(const char *str, size_t n) { auto len = strlen(str); @@ -217,4 +226,14 @@ char* strDuplicate(const char *str, size_t n) return (char *) memcpy(ret, str, n); } +char* strDirname(const char* path) +{ + const char *ptr = strrchr(path, '/'); +#ifdef _WIN32 + if (ptr) ptr = strrchr(ptr + 1, '\\'); +#endif + int len = int(ptr + 1 - path); // +1 to include '/' + return strDuplicate(path, len); +} + } diff --git a/src/utils/tvgStr.h b/src/utils/tvgStr.h index f3023a7e..448cc693 100644 --- a/src/utils/tvgStr.h +++ b/src/utils/tvgStr.h @@ -23,11 +23,15 @@ #ifndef _TVG_STR_H_ #define _TVG_STR_H_ +#include + namespace tvg { -float strToFloat(const char *nPtr, char **endPtr); -char* strDuplicate(const char *str, size_t n); +float strToFloat(const char *nPtr, char **endPtr); //convert to float +int str2int(const char* str, size_t n); //convert to integer +char* strDuplicate(const char *str, size_t n); //copy the string +char* strDirname(const char* path); //return the full directory name } #endif //_TVG_STR_H_