Team Fortress 2 Source Code as on 22/4/2020
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.

119 lines
3.2 KiB

  1. //========= Copyright 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 = V_strdup( str );
  22. // Parse the arguments out of the string
  23. char *token = strtok( argList, " " );
  24. while( token )
  25. {
  26. ++argCt;
  27. token = strtok( NULL, " " );
  28. }
  29. // Make sure someting was actually found in the file
  30. if( argCt > argc )
  31. {
  32. sFoundConfigArgs = true;
  33. // Allocate a new array for argv
  34. args = new char*[ argCt ];
  35. // Copy original arguments, up to the last one
  36. int i;
  37. for( i = 0; i < argc - 1; ++i )
  38. {
  39. args[ i ] = V_strdup( argv[ i ] );
  40. }
  41. // copy new arguments
  42. Q_strcpy( argList, str );
  43. token = strtok( argList, " " );
  44. for( ; i < argCt - 1; ++i )
  45. {
  46. args[ i ] = V_strdup( token );
  47. token = strtok( NULL, " " );
  48. }
  49. // Copy the last original argument
  50. args[ i ] = V_strdup( argv[ argc - 1 ] );
  51. argc = argCt;
  52. argv = args;
  53. }
  54. delete [] argList;
  55. }
  56. //-----------------------------------------------------------------------------
  57. // Purpose: Loads additional commandline arguments from a config file for an app.
  58. // Filesystem must be initialized before calling this function.
  59. // keyname: Name of the block containing the key/args pairs (ie map or model name)
  60. // appname: Keyname for the commandline arguments to be loaded - typically the exe name.
  61. //-----------------------------------------------------------------------------
  62. void LoadCmdLineFromFile( int &argc, char **&argv, const char *keyname, const char *appname )
  63. {
  64. sFoundConfigArgs = false;
  65. assert( g_pFileSystem );
  66. if( !g_pFileSystem )
  67. return;
  68. // Load the cfg file, and find the keyname
  69. KeyValues *kv = new KeyValues( "CommandLine" );
  70. char filename[512];
  71. Q_snprintf( filename, sizeof( filename ), "%s/cfg/commandline.cfg", gamedir );
  72. if ( kv->LoadFromFile( g_pFileSystem, filename ) )
  73. {
  74. // Load the commandline arguments for this app
  75. KeyValues *appKey = kv->FindKey( keyname );
  76. if( appKey )
  77. {
  78. const char *str = appKey->GetString( appname );
  79. Msg( "Command Line found: %s\n", str );
  80. AddArguments( argc, argv, str );
  81. }
  82. }
  83. kv->deleteThis();
  84. }
  85. //-----------------------------------------------------------------------------
  86. // Purpose: Cleans up any memory allocated for the new argv. Pass in the app's
  87. // argc and argv - this is safe even if no extra arguments were loaded.
  88. //-----------------------------------------------------------------------------
  89. void DeleteCmdLine( int argc, char **argv )
  90. {
  91. if( !sFoundConfigArgs )
  92. return;
  93. for( int i = 0; i < argc; ++i )
  94. {
  95. delete [] argv[i];
  96. }
  97. delete [] argv;
  98. }