OSDN Git Service

Update Gitignore file
[tortoisegit/TortoiseGitJp.git] / src / Utils / CmdLineParser.h
1 // TortoiseSVN - a Windows shell extension for easy version control\r
2 \r
3 // Copyright (C) 2003-2007 - Stefan Kueng\r
4 \r
5 // This program is free software; you can redistribute it and/or\r
6 // modify it under the terms of the GNU General Public License\r
7 // as published by the Free Software Foundation; either version 2\r
8 // of the License, or (at your option) any later version.\r
9 \r
10 // This program is distributed in the hope that it will be useful,\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 // GNU General Public License for more details.\r
14 \r
15 // You should have received a copy of the GNU General Public License\r
16 // along with this program; if not, write to the Free Software Foundation,\r
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\r
18 //\r
19 #pragma once\r
20 #include <map>\r
21 #include <string>\r
22 \r
23 using std::map;\r
24 \r
25 #pragma warning (push,1)\r
26 typedef std::wstring wide_string;\r
27 #ifndef stdstring\r
28 #ifdef UNICODE\r
29 #       define stdstring wide_string\r
30 #else\r
31 #       define stdstring std::string\r
32 #endif\r
33 #endif\r
34 #pragma warning (pop)\r
35 \r
36 /**\r
37  * \ingroup Utils\r
38  *\r
39  * A helper class for parsing command lines.\r
40  * It provides methods to extract 'key' and 'value'\r
41  * pairs of the form -keyname:value or /keyname:value.\r
42  * Parameter examples:\n\r
43  * \code\r
44  * "/key1 /key2:myvalue -key3:anothervalue -key4:"this could be a path with spaces"\r
45  * \endcode\r
46  * /key is the same as -key\n\r
47  * all keys and values are case-insensitive.\r
48  * Please note that both keys and values are strings although the class\r
49  * provides a method to get a long as a value.\r
50  * Example:\n\r
51  * \code\r
52  * CCmdLineParser parser(::GetCommandLine());\r
53  * if (parser.HasKey("r"))\r
54  * {\r
55  *      // the key -r or /r is there (could mean e.g. 'recurse')\r
56  * }\r
57  * //now assume the command line is /open:"c:\test.txt" /wait:30\r
58  * CString file = parser.GetVal("open");\r
59  * //file contains now c:\test.txt\r
60  * long number = parser.GetLongVal("seconds");\r
61  * //number has now the value 30\r
62  * \endcode\r
63  */\r
64 class CCmdLineParser \r
65 {\r
66 public:\r
67         typedef map<stdstring, stdstring> CValsMap;\r
68         typedef CValsMap::const_iterator ITERPOS;\r
69 public:\r
70         /**\r
71          * Creates a CCmdLineParser object and parses the parameters in.\r
72          * \param sCmdLine the command line\r
73          */\r
74         CCmdLineParser(LPCTSTR sCmdLine = NULL);\r
75         virtual ~CCmdLineParser();\r
76 \r
77         /**\r
78          * returns the command line string this object was created on.\r
79          * \return the command line\r
80          */\r
81         LPCTSTR getCmdLine() const { return m_sCmdLine.c_str(); }\r
82 \r
83         /**\r
84          * Starts an iteration over all command line parameters.\r
85          * \return the first position\r
86          */\r
87         ITERPOS begin() const;  \r
88 \r
89         /**\r
90          * Get the next key/value pair. If no more keys are available then\r
91          * an empty key is returned.\r
92          * \param the position from where to get. To get the first pair use the\r
93          * begin() method. \a pos is incremented by 1 on return.\r
94          * \param sKey returns the key\r
95          * \param sValue returns the value\r
96          * \return the next position\r
97          */\r
98         ITERPOS getNext(ITERPOS& pos, stdstring& sKey, stdstring& sValue) const;\r
99                 \r
100         /**\r
101          * Checks if the position is the last or if there are more key/value pairs in the command line.\r
102          * \param pos the position to check\r
103          * \return TRUE if no more key/value pairs are available\r
104          */\r
105         BOOL isLast(const ITERPOS& pos) const;\r
106 \r
107         /**\r
108          * Checks if the given key is in the command line.\r
109          * \param sKey the key to check for\r
110          * \return TRUE if the key exists, FALSE if the key is not in command line\r
111          */\r
112         BOOL HasKey(LPCTSTR sKey) const;\r
113 \r
114         /**\r
115          * Checks if a key also has a value or not.\r
116          * \param sKey the key to check for a value\r
117          * \return TRUE if the key has a value, FALSE if no value (or no key) was found\r
118          */\r
119         BOOL HasVal(LPCTSTR sKey) const;\r
120 \r
121         /**\r
122          * Reads the value for a key. If the key has no value then NULL is returned.\r
123          * \param sKey the key to get the value from\r
124          * \return the value string of the key\r
125          */\r
126         LPCTSTR GetVal(LPCTSTR sKey) const;\r
127         \r
128         /**\r
129          * Reads the value for a key as a long. If the value is a string which can't be\r
130          * converted to a number then 0 is returned.\r
131          * \param the key to get the value from\r
132          * \return the value converted to a long\r
133          */\r
134         LONG GetLongVal(LPCTSTR sKey) const;\r
135 \r
136 private:\r
137         BOOL Parse(LPCTSTR sCmdLine);\r
138         CValsMap::const_iterator findKey(LPCTSTR sKey) const;\r
139         const CValsMap& getVals() const { return m_valueMap; }\r
140 private:\r
141         stdstring       m_sCmdLine;\r
142         CValsMap        m_valueMap;\r
143 \r
144         static const TCHAR m_sDelims[];\r
145         static const TCHAR m_sValueSep[];\r
146         static const TCHAR m_sQuotes[];\r
147 };\r
148 \r