CmdLine.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "CrySystem_precompiled.h"
  9. #include "CmdLine.h"
  10. void CCmdLine::PushCommand(const AZStd::string& sCommand, const AZStd::string& sParameter)
  11. {
  12. if (sCommand.empty())
  13. {
  14. return;
  15. }
  16. ECmdLineArgType type = eCLAT_Normal;
  17. const char* szCommand = sCommand.c_str();
  18. if (sCommand[0] == '-')
  19. {
  20. type = eCLAT_Pre;
  21. ++szCommand;
  22. // Handle cmd line parameters that use -- properly
  23. if (szCommand[0] == '-')
  24. {
  25. ++szCommand;
  26. }
  27. }
  28. else if (sCommand[0] == '+')
  29. {
  30. type = eCLAT_Post;
  31. ++szCommand;
  32. }
  33. m_args.push_back(CCmdLineArg(szCommand, sParameter.c_str(), type));
  34. }
  35. CCmdLine::CCmdLine(const char* commandLine)
  36. {
  37. m_sCmdLine = commandLine;
  38. char* src = (char*)commandLine;
  39. AZStd::string command, parameter;
  40. for (;; )
  41. {
  42. if (*src == '\0')
  43. {
  44. break;
  45. }
  46. AZStd::string arg = Next(src);
  47. if (m_args.empty())
  48. {
  49. // this is the filename, convert backslash to forward slash
  50. AZ::StringFunc::Replace(arg, '\\', '/');
  51. m_args.push_back(CCmdLineArg("filename", arg.c_str(), eCLAT_Executable));
  52. }
  53. else if(!arg.empty())
  54. {
  55. bool bSecondCharIsNumber = false;
  56. if ((arg.length() > 1) && arg[1] >= '0' && arg[1] <= '9')
  57. {
  58. bSecondCharIsNumber = true;
  59. }
  60. if ((arg[0] == '-' && !bSecondCharIsNumber)
  61. || (arg[0] == '+' && !bSecondCharIsNumber)
  62. || command.empty()) // separator or first parameter
  63. {
  64. PushCommand(command, parameter);
  65. command = arg;
  66. parameter = "";
  67. }
  68. else
  69. {
  70. if (parameter.empty())
  71. {
  72. parameter = arg;
  73. }
  74. else
  75. {
  76. parameter += AZStd::string(" ") + arg;
  77. }
  78. }
  79. }
  80. }
  81. PushCommand(command, parameter);
  82. }
  83. CCmdLine::~CCmdLine()
  84. {
  85. }
  86. const ICmdLineArg* CCmdLine::GetArg(int n) const
  87. {
  88. if ((n >= 0) && (n < (int)m_args.size()))
  89. {
  90. return &m_args[n];
  91. }
  92. return 0;
  93. }
  94. int CCmdLine::GetArgCount() const
  95. {
  96. return (int)m_args.size();
  97. }
  98. const ICmdLineArg* CCmdLine::FindArg(const ECmdLineArgType ArgType, const char* name, bool caseSensitive) const
  99. {
  100. if (caseSensitive)
  101. {
  102. for (std::vector<CCmdLineArg>::const_iterator it = m_args.begin(); it != m_args.end(); ++it)
  103. {
  104. if (it->GetType() == ArgType)
  105. {
  106. if (!strcmp(it->GetName(), name))
  107. {
  108. return &(*it);
  109. }
  110. }
  111. }
  112. }
  113. else
  114. {
  115. for (std::vector<CCmdLineArg>::const_iterator it = m_args.begin(); it != m_args.end(); ++it)
  116. {
  117. if (it->GetType() == ArgType)
  118. {
  119. if (!_stricmp(it->GetName(), name))
  120. {
  121. return &(*it);
  122. }
  123. }
  124. }
  125. }
  126. return 0;
  127. }
  128. AZStd::string CCmdLine::Next(char*& src)
  129. {
  130. char ch = 0;
  131. char* org = src;
  132. ch = *src++;
  133. while (ch)
  134. {
  135. switch (ch)
  136. {
  137. case '\'':
  138. case '\"':
  139. org = src;
  140. while ((*src++ != ch) && *src)
  141. {
  142. ;
  143. }
  144. return AZStd::string(org, src - 1);
  145. case '[':
  146. org = src;
  147. while ((*src++ != ']') && *src)
  148. {
  149. ;
  150. }
  151. return AZStd::string(org, src - 1);
  152. case ' ':
  153. ch = *src++;
  154. continue;
  155. default:
  156. org = src - 1;
  157. for (; *src != ' ' && *src != '\t' && *src; ++src)
  158. {
  159. ;
  160. }
  161. return AZStd::string(org, src);
  162. }
  163. }
  164. return AZStd::string();
  165. }