Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion tools/cmd_dump_code.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
#include <google/protobuf/text_format.h>
#include <sys/stat.h>

#include <cctype>
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <sstream>

using namespace std;
Expand Down Expand Up @@ -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<std::string> 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,
Expand Down