xref: /aoo42x/main/registry/tools/regmerge.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_registry.hxx"
30 
31 #include "registry/registry.hxx"
32 #include "fileurl.hxx"
33 #include "options.hxx"
34 
35 #include "rtl/ustring.hxx"
36 #include "osl/diagnose.h"
37 
38 #include <stdio.h>
39 #include <string.h>
40 
41 using namespace rtl;
42 using namespace registry::tools;
43 
44 class Options_Impl : public Options
45 {
46     bool m_bVerbose;
47 
48 public:
49     explicit Options_Impl (char const * program)
50         : Options(program), m_bVerbose(false)
51     {}
52     bool isVerbose() const { return m_bVerbose; }
53 
54 protected:
55     virtual void printUsage_Impl() const;
56     virtual bool initOptions_Impl(std::vector< std::string > & rArgs);
57 };
58 
59 void Options_Impl::printUsage_Impl() const
60 {
61     fprintf(stderr, "using: regmerge [-v|--verbose] mergefile mergeKeyName regfile_1 ... regfile_n\n");
62     fprintf(stderr, "       regmerge @regcmds\nOptions:\n");
63     fprintf(stderr, "  -v, --verbose : verbose output on stdout.\n");
64     fprintf(stderr, "  mergefile     : specifies the merged registry file. If this file doesn't exists,\n");
65     fprintf(stderr, "                  it is created.\n");
66     fprintf(stderr, "  mergeKeyName  : specifies the merge key, everything is merged under this key.\n");
67     fprintf(stderr, "                  If this key doesn't exists, it is created.\n");
68     fprintf(stderr, "  regfile_1..n  : specifies one or more registry files which are merged.\n");
69 }
70 
71 bool Options_Impl::initOptions_Impl (std::vector< std::string > & rArgs)
72 {
73     std::vector< std::string >::iterator first = rArgs.begin(), last = rArgs.end();
74     if ((first != last) && ((*first)[0] == '-'))
75     {
76         std::string option(*first);
77         if ((option.compare("-v") == 0) || (option.compare("--verbose") == 0))
78         {
79             m_bVerbose = true;
80         }
81         else if ((option.compare("-h") == 0) || (option.compare("-?") == 0))
82         {
83             return printUsage();
84         }
85         else
86         {
87             return badOption("unknown", option.c_str());
88         }
89         (void) rArgs.erase(first);
90     }
91     return true;
92 }
93 
94 #if (defined UNX) || (defined OS2)
95 int main( int argc, char * argv[] )
96 #else
97 int _cdecl main( int argc, char * argv[] )
98 #endif
99 {
100     Options_Impl options(argv[0]);
101 
102     std::vector< std::string > args;
103     for (int i = 1; i < argc; i++)
104     {
105         if (!Options::checkArgument(args, argv[i], strlen(argv[i])))
106         {
107             options.printUsage();
108             return (1);
109         }
110     }
111     if (!options.initOptions(args))
112     {
113         return (1);
114     }
115     if (args.size() < 3)
116 	{
117         options.printUsage();
118 		return (1);
119 	}
120 
121 	Registry reg;
122 	OUString regName( convertToFileUrl(args[0].c_str(), args[0].size()) );
123 	if (reg.open(regName, REG_READWRITE) != REG_NO_ERROR)
124 	{
125 		if (reg.create(regName) != REG_NO_ERROR)
126 		{
127             if (options.isVerbose())
128                 fprintf(stderr, "open registry \"%s\" failed\n", args[0].c_str());
129 			return (-1);
130 		}
131 	}
132 
133 	RegistryKey rootKey;
134 	if (reg.openRootKey(rootKey) != REG_NO_ERROR)
135 	{
136         if (options.isVerbose())
137             fprintf(stderr, "open root key of registry \"%s\" failed\n", args[0].c_str());
138 		return (-4);
139     }
140 
141     OUString mergeKeyName( OUString::createFromAscii(args[1].c_str()) );
142     for (size_t i = 2; i < args.size(); i++)
143     {
144         OUString targetRegName( convertToFileUrl(args[i].c_str(), args[i].size()) );
145         RegError _ret = reg.mergeKey(rootKey, mergeKeyName, targetRegName, sal_False, options.isVerbose());
146         if (_ret != REG_NO_ERROR)
147         {
148             if (_ret == REG_MERGE_CONFLICT)
149             {
150                 if (options.isVerbose())
151                     fprintf(stderr, "merging registry \"%s\" under key \"%s\" in registry \"%s\".\n",
152                             args[i].c_str(), args[1].c_str(), args[0].c_str());
153             }
154             else
155             {
156                 if (options.isVerbose())
157                     fprintf(stderr, "ERROR: merging registry \"%s\" under key \"%s\" in registry \"%s\" failed.\n",
158                             args[i].c_str(), args[1].c_str(), args[0].c_str());
159                 return (-2);
160             }
161         }
162         else
163         {
164             if (options.isVerbose())
165                 fprintf(stderr, "merging registry \"%s\" under key \"%s\" in registry \"%s\".\n",
166                         args[i].c_str(), args[1].c_str(), args[0].c_str());
167         }
168     }
169 
170     rootKey.releaseKey();
171 	if (reg.close() != REG_NO_ERROR)
172 	{
173         if (options.isVerbose())
174             fprintf(stderr, "closing registry \"%s\" failed\n", args[0].c_str());
175         return (-5);
176 	}
177 
178 	return(0);
179 }
180