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