ARGS.CPP 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. //======================================================================================
  19. //
  20. // @@@@@ @@ @ @@@@ @@@@ @@@@ @@ @ @@@@ @@@@ @@@@@ @@@@@ @@@@ @@ @
  21. // @@ @ @@ @ @@ @ @@ @ @@ @ @@@ @ @@ @ @@ @ @@ @ @@ @ @@ @ @@ @
  22. // @@@@@ @@ @ @@ @@ @@@@@@ @@ @ @ @@@@ @@@@ @@@@@ @@@@@ @@@@@@ @@
  23. // @@ @ @@ @ @@ @ @@ @ @@ @ @@ @@ @@ @ @@ @ @@ @ @@ @ @@ @ @@
  24. // @@@@@ @@@@ @@@@ @@@@ @@ @ @@ @ @@@@ @@@@ @@ @ @@@@@ @@ @ @@
  25. //
  26. // Copyright (c) 1998, 1999 Westwood Studios -- CONFIDENTIAL
  27. //
  28. // ArgC_ArgV.cpp
  29. //
  30. //======================================================================================
  31. //-----------------------------------------------------------------------------
  32. // INCLUDES
  33. //-----------------------------------------------------------------------------
  34. #define STRICT
  35. #include <windows.h>
  36. #include <windowsx.h>
  37. #include <assert.h>
  38. #include <stdio.h>
  39. #include <string.h>
  40. #include "args.h"
  41. //-----------------------------------------------------------------------------
  42. // GLOBALS
  43. //-----------------------------------------------------------------------------
  44. Command_Line_Arguments *Args = NULL;
  45. //*****************************************************************************
  46. // COMMAND_LINE_ARGUMENTS::COMMAND_LINE_ARGUMENTS -- Constructor.
  47. //
  48. // INPUT: HINSTANCE hInstance
  49. // LPSTR lpszCmdLine
  50. //
  51. // OUTPUT: none.
  52. //
  53. // WARNINGS: none.
  54. //
  55. // HISTORY:
  56. // 09/01/1997 ML/MG : Created.
  57. //=============================================================================
  58. Command_Line_Arguments::Command_Line_Arguments (
  59. HINSTANCE current_instance_handle,
  60. LPTSTR windows_command_line_string )
  61. {
  62. //--------------------------------------------------------------------------
  63. // debug checks...
  64. //--------------------------------------------------------------------------
  65. assert( windows_command_line_string != NULL );
  66. //--------------------------------------------------------------------------
  67. // reset all class data
  68. //--------------------------------------------------------------------------
  69. memset( ArgV, 0, sizeof( ArgV ) );
  70. //--------------------------------------------------------------------------
  71. // Store the program name in ArgV[ 0 ].
  72. //--------------------------------------------------------------------------
  73. GetModuleFileName( current_instance_handle, ArgV[ 0 ], MAX_ARGUMENT_LENGTH );
  74. const char * ptr = windows_command_line_string;
  75. bool potentially_forever = true;
  76. ArgC = 1;
  77. while( potentially_forever ) {
  78. //-----------------------------------------------------------------------
  79. // Find the next non-whitespace character in the string.
  80. //-----------------------------------------------------------------------
  81. while ( *ptr == ' ' ) {
  82. ++ ptr;
  83. }
  84. //-----------------------------------------------------------------------
  85. // If we're at the end of the string, quit.
  86. //-----------------------------------------------------------------------
  87. if ( *ptr == '\0' ) {
  88. break;
  89. }
  90. //-----------------------------------------------------------------------
  91. // Store a command-line argument.
  92. //-----------------------------------------------------------------------
  93. int i = 0;
  94. if ( *ptr == '"' ) {
  95. //--------------------------------------------------------------------
  96. // Skip the opening quotation mark.
  97. //--------------------------------------------------------------------
  98. ++ ptr;
  99. //--------------------------------------------------------------------
  100. // Collect characters until another quotation mark is encountered.
  101. //--------------------------------------------------------------------
  102. while ( *ptr != '\0' && *ptr != '"' ) {
  103. if ( i < MAX_ARGUMENT_LENGTH - 1 ) {
  104. ArgV [ ArgC ][ i ] = *ptr;
  105. ++ i;
  106. }
  107. ++ ptr;
  108. }
  109. //--------------------------------------------------------------------
  110. // Skip the closing quotation mark.
  111. //--------------------------------------------------------------------
  112. if ( *ptr == '"' ) {
  113. ++ ptr;
  114. }
  115. } else {
  116. //--------------------------------------------------------------------
  117. // Collect characters until a whitespace character is encountered.
  118. //--------------------------------------------------------------------
  119. while ( *ptr != '\0' && *ptr != ' ' ) {
  120. if ( i < MAX_ARGUMENT_LENGTH - 1 ) {
  121. ArgV [ ArgC ][ i ] = *ptr;
  122. ++ i;
  123. }
  124. ++ ptr;
  125. }
  126. }
  127. ArgV [ ArgC ][ i ] = '\0';
  128. ++ ArgC;
  129. }
  130. }
  131. //*****************************************************************************
  132. // COMMAND_LINE_ARGUMENTS::COMMAND_LINE_ARGUMENTS -- Constructor.
  133. //
  134. // INPUT: HINSTANCE hInstance
  135. //
  136. // OUTPUT: none.
  137. //
  138. // WARNINGS: none.
  139. //
  140. // HISTORY:
  141. // 09/01/1997 ML/MG : Created.
  142. //=============================================================================
  143. Command_Line_Arguments::Command_Line_Arguments ( HINSTANCE current_instance_handle )
  144. {
  145. char * windows_command_line_string = GetCommandLine();
  146. //--------------------------------------------------------------------------
  147. // debug checks...
  148. //--------------------------------------------------------------------------
  149. assert( windows_command_line_string != NULL );
  150. //--------------------------------------------------------------------------
  151. // reset all class data
  152. //--------------------------------------------------------------------------
  153. memset( ArgV, 0, sizeof( ArgV ) );
  154. const char * ptr = windows_command_line_string;
  155. bool potentially_forever = true;
  156. ArgC = 1;
  157. while( potentially_forever ) {
  158. //-----------------------------------------------------------------------
  159. // Find the next non-whitespace character in the string.
  160. //-----------------------------------------------------------------------
  161. while ( *ptr == ' ' ) {
  162. ++ ptr;
  163. }
  164. //-----------------------------------------------------------------------
  165. // If we're at the end of the string, quit.
  166. //-----------------------------------------------------------------------
  167. if ( *ptr == '\0' ) {
  168. break;
  169. }
  170. //-----------------------------------------------------------------------
  171. // Store a command-line argument.
  172. //-----------------------------------------------------------------------
  173. int i = 0;
  174. if ( *ptr == '"' ) {
  175. //--------------------------------------------------------------------
  176. // Skip the opening quotation mark.
  177. //--------------------------------------------------------------------
  178. ++ ptr;
  179. //--------------------------------------------------------------------
  180. // Collect characters until another quotation mark is encountered.
  181. //--------------------------------------------------------------------
  182. while ( *ptr != '\0' && *ptr != '"' ) {
  183. if ( i < MAX_ARGUMENT_LENGTH - 1 ) {
  184. ArgV [ ArgC ][ i ] = *ptr;
  185. ++ i;
  186. }
  187. ++ ptr;
  188. }
  189. //--------------------------------------------------------------------
  190. // Skip the closing quotation mark.
  191. //--------------------------------------------------------------------
  192. if ( *ptr == '"' ) {
  193. ++ ptr;
  194. }
  195. } else {
  196. //--------------------------------------------------------------------
  197. // Collect characters until a whitespace character is encountered.
  198. //--------------------------------------------------------------------
  199. while ( *ptr != '\0' && *ptr != ' ' ) {
  200. if ( i < MAX_ARGUMENT_LENGTH - 1 ) {
  201. ArgV [ ArgC ][ i ] = *ptr;
  202. ++ i;
  203. }
  204. ++ ptr;
  205. }
  206. }
  207. ArgV [ ArgC ][ i ] = '\0';
  208. ++ ArgC;
  209. }
  210. }
  211. //*****************************************************************************
  212. // COMMAND_LINE_ARGUMENTS::~COMMAND_LINE_ARGUMENTS -- Destructor.
  213. //
  214. // INPUT: HINSTANCE hInstance
  215. // LPSTR lpszCmdLine
  216. //
  217. // OUTPUT: none.
  218. //
  219. // WARNINGS: none.
  220. //
  221. // HISTORY:
  222. // 09/01/1997 ML/MG : Created.
  223. //=============================================================================
  224. Command_Line_Arguments::~Command_Line_Arguments ( void )
  225. {
  226. //--------------------------------------------------------------------------
  227. // reset all data...
  228. //--------------------------------------------------------------------------
  229. ArgC = -1;
  230. memset( ArgV, 0, sizeof( ArgV ) );
  231. }
  232. //*****************************************************************************
  233. // COMMAND_LINE_ARGUMENTS::GET_ARGC -- Return ArgC.
  234. //
  235. // INPUT: none.
  236. //
  237. // OUTPUT: int ArgC.
  238. //
  239. // WARNINGS: none.
  240. //
  241. // HISTORY:
  242. // 09/01/1997 ML/MG : Created.
  243. //=============================================================================
  244. int Command_Line_Arguments::Get_argc ( void )
  245. {
  246. //--------------------------------------------------------------------------
  247. // debug checks - make sure we at least have the application name
  248. //--------------------------------------------------------------------------
  249. assert( ArgC >= 1 );
  250. //--------------------------------------------------------------------------
  251. // return how many string parameters there are in the "argv" list
  252. //--------------------------------------------------------------------------
  253. return( ArgC );
  254. }
  255. //*****************************************************************************
  256. // COMMAND_LINE_ARGUMENTS::GET_ARGV -- Return ArgV.
  257. //
  258. // INPUT: none.
  259. //
  260. // OUTPUT: int ArgV.
  261. //
  262. // WARNINGS: none.
  263. //
  264. // HISTORY:
  265. // 09/01/1997 ML/MG : Created.
  266. //=============================================================================
  267. const char *Command_Line_Arguments::Get_argv ( int argument_index )
  268. {
  269. //--------------------------------------------------------------------------
  270. // debug checks - make sure we at least have the application name
  271. //--------------------------------------------------------------------------
  272. assert( argument_index >= 0 );
  273. assert( argument_index < MAX_COMMAND_LINE_ARGUMENTS );
  274. assert( argument_index < ArgC );
  275. assert( ArgC >= 1 );
  276. //--------------------------------------------------------------------------
  277. // return
  278. //--------------------------------------------------------------------------
  279. return( ArgV[ argument_index ] );
  280. }
  281. void Command_Line_Arguments::Set_argv( int argument_index, char *arg )
  282. {
  283. if( arg == NULL || *arg == '\0' ) {
  284. return;
  285. }
  286. //--------------------------------------------------------------------------
  287. // debug checks - make sure we at least have the application name
  288. //--------------------------------------------------------------------------
  289. assert( argument_index >= 0 );
  290. assert( argument_index < MAX_COMMAND_LINE_ARGUMENTS );
  291. assert( argument_index < ArgC );
  292. assert( ArgC >= 1 );
  293. if (( argument_index >= 0 ) &&
  294. ( argument_index < MAX_COMMAND_LINE_ARGUMENTS ) &&
  295. ( argument_index < ArgC )) {
  296. strcpy( ArgV[ argument_index ], arg );
  297. }
  298. }