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 .
21 #include <registry/registry.hxx>
22 #include "fileurl.hxx"
23 #include "options.hxx"
25 #include <rtl/ustring.hxx>
26 #include <sal/log.hxx>
31 using namespace registry::tools
;
33 class Options_Impl
: public Options
38 explicit Options_Impl (char const * program
)
39 : Options(program
), m_bVerbose(false)
41 bool isVerbose() const { return m_bVerbose
; }
44 virtual void printUsage_Impl() const override
;
45 virtual bool initOptions_Impl(std::vector
< std::string
> & rArgs
) override
;
48 void Options_Impl::printUsage_Impl() const
50 fprintf(stderr
, "using: regmerge [-v|--verbose] mergefile mergeKeyName regfile_1 ... regfile_n\n");
51 fprintf(stderr
, " regmerge @regcmds\nOptions:\n");
52 fprintf(stderr
, " -v, --verbose : verbose output on stdout.\n");
53 fprintf(stderr
, " mergefile : specifies the merged registry file. If this file doesn't exists,\n");
54 fprintf(stderr
, " it is created.\n");
55 fprintf(stderr
, " mergeKeyName : specifies the merge key, everything is merged under this key.\n");
56 fprintf(stderr
, " If this key doesn't exists, it is created.\n");
57 fprintf(stderr
, " regfile_1..n : specifies one or more registry files which are merged.\n");
60 bool Options_Impl::initOptions_Impl (std::vector
< std::string
> & rArgs
)
62 std::vector
< std::string
>::iterator first
= rArgs
.begin(), last
= rArgs
.end();
63 if ((first
!= last
) && ((*first
)[0] == '-'))
65 std::string
option(*first
);
66 if ((option
.compare("-v") == 0) || (option
.compare("--verbose") == 0))
70 else if ((option
.compare("-h") == 0) || (option
.compare("-?") == 0))
76 return badOption("unknown", option
.c_str());
78 (void) rArgs
.erase(first
);
84 int main( int argc
, char * argv
[] )
86 int __cdecl
main( int argc
, char * argv
[] )
91 Options_Impl
options(argv
[0]);
93 std::vector
< std::string
> args
;
94 for (int i
= 1; i
< argc
; i
++)
96 if (!Options::checkArgument(args
, argv
[i
], strlen(argv
[i
])))
102 if (!options
.initOptions(args
))
108 options
.printUsage();
113 OUString
regName( convertToFileUrl(args
[0].c_str(), args
[0].size()) );
114 if (reg
.open(regName
, RegAccessMode::READWRITE
) != RegError::NO_ERROR
)
116 if (reg
.create(regName
) != RegError::NO_ERROR
)
118 if (options
.isVerbose())
119 fprintf(stderr
, "open registry \"%s\" failed\n", args
[0].c_str());
125 if (reg
.openRootKey(rootKey
) != RegError::NO_ERROR
)
127 if (options
.isVerbose())
128 fprintf(stderr
, "open root key of registry \"%s\" failed\n", args
[0].c_str());
132 OUString
mergeKeyName( OUString::createFromAscii(args
[1].c_str()) );
133 for (size_t i
= 2; i
< args
.size(); i
++)
135 OUString
targetRegName( convertToFileUrl(args
[i
].c_str(), args
[i
].size()) );
136 RegError _ret
= reg
.mergeKey(rootKey
, mergeKeyName
, targetRegName
, options
.isVerbose());
137 if (_ret
!= RegError::NO_ERROR
)
139 if (_ret
== RegError::MERGE_CONFLICT
)
141 if (options
.isVerbose())
142 fprintf(stderr
, "merging registry \"%s\" under key \"%s\" in registry \"%s\".\n",
143 args
[i
].c_str(), args
[1].c_str(), args
[0].c_str());
147 if (options
.isVerbose())
148 fprintf(stderr
, "ERROR: merging registry \"%s\" under key \"%s\" in registry \"%s\" failed.\n",
149 args
[i
].c_str(), args
[1].c_str(), args
[0].c_str());
155 if (options
.isVerbose())
156 fprintf(stderr
, "merging registry \"%s\" under key \"%s\" in registry \"%s\".\n",
157 args
[i
].c_str(), args
[1].c_str(), args
[0].c_str());
161 rootKey
.releaseKey();
162 if (reg
.close() != RegError::NO_ERROR
)
164 if (options
.isVerbose())
165 fprintf(stderr
, "closing registry \"%s\" failed\n", args
[0].c_str());
169 catch (const std::exception
&e
)
171 SAL_WARN("registry", "Fatal exception: " << e
.what());
179 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */