1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2006 - Stefan Kueng
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "CmdLineParser.h"
24 const TCHAR
CCmdLineParser::m_sDelims
[] = _T("-/");
25 const TCHAR
CCmdLineParser::m_sQuotes
[] = _T("\"");
26 const TCHAR
CCmdLineParser::m_sValueSep
[] = _T(" :"); // don't forget space!!
29 CCmdLineParser::CCmdLineParser(LPCTSTR sCmdLine
)
37 CCmdLineParser::~CCmdLineParser()
42 BOOL
CCmdLineParser::Parse(LPCTSTR sCmdLine
)
44 const stdstring sEmpty
= _T(""); //use this as a value if no actual value is given in commandline
51 m_sCmdLine
= sCmdLine
;
53 LPCTSTR sCurrent
= sCmdLine
;
57 //format is -Key:"arg"
59 if (_tcslen(sCurrent
) == 0)
60 break; // no more data, leave loop
62 LPCTSTR sArg
= _tcspbrk(sCurrent
, m_sDelims
);
64 break; // no (more) delimiters found
67 if(_tcslen(sArg
) == 0)
68 break; // ends with delim
70 LPCTSTR sVal
= _tcspbrk(sArg
, m_sValueSep
);
74 std::transform(Key
.begin(), Key
.end(), Key
.begin(), ::tolower
);
75 m_valueMap
.insert(CValsMap::value_type(Key
, sEmpty
));
78 else if (sVal
[0] == _T(' ') || _tcslen(sVal
) == 1 )
80 // cmdline ends with /Key: or a key with no value
81 stdstring
Key(sArg
, (int)(sVal
- sArg
));
84 std::transform(Key
.begin(), Key
.end(), Key
.begin(), ::tolower
);
85 m_valueMap
.insert(CValsMap::value_type(Key
, sEmpty
));
87 sCurrent
= _tcsinc(sVal
);
93 stdstring
Key(sArg
, (int)(sVal
- sArg
));
94 std::transform(Key
.begin(), Key
.end(), Key
.begin(), ::tolower
);
98 LPCTSTR sQuote
= _tcspbrk(sVal
, m_sQuotes
), sEndQuote(NULL
);
101 // string with quotes (defined in m_sQuotes, e.g. '")
102 sQuote
= _tcsinc(sVal
);
103 sEndQuote
= _tcspbrk(sQuote
, m_sQuotes
);
108 sEndQuote
= _tcschr(sQuote
, _T(' '));
111 if(sEndQuote
== NULL
)
113 // no end quotes or terminating space, take the rest of the string to its end
114 stdstring
csVal(sQuote
);
117 m_valueMap
.insert(CValsMap::value_type(Key
, csVal
));
126 stdstring
csVal(sQuote
, (int)(sEndQuote
- sQuote
));
127 m_valueMap
.insert(CValsMap::value_type(Key
, csVal
));
129 sCurrent
= _tcsinc(sEndQuote
);
135 return (nArgs
> 0); //TRUE if arguments were found
138 CCmdLineParser::CValsMap::const_iterator
CCmdLineParser::findKey(LPCTSTR sKey
) const
141 std::transform(s
.begin(), s
.end(), s
.begin(), ::tolower
);
142 return m_valueMap
.find(s
);
145 BOOL
CCmdLineParser::HasKey(LPCTSTR sKey
) const
147 CValsMap::const_iterator it
= findKey(sKey
);
148 if(it
== m_valueMap
.end())
154 BOOL
CCmdLineParser::HasVal(LPCTSTR sKey
) const
156 CValsMap::const_iterator it
= findKey(sKey
);
157 if(it
== m_valueMap
.end())
159 if(it
->second
.empty())
164 LPCTSTR
CCmdLineParser::GetVal(LPCTSTR sKey
) const
166 CValsMap::const_iterator it
= findKey(sKey
);
167 if (it
== m_valueMap
.end())
169 return it
->second
.c_str();
172 LONG
CCmdLineParser::GetLongVal(LPCTSTR sKey
) const
174 CValsMap::const_iterator it
= findKey(sKey
);
175 if (it
== m_valueMap
.end())
177 return _tstol(it
->second
.c_str());
181 CCmdLineParser::ITERPOS
CCmdLineParser::begin() const
183 return m_valueMap
.begin();
186 CCmdLineParser::ITERPOS
CCmdLineParser::getNext(ITERPOS
& pos
, stdstring
& sKey
, stdstring
& sValue
) const
188 if (m_valueMap
.end() == pos
)
196 sValue
= pos
->second
;
202 BOOL
CCmdLineParser::isLast(const ITERPOS
& pos
) const
204 return (pos
== m_valueMap
.end());