This commit is contained in:
Branimir Karadžić
2017-01-19 11:44:18 -08:00
parent 4cb7362e99
commit 2441cf2153
6 changed files with 34 additions and 24 deletions

View File

@@ -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;
};

View File

@@ -106,6 +106,9 @@ namespace bx
///
bool isAlphaNum(char _ch);
///
bool isPrint(char _ch);
///
char toLower(char _ch);

View File

@@ -4,14 +4,15 @@
*/
#include <bx/commandline.h>
#include <bx/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)
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)

View File

@@ -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);

View File

@@ -6,7 +6,6 @@
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <ctype.h> // isprint
#include "dbg.h"
#include <bx/string.h>
@@ -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)

View File

@@ -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)