diff --git a/tools/cmd_dump_code.cpp b/tools/cmd_dump_code.cpp index 6e217ad..681c30a 100644 --- a/tools/cmd_dump_code.cpp +++ b/tools/cmd_dump_code.cpp @@ -20,11 +20,13 @@ #include #include +#include #include #include #include #include #include +#include #include using namespace std; @@ -53,7 +55,30 @@ static void create_parent_path(const std::string& path) { } static std::string mk_file_name(const std::string& dirname, const std::string& file) { - return dirname + "/" + file; + std::string safe; + safe.reserve(file.size()); + for (char c : file) { + const bool unsafe = c == '/' || c == '\\' || c == ':' || c == '<' || + c == '>' || c == '"' || c == '|' || c == '?' || + c == '*'; + safe.push_back(unsafe ? '_' : c); + } + CHECK(!safe.empty() && safe != "." && safe != "..") + << "invalid output filename derived from xmodel: " << file; +#ifdef _WIN32 + { + auto stem = safe.substr(0, safe.find('.')); + for (auto& ch : stem) ch = (char)::toupper((unsigned char)ch); + static const std::set kReserved = { + "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", + "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", + "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"}; + if (kReserved.count(stem)) { + safe = "_" + safe; + } + } +#endif + return dirname + "/" + safe; } static void dump_subgraph1(const xir::Subgraph* sg,