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