1*51134e9eSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*51134e9eSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*51134e9eSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*51134e9eSAndrew Rist * distributed with this work for additional information
6*51134e9eSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*51134e9eSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*51134e9eSAndrew Rist * "License"); you may not use this file except in compliance
9*51134e9eSAndrew Rist * with the License. You may obtain a copy of the License at
10*51134e9eSAndrew Rist *
11*51134e9eSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*51134e9eSAndrew Rist *
13*51134e9eSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*51134e9eSAndrew Rist * software distributed under the License is distributed on an
15*51134e9eSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*51134e9eSAndrew Rist * KIND, either express or implied. See the License for the
17*51134e9eSAndrew Rist * specific language governing permissions and limitations
18*51134e9eSAndrew Rist * under the License.
19*51134e9eSAndrew Rist *
20*51134e9eSAndrew Rist *************************************************************/
21*51134e9eSAndrew Rist
22*51134e9eSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_registry.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include "registry/registry.hxx"
28cdf0e10cSrcweir #include "fileurl.hxx"
29cdf0e10cSrcweir #include "options.hxx"
30cdf0e10cSrcweir
31cdf0e10cSrcweir #include "rtl/ustring.hxx"
32cdf0e10cSrcweir #include "osl/diagnose.h"
33cdf0e10cSrcweir
34cdf0e10cSrcweir #include <stdio.h>
35cdf0e10cSrcweir #include <string.h>
36cdf0e10cSrcweir
37cdf0e10cSrcweir using namespace rtl;
38cdf0e10cSrcweir using namespace registry::tools;
39cdf0e10cSrcweir
40cdf0e10cSrcweir class Options_Impl : public Options
41cdf0e10cSrcweir {
42cdf0e10cSrcweir bool m_bVerbose;
43cdf0e10cSrcweir
44cdf0e10cSrcweir public:
Options_Impl(char const * program)45cdf0e10cSrcweir explicit Options_Impl (char const * program)
46cdf0e10cSrcweir : Options(program), m_bVerbose(false)
47cdf0e10cSrcweir {}
isVerbose() const48cdf0e10cSrcweir bool isVerbose() const { return m_bVerbose; }
49cdf0e10cSrcweir
50cdf0e10cSrcweir protected:
51cdf0e10cSrcweir virtual void printUsage_Impl() const;
52cdf0e10cSrcweir virtual bool initOptions_Impl(std::vector< std::string > & rArgs);
53cdf0e10cSrcweir };
54cdf0e10cSrcweir
printUsage_Impl() const55cdf0e10cSrcweir void Options_Impl::printUsage_Impl() const
56cdf0e10cSrcweir {
57cdf0e10cSrcweir fprintf(stderr, "using: regmerge [-v|--verbose] mergefile mergeKeyName regfile_1 ... regfile_n\n");
58cdf0e10cSrcweir fprintf(stderr, " regmerge @regcmds\nOptions:\n");
59cdf0e10cSrcweir fprintf(stderr, " -v, --verbose : verbose output on stdout.\n");
60cdf0e10cSrcweir fprintf(stderr, " mergefile : specifies the merged registry file. If this file doesn't exists,\n");
61cdf0e10cSrcweir fprintf(stderr, " it is created.\n");
62cdf0e10cSrcweir fprintf(stderr, " mergeKeyName : specifies the merge key, everything is merged under this key.\n");
63cdf0e10cSrcweir fprintf(stderr, " If this key doesn't exists, it is created.\n");
64cdf0e10cSrcweir fprintf(stderr, " regfile_1..n : specifies one or more registry files which are merged.\n");
65cdf0e10cSrcweir }
66cdf0e10cSrcweir
initOptions_Impl(std::vector<std::string> & rArgs)67cdf0e10cSrcweir bool Options_Impl::initOptions_Impl (std::vector< std::string > & rArgs)
68cdf0e10cSrcweir {
69cdf0e10cSrcweir std::vector< std::string >::iterator first = rArgs.begin(), last = rArgs.end();
70cdf0e10cSrcweir if ((first != last) && ((*first)[0] == '-'))
71cdf0e10cSrcweir {
72cdf0e10cSrcweir std::string option(*first);
73cdf0e10cSrcweir if ((option.compare("-v") == 0) || (option.compare("--verbose") == 0))
74cdf0e10cSrcweir {
75cdf0e10cSrcweir m_bVerbose = true;
76cdf0e10cSrcweir }
77cdf0e10cSrcweir else if ((option.compare("-h") == 0) || (option.compare("-?") == 0))
78cdf0e10cSrcweir {
79cdf0e10cSrcweir return printUsage();
80cdf0e10cSrcweir }
81cdf0e10cSrcweir else
82cdf0e10cSrcweir {
83cdf0e10cSrcweir return badOption("unknown", option.c_str());
84cdf0e10cSrcweir }
85cdf0e10cSrcweir (void) rArgs.erase(first);
86cdf0e10cSrcweir }
87cdf0e10cSrcweir return true;
88cdf0e10cSrcweir }
89cdf0e10cSrcweir
90cdf0e10cSrcweir #if (defined UNX) || (defined OS2)
main(int argc,char * argv[])91cdf0e10cSrcweir int main( int argc, char * argv[] )
92cdf0e10cSrcweir #else
93cdf0e10cSrcweir int _cdecl main( int argc, char * argv[] )
94cdf0e10cSrcweir #endif
95cdf0e10cSrcweir {
96cdf0e10cSrcweir Options_Impl options(argv[0]);
97cdf0e10cSrcweir
98cdf0e10cSrcweir std::vector< std::string > args;
99cdf0e10cSrcweir for (int i = 1; i < argc; i++)
100cdf0e10cSrcweir {
101cdf0e10cSrcweir if (!Options::checkArgument(args, argv[i], strlen(argv[i])))
102cdf0e10cSrcweir {
103cdf0e10cSrcweir options.printUsage();
104cdf0e10cSrcweir return (1);
105cdf0e10cSrcweir }
106cdf0e10cSrcweir }
107cdf0e10cSrcweir if (!options.initOptions(args))
108cdf0e10cSrcweir {
109cdf0e10cSrcweir return (1);
110cdf0e10cSrcweir }
111cdf0e10cSrcweir if (args.size() < 3)
112cdf0e10cSrcweir {
113cdf0e10cSrcweir options.printUsage();
114cdf0e10cSrcweir return (1);
115cdf0e10cSrcweir }
116cdf0e10cSrcweir
117cdf0e10cSrcweir Registry reg;
118cdf0e10cSrcweir OUString regName( convertToFileUrl(args[0].c_str(), args[0].size()) );
119cdf0e10cSrcweir if (reg.open(regName, REG_READWRITE) != REG_NO_ERROR)
120cdf0e10cSrcweir {
121cdf0e10cSrcweir if (reg.create(regName) != REG_NO_ERROR)
122cdf0e10cSrcweir {
123cdf0e10cSrcweir if (options.isVerbose())
124cdf0e10cSrcweir fprintf(stderr, "open registry \"%s\" failed\n", args[0].c_str());
125cdf0e10cSrcweir return (-1);
126cdf0e10cSrcweir }
127cdf0e10cSrcweir }
128cdf0e10cSrcweir
129cdf0e10cSrcweir RegistryKey rootKey;
130cdf0e10cSrcweir if (reg.openRootKey(rootKey) != REG_NO_ERROR)
131cdf0e10cSrcweir {
132cdf0e10cSrcweir if (options.isVerbose())
133cdf0e10cSrcweir fprintf(stderr, "open root key of registry \"%s\" failed\n", args[0].c_str());
134cdf0e10cSrcweir return (-4);
135cdf0e10cSrcweir }
136cdf0e10cSrcweir
137cdf0e10cSrcweir OUString mergeKeyName( OUString::createFromAscii(args[1].c_str()) );
138cdf0e10cSrcweir for (size_t i = 2; i < args.size(); i++)
139cdf0e10cSrcweir {
140cdf0e10cSrcweir OUString targetRegName( convertToFileUrl(args[i].c_str(), args[i].size()) );
141cdf0e10cSrcweir RegError _ret = reg.mergeKey(rootKey, mergeKeyName, targetRegName, sal_False, options.isVerbose());
142cdf0e10cSrcweir if (_ret != REG_NO_ERROR)
143cdf0e10cSrcweir {
144cdf0e10cSrcweir if (_ret == REG_MERGE_CONFLICT)
145cdf0e10cSrcweir {
146cdf0e10cSrcweir if (options.isVerbose())
147cdf0e10cSrcweir fprintf(stderr, "merging registry \"%s\" under key \"%s\" in registry \"%s\".\n",
148cdf0e10cSrcweir args[i].c_str(), args[1].c_str(), args[0].c_str());
149cdf0e10cSrcweir }
150cdf0e10cSrcweir else
151cdf0e10cSrcweir {
152cdf0e10cSrcweir if (options.isVerbose())
153cdf0e10cSrcweir fprintf(stderr, "ERROR: merging registry \"%s\" under key \"%s\" in registry \"%s\" failed.\n",
154cdf0e10cSrcweir args[i].c_str(), args[1].c_str(), args[0].c_str());
155cdf0e10cSrcweir return (-2);
156cdf0e10cSrcweir }
157cdf0e10cSrcweir }
158cdf0e10cSrcweir else
159cdf0e10cSrcweir {
160cdf0e10cSrcweir if (options.isVerbose())
161cdf0e10cSrcweir fprintf(stderr, "merging registry \"%s\" under key \"%s\" in registry \"%s\".\n",
162cdf0e10cSrcweir args[i].c_str(), args[1].c_str(), args[0].c_str());
163cdf0e10cSrcweir }
164cdf0e10cSrcweir }
165cdf0e10cSrcweir
166cdf0e10cSrcweir rootKey.releaseKey();
167cdf0e10cSrcweir if (reg.close() != REG_NO_ERROR)
168cdf0e10cSrcweir {
169cdf0e10cSrcweir if (options.isVerbose())
170cdf0e10cSrcweir fprintf(stderr, "closing registry \"%s\" failed\n", args[0].c_str());
171cdf0e10cSrcweir return (-5);
172cdf0e10cSrcweir }
173cdf0e10cSrcweir
174cdf0e10cSrcweir return(0);
175cdf0e10cSrcweir }
176