OpenBarnyard
 
Loading...
Searching...
No Matches
T2CommandLine.cpp
Go to the documentation of this file.
1#include "ToshiPCH.h"
2#include "T2CommandLine.h"
3
4//-----------------------------------------------------------------------------
5// Enables memory debugging.
6// Note: Should be the last include!
7//-----------------------------------------------------------------------------
9
11
13{
14 Create( a_pchCommandLine );
15}
16
18{
19 m_szCommandLine[ 0 ] = '\0';
20 m_bInitialised = TFALSE;
21}
22
23void T2CommandLine::Create( T2StringView a_pchCommandLine )
24{
25 if ( a_pchCommandLine )
26 {
27 TSIZE uiCommandLineLength = a_pchCommandLine.Length();
28 m_szCommandLine.Copy( a_pchCommandLine );
29 m_bInitialised = TTRUE;
30
31 m_Parser.SetBuffer( a_pchCommandLine, uiCommandLineLength );
32
33 // Parse the parameters
34 m_ParsedParams.Clear();
35
36 char szToken[ 1024 ];
37 while ( !m_Parser.IsOver() )
38 {
39 TBOOL bReadToken = m_Parser.ReadToken( szToken, sizeof( szToken ) );
40
41 if ( bReadToken )
42 m_ParsedParams.Push( szToken );
43 }
44 }
45}
46
47TBOOL T2CommandLine::HasParameter( const TCHAR* a_pszParameter, TINT* a_pIndex /*= TNULL */ ) const
48{
49 TASSERT( m_bInitialised );
50
51 if ( a_pIndex )
52 *a_pIndex = -1;
53
54 TARRAY_FOREACH( m_ParsedParams, it )
55 {
56 if ( it.Get() == a_pszParameter )
57 {
58 if ( a_pIndex )
59 *a_pIndex = it.Index();
60
61 return TTRUE;
62 }
63 }
64
65 return TFALSE;
66}
67
68TString8 T2CommandLine::GetParameterValue( const TCHAR* a_pszParameter, const TCHAR* a_pszDefaultValue ) const
69{
70 TASSERT( m_bInitialised );
71
72 const TCHAR* pszValue = GetNextParameter( a_pszParameter );
73
74 if ( pszValue )
75 return pszValue;
76
77 return a_pszDefaultValue;
78}
79
80const TCHAR* T2CommandLine::GetNextParameter( const TCHAR* a_pszParameter ) const
81{
82 TINT iParamIndex;
83 TBOOL bHasParam = HasParameter( a_pszParameter, &iParamIndex );
84
85 if ( bHasParam && iParamIndex + 1 < m_ParsedParams.Size() )
86 {
87 return m_ParsedParams[ iParamIndex + 1 ];
88 }
89
90 return TNULL;
91}
92
#define TASSERT(X,...)
Definition Defines.h:138
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
T2StringViewBase< T2StringTraits< TCHAR > > T2StringView
Definition T2String.h:77
#define TARRAY_FOREACH(vecName, iteratorName)
Definition TArray.h:3
size_t TSIZE
Definition Typedefs.h:9
char TCHAR
Definition Typedefs.h:20
#define TNULL
Definition Typedefs.h:23
int TINT
Definition Typedefs.h:7
#define TFALSE
Definition Typedefs.h:24
#define TTRUE
Definition Typedefs.h:25
bool TBOOL
Definition Typedefs.h:6
Command line parsing and processing for Toshi tools.
TFORCEINLINE TSIZE Length() const
Definition T2String.h:36
const TCHAR * GetNextParameter(const TCHAR *a_pszParameter) const
void Create(T2StringView a_pchCommandLine)
TBOOL HasParameter(const TCHAR *a_pszParameter, TINT *a_pIndex=nullptr) const
TString8 GetParameterValue(const TCHAR *a_pszParameter, const TCHAR *a_pszDefaultValue=nullptr) const