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