Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.5 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: loads additional command line options from a config file
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "KeyValues.h"
  8. #include "tier1/strtools.h"
  9. #include "FileSystem_Tools.h"
  10. #include "tier1/utlstring.h"
  11. // So we know whether or not we own argv's memory
  12. static bool sFoundConfigArgs = false;
  13. //-----------------------------------------------------------------------------
  14. // Purpose: Parses arguments and adds them to argv and argc
  15. //-----------------------------------------------------------------------------
  16. static void AddArguments( int &argc, char **&argv, const char *str )
  17. {
  18. char **args = 0;
  19. char *argList = 0;
  20. int argCt = argc;
  21. argList = new char[ Q_strlen( str ) + 1 ];
  22. Q_strcpy( argList, str );
  23. // Parse the arguments out of the string
  24. char *token = strtok( argList, " " );
  25. while( token )
  26. {
  27. ++argCt;
  28. token = strtok( NULL, " " );
  29. }
  30. // Make sure someting was actually found in the file
  31. if( argCt > argc )
  32. {
  33. sFoundConfigArgs = true;
  34. // Allocate a new array for argv
  35. args = new char*[ argCt ];
  36. // Copy original arguments, up to the last one
  37. int i;
  38. for( i = 0; i < argc - 1; ++i )
  39. {
  40. args[ i ] = new char[ Q_strlen( argv[ i ] ) + 1 ];
  41. Q_strcpy( args[ i ], argv[ i ] );
  42. }
  43. // copy new arguments
  44. Q_strcpy( argList, str );
  45. token = strtok( argList, " " );
  46. for( ; i < argCt - 1; ++i )
  47. {
  48. args[ i ] = new char[ Q_strlen( token ) + 1 ];
  49. Q_strcpy( args[ i ], token );
  50. token = strtok( NULL, " " );
  51. }
  52. // Copy the last original argument
  53. args[ i ] = new char[ Q_strlen( argv[ argc - 1 ] ) + 1 ];
  54. Q_strcpy( args[ i ], argv[ argc - 1 ] );
  55. argc = argCt;
  56. argv = args;
  57. }
  58. delete [] argList;
  59. }
  60. //-----------------------------------------------------------------------------
  61. // Purpose: Loads additional commandline arguments from a config file for an app.
  62. // Filesystem must be initialized before calling this function.
  63. // keyname: Name of the block containing the key/args pairs (ie map or model name)
  64. // appname: Keyname for the commandline arguments to be loaded - typically the exe name.
  65. //-----------------------------------------------------------------------------
  66. void LoadCmdLineFromFile( int &argc, char **&argv, const char *keyname, const char *appname )
  67. {
  68. sFoundConfigArgs = false;
  69. Assert( g_pFileSystem );
  70. if( !g_pFileSystem )
  71. return;
  72. // Load the cfg file, and find the keyname
  73. KeyValues *kv = new KeyValues( "CommandLine" );
  74. char filename[512];
  75. filename[0] = '\0';
  76. V_strcpy( filename, gamedir );
  77. V_StripTrailingSlash( filename );
  78. V_strncat( filename, "\\cfg\\commandline.cfg", sizeof( filename ) );
  79. if ( kv->LoadFromFile( g_pFileSystem, filename ) )
  80. {
  81. // Load the commandline arguments for this app
  82. KeyValues *appKey = kv->FindKey( keyname );
  83. if( appKey )
  84. {
  85. const char *str = appKey->GetString( appname );
  86. Msg( "Command Line found: %s\n", str );
  87. AddArguments( argc, argv, str );
  88. }
  89. }
  90. kv->deleteThis();
  91. }
  92. //-----------------------------------------------------------------------------
  93. // Purpose: Cleans up any memory allocated for the new argv. Pass in the app's
  94. // argc and argv - this is safe even if no extra arguments were loaded.
  95. //-----------------------------------------------------------------------------
  96. void DeleteCmdLine( int argc, char **argv )
  97. {
  98. if( !sFoundConfigArgs )
  99. return;
  100. for( int i = 0; i < argc; ++i )
  101. {
  102. delete [] argv[i];
  103. }
  104. delete [] argv;
  105. }