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 "osl/diagnose.h"
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 SAL_OVERRIDE
;
45 virtual bool initOptions_Impl(std::vector
< std::string
> & rArgs
) SAL_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
[] )
89 Options_Impl
options(argv
[0]);
91 std::vector
< std::string
> args
;
92 for (int i
= 1; i
< argc
; i
++)
94 if (!Options::checkArgument(args
, argv
[i
], strlen(argv
[i
])))
100 if (!options
.initOptions(args
))
106 options
.printUsage();
111 OUString
regName( convertToFileUrl(args
[0].c_str(), args
[0].size()) );
112 if (reg
.open(regName
, RegAccessMode::READWRITE
) != RegError::NO_ERROR
)
114 if (reg
.create(regName
) != RegError::NO_ERROR
)
116 if (options
.isVerbose())
117 fprintf(stderr
, "open registry \"%s\" failed\n", args
[0].c_str());
123 if (reg
.openRootKey(rootKey
) != RegError::NO_ERROR
)
125 if (options
.isVerbose())
126 fprintf(stderr
, "open root key of registry \"%s\" failed\n", args
[0].c_str());
130 OUString
mergeKeyName( OUString::createFromAscii(args
[1].c_str()) );
131 for (size_t i
= 2; i
< args
.size(); i
++)
133 OUString
targetRegName( convertToFileUrl(args
[i
].c_str(), args
[i
].size()) );
134 RegError _ret
= reg
.mergeKey(rootKey
, mergeKeyName
, targetRegName
, false, options
.isVerbose());
135 if (_ret
!= RegError::NO_ERROR
)
137 if (_ret
== RegError::MERGE_CONFLICT
)
139 if (options
.isVerbose())
140 fprintf(stderr
, "merging registry \"%s\" under key \"%s\" in registry \"%s\".\n",
141 args
[i
].c_str(), args
[1].c_str(), args
[0].c_str());
145 if (options
.isVerbose())
146 fprintf(stderr
, "ERROR: merging registry \"%s\" under key \"%s\" in registry \"%s\" failed.\n",
147 args
[i
].c_str(), args
[1].c_str(), args
[0].c_str());
153 if (options
.isVerbose())
154 fprintf(stderr
, "merging registry \"%s\" under key \"%s\" in registry \"%s\".\n",
155 args
[i
].c_str(), args
[1].c_str(), args
[0].c_str());
159 rootKey
.releaseKey();
160 if (reg
.close() != RegError::NO_ERROR
)
162 if (options
.isVerbose())
163 fprintf(stderr
, "closing registry \"%s\" failed\n", args
[0].c_str());
170 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */