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 "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>
35 #define SEPARATOR '\\'
38 using namespace ::rtl
;
39 using namespace ::osl
;
41 sal_Bool
isFileUrl(const OString
& fileName
)
43 if (fileName
.indexOf("file://") == 0 )
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 )
60 if (osl_getProcessWorkingDir(&uWorkingDir
.pData
) != osl_Process_E_None
)
64 if (FileBase::getAbsoluteFileURL(uWorkingDir
, uFileName
, uUrlFileName
)
71 if (FileBase::getFileURLFromSystemPath(uFileName
, uUrlFileName
)
82 OUStringToOString(s, RTL_TEXTENCODING_UTF8).getStr()
84 OStringToOUString(s, RTL_TEXTENCODING_UTF8)
88 sal_Bool
operator()(const OUString
& str1
, const OUString
& str2
) const
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
; }
121 { return m_bVerbose
; }
124 OString m_typeRegName
;
130 bool Options::initOptions(int ac
, char* av
[])
137 fprintf(stderr
, "%s", prepareHelp().getStr());
142 sal_Int32 index
= -1;
143 if ((index
=m_program
.lastIndexOf(SEPARATOR
)) > 0)
144 m_program
= av
[0]+index
+1;
155 if (av
[i
][2] == '\0')
157 if (i
< ac
- 1 && av
[i
+1][0] != '-')
163 fprintf(stderr
, "%s: invalid option '%s'\n", m_program
.getStr(), av
[i
]);
171 m_typeRegName
= OString(s
);
175 if (av
[i
][2] == '\0')
177 if (i
< ac
- 1 && av
[i
+1][0] != '-')
183 fprintf(stderr
, "%s: invalid option '%s'\n", m_program
.getStr(), av
[i
]);
191 m_keyName
= OString(s
);
195 if (av
[i
][2] != '\0')
197 fprintf(stderr
, "%s: invalid option '%s'\n", m_program
.getStr(), av
[i
]);
204 if (av
[i
][2] != '\0')
206 fprintf(stderr
, "%s: invalid option '%s'\n", m_program
.getStr(), av
[i
]);
210 fprintf(stdout
, "%s", prepareHelp().getStr());
215 fprintf(stderr
, "%s: unknown option '%s'\n", m_program
.getStr(), av
[i
]);
221 fprintf(stderr
, "%s: unknown option '%s'\n", m_program
.getStr(), av
[i
]);
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();
244 OString
Options::prepareVersion()
246 OString
version(m_program
);
247 version
+= " Version 1.0\n\n";
251 static Options options
;
254 #if (defined UNX) || (defined __MINGW32__)
255 int main( int argc
, char * argv
[] )
257 int _cdecl
main( int argc
, char * argv
[] )
260 if ( !options
.initOptions(argc
, argv
) )
265 OUString
typeRegName( convertToFileUrl(options
.getTypeReg()) );
269 if ( typeReg
.open(typeRegName
, REG_READWRITE
) )
271 fprintf(stderr
, "%s: open registry \"%s\" failed\n",
272 options
.getProgramName().getStr(), options
.getTypeReg().getStr());
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());
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());
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());
307 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */