[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
This commit is contained in:
mjvankampen
2020-06-25 23:40:10 +02:00
committed by GitHub
parent e5a8f76a4b
commit 7571b2ac56
2 changed files with 22 additions and 9 deletions

View File

@@ -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 <direct.h>
# include <winbase.h>
# undef interface // This is also important because of reasons
#else
# define _XOPEN_SOURCE 600 // For PATH_MAX from limits.h (SUSv2 extension)
# include <limits.h>
#endif
// clang-format on
@@ -42,6 +42,7 @@
#include <sys/stat.h>
#include <clocale>
#include <cstdlib>
#include <fstream>
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;