- Formatting fixes
[reactos.git] / rosapps / sysutils / regexpl / ArgumentParser.cpp
1 /* $Id$
2 *
3 * regexpl - Console Registry Explorer
4 *
5 * Copyright (C) 2000-2005 Nedko Arnaudov <nedko@users.sourceforge.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22
23 // ArgumentParser.cpp: implementation of the CArgumentParser class.
24 //
25 //////////////////////////////////////////////////////////////////////
26
27 #include "ph.h"
28 #include "ArgumentParser.h"
29
30 //////////////////////////////////////////////////////////////////////
31 // Construction/Destruction
32 //////////////////////////////////////////////////////////////////////
33
34 CArgumentParser::CArgumentParser()
35 {
36 m_pchArgumentList = NULL;
37 m_pchArgumentListEnd = NULL;
38 m_pchArgument = NULL;
39 }
40
41 CArgumentParser::~CArgumentParser()
42 {
43 }
44
45 void CArgumentParser::SetArgumentList(TCHAR *pchArguments)
46 {
47 TCHAR *pch = m_pchArgumentList = pchArguments;
48 m_pchArgumentListEnd = pchArguments + _tcslen(pchArguments);
49
50 BOOL blnLongArg = FALSE;
51 while (*pch)
52 {
53 switch(*pch)
54 {
55 case _T('^'): // argument parser ignores escape sequences
56 if (pch[1])
57 pch++;
58 break;
59 case _T('\"'):
60 blnLongArg = !blnLongArg;
61 break;
62 case _T(' '):
63 case _T('\t'):
64 case _T('\r'):
65 case _T('\n'):
66 if (!blnLongArg)
67 *pch = 0;
68 break;
69 }
70 pch++;
71 }
72
73 ResetArgumentIteration();
74 }
75
76 TCHAR * CArgumentParser::GetNextArgument()
77 {
78 ASSERT(m_pchArgumentList); // call SetArgumentList() before calling this function
79 ASSERT(m_pchArgumentListEnd); // call SetArgumentList() before calling this function
80 ASSERT(m_pchArgumentListEnd >= m_pchArgumentList);
81
82 // if this is begin of iteration
83 if (!m_pchArgument)
84 m_pchArgument = m_pchArgumentList;
85
86 while(m_pchArgument)
87 {
88 if (m_pchArgument > m_pchArgumentListEnd)
89 { // if end of arguments list reached
90 ASSERT(m_pchArgument - 1 == m_pchArgumentListEnd);
91 break;
92 }
93
94 TCHAR *pchArg = m_pchArgument;
95
96 // Next argument
97 m_pchArgument += _tcslen(m_pchArgument)+1;
98
99 if(*pchArg)
100 { // if argument is not an empty string
101 return pchArg;
102 }
103 }
104
105 return NULL;
106 }
107
108 void CArgumentParser::ResetArgumentIteration()
109 {
110 m_pchArgument = NULL;
111 }