From 7129b0ec042f58cbf9c38a7ea31c3e9d7557c127 Mon Sep 17 00:00:00 2001 From: "Emil J. Tywoniak" Date: Wed, 11 Sep 2024 21:04:24 +0200 Subject: [PATCH] reduce OS ifdefs with OS_PATH_SEP --- backends/cxxrtl/cxxrtl_backend.cc | 7 +------ frontends/ast/simplify.cc | 7 +------ frontends/verilog/preproc.cc | 6 +----- kernel/yosys_common.h | 5 +++++ 4 files changed, 8 insertions(+), 17 deletions(-) diff --git a/backends/cxxrtl/cxxrtl_backend.cc b/backends/cxxrtl/cxxrtl_backend.cc index 8dc14863d60..f4d1c856050 100644 --- a/backends/cxxrtl/cxxrtl_backend.cc +++ b/backends/cxxrtl/cxxrtl_backend.cc @@ -639,12 +639,7 @@ std::string escape_cxx_string(const std::string &input) std::string basename(const std::string &filepath) { -#ifdef _WIN32 - const std::string dir_seps = "\\/"; -#else - const std::string dir_seps = "/"; -#endif - size_t sep_pos = filepath.find_last_of(dir_seps); + size_t sep_pos = filepath.find_last_of(OS_PATH_SEP); if (sep_pos != std::string::npos) return filepath.substr(sep_pos + 1); else diff --git a/frontends/ast/simplify.cc b/frontends/ast/simplify.cc index 3d8478ef160..1f77d999d33 100644 --- a/frontends/ast/simplify.cc +++ b/frontends/ast/simplify.cc @@ -4382,12 +4382,7 @@ AstNode *AstNode::readmem(bool is_readmemh, std::string mem_filename, AstNode *m std::ifstream f; f.open(mem_filename.c_str()); if (f.fail()) { -#ifdef _WIN32 - char slash = '\\'; -#else - char slash = '/'; -#endif - std::string path = filename.substr(0, filename.find_last_of(slash)+1); + std::string path = filename.substr(0, filename.find_last_of(OS_PATH_SEP)+1); f.open(path + mem_filename.c_str()); yosys_input_files.insert(path + mem_filename); } else { diff --git a/frontends/verilog/preproc.cc b/frontends/verilog/preproc.cc index e33b0a2c326..e810b56ff6b 100644 --- a/frontends/verilog/preproc.cc +++ b/frontends/verilog/preproc.cc @@ -892,11 +892,7 @@ frontend_verilog_preproc(std::istream &f, // if the include file was not found, it is not given with an absolute path, and the // currently read file is given with a path, then try again relative to its directory ff.clear(); -#ifdef _WIN32 - fixed_fn = filename.substr(0, filename.find_last_of("/\\")+1) + fn; -#else - fixed_fn = filename.substr(0, filename.rfind('/')+1) + fn; -#endif + fixed_fn = filename.substr(0, filename.find_last_of(OS_PATH_SEP)+1) + fn; ff.open(fixed_fn); } if (ff.fail() && fn.size() > 0 && fn_relative) { diff --git a/kernel/yosys_common.h b/kernel/yosys_common.h index ec1b5aba2ba..81d5079e5e7 100644 --- a/kernel/yosys_common.h +++ b/kernel/yosys_common.h @@ -340,6 +340,11 @@ bool is_absolute_path(std::string filename); void remove_directory(std::string dirname); bool create_directory(const std::string& dirname); std::string escape_filename_spaces(const std::string& filename); +#ifdef _WIN32 +const char* const OS_PATH_SEP = "/\\"; +#else +const char* const OS_PATH_SEP = "/"; +#endif template int GetSize(const T &obj) { return obj.size(); } inline int GetSize(RTLIL::Wire *wire);