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