-
Notifications
You must be signed in to change notification settings - Fork 1
/
CmdLine.cpp
122 lines (100 loc) · 2.84 KB
/
CmdLine.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
#include "stdafx.h"
#include "CmdLine.h"
#define _IS_SPACE(ch) ( ch == ' ' || ch == '\t' )
#define _EAT_WHITE(p) while( * p == ' ' || * p == '\t' ) p++;
CCmdLine::CCmdLine(LPCTSTR lpszCmdLine)
{
TCHAR * argv[MAX_PARAM];
INT argc = SplitLine(lpszCmdLine, argv);
ParseParam(argc, argv);
FreeParam(argc, argv);
}
CCmdLine::CCmdLine(INT argc, TCHAR ** argv)
{
ParseParam(argc, argv);
}
BOOL CCmdLine::HasSwitch(LPCTSTR lpszSwitch, CString & szOption)
{
return m_mapSwitchs.Lookup(lpszSwitch, szOption);
}
INT CCmdLine::GetArgumentCount()
{
return m_arrArguments.GetSize();
}
CString CCmdLine::GetArgument(INT nIndex)
{
return m_arrArguments[nIndex];
}
CString CCmdLine::GetFullArgument()
{
INT nSize = m_arrArguments.GetSize(); if( nSize < 1 ) return "";
CString szFullArgument = m_arrArguments[0];
for(INT i = 1; i < nSize; i++)
szFullArgument += CString(" ") + m_arrArguments[i];
return szFullArgument;
}
INT CCmdLine::SplitLine(LPCTSTR lpszCmdLine, TCHAR ** argv)
{
INT i, j, argc = 0;
TCHAR * pSave, * pChar;
BOOL bQuoted = FALSE;
argv[argc] = new TCHAR [MAX_PATH];
strcpy(argv[argc], "__program_name");
argc++;
pChar = (TCHAR *)lpszCmdLine;
_EAT_WHITE(pChar);
while( * pChar ) {
pSave = pChar;
while( * pChar ) {
if( * pChar == '\\' && * (pChar+1) == '\"' && ! bQuoted ) pChar += 2;
else if( * pChar == '\"' ) { bQuoted = ! bQuoted; pChar++; }
else if( ! _IS_SPACE(* pChar) ) pChar++;
else if( _IS_SPACE(* pChar) && bQuoted ) pChar++;
else break;
}
argv[argc] = new TCHAR [pChar-pSave+1];
i = j = 0;
while( i < pChar - pSave ) {
if( pSave[i] == '\\' && pSave[i+1] == '\"' ) { argv[argc][j++] = pSave[i+1]; i += 2; }
else if( pSave[i] != '\"' ) argv[argc][j++] = pSave[i++];
else i++;
}
argv[argc][j] = '\0';
argc++;
_EAT_WHITE(pChar);
}
return argc;
}
INT CCmdLine::ParseParam(INT argc, TCHAR ** argv)
{
m_mapSwitchs.RemoveAll();
m_arrArguments.RemoveAll();
CString szSwitch, szOption, szArgument;
for(INT i = 1; i < argc; i++) {
if( IsSwitch(argv[i]) && ParseSwitch(argv[i], szSwitch, szOption) ) {
szSwitch.MakeUpper(); m_mapSwitchs.SetAt(szSwitch, szOption);
} else {
m_arrArguments.Add(argv[i]);
}
}
return argc;
}
void CCmdLine::FreeParam(INT argc, TCHAR ** argv)
{
for(INT i = 0; i < argc; i++) delete [] argv[i];
}
BOOL CCmdLine::IsSwitch(LPCTSTR lpszParam)
{
if( lpszParam == NULL ) return FALSE;
INT nLen = strlen(lpszParam); if( nLen < 2 ) return FALSE;
if( lpszParam[0] == '/' || lpszParam[0] == '-' ) return isalpha(lpszParam[1]);
else return FALSE;
}
BOOL CCmdLine::ParseSwitch(LPCTSTR lpszParam, CString & szSwitch, CString & szOption)
{
CString szParam(lpszParam); if( szParam.GetLength() < 2 ) return FALSE;
INT nPos = szParam.Find(':');
szSwitch = ( nPos > 0 ) ? szParam.Mid(1, nPos-1) : szParam.Mid(1);
szOption = ( nPos > 0 ) ? szParam.Mid(nPos+1) : "";
return TRUE;
}