nss: upgrade to release 3.73
[LibreOffice.git] / registry / tools / regmerge.cxx
blob409773a7017d78715738502791bd65feea1aaf1f
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 .
21 #include <registry/registry.hxx>
22 #include "fileurl.hxx"
23 #include "options.hxx"
25 #include <rtl/ustring.hxx>
26 #include <sal/log.hxx>
28 #include <stdio.h>
29 #include <string.h>
31 using namespace registry::tools;
33 namespace {
35 class Options_Impl : public Options
37 bool m_bVerbose;
39 public:
40 explicit Options_Impl (char const * program)
41 : Options(program), m_bVerbose(false)
43 bool isVerbose() const { return m_bVerbose; }
45 protected:
46 virtual void printUsage_Impl() const override;
47 virtual bool initOptions_Impl(std::vector< std::string > & rArgs) override;
52 void Options_Impl::printUsage_Impl() const
54 fprintf(stderr, "using: regmerge [-v|--verbose] mergefile mergeKeyName regfile_1 ... regfile_n\n");
55 fprintf(stderr, " regmerge @regcmds\nOptions:\n");
56 fprintf(stderr, " -v, --verbose : verbose output on stdout.\n");
57 fprintf(stderr, " mergefile : specifies the merged registry file. If this file doesn't exists,\n");
58 fprintf(stderr, " it is created.\n");
59 fprintf(stderr, " mergeKeyName : specifies the merge key, everything is merged under this key.\n");
60 fprintf(stderr, " If this key doesn't exists, it is created.\n");
61 fprintf(stderr, " regfile_1..n : specifies one or more registry files which are merged.\n");
64 bool Options_Impl::initOptions_Impl (std::vector< std::string > & rArgs)
66 std::vector< std::string >::iterator first = rArgs.begin(), last = rArgs.end();
67 if ((first != last) && ((*first)[0] == '-'))
69 std::string option(*first);
70 if ((option.compare("-v") == 0) || (option.compare("--verbose") == 0))
72 m_bVerbose = true;
74 else if ((option.compare("-h") == 0) || (option.compare("-?") == 0))
76 return printUsage();
78 else
80 return badOption("unknown", option.c_str());
82 (void) rArgs.erase(first);
84 return true;
87 #if (defined UNX)
88 int main( int argc, char * argv[] )
89 #else
90 int __cdecl main( int argc, char * argv[] )
91 #endif
93 try
95 Options_Impl options(argv[0]);
97 std::vector< std::string > args;
98 for (int i = 1; i < argc; i++)
100 if (!Options::checkArgument(args, argv[i], strlen(argv[i])))
102 options.printUsage();
103 return 1;
106 if (!options.initOptions(args))
108 return 1;
110 if (args.size() < 3)
112 options.printUsage();
113 return 1;
116 Registry reg;
117 OUString regName( convertToFileUrl(args[0].c_str(), args[0].size()) );
118 if (reg.open(regName, RegAccessMode::READWRITE) != RegError::NO_ERROR)
120 if (reg.create(regName) != RegError::NO_ERROR)
122 if (options.isVerbose())
123 fprintf(stderr, "open registry \"%s\" failed\n", args[0].c_str());
124 return -1;
128 RegistryKey rootKey;
129 if (reg.openRootKey(rootKey) != RegError::NO_ERROR)
131 if (options.isVerbose())
132 fprintf(stderr, "open root key of registry \"%s\" failed\n", args[0].c_str());
133 return -4;
136 OUString mergeKeyName( OUString::createFromAscii(args[1].c_str()) );
137 for (size_t i = 2; i < args.size(); i++)
139 OUString targetRegName( convertToFileUrl(args[i].c_str(), args[i].size()) );
140 RegError _ret = reg.mergeKey(rootKey, mergeKeyName, targetRegName, options.isVerbose());
141 if (_ret != RegError::NO_ERROR)
143 if (_ret == RegError::MERGE_CONFLICT)
145 if (options.isVerbose())
146 fprintf(stderr, "merging registry \"%s\" under key \"%s\" in registry \"%s\".\n",
147 args[i].c_str(), args[1].c_str(), args[0].c_str());
149 else
151 if (options.isVerbose())
152 fprintf(stderr, "ERROR: merging registry \"%s\" under key \"%s\" in registry \"%s\" failed.\n",
153 args[i].c_str(), args[1].c_str(), args[0].c_str());
154 return -2;
157 else
159 if (options.isVerbose())
160 fprintf(stderr, "merging registry \"%s\" under key \"%s\" in registry \"%s\".\n",
161 args[i].c_str(), args[1].c_str(), args[0].c_str());
165 rootKey.releaseKey();
166 if (reg.close() != RegError::NO_ERROR)
168 if (options.isVerbose())
169 fprintf(stderr, "closing registry \"%s\" failed\n", args[0].c_str());
170 return -5;
173 catch (const std::exception &e)
175 SAL_WARN("registry", "Fatal exception: " << e.what());
176 return -5;
180 return 0;
183 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */