1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: checksingleton.cxx,v $
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"
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>
48 #define SEPARATOR '\\'
51 using namespace ::rtl
;
52 using namespace ::osl
;
54 sal_Bool
isFileUrl(const OString
& fileName
)
56 if (fileName
.indexOf("file://") == 0 )
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 )
73 if (osl_getProcessWorkingDir(&uWorkingDir
.pData
) != osl_Process_E_None
)
77 if (FileBase::getAbsoluteFileURL(uWorkingDir
, uFileName
, uUrlFileName
)
84 if (FileBase::getFileURLFromSystemPath(uFileName
, uUrlFileName
)
95 OUStringToOString(s, RTL_TEXTENCODING_UTF8).getStr()
97 OStringToOUString(s, RTL_TEXTENCODING_UTF8)
101 sal_Bool
operator()(const OUString
& str1
, const OUString
& str2
) const
103 return (str1
< str2
);
111 : m_bForceOutput(sal_False
)
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
; }
128 { return m_base
.getLength() > 0; }
129 const OString
& getBase()
131 sal_Bool
forceOutput()
132 { return m_bForceOutput
; }
135 OString m_indexRegName
;
136 OString m_typeRegName
;
138 sal_Bool m_bForceOutput
;
141 sal_Bool
Options::initOptions(int ac
, char* av
[], sal_Bool bCmdFile
)
143 sal_Bool bRet
= sal_True
;
154 fprintf(stderr
, "%s", prepareHelp().getStr());
173 if (av
[i
][2] == '\0')
175 if (i
< ac
- 1 && av
[i
+1][0] != '-')
181 fprintf(stderr
, "%s: invalid option '%s'\n", m_program
.getStr(), av
[i
]);
189 m_typeRegName
= OString(s
);
193 if (av
[i
][2] == '\0')
195 if (i
< ac
- 1 && av
[i
+1][0] != '-')
201 fprintf(stderr
, "%s: invalid option '%s'\n", m_program
.getStr(), av
[i
]);
209 m_indexRegName
= OString(s
);
213 if (av
[i
][2] == '\0')
215 if (i
< ac
- 1 && av
[i
+1][0] != '-')
221 fprintf(stderr
, "%s: invalid option '%s'\n", m_program
.getStr(), av
[i
]);
233 if (av
[i
][2] != '\0')
235 fprintf(stderr
, "%s: invalid option '%s'\n", m_program
.getStr(), av
[i
]);
238 m_bForceOutput
= sal_True
;
242 if (av
[i
][2] != '\0')
244 fprintf(stderr
, "%s: invalid option '%s'\n", m_program
.getStr(), av
[i
]);
248 fprintf(stdout
, "%s", prepareHelp().getStr());
253 fprintf(stderr
, "%s: unknown option '%s'\n", m_program
.getStr(), av
[i
]);
261 FILE* cmdFile
= fopen(av
[i
]+1, "r");
262 if( cmdFile
== NULL
)
264 fprintf(stderr
, "%s", prepareHelp().getStr());
272 while ( fscanf(cmdFile
, "%s", buffer
) != EOF
)
274 rargv
[rargc
]= strdup(buffer
);
279 bRet
= initOptions(rargc
, rargv
, bCmdFile
);
281 for (long j
=0; j
< rargc
; j
++)
288 fprintf(stderr
, "%s: unknown option '%s'\n", m_program
.getStr(), av
[i
]);
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();
314 OString
Options::prepareVersion()
316 OString
version("\nSun Microsystems (R) ");
317 version
+= m_program
+ " Version 1.0\n\n";
321 static Options options
;
323 static sal_Bool
checkSingletons(RegistryKey
& singletonKey
, RegistryKey
& typeKey
)
325 RegValueType valueType
= RG_VALUETYPE_NOT_DEFINED
;
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
));
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
) )
388 #if (defined UNX) || (defined OS2) || (defined __MINGW32__)
389 int main( int argc
, char * argv
[] )
391 int _cdecl
main( int argc
, char * argv
[] )
394 if ( !options
.initOptions(argc
, argv
) )
399 OUString
indexRegName( convertToFileUrl(options
.getIndexReg()) );
400 OUString
typeRegName( convertToFileUrl(options
.getTypeReg()) );
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());
414 if ( typeReg
.open(typeRegName
, REG_READONLY
) )
416 fprintf(stderr
, "%s: open registry \"%s\" failed\n",
417 options
.getProgramName().getStr(), options
.getTypeReg().getStr());
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());
428 if ( typeReg
.openRootKey(typeRoot
) )
430 fprintf(stderr
, "%s: open root key of registry \"%s\" failed\n",
431 options
.getProgramName().getStr(), options
.getTypeReg().getStr());
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());
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());
456 sal_Bool bSingletonsExist
= checkSingletons(singletonKey
, typeKey
);
458 indexRoot
.closeKey();
461 singletonKey
.closeKey();
462 if ( indexReg
.close() )
464 fprintf(stderr
, "%s: closing registry \"%s\" failed\n",
465 options
.getProgramName().getStr(), options
.getIndexReg().getStr());
468 if ( !bSingletonsExist
)
470 if ( indexReg
.destroy(OUString()) )
472 fprintf(stderr
, "%s: destroy registry \"%s\" failed\n",
473 options
.getProgramName().getStr(), options
.getIndexReg().getStr());
477 if ( typeReg
.close() )
479 fprintf(stderr
, "%s: closing registry \"%s\" failed\n",
480 options
.getProgramName().getStr(), options
.getTypeReg().getStr());