From 40e380f2c08686395b6a18f9fefec25c5b7b1a1b Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 11 Apr 2020 12:23:11 +0200 Subject: [PATCH] - updated m_argv.cpp from Raze. --- src/utility/m_argv.cpp | 62 ++++++++++++++++++++++++++++++++---------- src/utility/m_argv.h | 38 +++++++++++++++++++++++++- 2 files changed, 84 insertions(+), 16 deletions(-) diff --git a/src/utility/m_argv.cpp b/src/utility/m_argv.cpp index ed2b08f1ca..d6f834d88c 100644 --- a/src/utility/m_argv.cpp +++ b/src/utility/m_argv.cpp @@ -34,7 +34,7 @@ #include #include "m_argv.h" -#include "i_system.h" +#include "zstring.h" //=========================================================================== // @@ -68,6 +68,17 @@ FArgs::FArgs(int argc, char **argv) SetArgs(argc, argv); } +//=========================================================================== +// +// FArgs Argv Constructor +// +//=========================================================================== + +FArgs::FArgs(int argc, const char** argv) +{ + SetArgs(argc, const_cast(argv)); // Thanks, C++, for the inflexible const casting rules... +} + //=========================================================================== // // FArgs String Argv Constructor @@ -128,7 +139,22 @@ void FArgs::FlushArgs() // //=========================================================================== -int FArgs::CheckParm(const char *check, int start) const +int stricmp(const char** check, const char* str) +{ + for (int i = 0; check[i]; i++) + { + if (!stricmp(check[i], str)) return 0; + } + return 1; // we do not care about order here. +} + +int FArgs::CheckParm(const char* check, int start) const +{ + const char* array[] = { check, nullptr }; + return CheckParm(array, start); +} + +int FArgs::CheckParm(const char** check, int start) const { for (unsigned i = start; i < Argv.Size(); ++i) { @@ -155,9 +181,9 @@ int FArgs::CheckParmList(const char *check, FString **strings, int start) const if (parmat == 0) { - if (strings != NULL) + if (strings != nullptr) { - *strings = NULL; + *strings = nullptr; } return 0; } @@ -168,7 +194,7 @@ int FArgs::CheckParmList(const char *check, FString **strings, int start) const break; } } - if (strings != NULL) + if (strings != nullptr) { *strings = &Argv[parmat]; } @@ -180,7 +206,7 @@ int FArgs::CheckParmList(const char *check, FString **strings, int start) const // FArgs :: CheckValue // // Like CheckParm, but it also checks that the parameter has a value after -// it and returns that or NULL if not present. +// it and returns that or nullptr if not present. // //=========================================================================== @@ -191,11 +217,11 @@ const char *FArgs::CheckValue(const char *check) const if (i > 0 && i < (int)Argv.Size() - 1) { i++; - return Argv[i][0] != '+' && Argv[i][0] != '-' ? Argv[i].GetChars() : NULL; + return Argv[i][0] != '+' && Argv[i][0] != '-' ? Argv[i].GetChars() : nullptr; } else { - return NULL; + return nullptr; } } @@ -258,7 +284,7 @@ void FArgs::RemoveArgs(const char *check) const char *FArgs::GetArg(int arg) const { - return ((unsigned)arg < Argv.Size()) ? Argv[arg].GetChars() : NULL; + return ((unsigned)arg < Argv.Size()) ? Argv[arg].GetChars() : nullptr; } //=========================================================================== @@ -271,7 +297,7 @@ const char *FArgs::GetArg(int arg) const FString *FArgs::GetArgList(int arg) const { - return ((unsigned)arg < Argv.Size()) ? &Argv[arg] : NULL; + return ((unsigned)arg < Argv.Size()) ? &Argv[arg] : nullptr; } //=========================================================================== @@ -338,16 +364,22 @@ void FArgs::RemoveArg(int argindex) // // Takes all arguments after any instance of -param and any arguments before // all switches that end in .extension and combines them into a single -// -switch block at the end of the arguments. If extension is NULL, then +// -switch block at the end of the arguments. If extension is nullptr, then // every parameter before the first switch is added after this -param. // //=========================================================================== -void FArgs::CollectFiles(const char *param, const char *extension) +void FArgs::CollectFiles(const char* param, const char* extension) +{ + const char* array[] = { param, nullptr }; + CollectFiles(param, array, extension); +} + +void FArgs::CollectFiles(const char *finalname, const char **param, const char *extension) { TArray work; unsigned int i; - size_t extlen = extension == NULL ? 0 : strlen(extension); + size_t extlen = extension == nullptr ? 0 : strlen(extension); // Step 1: Find suitable arguments before the first switch. i = 1; @@ -387,7 +419,7 @@ void FArgs::CollectFiles(const char *param, const char *extension) } // Optional: Replace short path names with long path names -#ifdef _WIN32 +#if 0 //def _WIN32 for (i = 0; i < work.Size(); ++i) { work[i] = I_GetLongPathName(work[i]); @@ -397,7 +429,7 @@ void FArgs::CollectFiles(const char *param, const char *extension) // Step 3: Add work back to Argv, as long as it's non-empty. if (work.Size() > 0) { - Argv.Push(param); + Argv.Push(finalname); AppendArgs(work.Size(), &work[0]); } } diff --git a/src/utility/m_argv.h b/src/utility/m_argv.h index 31df56ad8e..9537697038 100644 --- a/src/utility/m_argv.h +++ b/src/utility/m_argv.h @@ -34,7 +34,7 @@ #ifndef __M_ARGV_H__ #define __M_ARGV_H__ -#include "dobject.h" +#include "tarray.h" #include "zstring.h" // @@ -43,23 +43,58 @@ class FArgs { public: + + typedef TIterator iterator; + typedef TIterator const_iterator; + typedef FString value_type; + + iterator begin() + { + return Argv.begin(); + } + const_iterator begin() const + { + return Argv.begin(); + } + const_iterator cbegin() const + { + return Argv.begin(); + } + + iterator end() + { + return Argv.end(); + } + const_iterator end() const + { + return Argv.end(); + } + const_iterator cend() const + { + return Argv.end(); + } + FArgs(); FArgs(const FArgs &args); FArgs(int argc, char **argv); + FArgs(int argc, const char** argv); FArgs(int argc, FString *argv); FArgs &operator=(const FArgs &other); + const FString& operator[](size_t index) { return Argv[index]; } void AppendArg(FString arg); void AppendArgs(int argc, const FString *argv); void RemoveArg(int argindex); void RemoveArgs(const char *check); void SetArgs(int argc, char **argv); + void CollectFiles(const char *finalname, const char** param, const char* extension); void CollectFiles(const char *param, const char *extension); FArgs *GatherFiles(const char *param) const; void SetArg(int argnum, const char *arg); int CheckParm(const char *check, int start=1) const; // Returns the position of the given parameter in the arg list (0 if not found). + int CheckParm(const char** check, int start = 1) const; // Returns the position of the given parameter in the arg list (0 if not found). Allows checking for multiple switches int CheckParmList(const char *check, FString **strings, int start=1) const; const char *CheckValue(const char *check) const; const char *GetArg(int arg) const; @@ -67,6 +102,7 @@ public: FString TakeValue(const char *check); int NumArgs() const; void FlushArgs(); + TArray& Array() { return Argv; } private: TArray Argv;