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: regsingleton.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 ************************************************************************/
34 #include <osl/diagnose.h>
37 #include <cppuhelper/bootstrap.hxx>
39 #include <com/sun/star/registry/XSimpleRegistry.hpp>
41 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
44 using namespace ::rtl
;
45 using namespace ::com::sun::star
;
46 using namespace ::com::sun::star::uno
;
48 static void print_options() SAL_THROW( () )
51 "\nusage: regsingleton [-r|-ra] registry_file singleton_name[=service_name] ...\n\n"
52 "Inserts a singleton entry into rdb.\n"
53 "Option -r revokes given entries, -ra revokes all entries.\n" );
56 //==================================================================================================
57 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc
, argv
)
65 bool insert_entry
= true;
66 bool remove_all
= false;
68 if ('-' == argv
[ nPos
][ 0 ] && 'r' == argv
[ nPos
][ 1 ])
70 if ('a' == argv
[ nPos
][ 2 ] && '\0' == argv
[ nPos
][ 3 ])
74 else if ('\0' != argv
[ nPos
][ 2 ])
83 OUString
sys_path( OUString::createFromAscii( argv
[ nPos
] ) );
85 oslFileError rc
= osl_getFileURLFromSystemPath( sys_path
.pData
, &file_url
.pData
);
86 if (osl_File_E_None
!= rc
)
88 fprintf( stderr
, "\nerror: cannot make file url out of %s\n", argv
[ nPos
] );
95 Reference
< registry::XSimpleRegistry
> xSimReg( ::cppu::createSimpleRegistry() );
96 xSimReg
->open( file_url
, sal_False
, sal_True
);
97 Reference
< registry::XRegistryKey
> xRoot( xSimReg
->getRootKey() );
103 xRoot
->deleteKey( OUSTR("SINGLETONS") );
105 catch (registry::InvalidRegistryException
& exc
)
108 OUStringToOString( exc
.Message
, RTL_TEXTENCODING_ASCII_US
) );
110 stderr
, "\nwarning: removing all singletons failed: %s\n",
116 Reference
< registry::XRegistryKey
> xKey( xRoot
->openKey( OUSTR("SINGLETONS") ) );
118 xKey
= xRoot
->createKey( OUSTR("SINGLETONS") );
120 for ( ; nPos
< argc
; ++nPos
)
122 OUString
singleton( OUString::createFromAscii( argv
[ nPos
] ) );
124 sal_Int32 eq
= singleton
.indexOf( '=' );
127 service
= singleton
.copy( eq
+1 );
128 singleton
= singleton
.copy( 0, eq
);
133 if (service
.getLength())
135 Reference
< registry::XRegistryKey
> xEntry( xKey
->openKey( singleton
) );
137 xEntry
= xKey
->createKey( singleton
);
138 xEntry
->setStringValue( service
);
142 OString
entry( OUStringToOString( singleton
, RTL_TEXTENCODING_ASCII_US
) );
144 stderr
, "\nwarning: no service name given for singleton %s!\n",
152 xKey
->deleteKey( singleton
);
154 catch (registry::InvalidRegistryException
& exc
)
156 OString
cstr_singleton(
157 OUStringToOString( singleton
, RTL_TEXTENCODING_ASCII_US
) );
159 OUStringToOString( exc
.Message
, RTL_TEXTENCODING_ASCII_US
) );
161 stderr
, "\nwarning: singleton %s is not registered: %s\n",
162 cstr_singleton
.getStr(), cstr_msg
.getStr() );
170 catch (Exception
& rExc
)
172 OString
msg( OUStringToOString( rExc
.Message
, RTL_TEXTENCODING_ASCII_US
) );
173 fprintf( stderr
, "\nerror: %s\n", msg
.getStr() );