1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
23 #include <osl/diagnose.h>
26 #include <cppuhelper/bootstrap.hxx>
28 #include <com/sun/star/registry/XSimpleRegistry.hpp>
30 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
33 using namespace ::rtl
;
34 using namespace ::com::sun::star
;
35 using namespace ::com::sun::star::uno
;
37 static void print_options() SAL_THROW(())
40 "\nusage: regsingleton [-r|-ra] registry_file singleton_name[=service_name] ...\n\n"
41 "Inserts a singleton entry into rdb.\n"
42 "Option -r revokes given entries, -ra revokes all entries.\n" );
45 //==================================================================================================
46 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc
, argv
)
54 bool insert_entry
= true;
55 bool remove_all
= false;
57 if ('-' == argv
[ nPos
][ 0 ] && 'r' == argv
[ nPos
][ 1 ])
59 if ('a' == argv
[ nPos
][ 2 ] && '\0' == argv
[ nPos
][ 3 ])
63 else if ('\0' != argv
[ nPos
][ 2 ])
72 OUString
sys_path( OUString::createFromAscii( argv
[ nPos
] ) );
74 oslFileError rc
= osl_getFileURLFromSystemPath( sys_path
.pData
, &file_url
.pData
);
75 if (osl_File_E_None
!= rc
)
77 fprintf( stderr
, "\nerror: cannot make file url out of %s\n", argv
[ nPos
] );
84 Reference
< registry::XSimpleRegistry
> xSimReg( ::cppu::createSimpleRegistry() );
85 xSimReg
->open( file_url
, sal_False
, sal_True
);
86 Reference
< registry::XRegistryKey
> xRoot( xSimReg
->getRootKey() );
92 xRoot
->deleteKey( OUSTR("SINGLETONS") );
94 catch (registry::InvalidRegistryException
& exc
)
97 OUStringToOString( exc
.Message
, RTL_TEXTENCODING_ASCII_US
) );
99 stderr
, "\nwarning: removing all singletons failed: %s\n",
105 Reference
< registry::XRegistryKey
> xKey( xRoot
->openKey( OUSTR("SINGLETONS") ) );
107 xKey
= xRoot
->createKey( OUSTR("SINGLETONS") );
109 for ( ; nPos
< argc
; ++nPos
)
111 OUString
singleton( OUString::createFromAscii( argv
[ nPos
] ) );
113 sal_Int32 eq
= singleton
.indexOf( '=' );
116 service
= singleton
.copy( eq
+1 );
117 singleton
= singleton
.copy( 0, eq
);
122 if (!service
.isEmpty())
124 Reference
< registry::XRegistryKey
> xEntry( xKey
->openKey( singleton
) );
126 xEntry
= xKey
->createKey( singleton
);
127 xEntry
->setStringValue( service
);
131 OString
entry( OUStringToOString( singleton
, RTL_TEXTENCODING_ASCII_US
) );
133 stderr
, "\nwarning: no service name given for singleton %s!\n",
141 xKey
->deleteKey( singleton
);
143 catch (registry::InvalidRegistryException
& exc
)
145 OString
cstr_singleton(
146 OUStringToOString( singleton
, RTL_TEXTENCODING_ASCII_US
) );
148 OUStringToOString( exc
.Message
, RTL_TEXTENCODING_ASCII_US
) );
150 stderr
, "\nwarning: singleton %s is not registered: %s\n",
151 cstr_singleton
.getStr(), cstr_msg
.getStr() );
159 catch (Exception
& rExc
)
161 OString
msg( OUStringToOString( rExc
.Message
, RTL_TEXTENCODING_ASCII_US
) );
162 fprintf( stderr
, "\nerror: %s\n", msg
.getStr() );
167 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */