1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: regmerge.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_registry.hxx"
37 #include "registry/registry.h"
38 #include <rtl/ustring.hxx>
39 #include <rtl/alloc.h>
40 #include <osl/process.h>
41 #include <osl/diagnose.h>
42 #include <osl/thread.h>
43 #include <osl/file.hxx>
48 #define SEPARATOR '\\'
51 using namespace ::rtl
;
52 using namespace ::osl
;
54 sal_Bool
isFileUrl(const OString
& fileName
)
56 if (fileName
.indexOf("file://") == 0 )
61 OUString
convertToFileUrl(const OString
& fileName
)
63 if ( isFileUrl(fileName
) )
65 return OStringToOUString(fileName
, osl_getThreadTextEncoding());
68 OUString uUrlFileName
;
69 OUString
uFileName(fileName
.getStr(), fileName
.getLength(), osl_getThreadTextEncoding());
70 if ( fileName
.indexOf('.') == 0 || fileName
.indexOf(SEPARATOR
) < 0 )
73 if (osl_getProcessWorkingDir(&uWorkingDir
.pData
) != osl_Process_E_None
)
77 if (FileBase::getAbsoluteFileURL(uWorkingDir
, uFileName
, uUrlFileName
)
84 if (FileBase::getFileURLFromSystemPath(uFileName
, uUrlFileName
)
97 static void dumpHelp()
99 fprintf(stderr
, "using: regmerge [-v|--verbose] mergefile mergeKeyName regfile_1 ... regfile_n\n");
100 fprintf(stderr
, " regmerge @regcmds\nOptions:\n");
101 fprintf(stderr
, " -v, --verbose : verbose output on stdout.\n");
102 fprintf(stderr
, " mergefile : specifies the merged registry file. If this file doesn't exists,\n");
103 fprintf(stderr
, " it is created.\n");
104 fprintf(stderr
, " mergeKeyName : specifies the merge key, everything is merged under this key.\n");
105 fprintf(stderr
, " If this key doesn't exists, it is created.\n");
106 fprintf(stderr
, " regfile_1..n : specifies one or more registry files which are merged.\n");
109 static bool checkCommandFile(char* cmdfile
)
113 bool bVerbose
= false;
115 commandfile
= fopen(cmdfile
+1, "r");
116 if( commandfile
== NULL
)
118 fprintf(stderr
, "ERROR: Can't open command file \"%s\"\n", cmdfile
);
121 while ( fscanf(commandfile
, "%s", option
) != EOF
)
125 bool bRet
= checkCommandFile(option
);
126 // ensure that the option will not be overwritten
130 if (option
[0]== '-') {
131 if (strncmp(option
, "-v", 2) == 0 ||
132 strncmp(option
, "--verbose", 9) == 0)
136 fprintf(stderr
, "ERROR: unknown option \"%s\"\n", option
);
142 realargv
[realargc
]= strdup(option
);
146 if (realargc
== 2047)
148 fprintf(stderr
, "ERROR: more than 2048 arguments.\n");
158 static bool checkCommandArgs(int argc
, char **argv
)
160 bool bVerbose
= false;
164 for (int i
=0; i
<argc
; i
++)
166 if (argv
[i
][0]== '@')
168 bool bRet
= checkCommandFile(argv
[i
]);
169 // ensure that the option will not be overwritten
173 if (argv
[i
][0]== '-') {
174 if (strncmp(argv
[i
], "-v", 2) == 0 ||
175 strncmp(argv
[i
], "--verbose", 9) == 0)
179 fprintf(stderr
, "ERROR: unknown option \"%s\"\n", argv
[i
]);
185 realargv
[realargc
]= strdup(argv
[i
]);
194 static void cleanCommandArgs()
196 for (int i
=0; i
<realargc
; i
++)
204 #if (defined UNX) || (defined OS2)
205 int main( int argc
, char * argv
[] )
207 int _cdecl
main( int argc
, char * argv
[] )
211 RegKeyHandle hRootKey
;
212 bool bVerbose
= checkCommandArgs(argc
, argv
);
221 ::rtl::OUString
regName( convertToFileUrl(realargv
[1]) );
222 if (reg_openRegistry(regName
.pData
, &hReg
, REG_READWRITE
))
224 if (reg_createRegistry(regName
.pData
, &hReg
))
227 fprintf(stderr
, "open registry \"%s\" failed\n", realargv
[1]);
233 if (!reg_openRootKey(hReg
, &hRootKey
))
235 ::rtl::OUString
mergeKeyName( ::rtl::OUString::createFromAscii(realargv
[2]) );
236 ::rtl::OUString targetRegName
;
237 for (int i
= 3; i
< realargc
; i
++)
239 targetRegName
= convertToFileUrl(realargv
[i
]);
240 RegError _ret
= reg_mergeKey(
241 hRootKey
, mergeKeyName
.pData
, targetRegName
.pData
, sal_False
,
245 if (_ret
== REG_MERGE_CONFLICT
)
248 fprintf(stderr
, "merging registry \"%s\" under key \"%s\" in registry \"%s\".\n",
249 realargv
[i
], realargv
[2], realargv
[1]);
253 fprintf(stderr
, "ERROR: merging registry \"%s\" under key \"%s\" in registry \"%s\" failed.\n",
254 realargv
[i
], realargv
[2], realargv
[1]);
260 fprintf(stderr
, "merging registry \"%s\" under key \"%s\" in registry \"%s\".\n",
261 realargv
[i
], realargv
[2], realargv
[1]);
265 if (reg_closeKey(hRootKey
))
268 fprintf(stderr
, "closing root key of registry \"%s\" failed\n",
275 fprintf(stderr
, "open root key of registry \"%s\" failed\n",
280 if (reg_closeRegistry(hReg
))
283 fprintf(stderr
, "closing registry \"%s\" failed\n", realargv
[1]);