merge the formfield patch from ooo-build
[ooovba.git] / cpputools / source / regsingleton / regsingleton.cxx
blob3fbfa25e5e0739e87598e8505070d9495d3a5223
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: regsingleton.cxx,v $
10 * $Revision: 1.5 $
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 #include <stdio.h>
33 #include "sal/main.h"
34 #include <osl/diagnose.h>
35 #include <osl/file.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( () )
50 printf(
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)
59 if (argc < 3)
61 print_options();
62 return 1;
65 bool insert_entry = true;
66 bool remove_all = false;
67 int nPos = 1;
68 if ('-' == argv[ nPos ][ 0 ] && 'r' == argv[ nPos ][ 1 ])
70 if ('a' == argv[ nPos ][ 2 ] && '\0' == argv[ nPos ][ 3 ])
72 remove_all = true;
74 else if ('\0' != argv[ nPos ][ 2 ])
76 print_options();
77 return 1;
79 insert_entry = false;
80 ++nPos;
83 OUString sys_path( OUString::createFromAscii( argv[ nPos ] ) );
84 OUString file_url;
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 ] );
89 return 1;
91 ++nPos;
93 try
95 Reference< registry::XSimpleRegistry > xSimReg( ::cppu::createSimpleRegistry() );
96 xSimReg->open( file_url, sal_False, sal_True );
97 Reference< registry::XRegistryKey > xRoot( xSimReg->getRootKey() );
99 if (remove_all)
103 xRoot->deleteKey( OUSTR("SINGLETONS") );
105 catch (registry::InvalidRegistryException & exc)
107 OString cstr_msg(
108 OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US ) );
109 fprintf(
110 stderr, "\nwarning: removing all singletons failed: %s\n",
111 cstr_msg.getStr() );
114 else
116 Reference< registry::XRegistryKey > xKey( xRoot->openKey( OUSTR("SINGLETONS") ) );
117 if (! xKey.is())
118 xKey = xRoot->createKey( OUSTR("SINGLETONS") );
120 for ( ; nPos < argc; ++nPos )
122 OUString singleton( OUString::createFromAscii( argv[ nPos ] ) );
123 OUString service;
124 sal_Int32 eq = singleton.indexOf( '=' );
125 if (eq >= 0)
127 service = singleton.copy( eq +1 );
128 singleton = singleton.copy( 0, eq );
131 if (insert_entry)
133 if (service.getLength())
135 Reference< registry::XRegistryKey > xEntry( xKey->openKey( singleton ) );
136 if (! xEntry.is())
137 xEntry = xKey->createKey( singleton );
138 xEntry->setStringValue( service );
140 else
142 OString entry( OUStringToOString( singleton, RTL_TEXTENCODING_ASCII_US ) );
143 fprintf(
144 stderr, "\nwarning: no service name given for singleton %s!\n",
145 entry.getStr() );
148 else
152 xKey->deleteKey( singleton );
154 catch (registry::InvalidRegistryException & exc)
156 OString cstr_singleton(
157 OUStringToOString( singleton, RTL_TEXTENCODING_ASCII_US ) );
158 OString cstr_msg(
159 OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US ) );
160 fprintf(
161 stderr, "\nwarning: singleton %s is not registered: %s\n",
162 cstr_singleton.getStr(), cstr_msg.getStr() );
168 return 0;
170 catch (Exception & rExc)
172 OString msg( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
173 fprintf( stderr, "\nerror: %s\n", msg.getStr() );
174 return 1;