diff options
Diffstat (limited to 'tools/mid2agb/main.cpp')
-rw-r--r-- | tools/mid2agb/main.cpp | 124 |
1 files changed, 55 insertions, 69 deletions
diff --git a/tools/mid2agb/main.cpp b/tools/mid2agb/main.cpp index 5659e49b1..03feabdd8 100644 --- a/tools/mid2agb/main.cpp +++ b/tools/mid2agb/main.cpp @@ -50,7 +50,8 @@ bool g_compressionEnabled = true; " input_file filename(.mid) of MIDI file\n" " output_file filename(.s) for AGB file (default:input_file)\n" "\n" - "options -V??? master volume (default:127)\n" + "options -L??? label for assembler (default:output_file)\n" + " -V??? master volume (default:127)\n" " -G??? voice group number (default:0)\n" " -P??? priority (default:0)\n" " -R??? reverb (default:off)\n" @@ -73,18 +74,6 @@ static std::string StripExtension(std::string s) return s; } -static std::string StripPathAndExtension(std::string s) -{ - std::size_t pos = s.find_last_of("/\\"); - - if (pos > 0 && pos != std::string::npos) - { - s = s.substr(pos + 1, pos); - } - - return StripExtension(s); -} - static std::string GetExtension(std::string s) { std::size_t pos = s.find_last_of('.'); @@ -97,57 +86,45 @@ static std::string GetExtension(std::string s) return ""; } -struct Option -{ - char letter = 0; - const char *arg = NULL; -}; - -static Option ParseOption(int& index, const int argc, char** argv) +static std::string BaseName(std::string s) { - static std::set<char> optionsWithArg = { 'L', 'V', 'G', 'P', 'R' }; - static std::set<char> optionsWithoutArg = { 'X', 'E', 'N' }; - - assert(index >= 0 && index < argc); + std::size_t posAfterSlash = s.find_last_of("/\\"); - const char *opt = argv[index]; + if (posAfterSlash == std::string::npos) + posAfterSlash = 0; + else + posAfterSlash++; - assert(opt[0] == '-'); - assert(std::strlen(opt) == 2); + std::size_t dotPos = s.find_first_of('.', posAfterSlash); + if (dotPos > posAfterSlash && dotPos != std::string::npos) + s = s.substr(posAfterSlash, dotPos - posAfterSlash); - char letter = std::toupper(opt[1]); - - bool isOption = false; - bool hasArg = false; + return s; +} - if (optionsWithArg.count(letter) != 0) - { - isOption = true; - hasArg = true; - } - else if (optionsWithoutArg.count(letter) != 0) - { - isOption = true; - } +static const char *GetArgument(int argc, char **argv, int& index) +{ + assert(index >= 0 && index < argc); - if (!isOption) - PrintUsage(); + const char *option = argv[index]; - Option retVal; + assert(option != nullptr); + assert(option[0] == '-'); - retVal.letter = letter; + // If there is text following the letter, return that. + if (std::strlen(option) >= 3) + return option + 2; - if (hasArg) + // Otherwise, try to get the next arg. + if (index + 1 < argc) { index++; - - if (index >= argc) - RaiseError("missing argument for \"-%c\"", letter); - - retVal.arg = argv[index]; + return argv[index]; + } + else + { + return nullptr; } - - return retVal; } int main(int argc, char** argv) @@ -157,51 +134,60 @@ int main(int argc, char** argv) for (int i = 1; i < argc; i++) { - if (argv[i][0] == '-' && std::strlen(argv[i]) == 2) + const char *option = argv[i]; + + if (option[0] == '-' && option[1] != '\0') { - Option option = ParseOption(i, argc, argv); + const char *arg = GetArgument(argc, argv, i); - switch (option.letter) + switch (std::toupper(option[1])) { case 'E': g_exactGateTime = true; break; case 'G': - g_voiceGroup = std::stoi(option.arg); + if (arg == nullptr) + PrintUsage(); + g_voiceGroup = std::stoi(arg); break; case 'L': - g_asmLabel = option.arg; + if (arg == nullptr) + PrintUsage(); + g_asmLabel = std::stoi(arg); break; case 'N': g_compressionEnabled = false; break; case 'P': - g_priority = std::stoi(option.arg); + if (arg == nullptr) + PrintUsage(); + g_priority = std::stoi(arg); break; case 'R': - g_reverb = std::stoi(option.arg); + if (arg == nullptr) + PrintUsage(); + g_reverb = std::stoi(arg); break; case 'V': - g_masterVolume = std::stoi(option.arg); + if (arg == nullptr) + PrintUsage(); + g_masterVolume = std::stoi(arg); break; case 'X': g_clocksPerBeat = 2; break; + default: + PrintUsage(); } } else { - switch (i) - { - case 1: + if (inputFilename.empty()) inputFilename = argv[i]; - break; - case 2: + else if (outputFilename.empty()) outputFilename = argv[i]; - break; - default: + else PrintUsage(); - } } } @@ -218,7 +204,7 @@ int main(int argc, char** argv) RaiseError("output filename extension is not \"s\""); if (g_asmLabel.empty()) - g_asmLabel = StripPathAndExtension(outputFilename); + g_asmLabel = BaseName(outputFilename); g_inputFile = std::fopen(inputFilename.c_str(), "rb"); |