Move string utilities out to separate file (really fixing Linux build this time)
Jeffrey Pfau jeffrey@endrift.com
Sun, 19 Oct 2014 03:01:26 -0700
5 files changed,
48 insertions(+),
26 deletions(-)
M
CMakeLists.txt
→
CMakeLists.txt
@@ -82,6 +82,13 @@ find_feature(USE_FFMPEG "libavcodec;libavformat;libavutil")
find_feature(USE_PNG "PNG;ZLIB") find_feature(USE_LIBZIP "libzip") +include(CheckFunctionExists) +check_function_exists(strndup HAVE_STRNDUP) + +if(HAVE_STRNDUP) + add_definitions(-DHAVE_STRNDUP) +endif() + # Platform support if(WIN32) add_definitions(-D_WIN32_WINNT=0x0600)@@ -100,10 +107,6 @@ enable_language(ASM)
if(NOT BUILD_EGL) add_definitions(-DCOLOR_16_BIT -DCOLOR_5_6_5) endif() -endif() - -if(CMAKE_SYSTEM_NAME STREQUAL "Linux") - list(APPEND OS_LIB bsd) endif() # Features
M
src/debugger/parser.c
→
src/debugger/parser.c
@@ -1,16 +1,6 @@
#include "parser.h" -static inline char* _strndup(const char* start, size_t len) { -#ifdef HAVE_STRNDUP - return strndup(start, len); -#else - // This is suboptimal, but anything recent should have strndup - char* out = malloc((len + 1) * sizeof(char)); - strncpy(out, start, len); - out[len] = '\0'; - return out; -#endif -} +#include "util/string.h" static struct LexVector* _lexOperator(struct LexVector* lv, char operator) { struct LexVector* lvNext = malloc(sizeof(struct LexVector));@@ -108,13 +98,13 @@ case '-':
case '*': case '/': lv->token.type = TOKEN_IDENTIFIER_TYPE; - lv->token.identifierValue = _strndup(tokenStart, string - tokenStart - 1); + lv->token.identifierValue = strndup(tokenStart, string - tokenStart - 1); lv = _lexOperator(lv, token); state = LEX_ROOT; break; case ')': lv->token.type = TOKEN_IDENTIFIER_TYPE; - lv->token.identifierValue = _strndup(tokenStart, string - tokenStart - 1); + lv->token.identifierValue = strndup(tokenStart, string - tokenStart - 1); state = LEX_EXPECT_OPERATOR; break; default:@@ -298,7 +288,7 @@ lv->token.uintValue = next;
break; case LEX_EXPECT_IDENTIFIER: lv->token.type = TOKEN_IDENTIFIER_TYPE; - lv->token.identifierValue = _strndup(tokenStart, string - tokenStart); + lv->token.identifierValue = strndup(tokenStart, string - tokenStart); break; case LEX_EXPECT_OPERATOR: lvNext = malloc(sizeof(struct LexVector));
A
src/util/string.c
@@ -0,0 +1,25 @@
+#include "util/string.h" + +#include <string.h> + +#ifndef HAVE_STRNDUP +char* strndup(const char* start, size_t len) { + // This is suboptimal, but anything recent should have strndup + char* out = malloc((len + 1) * sizeof(char)); + strncpy(out, start, len); + out[len] = '\0'; + return out; +} +#endif + +char* strnrstr(const char* restrict haystack, const char* restrict needle, size_t len) { + char* last = 0; + const char* next = haystack; + size_t needleLen = strlen(needle); + for (; len >= needleLen; --len, ++next) { + if (strncmp(needle, next, needleLen) == 0) { + last = (char*) next; + } + } + return last; +}
A
src/util/string.h
@@ -0,0 +1,9 @@
+#ifndef UTIL_STRING_H +#define UTIL_STRING_H + +#include "util/common.h" + +char* strndup(const char* start, size_t len); +char* strnrstr(const char* restrict s1, const char* restrict s2, size_t len); + +#endif
M
src/util/vfs.c
→
src/util/vfs.c
@@ -1,5 +1,7 @@
#include "util/vfs.h" +#include "util/string.h" + #include <fcntl.h> #include <dirent.h>@@ -273,14 +275,7 @@ size_t len = strlen(filename);
if (dotPoint) { len = (dotPoint - filename); } - const char* separator = 0; - const char* nextSeparator = filename; - size_t strstrlen = len; - while ((nextSeparator = strnstr(nextSeparator, infix, strstrlen))) { - strstrlen -= nextSeparator - separator - 1; - separator = nextSeparator; - ++nextSeparator; - } + const char* separator = strnrstr(filename, infix, len); if (!separator) { continue; }