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 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_cppumaker.hxx"
26 
27 #include <stdio.h>
28 
29 #include "sal/main.h"
30 
31 #include "codemaker/typemanager.hxx"
32 #include "codemaker/generatedtypeset.hxx"
33 
34 #include "cppuoptions.hxx"
35 #include "cpputype.hxx"
36 
37 using namespace rtl;
38 
39 namespace {
40 
failed(rtl::OString const & typeName,CppuOptions * options)41 void failed(rtl::OString const & typeName, CppuOptions * options) {
42     fprintf(stderr, "%s ERROR: %s\n", options->getProgramName().getStr(),
43             rtl::OString("cannot dump Type '" + typeName + "'").getStr());
44     exit(99);
45 }
46 
produce(RegistryKey & rTypeKey,bool bIsExtraType,TypeManager const & typeMgr,codemaker::GeneratedTypeSet & generated,CppuOptions * options)47 void produce(
48     RegistryKey& rTypeKey, bool bIsExtraType, TypeManager const & typeMgr,
49     codemaker::GeneratedTypeSet & generated, CppuOptions * options)
50 {
51     if (!produceType(rTypeKey, bIsExtraType, typeMgr, generated, options)) {
52         OString typeName = typeMgr.getTypeName(rTypeKey);
53         failed(typeName, options);
54     }
55 }
56 
produce(rtl::OString const & typeName,TypeManager const & typeMgr,codemaker::GeneratedTypeSet & generated,CppuOptions * options)57 void produce(
58     rtl::OString const & typeName, TypeManager const & typeMgr,
59     codemaker::GeneratedTypeSet & generated, CppuOptions * options)
60 {
61     if (!produceType(typeName, typeMgr, generated, options)) {
62         failed(typeName, options);
63     }
64 }
65 
produceAllTypes(RegistryKey & rTypeKey,bool bIsExtraType,TypeManager const & typeMgr,codemaker::GeneratedTypeSet & generated,CppuOptions * pOptions,sal_Bool bFullScope)66 void produceAllTypes(RegistryKey& rTypeKey, bool bIsExtraType,
67 						 TypeManager const & typeMgr,
68                          codemaker::GeneratedTypeSet & generated,
69 						 CppuOptions* pOptions,
70 						 sal_Bool bFullScope)
71 	throw( CannotDumpException )
72 {
73     OString typeName = typeMgr.getTypeName(rTypeKey);
74 
75     produce(rTypeKey, bIsExtraType, typeMgr, generated, pOptions);
76 
77     RegistryKeyList typeKeys = typeMgr.getTypeKeys(typeName);
78 	RegistryKeyList::const_iterator iter = typeKeys.begin();
79     RegistryKey key, subKey;
80     RegistryKeyArray subKeys;
81 
82 	while (iter != typeKeys.end())
83 	{
84         key = (*iter).first;
85 
86         if (!(*iter).second  && !key.openSubKeys(OUString(), subKeys))
87         {
88             for (sal_uInt32 i = 0; i < subKeys.getLength(); i++)
89             {
90                 subKey = subKeys.getElement(i);
91                 if (bFullScope)
92                 {
93                     produceAllTypes(subKey, (*iter).second, typeMgr,
94                                     generated, pOptions, true);
95                 } else
96                 {
97                     produce(subKey, (*iter).second,
98                             typeMgr, generated, pOptions);
99                 }
100             }
101         }
102 
103         ++iter;
104 	}
105 }
106 
produceAllTypes(const OString & typeName,TypeManager const & typeMgr,codemaker::GeneratedTypeSet & generated,CppuOptions * pOptions,sal_Bool bFullScope)107 void produceAllTypes(const OString& typeName,
108                      TypeManager const & typeMgr,
109                      codemaker::GeneratedTypeSet & generated,
110                      CppuOptions* pOptions,
111                      sal_Bool bFullScope)
112 	throw( CannotDumpException )
113 {
114     produce(typeName, typeMgr, generated, pOptions);
115 
116     RegistryKeyList typeKeys = typeMgr.getTypeKeys(typeName);
117 	RegistryKeyList::const_iterator iter = typeKeys.begin();
118     RegistryKey key, subKey;
119     RegistryKeyArray subKeys;
120 
121 	while (iter != typeKeys.end())
122 	{
123         key = (*iter).first;
124         if (!(*iter).second  && !key.openSubKeys(OUString(), subKeys))
125         {
126             for (sal_uInt32 i = 0; i < subKeys.getLength(); i++)
127             {
128                 subKey = subKeys.getElement(i);
129                 if (bFullScope)
130                 {
131                     produceAllTypes(subKey, (*iter).second, typeMgr,
132                                     generated, pOptions, true);
133                 } else
134                 {
135                     produce(subKey, (*iter).second,
136                             typeMgr, generated, pOptions);
137                 }
138             }
139         }
140 
141         ++iter;
142 	}
143 }
144 
145 }
146 
SAL_IMPLEMENT_MAIN_WITH_ARGS(argc,argv)147 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
148 {
149 	CppuOptions options;
150 
151 	try
152 	{
153 		if (!options.initOptions(argc, argv))
154 		{
155 			exit(1);
156 		}
157 	}
158 	catch( IllegalArgument& e)
159 	{
160 		fprintf(stderr, "Illegal option: %s\n", e.m_message.getStr());
161 		exit(99);
162 	}
163 
164 	RegistryTypeManager typeMgr;
165 
166 	if (!typeMgr.init(options.getInputFiles(), options.getExtraInputFiles()))
167 	{
168 		fprintf(stderr, "%s : init registries failed, check your registry files.\n", options.getProgramName().getStr());
169 		exit(99);
170 	}
171 
172 	if (options.isValid("-B"))
173 	{
174 		typeMgr.setBase(options.getOption("-B"));
175 	}
176 
177     codemaker::GeneratedTypeSet generated;
178 	try
179 	{
180 		if (options.isValid("-T"))
181 		{
182 			OString tOption(options.getOption("-T"));
183 
184 			OString typeName, tmpName;
185             sal_Int32 nIndex = 0;
186             do
187 			{
188 				typeName = tOption.getToken(0, ';', nIndex);
189 
190                 sal_Int32 nPos = typeName.lastIndexOf( '.' );
191                 tmpName = typeName.copy( nPos != -1 ? nPos+1 : 0 );
192 				if (tmpName == "*")
193 				{
194 					// produce this type and his scope
195 					if (typeName.equals("*"))
196 					{
197 						tmpName = "/";
198 					} else
199 					{
200 						tmpName = typeName.copy(0, typeName.lastIndexOf('.')).replace('.', '/');
201 						if ( tmpName.isEmpty() )
202 							tmpName = "/";
203 						else
204 							tmpName.replace('.', '/');
205 					}
206                     // related to task #116780# the scope is recursively
207                     // generated.  bFullScope = true
208 					produceAllTypes(
209                         tmpName, typeMgr, generated, &options, true);
210 				} else
211 				{
212 					// produce only this type
213                     produce(
214                         typeName.replace('.', '/'), typeMgr, generated, &options);
215 				}
216 			} while( nIndex != -1 );
217 		} else
218 		{
219 			// produce all types
220 			produceAllTypes("/", typeMgr, generated, &options, true);
221 		}
222         // C++ header files generated for the following UNO types are included
223         // in header files in cppu/inc/com/sun/star/uno (Any.hxx, Reference.hxx,
224         // Type.h), so it seems best to always generate those C++ header files:
225         produce("com/sun/star/uno/RuntimeException", typeMgr, generated, &options);
226         produce("com/sun/star/uno/TypeClass", typeMgr, generated, &options);
227         produce("com/sun/star/uno/XInterface", typeMgr, generated, &options);
228 	}
229 	catch( CannotDumpException& e)
230 	{
231 		fprintf(stderr, "%s ERROR: %s\n",
232 				options.getProgramName().getStr(),
233 				e.m_message.getStr());
234 		exit(99);
235 	}
236 
237 	return 0;
238 }
239 
240 
241