mirror of
https://github.com/ScrelliCopter/tmx2gba.git
synced 2025-02-21 03:29:25 +11:00
implement label sanitisation
This commit is contained in:
@@ -38,12 +38,10 @@ void HeaderWriter::WriteObjects(const std::span<uint32_t> objData)
|
||||
}
|
||||
|
||||
|
||||
static std::string GuardName(const std::string_view name)
|
||||
static std::string GuardName(std::string label)
|
||||
{
|
||||
auto upper = std::string(name);
|
||||
for (auto& c: upper)
|
||||
c = static_cast<char>(toupper(c));
|
||||
return "TMX2GBA_" + upper;
|
||||
std::transform(label.begin(), label.end(), label.begin(), ::toupper);
|
||||
return "TMX2GBA_" + label;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ static const ArgParse::Options options =
|
||||
" specified on the command line")
|
||||
};
|
||||
|
||||
bool ParseArgs(int argc, char** argv, Arguments& params)
|
||||
static bool ParseArgs(int argc, char** argv, Arguments& params)
|
||||
{
|
||||
auto parser = ArgParse::ArgParser(argv[0], options, [&](int opt, const std::string_view arg)
|
||||
-> ArgParse::ParseCtrl
|
||||
@@ -117,6 +117,25 @@ bool ParseArgs(int argc, char** argv, Arguments& params)
|
||||
}
|
||||
|
||||
|
||||
static std::string SanitiseLabel(const std::string& ident)
|
||||
{
|
||||
std::string out;
|
||||
out.reserve(ident.length());
|
||||
|
||||
int last = '_';
|
||||
for (int i : ident)
|
||||
{
|
||||
if (out.empty() && std::isdigit(i))
|
||||
continue;
|
||||
if (!std::isalnum(i))
|
||||
i = '_';
|
||||
if (i != '_' || last != '_')
|
||||
out.push_back(i);
|
||||
last = i;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
Arguments p;
|
||||
@@ -185,13 +204,7 @@ int main(int argc, char** argv)
|
||||
}
|
||||
|
||||
// Get name from file
|
||||
//TODO: properly sanitise
|
||||
int slashPos = std::max(
|
||||
static_cast<int>(p.outPath.find_last_of('/')),
|
||||
static_cast<int>(p.outPath.find_last_of('\\')));
|
||||
std::string name = p.outPath;
|
||||
if (slashPos != -1)
|
||||
name = name.substr(slashPos + 1);
|
||||
std::string name = SanitiseLabel(std::filesystem::path(p.outPath).stem());
|
||||
|
||||
// Open output files
|
||||
SWriter outS;
|
||||
|
||||
Reference in New Issue
Block a user