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