svg_loader: custom strndup moved into utils

The custom _strndup was used only in one file
as an internal function and wasn't accessible
in other parts of the code. Now function
is available as svgUtilStrndup.
This commit is contained in:
Mira Grudzinska 2023-07-25 14:56:37 +02:00 committed by Hermet Park
parent a1f0b06f41
commit de41853bcf
4 changed files with 20 additions and 19 deletions

View file

@ -393,14 +393,7 @@ static char* _idFromUrl(const char* url)
int i = 0;
while (url[i] > ' ' && url[i] != ')' && url[i] != '\'') ++i;
//custom strndup() for portability
int len = strlen(url);
if (i < len) len = i;
auto ret = (char*) malloc(len + 1);
if (!ret) return 0;
ret[len] = '\0';
return (char*) memcpy(ret, url, len);
return svgUtilStrndup(url, i);
}

View file

@ -275,3 +275,16 @@ string svgUtilBase64Decode(const char *src)
}
return decoded;
}
char* svgUtilStrndup(const char* str, size_t n)
{
int len = strlen(str);
if (len < n) n = len;
auto ret = (char*)malloc(n + 1);
if (!ret) return nullptr;
ret[n] = '\0';
return (char*)memcpy(ret, str, n);
}

View file

@ -30,4 +30,6 @@ float svgUtilStrtof(const char *nPtr, char **endPtr);
string svgUtilURLDecode(const char *src);
string svgUtilBase64Decode(const char *src);
char* svgUtilStrndup(const char* str, size_t n);
#endif //_TVG_SVG_UTIL_H_

View file

@ -33,6 +33,7 @@
#endif
#include "tvgXmlParser.h"
#include "tvgSvgUtil.h"
/************************************************************************/
/* Internal Class Implementation */
@ -238,14 +239,6 @@ static SimpleXMLType _getXMLType(const char* itr, const char* itrEnd, size_t &to
}
static char* _strndup(const char* src, unsigned len)
{
auto ret = (char*)malloc(len + 1);
if (!ret) return nullptr;
ret[len] = '\0';
return (char*)memcpy(ret, src, len);
}
/************************************************************************/
/* External Class Implementation */
/************************************************************************/
@ -564,10 +557,10 @@ const char* simpleXmlParseCSSAttribute(const char* buf, unsigned bufLength, char
}
if (p == itr) *tag = strdup("all");
else *tag = _strndup(itr, p - itr);
else *tag = svgUtilStrndup(itr, p - itr);
if (p == itrEnd) *name = nullptr;
else *name = _strndup(p + 1, itrEnd - p - 1);
else *name = svgUtilStrndup(p + 1, itrEnd - p - 1);
return (nextElement ? nextElement + 1 : nullptr);
}