From 7571b2ac56271777e0c5fde11371c0d054e9c1f5 Mon Sep 17 00:00:00 2001 From: mjvankampen Date: Thu, 25 Jun 2020 23:40:10 +0200 Subject: [PATCH] [C++] Updates real_path to be truly portable (#5787) * Removes posix definition stat and fseeko are not used. Tested on QNX (GCC 5.4.0) and MinGw * Updates realpath to more modern (2008) version * Removes unix specific headers * Adds detection of strtoll_l as android API < 21 does not provide it * Includes cstdlib and formatting Removes cmakelist alterations as not necessary. Formatting not complete. * Stdlib outside if Stdlib is available on all platforms * Fixes indentation * Adds locale check to android build * Adds missing brace * full names * Removes again, no clue * Updates base to check for locale independent android Cmake already checks for others, also MSC? * Changes to test on available and not requested * Fixes android bad xopen_version define * Removes warning --- include/flatbuffers/base.h | 12 +++++++++--- src/util.cpp | 19 +++++++++++++------ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/include/flatbuffers/base.h b/include/flatbuffers/base.h index 3d7aeb4ac..bfa02666d 100644 --- a/include/flatbuffers/base.h +++ b/include/flatbuffers/base.h @@ -46,6 +46,10 @@ #include #include +#if defined(__unix__) && !defined(FLATBUFFERS_LOCALE_INDEPENDENT) +#include +#endif + #ifdef _STLPORT_VERSION #define FLATBUFFERS_CPP98_STL #endif @@ -53,6 +57,10 @@ #include #endif +#ifdef __ANDROID__ + #include +#endif + #include "flatbuffers/stl_emulation.h" #if defined(__ICCARM__) @@ -236,10 +244,8 @@ namespace flatbuffers { #ifndef FLATBUFFERS_LOCALE_INDEPENDENT // Enable locale independent functions {strtof_l, strtod_l,strtoll_l, strtoull_l}. - // They are part of the POSIX-2008 but not part of the C/C++ standard. - // GCC/Clang have definition (_XOPEN_SOURCE>=700) if POSIX-2008. #if ((defined(_MSC_VER) && _MSC_VER >= 1800) || \ - (defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE>=700))) + (defined(_XOPEN_VERSION) && (_XOPEN_VERSION>=700)) && (!defined(__ANDROID_API__) || (defined(__ANDROID_API__) && (__ANDROID_API__>=21)))) #define FLATBUFFERS_LOCALE_INDEPENDENT 1 #else #define FLATBUFFERS_LOCALE_INDEPENDENT 0 diff --git a/src/util.cpp b/src/util.cpp index 08b779189..2eda18048 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -16,7 +16,10 @@ // clang-format off // Dont't remove `format off`, it prevent reordering of win-includes. -#define _POSIX_C_SOURCE 200112L // For stat from stat/stat.h and fseeko() (POSIX extensions). + +# define _POSIX_C_SOURCE 200809L +# define _XOPEN_SOURCE 700L + #ifdef _WIN32 # ifndef WIN32_LEAN_AND_MEAN # define WIN32_LEAN_AND_MEAN @@ -31,9 +34,6 @@ # include # include # undef interface // This is also important because of reasons -#else -# define _XOPEN_SOURCE 600 // For PATH_MAX from limits.h (SUSv2 extension) -# include #endif // clang-format on @@ -42,6 +42,7 @@ #include #include +#include #include namespace flatbuffers { @@ -196,8 +197,14 @@ std::string AbsolutePath(const std::string &filepath) { char abs_path[MAX_PATH]; return GetFullPathNameA(filepath.c_str(), MAX_PATH, abs_path, nullptr) #else - char abs_path[PATH_MAX]; - return realpath(filepath.c_str(), abs_path) + char *abs_path_temp = realpath(filepath.c_str(), nullptr); + bool success = abs_path_temp != nullptr; + std::string abs_path; + if(success) { + abs_path = abs_path_temp; + free(abs_path_temp); + } + return success #endif ? abs_path : filepath;