argv.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. ** Command & Conquer Renegade(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. /* $Header: /VSS_Sync/wwlib/argv.h 7 8/29/01 10:25p Vss_sync $ */
  19. /***********************************************************************************************
  20. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Library *
  24. * *
  25. * $Archive:: /VSS_Sync/wwlib/argv.h $*
  26. * *
  27. * $Author:: Vss_sync $*
  28. * *
  29. * $Modtime:: 8/29/01 10:24p $*
  30. * *
  31. * $Revision:: 7 $*
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  36. #if defined(_MSC_VER)
  37. #pragma once
  38. #endif
  39. #ifndef ARGV_H
  40. #define ARGV_H
  41. #ifndef ALWAYS_H
  42. #include "always.h"
  43. #endif
  44. #ifdef _UNIX
  45. #include "osdep.h"
  46. #endif
  47. // Used to parse command line that is passed into WinMain.
  48. // It also has the ability to load a file with values to append to the command line.
  49. // Normally in WinMain() there would be a call Argv::Init(lpCmdLine, fileprefix).
  50. // Once this is done, user can create an ArgvClass object (say argv) then argv.Find() can be called.
  51. // If there is a arguement <fileprefix><fname> (for example @file.arg) then the fname is loaded up,
  52. // parsed, and put into the command line. The format of the parameter file is as follows:
  53. // 1. a semicolon (;) at the start of the line is a comment and will be ignored.
  54. // 2. Each line is a seperate parameter. This enables white space to be embeded.
  55. // In typical Argv implementation, the first argument is the name of the application. This
  56. // is not the case with this.
  57. class ArgvClass
  58. {
  59. public:
  60. // As passed into WinMain.
  61. // Should be called before any objects are created.
  62. // This can be called multible times.
  63. static int Init(char *lpCmdLine, char *fileprefix = "@");
  64. static bool Load_File(const char *fname);
  65. static void Free();
  66. // Create an object that can search the args.
  67. ArgvClass(bool case_sensitive = false, bool exact_size = false);
  68. ~ArgvClass() {}
  69. // Functions to find a value.
  70. const char *Find(const char *arg) {
  71. CurrentPos = -1;
  72. return(Find_Again(arg));
  73. }
  74. // If NULL passed, original string will be used.
  75. const char *Find_Again(const char *arg = 0L);
  76. // Return pointer to data after 'arg'.
  77. // White space is skipped.
  78. // So give a line '-Pdata' or '-P data' and arg==-P, 'data' would be returned in
  79. // both cases.
  80. const char *Find_Value(const char *arg);
  81. // Similar to Find_Value, only gets value of current arg. User needs to pass
  82. // in the strlen of the prefix (2 for -P example above) to skip.
  83. // Val_in_next is set if the value was extracted from the next Argv,
  84. // this way the programmer can know if he needs to call an extra Next().
  85. const char *Get_Cur_Value(unsigned prefixlen, bool * val_in_next = 0);
  86. // Update an existing attrib to use this new value
  87. void Update_Value(const char *attrib, const char *value);
  88. // Add a new attrib value pair (or just an option)
  89. void Add_Value(const char *attrib, const char *value=NULL);
  90. // Remove an option (and its value)
  91. bool Remove_Value(const char *attrib);
  92. // First parameter.
  93. const char *First() {
  94. CurrentPos = 0;
  95. return(Argv[0]);
  96. }
  97. // Next works after a Find() call also.
  98. const char *Next() {
  99. CurrentPos++;
  100. if (CurrentPos < Argc) {
  101. return(Argv[CurrentPos]);
  102. }
  103. return(0L);
  104. }
  105. // Can be called so Next() will return First()...
  106. void Reset() {
  107. CurrentPos = -1;
  108. }
  109. const char *Cur() {
  110. if (CurrentPos < Argc) {
  111. return(Argv[CurrentPos]);
  112. }
  113. return(0L);
  114. }
  115. // Allow user to change states.
  116. void Case_Sensitive(bool on) {Flag.CaseSensitive = on;}
  117. bool Is_Case_Sensitive() {return (Flag.CaseSensitive);}
  118. // Allow user to change states.
  119. void Exact_Size(bool on) {Flag.ExactSize = on;}
  120. bool Is_Exact_Size() {return (Flag.ExactSize);}
  121. protected:
  122. // Current position to be used when Next or Find_Again are called.
  123. int CurrentPos;
  124. // Last arg that we are searching for.
  125. const char *LastArg;
  126. union {
  127. unsigned Flags;
  128. struct {
  129. unsigned CaseSensitive:1;
  130. unsigned ExactSize:1;
  131. } Flag;
  132. };
  133. // Number of args.
  134. static int Argc;
  135. // The actual data.
  136. enum {MAX_ARGC = 256};
  137. static char *Argv[MAX_ARGC];
  138. };
  139. #endif