Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / cpputools / source / regsingleton / regsingleton.cxx
blob7224434691f20390962e9af3f3335912d92933a2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <stdio.h>
22 #include "sal/main.h"
23 #include <osl/diagnose.h>
24 #include <osl/file.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(())
39 printf(
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)
48 if (argc < 3)
50 print_options();
51 return 1;
54 bool insert_entry = true;
55 bool remove_all = false;
56 int nPos = 1;
57 if ('-' == argv[ nPos ][ 0 ] && 'r' == argv[ nPos ][ 1 ])
59 if ('a' == argv[ nPos ][ 2 ] && '\0' == argv[ nPos ][ 3 ])
61 remove_all = true;
63 else if ('\0' != argv[ nPos ][ 2 ])
65 print_options();
66 return 1;
68 insert_entry = false;
69 ++nPos;
72 OUString sys_path( OUString::createFromAscii( argv[ nPos ] ) );
73 OUString file_url;
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 ] );
78 return 1;
80 ++nPos;
82 try
84 Reference< registry::XSimpleRegistry > xSimReg( ::cppu::createSimpleRegistry() );
85 xSimReg->open( file_url, sal_False, sal_True );
86 Reference< registry::XRegistryKey > xRoot( xSimReg->getRootKey() );
88 if (remove_all)
90 try
92 xRoot->deleteKey( OUSTR("SINGLETONS") );
94 catch (registry::InvalidRegistryException & exc)
96 OString cstr_msg(
97 OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US ) );
98 fprintf(
99 stderr, "\nwarning: removing all singletons failed: %s\n",
100 cstr_msg.getStr() );
103 else
105 Reference< registry::XRegistryKey > xKey( xRoot->openKey( OUSTR("SINGLETONS") ) );
106 if (! xKey.is())
107 xKey = xRoot->createKey( OUSTR("SINGLETONS") );
109 for ( ; nPos < argc; ++nPos )
111 OUString singleton( OUString::createFromAscii( argv[ nPos ] ) );
112 OUString service;
113 sal_Int32 eq = singleton.indexOf( '=' );
114 if (eq >= 0)
116 service = singleton.copy( eq +1 );
117 singleton = singleton.copy( 0, eq );
120 if (insert_entry)
122 if (!service.isEmpty())
124 Reference< registry::XRegistryKey > xEntry( xKey->openKey( singleton ) );
125 if (! xEntry.is())
126 xEntry = xKey->createKey( singleton );
127 xEntry->setStringValue( service );
129 else
131 OString entry( OUStringToOString( singleton, RTL_TEXTENCODING_ASCII_US ) );
132 fprintf(
133 stderr, "\nwarning: no service name given for singleton %s!\n",
134 entry.getStr() );
137 else
141 xKey->deleteKey( singleton );
143 catch (registry::InvalidRegistryException & exc)
145 OString cstr_singleton(
146 OUStringToOString( singleton, RTL_TEXTENCODING_ASCII_US ) );
147 OString cstr_msg(
148 OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US ) );
149 fprintf(
150 stderr, "\nwarning: singleton %s is not registered: %s\n",
151 cstr_singleton.getStr(), cstr_msg.getStr() );
157 return 0;
159 catch (Exception & rExc)
161 OString msg( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
162 fprintf( stderr, "\nerror: %s\n", msg.getStr() );
163 return 1;
167 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */