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