Added a test build
[RedVex.git] / core / Command.cpp
bloba257d5e6da29291894c8d3b367b4bd6effe545c2
1 #include "Command.h"
2 #include <string.h>
4 Command::Command(const char* commandLine) :
5 _tokens(0)
7 Parse(commandLine);
10 Command::Command()
15 Command::~Command()
17 Clear();
20 int Command::Parse(const char* commandLine, const char* delimiters)
22 // clear any parameters that may already be in the command
23 Clear();
25 // allocate and store off a local version of the command line string
26 int commandLineLength = static_cast<int>(strlen(commandLine));
27 _tokens = new char[commandLineLength + 1];
28 strcpy_s(_tokens, commandLineLength + 1, commandLine);
30 // parse out the individual parameters in the command line string
31 char* locale = 0;
32 char* token = strtok_s(_tokens, delimiters, &locale);
33 while (token != 0)
35 _parameters.push_back(token);
36 token = strtok_s(0, delimiters, &locale);
39 return GetParameterCount();
42 void Command::Clear()
44 _parameters.clear();
46 if (_tokens != 0)
48 delete[] _tokens;
49 _tokens = 0;
53 char* Command::GetParameter(int index, char* parameter, int size) const
55 if (index < GetParameterCount())
57 strcpy_s(parameter, size, _parameters[index]);
59 else
61 parameter[0] = '\0';
64 return parameter;
67 const char* Command::GetParameter(int index) const
69 if (index < GetParameterCount())
71 return _parameters[index];
74 return 0;
77 int Command::GetParameterCount() const
79 return static_cast<int>(_parameters.size());
82 int Command::FindParameter(const char* parameter, bool caseSensitive) const
84 for (int i = 0; i < GetParameterCount(); i++)
86 if (caseSensitive)
88 if (strcmp(parameter, GetParameter(i)) == 0)
90 return i;
94 else
96 if (_stricmp(parameter, GetParameter(i)) == 0)
98 return i;
103 return -1;
106 bool Command::HasParameter(const char* parameter, bool caseSensitive) const
108 return FindParameter(parameter, caseSensitive) > -1;