mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 14:48:21 +00:00
binpac: Wrap native dirname() call in ifdef, call std::filesystem on Windows
This commit is contained in:
parent
faa1b7abbf
commit
d21f99ef2b
1 changed files with 37 additions and 23 deletions
|
@ -22,9 +22,15 @@
|
|||
#include "pac_utils.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <filesystem>
|
||||
#include <string_view>
|
||||
|
||||
#ifdef MSVC
|
||||
#include <filesystem>
|
||||
#else
|
||||
#include <libgen.h>
|
||||
#include <memory>
|
||||
#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)
|
||||
{
|
||||
std::string do_dirname(std::string_view s)
|
||||
{
|
||||
#ifdef MSVC
|
||||
return std::filesystem::path(path).parent_path().string();
|
||||
}
|
||||
#else
|
||||
std::unique_ptr<char[]> 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 */
|
||||
|
@ -339,9 +358,7 @@ void include_file(const char *filename)
|
|||
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);
|
||||
string dir = do_dirname(input_filename);
|
||||
|
||||
if ( ! dir.empty() )
|
||||
full_filename = dir + "/" + filename;
|
||||
|
@ -350,11 +367,8 @@ void include_file(const char *filename)
|
|||
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;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue