a580489d922bbada0a1170908e8afde285467cad
[reactos.git] / rosapps / sysutils / regexpl / ShellCommandDeleteKey.cpp
1 /* $Id: ShellCommandDeleteKey.cpp,v 1.3 2001/01/10 01:25:29 narnaoud Exp $
2 *
3 * regexpl - Console Registry Explorer
4 *
5 * Copyright (C) 2000 Nedko Arnaoudov <nedkohome@atia.com>
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 // ShellCommandDeleteKey.cpp: implementation of the CShellCommandDeleteKey class.
24 //
25 //////////////////////////////////////////////////////////////////////
26
27 #include "ph.h"
28 #include "ShellCommandDeleteKey.h"
29 #include "RegistryExplorer.h"
30
31 #define DK_CMD _T("DK")
32 #define DK_CMD_SHORT_DESC DK_CMD _T(" command is used to delete key(s).\n")
33
34 //////////////////////////////////////////////////////////////////////
35 // Construction/Destruction
36 //////////////////////////////////////////////////////////////////////
37
38 CShellCommandDeleteKey::CShellCommandDeleteKey(CRegistryTree& rTree):m_rTree(rTree)
39 {
40 }
41
42 CShellCommandDeleteKey::~CShellCommandDeleteKey()
43 {
44 }
45
46 BOOL CShellCommandDeleteKey::Match(const TCHAR *pchCommand)
47 {
48 return _tcsicmp(pchCommand,DK_CMD) == 0;
49 }
50
51 int CShellCommandDeleteKey::Execute(CConsole &rConsole, CArgumentParser& rArguments)
52 {
53 TCHAR *pchKey = NULL, *pchArg;
54
55 BOOL blnHelp = FALSE;
56 BOOL blnExitAfterHelp = FALSE;
57 BOOL blnRecursive = FALSE;
58
59 while((pchArg = rArguments.GetNextArgument()) != NULL)
60 {
61 if ((_tcsicmp(pchArg,_T("/?")) == 0)
62 ||(_tcsicmp(pchArg,_T("-?")) == 0))
63 {
64 blnHelp = TRUE;
65 }
66 else if ((_tcsicmp(pchArg,_T("/s")) == 0)
67 ||(_tcsicmp(pchArg,_T("-s")) == 0))
68 {
69 blnRecursive = TRUE;
70 }
71 else
72 {
73 if (pchKey)
74 {
75 rConsole.Write(_T("Wrong parameter : \""));
76 rConsole.Write(pchArg);
77 rConsole.Write(_T("\"\n\n"));
78 blnHelp = TRUE;
79 }
80 else
81 {
82 pchKey = pchArg;
83 }
84 }
85 }
86
87 if ((!blnHelp) && (!pchKey))
88 {
89 rConsole.Write(_T("Key name not specified !\n\n"));
90 blnExitAfterHelp = TRUE;
91 }
92
93 if (blnHelp)
94 {
95 rConsole.Write(GetHelpString());
96 if (blnExitAfterHelp)
97 return 0;
98 else
99 rConsole.Write(_T("\n"));
100 }
101
102 // search for last key name token
103 TCHAR *pch = pchKey;
104 while(*pch)
105 pch++;
106
107 if (pch > pchKey)
108 pch--;
109
110 while(*pch == _T('\\'))
111 *pch = 0;
112
113 while((pch > pchKey)&&(*pch != _T('\\')))
114 pch--;
115
116 ASSERT(pch >= pchKey);
117
118 const TCHAR *pszPath;
119 TCHAR *pszPattern = pch+1;
120 if (pch == pchKey)
121 {
122 pszPath = _T(".");
123 }
124 else
125 {
126 if (pch-1 == pchKey)
127 {
128 rConsole.Write(DK_CMD COMMAND_NA_ON_ROOT);
129 return 0;
130 }
131 else
132 {
133 *pch = 0;
134 pszPath = pchKey;
135 }
136 }
137
138 {
139 size_t s = _tcslen(pszPattern);
140 if (s && (pszPattern[0] == _T('\"'))&&(pszPattern[s-1] == _T('\"')))
141 {
142 pszPattern[s-1] = 0;
143 pszPattern++;
144 }
145 }
146
147 if (!m_rTree.DeleteSubkeys(pszPattern,pszPath,blnRecursive))
148 {
149 rConsole.Write(_T("Cannot delete key(s).\n"));
150 rConsole.Write(m_rTree.GetLastErrorDescription());
151 }
152 else
153 {
154 InvalidateCompletion();
155 }
156
157 return 0;
158 }
159
160 const TCHAR * CShellCommandDeleteKey::GetHelpString()
161 {
162 return DK_CMD_SHORT_DESC
163 _T("Syntax: ") DK_CMD _T(" [/s] [/?] [PATH]KEY_NAME\n\n")
164 _T(" PATH - optional path to key which subkey(s) will be deleted. Default is current key.")
165 _T(" KEY_NAME - name of key to be deleted. Wildcards can be used.")
166 _T(" /? - This help.\n\n")
167 _T(" /s - Delete key and all subkeys.\n");
168 }
169
170 const TCHAR * CShellCommandDeleteKey::GetHelpShortDescriptionString()
171 {
172 return DK_CMD_SHORT_DESC;
173 }