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