Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / registry / tools / rdbedit.cxx
blob6e8a99f3c2b7f1bf17569d246ccc2a23fcd54100
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>
21 #include <string.h>
23 #include "registry/registry.hxx"
24 #include "registry/reflread.hxx"
25 #include <rtl/ustring.hxx>
26 #include <rtl/alloc.h>
27 #include <osl/process.h>
28 #include <osl/diagnose.h>
29 #include <osl/thread.h>
30 #include <osl/file.hxx>
32 #ifdef SAL_UNX
33 #define SEPARATOR '/'
34 #else
35 #define SEPARATOR '\\'
36 #endif
38 using namespace ::rtl;
39 using namespace ::osl;
41 sal_Bool isFileUrl(const OString& fileName)
43 if (fileName.indexOf("file://") == 0 )
44 return sal_True;
45 return sal_False;
48 OUString convertToFileUrl(const OString& fileName)
50 if ( isFileUrl(fileName) )
52 return OStringToOUString(fileName, osl_getThreadTextEncoding());
55 OUString uUrlFileName;
56 OUString uFileName(fileName.getStr(), fileName.getLength(), osl_getThreadTextEncoding());
57 if ( fileName.indexOf('.') == 0 || fileName.indexOf(SEPARATOR) < 0 )
59 OUString uWorkingDir;
60 if (osl_getProcessWorkingDir(&uWorkingDir.pData) != osl_Process_E_None)
62 OSL_ASSERT(false);
64 if (FileBase::getAbsoluteFileURL(uWorkingDir, uFileName, uUrlFileName)
65 != FileBase::E_None)
67 OSL_ASSERT(false);
69 } else
71 if (FileBase::getFileURLFromSystemPath(uFileName, uUrlFileName)
72 != FileBase::E_None)
74 OSL_ASSERT(false);
78 return uUrlFileName;
81 #define U2S( s ) \
82 OUStringToOString(s, RTL_TEXTENCODING_UTF8).getStr()
83 #define S2U( s ) \
84 OStringToOUString(s, RTL_TEXTENCODING_UTF8)
86 struct LessString
88 sal_Bool operator()(const OUString& str1, const OUString& str2) const
90 return (str1 < str2);
94 enum Command {
95 DELETEKEY
98 class Options
100 public:
101 Options()
102 : m_bVerbose(false)
104 ~Options()
107 bool initOptions(int ac, char* av[]);
109 OString prepareHelp();
110 OString prepareVersion();
112 const OString& getProgramName() const
113 { return m_program; }
114 const OString& getTypeReg() const
115 { return m_typeRegName; }
116 const OString& getKeyName() const
117 { return m_keyName; }
118 Command getCommand() const
119 { return m_command; }
120 bool verbose() const
121 { return m_bVerbose; }
122 protected:
123 OString m_program;
124 OString m_typeRegName;
125 OString m_keyName;
126 Command m_command;
127 bool m_bVerbose;
130 bool Options::initOptions(int ac, char* av[])
132 bool bRet = true;
133 sal_uInt16 i=1;
135 if (ac < 2)
137 fprintf(stderr, "%s", prepareHelp().getStr());
138 bRet = sal_False;
141 m_program = av[0];
142 sal_Int32 index = -1;
143 if ((index=m_program.lastIndexOf(SEPARATOR)) > 0)
144 m_program = av[0]+index+1;
146 char *s=NULL;
147 for (; i < ac; i++)
149 if (av[i][0] == '-')
151 switch (av[i][1])
153 case 'r':
154 case 'R':
155 if (av[i][2] == '\0')
157 if (i < ac - 1 && av[i+1][0] != '-')
159 i++;
160 s = av[i];
161 } else
163 fprintf(stderr, "%s: invalid option '%s'\n", m_program.getStr(), av[i]);
164 bRet = sal_False;
165 break;
167 } else
169 s = av[i] + 2;
171 m_typeRegName = OString(s);
172 break;
173 case 'd':
174 case 'D':
175 if (av[i][2] == '\0')
177 if (i < ac - 1 && av[i+1][0] != '-')
179 i++;
180 s = av[i];
181 } else
183 fprintf(stderr, "%s: invalid option '%s'\n", m_program.getStr(), av[i]);
184 bRet = sal_False;
185 break;
187 } else
189 s = av[i] + 2;
191 m_keyName = OString(s);
192 break;
193 case 'v':
194 case 'V':
195 if (av[i][2] != '\0')
197 fprintf(stderr, "%s: invalid option '%s'\n", m_program.getStr(), av[i]);
198 bRet = sal_False;
200 m_bVerbose = true;
201 break;
202 case 'h':
203 case '?':
204 if (av[i][2] != '\0')
206 fprintf(stderr, "%s: invalid option '%s'\n", m_program.getStr(), av[i]);
207 bRet = false;
208 } else
210 fprintf(stdout, "%s", prepareHelp().getStr());
211 exit(0);
213 break;
214 default:
215 fprintf(stderr, "%s: unknown option '%s'\n", m_program.getStr(), av[i]);
216 bRet = false;
217 break;
219 } else
221 fprintf(stderr, "%s: unknown option '%s'\n", m_program.getStr(), av[i]);
222 bRet = false;
226 return bRet;
229 OString Options::prepareHelp()
231 OString help("\nusing: ");
232 help += m_program + " -r<filename> <command>\n";
233 help += " -r<filename> = filename specifies the name of the type registry.\n";
234 help += "Commands:\n";
235 help += " -d <keyname> = delete the specified key from the registry. Keyname\n";
236 help += " specifies the name of the key that get deleted.\n";
237 help += " -v = verbose output.\n";
238 help += " -h|-? = print this help message and exit.\n";
239 help += prepareVersion();
241 return help;
244 OString Options::prepareVersion()
246 OString version(m_program);
247 version += " Version 1.0\n\n";
248 return version;
251 static Options options;
254 #if (defined UNX) || (defined __MINGW32__)
255 int main( int argc, char * argv[] )
256 #else
257 int _cdecl main( int argc, char * argv[] )
258 #endif
260 if ( !options.initOptions(argc, argv) )
262 exit(1);
265 OUString typeRegName( convertToFileUrl(options.getTypeReg()) );
267 Registry typeReg;
269 if ( typeReg.open(typeRegName, REG_READWRITE) )
271 fprintf(stderr, "%s: open registry \"%s\" failed\n",
272 options.getProgramName().getStr(), options.getTypeReg().getStr());
273 exit(2);
276 RegistryKey typeRoot;
277 if ( typeReg.openRootKey(typeRoot) )
279 fprintf(stderr, "%s: open root key of registry \"%s\" failed\n",
280 options.getProgramName().getStr(), options.getTypeReg().getStr());
281 exit(3);
284 if ( options.getCommand() == DELETEKEY )
286 if ( typeRoot.deleteKey(S2U(options.getKeyName())) )
288 fprintf(stderr, "%s: delete key \"%s\" of registry \"%s\" failed\n",
289 options.getProgramName().getStr(), options.getKeyName().getStr(), options.getTypeReg().getStr());
290 exit(4);
291 } else {
292 if (options.verbose())
293 fprintf(stderr, "%s: delete key \"%s\" of registry \"%s\"\n",
294 options.getProgramName().getStr(), options.getKeyName().getStr(), options.getTypeReg().getStr());
298 typeRoot.releaseKey();
299 if ( typeReg.close() )
301 fprintf(stderr, "%s: closing registry \"%s\" failed\n",
302 options.getProgramName().getStr(), options.getTypeReg().getStr());
303 exit(5);
307 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */