From d21f99ef2b2803df6350bc331896ec322cdf4141 Mon Sep 17 00:00:00 2001 From: Tim Wojtulewicz Date: Tue, 18 Oct 2022 13:30:06 -0700 Subject: [PATCH] binpac: Wrap native dirname() call in ifdef, call std::filesystem on Windows --- tools/binpac/src/pac_scan.ll | 60 ++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 23 deletions(-) diff --git a/tools/binpac/src/pac_scan.ll b/tools/binpac/src/pac_scan.ll index dd90792bff..0a11d9bb93 100644 --- a/tools/binpac/src/pac_scan.ll +++ b/tools/binpac/src/pac_scan.ll @@ -22,9 +22,15 @@ #include "pac_utils.h" #include -#include #include +#ifdef MSVC +#include +#else +#include +#include +#endif + int line_number = 1; int begin_pac_primitive(int tok); @@ -44,11 +50,24 @@ int char_token(int tok) void include_file(const char *filename); -string dirname(std::string_view path) -{ - return std::filesystem::path(path).parent_path().string(); -} +std::string do_dirname(std::string_view s) + { +#ifdef MSVC + return std::filesystem::path(path).parent_path().string(); +#else + std::unique_ptr tmp{new char[s.size()+1]}; + strncpy(tmp.get(), s.data(), s.size()); + tmp[s.size()] = '\0'; + char* dn = dirname(tmp.get()); + if ( !dn ) + return ""; + + std::string res{dn}; + + return res; +#endif + } %} /* EC -- embedded code state */ @@ -337,25 +356,20 @@ void include_file(const char *filename) string full_filename; if ( filename[0] == '/' ) full_filename = filename; - else if ( filename[0] == '.' ) - { - char* tmp = new char[strlen(input_filename.c_str()) + 1]; - strcpy(tmp, input_filename.c_str()); - string dir = dirname(tmp); + else if ( filename[0] == '.' ) + { + string dir = do_dirname(input_filename); - if ( ! dir.empty() ) - full_filename = dir + "/" + filename; - else - { - fprintf(stderr, "%s:%d error: cannot include file \"%s\": %s\n", - input_filename.c_str(), line_number, filename, - strerror(errno)); - delete [] tmp; - return; - } - - delete [] tmp; - } + if ( ! dir.empty() ) + full_filename = dir + "/" + filename; + else + { + fprintf(stderr, "%s:%d error: cannot include file \"%s\": %s\n", + input_filename.c_str(), line_number, filename, + strerror(errno)); + return; + } + } else { int i;