1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
5 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
6 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | * http://www.mozilla.org/MPL/
|
---|
9 | *
|
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
12 | * for the specific language governing rights and limitations under the
|
---|
13 | * License.
|
---|
14 | *
|
---|
15 | * The Original Code is mozilla.org code.
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is
|
---|
18 | * Netscape Communications Corporation.
|
---|
19 | * Portions created by the Initial Developer are Copyright (C) 1998
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either of the GNU General Public License Version 2 or later (the "GPL"),
|
---|
26 | * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | #include <stdio.h>
|
---|
39 |
|
---|
40 | #include "nsIServiceManager.h"
|
---|
41 | #include "nsIComponentManager.h"
|
---|
42 | #include "nsCOMPtr.h"
|
---|
43 | #include "nsIRegistry.h"
|
---|
44 | #include "nsIEnumerator.h"
|
---|
45 | #include "nsILocalFile.h"
|
---|
46 | #include "nsDependentString.h"
|
---|
47 | #include "prmem.h"
|
---|
48 | #include "plstr.h"
|
---|
49 | #include "nsMemory.h"
|
---|
50 |
|
---|
51 | static void display( nsIRegistry *reg, nsRegistryKey root, const char *name );
|
---|
52 | static void displayValues( nsIRegistry *reg, nsRegistryKey root );
|
---|
53 | static void printString( const char *value, int indent );
|
---|
54 |
|
---|
55 | int main( int argc, char *argv[] ) {
|
---|
56 |
|
---|
57 |
|
---|
58 | #ifdef __MWERKS__
|
---|
59 | // Hack in some arguments. A NULL registry name is supposed to tell libreg
|
---|
60 | // to use the default registry (which does seem to work).
|
---|
61 | argc = 1;
|
---|
62 | const char* myArgs[] =
|
---|
63 | {
|
---|
64 | "regExport"
|
---|
65 | };
|
---|
66 | argv = const_cast<char**>(myArgs);
|
---|
67 | #endif
|
---|
68 |
|
---|
69 | nsresult rv;
|
---|
70 |
|
---|
71 | // Initialize XPCOM
|
---|
72 | nsIServiceManager *servMgr = NULL;
|
---|
73 | rv = NS_InitXPCOM2(&servMgr, NULL, NULL);
|
---|
74 | if (NS_FAILED(rv))
|
---|
75 | {
|
---|
76 | // Cannot initialize XPCOM
|
---|
77 | printf("Cannot initialize XPCOM. Exit. [rv=0x%08X]\n", (int)rv);
|
---|
78 | exit(-1);
|
---|
79 | }
|
---|
80 | {
|
---|
81 | // Get the component manager
|
---|
82 | static NS_DEFINE_CID(kComponentManagerCID, NS_COMPONENTMANAGER_CID);
|
---|
83 | nsCOMPtr<nsIComponentManager> compMgr = do_GetService(kComponentManagerCID, &rv);
|
---|
84 | if (NS_FAILED(rv))
|
---|
85 | {
|
---|
86 | // Cant get component manager
|
---|
87 | printf("Cannot get component manager from service manager.. Exit. [rv=0x%08X]\n", (int)rv);
|
---|
88 | exit(-1);
|
---|
89 | }
|
---|
90 |
|
---|
91 | nsIRegistry *reg;
|
---|
92 |
|
---|
93 | if (argc>1) {
|
---|
94 | // Create the registry
|
---|
95 | rv = compMgr->CreateInstanceByContractID(NS_REGISTRY_CONTRACTID, NULL,
|
---|
96 | NS_GET_IID(nsIRegistry),
|
---|
97 | (void **) ®);
|
---|
98 | // Check result.
|
---|
99 | if ( NS_FAILED(rv) )
|
---|
100 | {
|
---|
101 | printf( "Error opening registry file %s, rv=0x%08X\n", argv[1] , (int)rv );
|
---|
102 | return rv;
|
---|
103 | }
|
---|
104 | // Open it against the input file name.
|
---|
105 | nsCOMPtr<nsILocalFile> regFile;
|
---|
106 | rv = NS_NewNativeLocalFile( nsDependentCString(argv[1]), PR_FALSE, getter_AddRefs(regFile) );
|
---|
107 | if ( NS_FAILED(rv) ) {
|
---|
108 | printf( "Error instantiating local file for %s, rv=0x%08X\n", argv[1], (int)rv );
|
---|
109 | return rv;
|
---|
110 | }
|
---|
111 |
|
---|
112 | rv = reg->Open( regFile );
|
---|
113 |
|
---|
114 | if ( rv == NS_OK )
|
---|
115 | {
|
---|
116 | printf( "Registry %s opened OK.\n", argv[1] );
|
---|
117 |
|
---|
118 | // Recurse over all 3 branches.
|
---|
119 | display( reg, nsIRegistry::Common, "nsRegistry::Common" );
|
---|
120 | display( reg, nsIRegistry::Users, "nsRegistry::Users" );
|
---|
121 | }
|
---|
122 | NS_RELEASE(reg);
|
---|
123 | }
|
---|
124 | else
|
---|
125 | {
|
---|
126 | // Called with no arguments. Print both the default registry and
|
---|
127 | // the components registry. We already printed the default regsitry.
|
---|
128 | // So just do the component registry.
|
---|
129 | rv = compMgr->CreateInstanceByContractID(NS_REGISTRY_CONTRACTID, NULL,
|
---|
130 | NS_GET_IID(nsIRegistry),
|
---|
131 | (void **) ®);
|
---|
132 |
|
---|
133 | // Check result.
|
---|
134 | if ( NS_FAILED(rv) )
|
---|
135 | {
|
---|
136 | printf( "Error opening creating registry instance, rv=0x%08X\n", (int)rv );
|
---|
137 | return rv;
|
---|
138 | }
|
---|
139 | rv = reg->OpenWellKnownRegistry(nsIRegistry::ApplicationComponentRegistry);
|
---|
140 | if ( rv == NS_ERROR_REG_BADTYPE ) {
|
---|
141 | printf( "\n\n\nThere is no <Application Component Registry>\n" );
|
---|
142 | }
|
---|
143 | else if ( rv == NS_OK ) {
|
---|
144 |
|
---|
145 | printf( "\n\n\nRegistry %s opened OK.\n", "<Application Component Registry>\n" );
|
---|
146 |
|
---|
147 | // Recurse over all 3 branches.
|
---|
148 | display( reg, nsIRegistry::Common, "nsRegistry::Common" );
|
---|
149 | display( reg, nsIRegistry::Users, "nsRegistry::Users" );
|
---|
150 | }
|
---|
151 | NS_RELEASE(reg);
|
---|
152 | }
|
---|
153 | }
|
---|
154 | NS_ShutdownXPCOM( servMgr );
|
---|
155 |
|
---|
156 | return rv;
|
---|
157 | }
|
---|
158 |
|
---|
159 | void display( nsIRegistry *reg, nsRegistryKey root, const char *rootName ) {
|
---|
160 | // Print out key name.
|
---|
161 | printf( "%s\n", rootName );
|
---|
162 |
|
---|
163 | // Make sure it isn't a "root" key.
|
---|
164 | if ( root != nsIRegistry::Common
|
---|
165 | &&
|
---|
166 | root != nsIRegistry::Users
|
---|
167 | &&
|
---|
168 | root != nsIRegistry::CurrentUser ) {
|
---|
169 | // Print values stored under this key.
|
---|
170 | displayValues( reg, root );
|
---|
171 | }
|
---|
172 |
|
---|
173 | // Enumerate all subkeys (immediately) under the given node.
|
---|
174 | nsIEnumerator *keys;
|
---|
175 | nsresult rv = reg->EnumerateSubtrees( root, &keys );
|
---|
176 |
|
---|
177 | // Check result.
|
---|
178 | if ( rv == NS_OK ) {
|
---|
179 | // Set enumerator to beginning.
|
---|
180 | rv = keys->First();
|
---|
181 | // Enumerate subkeys till done.
|
---|
182 | while( NS_SUCCEEDED( rv ) && (NS_OK != keys->IsDone()) ) {
|
---|
183 | nsISupports *base;
|
---|
184 | rv = keys->CurrentItem( &base );
|
---|
185 | // Test result.
|
---|
186 | if ( rv == NS_OK ) {
|
---|
187 | // Get specific interface.
|
---|
188 | nsIRegistryNode *node;
|
---|
189 | nsIID nodeIID = NS_IREGISTRYNODE_IID;
|
---|
190 | rv = base->QueryInterface( nodeIID, (void**)&node );
|
---|
191 | // Test that result.
|
---|
192 | if ( rv == NS_OK ) {
|
---|
193 | // Get node name.
|
---|
194 | char *name;
|
---|
195 | rv = node->GetNameUTF8( &name );
|
---|
196 | // Test result.
|
---|
197 | if ( rv == NS_OK ) {
|
---|
198 | // Build complete name.
|
---|
199 | char *fullName = new char[ PL_strlen(rootName) + PL_strlen(name) + 5 ];
|
---|
200 | PL_strcpy( fullName, rootName );
|
---|
201 | PL_strcat( fullName, " - " );
|
---|
202 | PL_strcat( fullName, name );
|
---|
203 | // Display contents under this subkey.
|
---|
204 | nsRegistryKey key;
|
---|
205 | rv = reg->GetSubtreeRaw( root, name, &key );
|
---|
206 | if ( rv == NS_OK ) {
|
---|
207 | display( reg, key, fullName );
|
---|
208 | printf( "\n" );
|
---|
209 | } else {
|
---|
210 | printf( "Error getting key, rv=0x%08X\n", (int)rv );
|
---|
211 | }
|
---|
212 | delete [] fullName;
|
---|
213 | } else {
|
---|
214 | printf( "Error getting subtree name, rv=0x%08X\n", (int)rv );
|
---|
215 | }
|
---|
216 | // Release node.
|
---|
217 | node->Release();
|
---|
218 | } else {
|
---|
219 | printf( "Error converting base node ptr to nsIRegistryNode, rv=0x%08X\n", (int)rv );
|
---|
220 | }
|
---|
221 | // Release item.
|
---|
222 | base->Release();
|
---|
223 |
|
---|
224 | // Advance to next key.
|
---|
225 | rv = keys->Next();
|
---|
226 | // Check result.
|
---|
227 | if ( NS_SUCCEEDED( rv ) ) {
|
---|
228 | } else {
|
---|
229 | printf( "Error advancing enumerator, rv=0x%08X\n", (int)rv );
|
---|
230 | }
|
---|
231 | } else {
|
---|
232 | printf( "Error getting current item, rv=0x%08X\n", (int)rv );
|
---|
233 | }
|
---|
234 | }
|
---|
235 | // Release key enumerator.
|
---|
236 | keys->Release();
|
---|
237 | } else {
|
---|
238 | printf( "Error creating enumerator for %s, root=0x%08X, rv=0x%08X\n",
|
---|
239 | rootName, (int)root, (int)rv );
|
---|
240 | }
|
---|
241 | return;
|
---|
242 | }
|
---|
243 |
|
---|
244 | static void displayValues( nsIRegistry *reg, nsRegistryKey root ) {
|
---|
245 | // Emumerate values at this registry location.
|
---|
246 | nsIEnumerator *values;
|
---|
247 | nsresult rv = reg->EnumerateValues( root, &values );
|
---|
248 |
|
---|
249 | // Check result.
|
---|
250 | if ( rv == NS_OK ) {
|
---|
251 | // Go to beginning.
|
---|
252 | rv = values->First();
|
---|
253 |
|
---|
254 | // Enumerate values till done.
|
---|
255 | while( rv == NS_OK && (NS_OK != values->IsDone()) ) {
|
---|
256 | nsISupports *base;
|
---|
257 | rv = values->CurrentItem( &base );
|
---|
258 | // Test result.
|
---|
259 | if ( rv == NS_OK ) {
|
---|
260 | // Get specific interface.
|
---|
261 | nsIRegistryValue *value;
|
---|
262 | nsIID valueIID = NS_IREGISTRYVALUE_IID;
|
---|
263 | rv = base->QueryInterface( valueIID, (void**)&value );
|
---|
264 | // Test that result.
|
---|
265 | if ( rv == NS_OK ) {
|
---|
266 | // Get node name.
|
---|
267 | char *name;
|
---|
268 | rv = value->GetNameUTF8( &name );
|
---|
269 | // Test result.
|
---|
270 | if ( rv == NS_OK ) {
|
---|
271 | // Print name:
|
---|
272 | printf( "\t\t%s", name );
|
---|
273 | // Get info about this value.
|
---|
274 | PRUint32 type;
|
---|
275 | rv = reg->GetValueType( root, name, &type );
|
---|
276 | if ( rv == NS_OK ) {
|
---|
277 | // Print value contents.
|
---|
278 | switch ( type ) {
|
---|
279 | case nsIRegistry::String: {
|
---|
280 | char *strValue;
|
---|
281 | rv = reg->GetStringUTF8( root, name, &strValue );
|
---|
282 | if ( rv == NS_OK ) {
|
---|
283 | printString( strValue, strlen(name) );
|
---|
284 | nsMemory::Free( strValue );
|
---|
285 | } else {
|
---|
286 | printf( "\t Error getting string value, rv=0x%08X", (int)rv );
|
---|
287 | }
|
---|
288 | }
|
---|
289 | break;
|
---|
290 |
|
---|
291 | case nsIRegistry::Int32:
|
---|
292 | {
|
---|
293 | PRInt32 val = 0;
|
---|
294 | rv = reg->GetInt( root, name, &val );
|
---|
295 | if (NS_SUCCEEDED(rv)) {
|
---|
296 | printf( "\t= Int32 [%d, 0x%x]", val, val);
|
---|
297 | }
|
---|
298 | else {
|
---|
299 | printf( "\t Error getting int32 value, rv=%08X", (int)rv);
|
---|
300 | }
|
---|
301 | }
|
---|
302 | break;
|
---|
303 |
|
---|
304 | case nsIRegistry::Bytes:
|
---|
305 | printf( "\t= Bytes" );
|
---|
306 | break;
|
---|
307 |
|
---|
308 | case nsIRegistry::File:
|
---|
309 | printf( "\t= File (?)" );
|
---|
310 | break;
|
---|
311 |
|
---|
312 | default:
|
---|
313 | printf( "\t= ? (unknown type=0x%02X)", (int)type );
|
---|
314 | break;
|
---|
315 | }
|
---|
316 | } else {
|
---|
317 | printf( "\t= ? (error getting value, rv=0x%08X)", (int)rv );
|
---|
318 | }
|
---|
319 | printf("\n");
|
---|
320 | nsMemory::Free( name );
|
---|
321 | } else {
|
---|
322 | printf( "Error getting value name, rv=0x%08X\n", (int)rv );
|
---|
323 | }
|
---|
324 | // Release node.
|
---|
325 | value->Release();
|
---|
326 | } else {
|
---|
327 | printf( "Error converting base node ptr to nsIRegistryNode, rv=0x%08X\n", (int)rv );
|
---|
328 | }
|
---|
329 | // Release item.
|
---|
330 | base->Release();
|
---|
331 |
|
---|
332 | // Advance to next key.
|
---|
333 | rv = values->Next();
|
---|
334 | // Check result.
|
---|
335 | if ( NS_SUCCEEDED( rv ) ) {
|
---|
336 | } else {
|
---|
337 | printf( "Error advancing enumerator, rv=0x%08X\n", (int)rv );
|
---|
338 | break;
|
---|
339 | }
|
---|
340 | } else {
|
---|
341 | printf( "Error getting current item, rv=0x%08X\n", (int)rv );
|
---|
342 | break;
|
---|
343 | }
|
---|
344 | }
|
---|
345 |
|
---|
346 | values->Release();
|
---|
347 | } else {
|
---|
348 | printf( "\t\tError enumerating values, rv=0x%08X\n", (int)rv );
|
---|
349 | }
|
---|
350 | return;
|
---|
351 | }
|
---|
352 |
|
---|
353 | static void printString( const char *value, int /*indent*/ ) {
|
---|
354 | // For now, just dump contents.
|
---|
355 | printf( "\t = %s", value );
|
---|
356 | return;
|
---|
357 | }
|
---|