From 2441cf2153838593669f0731fa7a7f0410d19420 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Thu, 19 Jan 2017 11:44:18 -0800 Subject: [PATCH] Cleanup. --- include/bx/commandline.h | 19 +++++++++---------- include/bx/string.h | 3 +++ src/commandline.cpp | 25 +++++++++++++------------ src/string.cpp | 5 +++++ tests/dbg.cpp | 3 +-- tests/tokenizecmd_test.cpp | 3 +++ 6 files changed, 34 insertions(+), 24 deletions(-) diff --git a/include/bx/commandline.h b/include/bx/commandline.h index b14b2d0..9b9a397 100644 --- a/include/bx/commandline.h +++ b/include/bx/commandline.h @@ -7,20 +7,19 @@ #define BX_COMMANDLINE_H_HEADER_GUARD #include "bx.h" -#include "string.h" namespace bx { /// Reference: /// http://msdn.microsoft.com/en-us/library/a1y7w461.aspx - const char* tokenizeCommandLine(const char* _commandLine, char* _buffer, uint32_t& _bufferSize, int& _argc, char* _argv[], int _maxArgvs, char _term = '\0'); + const char* tokenizeCommandLine(const char* _commandLine, char* _buffer, uint32_t& _bufferSize, int32_t& _argc, char* _argv[], int32_t _maxArgvs, char _term = '\0'); /// class CommandLine { public: /// - CommandLine(int _argc, char const* const* _argv); + CommandLine(int32_t _argc, char const* const* _argv); /// const char* findOption(const char* _long, const char* _default) const; @@ -29,13 +28,13 @@ namespace bx const char* findOption(const char _short, const char* _long, const char* _default) const; /// - const char* findOption(const char* _long, int _numParams = 1) const; + const char* findOption(const char* _long, int32_t _numParams = 1) const; /// - const char* findOption(const char _short, const char* _long = NULL, int _numParams = 1) const; + const char* findOption(const char _short, const char* _long = NULL, int32_t _numParams = 1) const; /// - const char* findOption(int _skip, const char _short, const char* _long = NULL, int _numParams = 1) const; + const char* findOption(int32_t _skip, const char _short, const char* _long = NULL, int32_t _numParams = 1) const; /// bool hasArg(const char _short, const char* _long = NULL) const; @@ -47,10 +46,10 @@ namespace bx bool hasArg(const char*& _value, const char _short, const char* _long = NULL) const; /// - bool hasArg(int& _value, const char _short, const char* _long = NULL) const; + bool hasArg(int32_t& _value, const char _short, const char* _long = NULL) const; /// - bool hasArg(unsigned int& _value, const char _short, const char* _long = NULL) const; + bool hasArg(uint32_t& _value, const char _short, const char* _long = NULL) const; /// bool hasArg(float& _value, const char _short, const char* _long = NULL) const; @@ -63,9 +62,9 @@ namespace bx private: /// - const char* find(int _skip, const char _short, const char* _long, int _numParams) const; + const char* find(int32_t _skip, const char _short, const char* _long, int32_t _numParams) const; - int m_argc; + int32_t m_argc; char const* const* m_argv; }; diff --git a/include/bx/string.h b/include/bx/string.h index 8c01679..4815c9c 100644 --- a/include/bx/string.h +++ b/include/bx/string.h @@ -106,6 +106,9 @@ namespace bx /// bool isAlphaNum(char _ch); + /// + bool isPrint(char _ch); + /// char toLower(char _ch); diff --git a/src/commandline.cpp b/src/commandline.cpp index b303fbc..400e496 100644 --- a/src/commandline.cpp +++ b/src/commandline.cpp @@ -4,14 +4,15 @@ */ #include +#include namespace bx { // Reference: // http://msdn.microsoft.com/en-us/library/a1y7w461.aspx - const char* tokenizeCommandLine(const char* _commandLine, char* _buffer, uint32_t& _bufferSize, int& _argc, char* _argv[], int _maxArgvs, char _term) + const char* tokenizeCommandLine(const char* _commandLine, char* _buffer, uint32_t& _bufferSize, int32_t& _argc, char* _argv[], int32_t _maxArgvs, char _term) { - int argc = 0; + int32_t argc = 0; const char* curr = _commandLine; char* currOut = _buffer; char term = ' '; @@ -89,10 +90,10 @@ namespace bx if ('"' != *curr) { - int count = (int)(curr-start); + int32_t count = (int32_t)(curr-start); curr = start; - for (int ii = 0; ii < count; ++ii) + for (int32_t ii = 0; ii < count; ++ii) { *currOut = *curr; ++currOut; @@ -136,7 +137,7 @@ namespace bx return curr; } - CommandLine::CommandLine(int _argc, char const* const* _argv) + CommandLine::CommandLine(int32_t _argc, char const* const* _argv) : m_argc(_argc) , m_argv(_argv) { @@ -154,19 +155,19 @@ namespace bx return result == NULL ? _default : result; } - const char* CommandLine::findOption(const char* _long, int _numParams) const + const char* CommandLine::findOption(const char* _long, int32_t _numParams) const { const char* result = find(0, '\0', _long, _numParams); return result; } - const char* CommandLine::findOption(const char _short, const char* _long, int _numParams) const + const char* CommandLine::findOption(const char _short, const char* _long, int32_t _numParams) const { const char* result = find(0, _short, _long, _numParams); return result; } - const char* CommandLine::findOption(int _skip, const char _short, const char* _long, int _numParams) const + const char* CommandLine::findOption(int32_t _skip, const char _short, const char* _long, int32_t _numParams) const { const char* result = find(_skip, _short, _long, _numParams); return result; @@ -191,7 +192,7 @@ namespace bx return NULL != arg; } - bool CommandLine::hasArg(int& _value, const char _short, const char* _long) const + bool CommandLine::hasArg(int32_t& _value, const char _short, const char* _long) const { const char* arg = findOption(_short, _long, 1); if (NULL != arg) @@ -203,7 +204,7 @@ namespace bx return false; } - bool CommandLine::hasArg(unsigned int& _value, const char _short, const char* _long) const + bool CommandLine::hasArg(uint32_t& _value, const char _short, const char* _long) const { const char* arg = findOption(_short, _long, 1); if (NULL != arg) @@ -259,9 +260,9 @@ namespace bx return false; } - const char* CommandLine::find(int _skip, const char _short, const char* _long, int _numParams) const + const char* CommandLine::find(int32_t _skip, const char _short, const char* _long, int32_t _numParams) const { - for (int ii = 0; ii < m_argc; ++ii) + for (int32_t ii = 0; ii < m_argc; ++ii) { const char* arg = m_argv[ii]; if ('-' == *arg) diff --git a/src/string.cpp b/src/string.cpp index 4fd9bdc..0f694f8 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -50,6 +50,11 @@ namespace bx return isAlpha(_ch) || isNumeric(_ch); } + bool isPrint(char _ch) + { + return isAlphaNum(_ch) || isSpace(_ch); + } + char toLower(char _ch) { return _ch + (isUpper(_ch) ? 0x20 : 0); diff --git a/tests/dbg.cpp b/tests/dbg.cpp index 58e3f03..b960dc4 100644 --- a/tests/dbg.cpp +++ b/tests/dbg.cpp @@ -6,7 +6,6 @@ #include #include #include -#include // isprint #include "dbg.h" #include @@ -61,7 +60,7 @@ void dbgPrintfData(const void* _data, uint32_t _size, const char* _format, ...) bx::snprintf(&hex[hexPos], sizeof(hex)-hexPos, "%02x ", data[asciiPos]); hexPos += 3; - ascii[asciiPos] = isprint(data[asciiPos]) ? data[asciiPos] : '.'; + ascii[asciiPos] = bx::isPrint(data[asciiPos]) ? data[asciiPos] : '.'; asciiPos++; if (HEX_DUMP_WIDTH == asciiPos) diff --git a/tests/tokenizecmd_test.cpp b/tests/tokenizecmd_test.cpp index 332e8b0..7d35ac7 100644 --- a/tests/tokenizecmd_test.cpp +++ b/tests/tokenizecmd_test.cpp @@ -13,6 +13,8 @@ TEST(commandLine) { "-s", "--long", + "--platform", + "x", }; bx::CommandLine cmdLine(BX_COUNTOF(args), args); @@ -22,6 +24,7 @@ TEST(commandLine) // non-existing argument CHECK(!cmdLine.hasArg('x') ); + CHECK(!cmdLine.hasArg("preprocess") ); } TEST(tokenizeCommandLine)