merge the formfield patch from ooo-build
[ooovba.git] / registry / tools / regmerge.cxx
blob009882069b554b087901fe6539f9f6930f50feee
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: regmerge.cxx,v $
10 * $Revision: 1.7 $
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"
34 #include <stdio.h>
35 #include <string.h>
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>
45 #ifdef SAL_UNX
46 #define SEPARATOR '/'
47 #else
48 #define SEPARATOR '\\'
49 #endif
51 using namespace ::rtl;
52 using namespace ::osl;
54 sal_Bool isFileUrl(const OString& fileName)
56 if (fileName.indexOf("file://") == 0 )
57 return sal_True;
58 return sal_False;
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 )
72 OUString uWorkingDir;
73 if (osl_getProcessWorkingDir(&uWorkingDir.pData) != osl_Process_E_None)
75 OSL_ASSERT(false);
77 if (FileBase::getAbsoluteFileURL(uWorkingDir, uFileName, uUrlFileName)
78 != FileBase::E_None)
80 OSL_ASSERT(false);
82 } else
84 if (FileBase::getFileURLFromSystemPath(uFileName, uUrlFileName)
85 != FileBase::E_None)
87 OSL_ASSERT(false);
91 return uUrlFileName;
94 int realargc;
95 char* realargv[2048];
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)
111 FILE *commandfile;
112 char option[256];
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);
119 } else
121 while ( fscanf(commandfile, "%s", option) != EOF )
123 if (option[0]== '@')
125 bool bRet = checkCommandFile(option);
126 // ensure that the option will not be overwritten
127 if ( !bRet )
128 bVerbose = bRet;
129 } else {
130 if (option[0]== '-') {
131 if (strncmp(option, "-v", 2) == 0 ||
132 strncmp(option, "--verbose", 9) == 0)
134 bVerbose = true;
135 } else {
136 fprintf(stderr, "ERROR: unknown option \"%s\"\n", option);
137 dumpHelp();
138 exit(-1);
140 }else
142 realargv[realargc]= strdup(option);
143 realargc++;
146 if (realargc == 2047)
148 fprintf(stderr, "ERROR: more than 2048 arguments.\n");
149 break;
152 fclose(commandfile);
155 return bVerbose;
158 static bool checkCommandArgs(int argc, char **argv)
160 bool bVerbose = false;
162 realargc = 0;
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
170 if ( !bRet )
171 bVerbose = bRet;
172 } else {
173 if (argv[i][0]== '-') {
174 if (strncmp(argv[i], "-v", 2) == 0 ||
175 strncmp(argv[i], "--verbose", 9) == 0)
177 bVerbose = true;
178 } else {
179 fprintf(stderr, "ERROR: unknown option \"%s\"\n", argv[i]);
180 dumpHelp();
181 exit(-1);
183 } else
185 realargv[realargc]= strdup(argv[i]);
186 realargc++;
191 return bVerbose;
194 static void cleanCommandArgs()
196 for (int i=0; i<realargc; i++)
198 free(realargv[i]);
204 #if (defined UNX) || (defined OS2)
205 int main( int argc, char * argv[] )
206 #else
207 int _cdecl main( int argc, char * argv[] )
208 #endif
210 RegHandle hReg;
211 RegKeyHandle hRootKey;
212 bool bVerbose = checkCommandArgs(argc, argv);
214 if (realargc < 4)
216 dumpHelp();
217 cleanCommandArgs();
218 exit(1);
221 ::rtl::OUString regName( convertToFileUrl(realargv[1]) );
222 if (reg_openRegistry(regName.pData, &hReg, REG_READWRITE))
224 if (reg_createRegistry(regName.pData, &hReg))
226 if (bVerbose)
227 fprintf(stderr, "open registry \"%s\" failed\n", realargv[1]);
228 cleanCommandArgs();
229 exit(-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,
242 bVerbose);
243 if (_ret)
245 if (_ret == REG_MERGE_CONFLICT)
247 if (bVerbose)
248 fprintf(stderr, "merging registry \"%s\" under key \"%s\" in registry \"%s\".\n",
249 realargv[i], realargv[2], realargv[1]);
250 } else
252 if (bVerbose)
253 fprintf(stderr, "ERROR: merging registry \"%s\" under key \"%s\" in registry \"%s\" failed.\n",
254 realargv[i], realargv[2], realargv[1]);
255 exit(-2);
257 } else
259 if (bVerbose)
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))
267 if (bVerbose)
268 fprintf(stderr, "closing root key of registry \"%s\" failed\n",
269 realargv[1]);
270 exit(-3);
272 } else
274 if (bVerbose)
275 fprintf(stderr, "open root key of registry \"%s\" failed\n",
276 realargv[1]);
277 exit(-4);
280 if (reg_closeRegistry(hReg))
282 if (bVerbose)
283 fprintf(stderr, "closing registry \"%s\" failed\n", realargv[1]);
284 cleanCommandArgs();
285 exit(-5);
288 cleanCommandArgs();
289 return(0);