1*cdf0e10cSrcweir /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2*cdf0e10cSrcweir /*************************************************************************
3*cdf0e10cSrcweir  *
4*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
7*cdf0e10cSrcweir  *
8*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
9*cdf0e10cSrcweir  *
10*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
11*cdf0e10cSrcweir  *
12*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
13*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
14*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
15*cdf0e10cSrcweir  *
16*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
17*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
20*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
21*cdf0e10cSrcweir  *
22*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
23*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
24*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
25*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
26*cdf0e10cSrcweir  *
27*cdf0e10cSrcweir  ************************************************************************/
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
30*cdf0e10cSrcweir #include "precompiled_codemaker.hxx"
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include <stdio.h>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include "sal/main.h"
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include <codemaker/typemanager.hxx>
37*cdf0e10cSrcweir #include <codemaker/dependency.hxx>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir #include "corbaoptions.hxx"
40*cdf0e10cSrcweir #include "corbatype.hxx"
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir using namespace rtl;
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir sal_Bool produceAllTypes(const OString& typeName,
45*cdf0e10cSrcweir 						TypeManager& typeMgr,
46*cdf0e10cSrcweir 						TypeDependency& typeDependencies,
47*cdf0e10cSrcweir 						CorbaOptions* pOptions,
48*cdf0e10cSrcweir 						sal_Bool bFullScope,
49*cdf0e10cSrcweir 						FileStream& o,
50*cdf0e10cSrcweir 						TypeSet* pAllreadyDumped,
51*cdf0e10cSrcweir 						TypeSet* generatedConversion)
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir 	throw( CannotDumpException )
54*cdf0e10cSrcweir {
55*cdf0e10cSrcweir 	if (!produceType(typeName, typeMgr,	typeDependencies, pOptions, o, pAllreadyDumped, generatedConversion))
56*cdf0e10cSrcweir 	{
57*cdf0e10cSrcweir 		fprintf(stderr, "%s ERROR: %s\n",
58*cdf0e10cSrcweir 				pOptions->getProgramName().getStr(),
59*cdf0e10cSrcweir 				OString("cannot dump Type '" + typeName + "'").getStr());
60*cdf0e10cSrcweir 		exit(99);
61*cdf0e10cSrcweir 	}
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir 	RegistryKey	typeKey = typeMgr.getTypeKey(typeName);
64*cdf0e10cSrcweir 	RegistryKeyNames subKeys;
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir 	if (typeKey.getKeyNames(OUString(), subKeys))
67*cdf0e10cSrcweir 		return sal_False;
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir 	OString tmpName;
70*cdf0e10cSrcweir 	for (sal_uInt32 i=0; i < subKeys.getLength(); i++)
71*cdf0e10cSrcweir 	{
72*cdf0e10cSrcweir 		tmpName = OUStringToOString(subKeys.getElement(i), RTL_TEXTENCODING_UTF8);
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir 		if (pOptions->isValid("-B"))
75*cdf0e10cSrcweir 			tmpName = tmpName.copy(tmpName.indexOf('/', 1) + 1);
76*cdf0e10cSrcweir 		else
77*cdf0e10cSrcweir 			tmpName = tmpName.copy(1);
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir 		if (bFullScope)
80*cdf0e10cSrcweir 		{
81*cdf0e10cSrcweir 			if (!produceAllTypes(tmpName, typeMgr, typeDependencies, pOptions, sal_True, o, pAllreadyDumped, generatedConversion))
82*cdf0e10cSrcweir 				return sal_False;
83*cdf0e10cSrcweir 		} else
84*cdf0e10cSrcweir 		{
85*cdf0e10cSrcweir 			if (!produceType(tmpName, typeMgr, typeDependencies, pOptions, o, pAllreadyDumped, generatedConversion))
86*cdf0e10cSrcweir 				return sal_False;
87*cdf0e10cSrcweir 		}
88*cdf0e10cSrcweir 	}
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir 	return sal_True;
91*cdf0e10cSrcweir }
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
94*cdf0e10cSrcweir {
95*cdf0e10cSrcweir 	CorbaOptions options;
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir 	try
98*cdf0e10cSrcweir 	{
99*cdf0e10cSrcweir 		if (!options.initOptions(argc, argv))
100*cdf0e10cSrcweir 		{
101*cdf0e10cSrcweir 			exit(1);
102*cdf0e10cSrcweir 		}
103*cdf0e10cSrcweir 	}
104*cdf0e10cSrcweir 	catch( IllegalArgument& e)
105*cdf0e10cSrcweir 	{
106*cdf0e10cSrcweir 		fprintf(stderr, "Illegal option: %s\n", e.m_message.getStr());
107*cdf0e10cSrcweir 		exit(99);
108*cdf0e10cSrcweir 	}
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir 	RegistryTypeManager typeMgr;
111*cdf0e10cSrcweir 	TypeDependency		typeDependencies;
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir 	if (!typeMgr.init(!options.isValid("-T"), options.getInputFiles()))
114*cdf0e10cSrcweir 	{
115*cdf0e10cSrcweir 		fprintf(stderr, "%s : init registries failed, check your registry files.\n", options.getProgramName().getStr());
116*cdf0e10cSrcweir 		exit(99);
117*cdf0e10cSrcweir 	}
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir 	if (options.isValid("-B"))
120*cdf0e10cSrcweir 	{
121*cdf0e10cSrcweir 		typeMgr.setBase(options.getOption("-B"));
122*cdf0e10cSrcweir 	}
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir 	try
125*cdf0e10cSrcweir 	{
126*cdf0e10cSrcweir 		TypeSet generatedConversion;
127*cdf0e10cSrcweir 		FileStream cppFile;
128*cdf0e10cSrcweir 		OString outPath;
129*cdf0e10cSrcweir 		if (options.isValid("-O"))
130*cdf0e10cSrcweir 			outPath = options.getOption("-O");
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir 		cppFile.open(outPath);
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir 		if(!cppFile.isValid())
135*cdf0e10cSrcweir 		{
136*cdf0e10cSrcweir 			OString message("cannot open ");
137*cdf0e10cSrcweir 			message += outPath + " for writing";
138*cdf0e10cSrcweir 			throw CannotDumpException(message);
139*cdf0e10cSrcweir 		}
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir 		if (options.isValid("-H"))
142*cdf0e10cSrcweir 		{
143*cdf0e10cSrcweir 			OString corbaHeader = options.getOption("-H");
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir 			cppFile << "#include <"
146*cdf0e10cSrcweir 					<< corbaHeader
147*cdf0e10cSrcweir 					<< ">\n\n";
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir 			CorbaType::dumpDefaultHxxIncludes(cppFile);
150*cdf0e10cSrcweir 			cppFile << "\n";
151*cdf0e10cSrcweir 		}
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir 		if (options.isValid("-T"))
154*cdf0e10cSrcweir 		{
155*cdf0e10cSrcweir 			OString tOption(options.getOption("-T"));
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir 			OString typeName, tmpName;
158*cdf0e10cSrcweir 			sal_Bool ret = sal_False;
159*cdf0e10cSrcweir             sal_Int32 nIndex = 0;
160*cdf0e10cSrcweir 			do
161*cdf0e10cSrcweir 			{
162*cdf0e10cSrcweir 				typeName = tOption.getToken(0, ';', nIndex);
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir                 sal_Int32 nPos = typeName.lastIndexOf( '.' );
165*cdf0e10cSrcweir                 tmpName = typeName.copy( nPos != -1 ? nPos+1 : 0 );
166*cdf0e10cSrcweir 				if (tmpName == "*")
167*cdf0e10cSrcweir 				{
168*cdf0e10cSrcweir 					// produce this type and his scope, but the scope is not recursively  generated.
169*cdf0e10cSrcweir 					if (typeName.equals("*"))
170*cdf0e10cSrcweir 					{
171*cdf0e10cSrcweir 						tmpName = "/";
172*cdf0e10cSrcweir 					} else
173*cdf0e10cSrcweir 					{
174*cdf0e10cSrcweir 						tmpName = typeName.copy(0, typeName.lastIndexOf('.')).replace('.', '/');
175*cdf0e10cSrcweir 						if (tmpName.getLength() == 0)
176*cdf0e10cSrcweir 							tmpName = "/";
177*cdf0e10cSrcweir 						else
178*cdf0e10cSrcweir 							tmpName.replace('.', '/');
179*cdf0e10cSrcweir 					}
180*cdf0e10cSrcweir 					ret = produceAllTypes(tmpName, typeMgr, typeDependencies, &options, sal_False, cppFile, NULL, &generatedConversion);
181*cdf0e10cSrcweir 				} else
182*cdf0e10cSrcweir 				{
183*cdf0e10cSrcweir 					// produce only this type
184*cdf0e10cSrcweir 					ret = produceType(typeName.replace('.', '/'), typeMgr, typeDependencies, &options, cppFile, NULL, &generatedConversion);
185*cdf0e10cSrcweir 				}
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir 				if (!ret)
188*cdf0e10cSrcweir 				{
189*cdf0e10cSrcweir 					fprintf(stderr, "%s ERROR: %s\n",
190*cdf0e10cSrcweir 							options.getProgramName().getStr(),
191*cdf0e10cSrcweir 							OString("cannot dump Type '" + typeName + "'").getStr());
192*cdf0e10cSrcweir 					exit(99);
193*cdf0e10cSrcweir 				}
194*cdf0e10cSrcweir 			} while( nIndex != -1 );
195*cdf0e10cSrcweir 		} else
196*cdf0e10cSrcweir 		{
197*cdf0e10cSrcweir 			// produce all types
198*cdf0e10cSrcweir 			if (!produceAllTypes("/", typeMgr, typeDependencies, &options, sal_True, cppFile, NULL, &generatedConversion))
199*cdf0e10cSrcweir 			{
200*cdf0e10cSrcweir 				fprintf(stderr, "%s ERROR: %s\n",
201*cdf0e10cSrcweir 						options.getProgramName().getStr(),
202*cdf0e10cSrcweir 						"an error occurs while dumping all types.");
203*cdf0e10cSrcweir 				exit(99);
204*cdf0e10cSrcweir 			}
205*cdf0e10cSrcweir 		}
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir 		cppFile << "namespace bonobobridge {\n"
208*cdf0e10cSrcweir 			    << "const ConversionInfo* get_conversion_functions() {\n"
209*cdf0e10cSrcweir 				<< "  static ConversionInfo allFunctions[" << generatedConversion.size()+1<< "] = {\n";
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir 		for (TypeSet::iterator iter = generatedConversion.begin(); iter != generatedConversion.end(); iter++)
212*cdf0e10cSrcweir 		{
213*cdf0e10cSrcweir 			cppFile << "    {\"" << (*iter).getStr() << "\""
214*cdf0e10cSrcweir 				    << ", &TC_" << (*iter).replace('/','_').getStr() << "_struct"
215*cdf0e10cSrcweir 					<< ", sizeof(" << (*iter).replace('/','_').getStr() << ")"
216*cdf0e10cSrcweir 					<< ", convert_b2u_" << (*iter).replace('/','_').getStr()
217*cdf0e10cSrcweir 					<< ", convert_u2b_" <<  (*iter).replace('/','_').getStr()
218*cdf0e10cSrcweir 					<< " },\n";
219*cdf0e10cSrcweir 		}
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir 		cppFile << "    {NULL, NULL, 0 , NULL, NULL} };\n"
222*cdf0e10cSrcweir 				<< "  return allFunctions;\n"
223*cdf0e10cSrcweir 				<< "}\n"
224*cdf0e10cSrcweir 				<< "}; // namespace bonobobridge\n";
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir 		cppFile.close();
227*cdf0e10cSrcweir 	}
228*cdf0e10cSrcweir 	catch( CannotDumpException& e)
229*cdf0e10cSrcweir 	{
230*cdf0e10cSrcweir 		fprintf(stderr, "%s ERROR: %s\n",
231*cdf0e10cSrcweir 				options.getProgramName().getStr(),
232*cdf0e10cSrcweir 				e.m_message.getStr());
233*cdf0e10cSrcweir 		exit(99);
234*cdf0e10cSrcweir 	}
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir 	return 0;
237*cdf0e10cSrcweir }
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir 
240