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