update dev300-m58
[ooovba.git] / registry / tools / checksingleton.cxx
blob8fcdfdc6df331f7fe467ceb6a884afe679ac33b2
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: checksingleton.cxx,v $
10 * $Revision: 1.13 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_registry.hxx"
33 #include <stdio.h>
34 #include <string.h>
36 #include "registry/registry.hxx"
37 #include "registry/reflread.hxx"
38 #include <rtl/ustring.hxx>
39 #include <rtl/alloc.h>
40 #include <osl/process.h>
41 #include <osl/diagnose.h>
42 #include <osl/thread.h>
43 #include <osl/file.hxx>
45 #ifdef SAL_UNX
46 #define SEPARATOR '/'
47 #else
48 #define SEPARATOR '\\'
49 #endif
51 using namespace ::rtl;
52 using namespace ::osl;
54 sal_Bool isFileUrl(const OString& fileName)
56 if (fileName.indexOf("file://") == 0 )
57 return sal_True;
58 return sal_False;
61 OUString convertToFileUrl(const OString& fileName)
63 if ( isFileUrl(fileName) )
65 return OStringToOUString(fileName, osl_getThreadTextEncoding());
68 OUString uUrlFileName;
69 OUString uFileName(fileName.getStr(), fileName.getLength(), osl_getThreadTextEncoding());
70 if ( fileName.indexOf('.') == 0 || fileName.indexOf(SEPARATOR) < 0 )
72 OUString uWorkingDir;
73 if (osl_getProcessWorkingDir(&uWorkingDir.pData) != osl_Process_E_None)
75 OSL_ASSERT(false);
77 if (FileBase::getAbsoluteFileURL(uWorkingDir, uFileName, uUrlFileName)
78 != FileBase::E_None)
80 OSL_ASSERT(false);
82 } else
84 if (FileBase::getFileURLFromSystemPath(uFileName, uUrlFileName)
85 != FileBase::E_None)
87 OSL_ASSERT(false);
91 return uUrlFileName;
94 #define U2S( s ) \
95 OUStringToOString(s, RTL_TEXTENCODING_UTF8).getStr()
96 #define S2U( s ) \
97 OStringToOUString(s, RTL_TEXTENCODING_UTF8)
99 struct LessString
101 sal_Bool operator()(const OUString& str1, const OUString& str2) const
103 return (str1 < str2);
107 class Options
109 public:
110 Options()
111 : m_bForceOutput(sal_False)
113 ~Options()
116 sal_Bool initOptions(int ac, char* av[], sal_Bool bCmdFile=sal_False);
118 OString prepareHelp();
119 OString prepareVersion();
121 const OString& getProgramName()
122 { return m_program; }
123 const OString& getIndexReg()
124 { return m_indexRegName; }
125 const OString& getTypeReg()
126 { return m_typeRegName; }
127 sal_Bool hasBase()
128 { return m_base.getLength() > 0; }
129 const OString& getBase()
130 { return m_base; }
131 sal_Bool forceOutput()
132 { return m_bForceOutput; }
133 protected:
134 OString m_program;
135 OString m_indexRegName;
136 OString m_typeRegName;
137 OString m_base;
138 sal_Bool m_bForceOutput;
141 sal_Bool Options::initOptions(int ac, char* av[], sal_Bool bCmdFile)
143 sal_Bool bRet = sal_True;
144 sal_uInt16 i=0;
146 if (!bCmdFile)
148 bCmdFile = sal_True;
150 m_program = av[0];
152 if (ac < 2)
154 fprintf(stderr, "%s", prepareHelp().getStr());
155 bRet = sal_False;
158 i = 1;
159 } else
161 i = 0;
164 char *s=NULL;
165 for (; i < ac; i++)
167 if (av[i][0] == '-')
169 switch (av[i][1])
171 case 'r':
172 case 'R':
173 if (av[i][2] == '\0')
175 if (i < ac - 1 && av[i+1][0] != '-')
177 i++;
178 s = av[i];
179 } else
181 fprintf(stderr, "%s: invalid option '%s'\n", m_program.getStr(), av[i]);
182 bRet = sal_False;
183 break;
185 } else
187 s = av[i] + 2;
189 m_typeRegName = OString(s);
190 break;
191 case 'o':
192 case 'O':
193 if (av[i][2] == '\0')
195 if (i < ac - 1 && av[i+1][0] != '-')
197 i++;
198 s = av[i];
199 } else
201 fprintf(stderr, "%s: invalid option '%s'\n", m_program.getStr(), av[i]);
202 bRet = sal_False;
203 break;
205 } else
207 s = av[i] + 2;
209 m_indexRegName = OString(s);
210 break;
211 case 'b':
212 case 'B':
213 if (av[i][2] == '\0')
215 if (i < ac - 1 && av[i+1][0] != '-')
217 i++;
218 s = av[i];
219 } else
221 fprintf(stderr, "%s: invalid option '%s'\n", m_program.getStr(), av[i]);
222 bRet = sal_False;
223 break;
225 } else
227 s = av[i] + 2;
229 m_base = OString(s);
230 break;
231 case 'f':
232 case 'F':
233 if (av[i][2] != '\0')
235 fprintf(stderr, "%s: invalid option '%s'\n", m_program.getStr(), av[i]);
236 bRet = sal_False;
238 m_bForceOutput = sal_True;
239 break;
240 case 'h':
241 case '?':
242 if (av[i][2] != '\0')
244 fprintf(stderr, "%s: invalid option '%s'\n", m_program.getStr(), av[i]);
245 bRet = sal_False;
246 } else
248 fprintf(stdout, "%s", prepareHelp().getStr());
249 exit(0);
251 break;
252 default:
253 fprintf(stderr, "%s: unknown option '%s'\n", m_program.getStr(), av[i]);
254 bRet = sal_False;
255 break;
257 } else
259 if (av[i][0] == '@')
261 FILE* cmdFile = fopen(av[i]+1, "r");
262 if( cmdFile == NULL )
264 fprintf(stderr, "%s", prepareHelp().getStr());
265 bRet = sal_False;
266 } else
268 int rargc=0;
269 char* rargv[512];
270 char buffer[512];
272 while ( fscanf(cmdFile, "%s", buffer) != EOF )
274 rargv[rargc]= strdup(buffer);
275 rargc++;
277 fclose(cmdFile);
279 bRet = initOptions(rargc, rargv, bCmdFile);
281 for (long j=0; j < rargc; j++)
283 free(rargv[j]);
286 } else
288 fprintf(stderr, "%s: unknown option '%s'\n", m_program.getStr(), av[i]);
289 bRet = sal_False;
294 return bRet;
297 OString Options::prepareHelp()
299 OString help("\nusing: ");
300 help += m_program + " -r<filename> -o<filename> [-options] | @<filename>\n";
301 help += " -o<filename> = filename specifies the name of the new singleton index registry.\n";
302 help += " -r<filename> = filename specifies the name of the type registry.\n";
303 help += " @<filename> = filename specifies a command file.\n";
304 help += "Options:\n";
305 help += " -b<name> = name specifies the name of a start key. The types will be searched\n";
306 help += " under this key in the type registry.\n";
307 help += " -f = force the output of all found singletons.\n";
308 help += " -h|-? = print this help message and exit.\n";
309 help += prepareVersion();
311 return help;
314 OString Options::prepareVersion()
316 OString version("\nSun Microsystems (R) ");
317 version += m_program + " Version 1.0\n\n";
318 return version;
321 static Options options;
323 static sal_Bool checkSingletons(RegistryKey& singletonKey, RegistryKey& typeKey)
325 RegValueType valueType = RG_VALUETYPE_NOT_DEFINED;
326 sal_uInt32 size = 0;
327 OUString tmpName;
328 sal_Bool bRet = sal_False;
330 RegError e = typeKey.getValueInfo(tmpName, &valueType, &size);
332 if ( e != REG_VALUE_NOT_EXISTS && e != REG_INVALID_VALUE && valueType == RG_VALUETYPE_BINARY)
334 RegistryKey entryKey;
335 RegValue value = rtl_allocateMemory(size);
337 typeKey.getValue(tmpName, value);
339 RegistryTypeReader reader((sal_uInt8*)value, size, sal_False);
341 if ( reader.isValid() && reader.getTypeClass() == RT_TYPE_SINGLETON )
343 OUString singletonName = reader.getTypeName().replace('/', '.');
344 if ( singletonKey.createKey(singletonName, entryKey) )
346 fprintf(stderr, "%s: could not create SINGLETONS entry for \"%s\"\n",
347 options.getProgramName().getStr(), U2S( singletonName ));
348 } else
350 bRet = sal_True;
351 OUString value2 = reader.getSuperTypeName();
353 if ( entryKey.setValue(tmpName, RG_VALUETYPE_UNICODE,
354 (RegValue)value2.getStr(), sizeof(sal_Unicode)* (value2.getLength()+1)) )
356 fprintf(stderr, "%s: could not create data entry for singleton \"%s\"\n",
357 options.getProgramName().getStr(), U2S( singletonName ));
360 if ( options.forceOutput() )
362 fprintf(stderr, "%s: create SINGLETON entry for \"%s\" -> \"%s\"\n",
363 options.getProgramName().getStr(), U2S( singletonName ), U2S(value2));
368 rtl_freeMemory(value);
371 RegistryKeyArray subKeys;
373 typeKey.openSubKeys(tmpName, subKeys);
375 sal_uInt32 length = subKeys.getLength();
376 RegistryKey elementKey;
377 for (sal_uInt32 i = 0; i < length; i++)
379 elementKey = subKeys.getElement(i);
380 if ( checkSingletons(singletonKey, elementKey) )
382 bRet = sal_True;
385 return bRet;
388 #if (defined UNX) || (defined OS2) || (defined __MINGW32__)
389 int main( int argc, char * argv[] )
390 #else
391 int _cdecl main( int argc, char * argv[] )
392 #endif
394 if ( !options.initOptions(argc, argv) )
396 exit(1);
399 OUString indexRegName( convertToFileUrl(options.getIndexReg()) );
400 OUString typeRegName( convertToFileUrl(options.getTypeReg()) );
402 Registry indexReg;
403 Registry typeReg;
405 if ( indexReg.open(indexRegName, REG_READWRITE) )
407 if ( indexReg.create(indexRegName) )
409 fprintf(stderr, "%s: open registry \"%s\" failed\n",
410 options.getProgramName().getStr(), options.getIndexReg().getStr());
411 exit(2);
414 if ( typeReg.open(typeRegName, REG_READONLY) )
416 fprintf(stderr, "%s: open registry \"%s\" failed\n",
417 options.getProgramName().getStr(), options.getTypeReg().getStr());
418 exit(3);
421 RegistryKey indexRoot, typeRoot;
422 if ( indexReg.openRootKey(indexRoot) )
424 fprintf(stderr, "%s: open root key of registry \"%s\" failed\n",
425 options.getProgramName().getStr(), options.getIndexReg().getStr());
426 exit(4);
428 if ( typeReg.openRootKey(typeRoot) )
430 fprintf(stderr, "%s: open root key of registry \"%s\" failed\n",
431 options.getProgramName().getStr(), options.getTypeReg().getStr());
432 exit(5);
435 RegistryKey singletonKey, typeKey;
436 if ( options.hasBase() )
438 if ( typeRoot.openKey(S2U(options.getBase()), typeKey) )
440 fprintf(stderr, "%s: open base key of registry \"%s\" failed\n",
441 options.getProgramName().getStr(), options.getTypeReg().getStr());
442 exit(6);
444 } else
446 typeKey = typeRoot;
449 if ( indexRoot.createKey(OUString::createFromAscii("SINGLETONS"), singletonKey) )
451 fprintf(stderr, "%s: open/create SINGLETONS key of registry \"%s\" failed\n",
452 options.getProgramName().getStr(), options.getIndexReg().getStr());
453 exit(7);
456 sal_Bool bSingletonsExist = checkSingletons(singletonKey, typeKey);
458 indexRoot.closeKey();
459 typeRoot.closeKey();
460 typeKey.closeKey();
461 singletonKey.closeKey();
462 if ( indexReg.close() )
464 fprintf(stderr, "%s: closing registry \"%s\" failed\n",
465 options.getProgramName().getStr(), options.getIndexReg().getStr());
466 exit(9);
468 if ( !bSingletonsExist )
470 if ( indexReg.destroy(OUString()) )
472 fprintf(stderr, "%s: destroy registry \"%s\" failed\n",
473 options.getProgramName().getStr(), options.getIndexReg().getStr());
474 exit(10);
477 if ( typeReg.close() )
479 fprintf(stderr, "%s: closing registry \"%s\" failed\n",
480 options.getProgramName().getStr(), options.getTypeReg().getStr());
481 exit(11);