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 // MARKER(update_precomp.py): autogen include statement, do not remove
23 #include "precompiled_codemaker.hxx"
24
25 #include <stdio.h>
26
27 #include "sal/main.h"
28
29 #include <codemaker/typemanager.hxx>
30 #include <codemaker/dependency.hxx>
31
32 #include "corbaoptions.hxx"
33 #include "corbatype.hxx"
34
35 using namespace rtl;
36
produceAllTypes(const OString & typeName,TypeManager & typeMgr,TypeDependency & typeDependencies,CorbaOptions * pOptions,sal_Bool bFullScope,FileStream & o,TypeSet * pAllreadyDumped,TypeSet * generatedConversion)37 sal_Bool produceAllTypes(const OString& typeName,
38 TypeManager& typeMgr,
39 TypeDependency& typeDependencies,
40 CorbaOptions* pOptions,
41 sal_Bool bFullScope,
42 FileStream& o,
43 TypeSet* pAllreadyDumped,
44 TypeSet* generatedConversion)
45
46 throw( CannotDumpException )
47 {
48 if (!produceType(typeName, typeMgr, typeDependencies, pOptions, o, pAllreadyDumped, generatedConversion))
49 {
50 fprintf(stderr, "%s ERROR: %s\n",
51 pOptions->getProgramName().getStr(),
52 OString("cannot dump Type '" + typeName + "'").getStr());
53 exit(99);
54 }
55
56 RegistryKey typeKey = typeMgr.getTypeKey(typeName);
57 RegistryKeyNames subKeys;
58
59 if (typeKey.getKeyNames(OUString(), subKeys))
60 return sal_False;
61
62 OString tmpName;
63 for (sal_uInt32 i=0; i < subKeys.getLength(); i++)
64 {
65 tmpName = OUStringToOString(subKeys.getElement(i), RTL_TEXTENCODING_UTF8);
66
67 if (pOptions->isValid("-B"))
68 tmpName = tmpName.copy(tmpName.indexOf('/', 1) + 1);
69 else
70 tmpName = tmpName.copy(1);
71
72 if (bFullScope)
73 {
74 if (!produceAllTypes(tmpName, typeMgr, typeDependencies, pOptions, sal_True, o, pAllreadyDumped, generatedConversion))
75 return sal_False;
76 } else
77 {
78 if (!produceType(tmpName, typeMgr, typeDependencies, pOptions, o, pAllreadyDumped, generatedConversion))
79 return sal_False;
80 }
81 }
82
83 return sal_True;
84 }
85
SAL_IMPLEMENT_MAIN_WITH_ARGS(argc,argv)86 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
87 {
88 CorbaOptions options;
89
90 try
91 {
92 if (!options.initOptions(argc, argv))
93 {
94 exit(1);
95 }
96 }
97 catch( IllegalArgument& e)
98 {
99 fprintf(stderr, "Illegal option: %s\n", e.m_message.getStr());
100 exit(99);
101 }
102
103 RegistryTypeManager typeMgr;
104 TypeDependency typeDependencies;
105
106 if (!typeMgr.init(!options.isValid("-T"), options.getInputFiles()))
107 {
108 fprintf(stderr, "%s : init registries failed, check your registry files.\n", options.getProgramName().getStr());
109 exit(99);
110 }
111
112 if (options.isValid("-B"))
113 {
114 typeMgr.setBase(options.getOption("-B"));
115 }
116
117 try
118 {
119 TypeSet generatedConversion;
120 FileStream cppFile;
121 OString outPath;
122 if (options.isValid("-O"))
123 outPath = options.getOption("-O");
124
125 cppFile.open(outPath);
126
127 if(!cppFile.isValid())
128 {
129 OString message("cannot open ");
130 message += outPath + " for writing";
131 throw CannotDumpException(message);
132 }
133
134 if (options.isValid("-H"))
135 {
136 OString corbaHeader = options.getOption("-H");
137
138 cppFile << "#include <"
139 << corbaHeader
140 << ">\n\n";
141
142 CorbaType::dumpDefaultHxxIncludes(cppFile);
143 cppFile << "\n";
144 }
145
146 if (options.isValid("-T"))
147 {
148 OString tOption(options.getOption("-T"));
149
150 OString typeName, tmpName;
151 sal_Bool ret = sal_False;
152 sal_Int32 nIndex = 0;
153 do
154 {
155 typeName = tOption.getToken(0, ';', nIndex);
156
157 sal_Int32 nPos = typeName.lastIndexOf( '.' );
158 tmpName = typeName.copy( nPos != -1 ? nPos+1 : 0 );
159 if (tmpName == "*")
160 {
161 // produce this type and his scope, but the scope is not recursively generated.
162 if (typeName.equals("*"))
163 {
164 tmpName = "/";
165 } else
166 {
167 tmpName = typeName.copy(0, typeName.lastIndexOf('.')).replace('.', '/');
168 if ( tmpName.isEmpty() )
169 tmpName = "/";
170 else
171 tmpName.replace('.', '/');
172 }
173 ret = produceAllTypes(tmpName, typeMgr, typeDependencies, &options, sal_False, cppFile, NULL, &generatedConversion);
174 } else
175 {
176 // produce only this type
177 ret = produceType(typeName.replace('.', '/'), typeMgr, typeDependencies, &options, cppFile, NULL, &generatedConversion);
178 }
179
180 if (!ret)
181 {
182 fprintf(stderr, "%s ERROR: %s\n",
183 options.getProgramName().getStr(),
184 OString("cannot dump Type '" + typeName + "'").getStr());
185 exit(99);
186 }
187 } while( nIndex != -1 );
188 } else
189 {
190 // produce all types
191 if (!produceAllTypes("/", typeMgr, typeDependencies, &options, sal_True, cppFile, NULL, &generatedConversion))
192 {
193 fprintf(stderr, "%s ERROR: %s\n",
194 options.getProgramName().getStr(),
195 "an error occurs while dumping all types.");
196 exit(99);
197 }
198 }
199
200 cppFile << "namespace bonobobridge {\n"
201 << "const ConversionInfo* get_conversion_functions() {\n"
202 << " static ConversionInfo allFunctions[" << generatedConversion.size()+1<< "] = {\n";
203
204 for (TypeSet::iterator iter = generatedConversion.begin(); iter != generatedConversion.end(); iter++)
205 {
206 cppFile << " {\"" << (*iter).getStr() << "\""
207 << ", &TC_" << (*iter).replace('/','_').getStr() << "_struct"
208 << ", sizeof(" << (*iter).replace('/','_').getStr() << ")"
209 << ", convert_b2u_" << (*iter).replace('/','_').getStr()
210 << ", convert_u2b_" << (*iter).replace('/','_').getStr()
211 << " },\n";
212 }
213
214 cppFile << " {NULL, NULL, 0 , NULL, NULL} };\n"
215 << " return allFunctions;\n"
216 << "}\n"
217 << "}; // namespace bonobobridge\n";
218
219 cppFile.close();
220 }
221 catch( CannotDumpException& e)
222 {
223 fprintf(stderr, "%s ERROR: %s\n",
224 options.getProgramName().getStr(),
225 e.m_message.getStr());
226 exit(99);
227 }
228
229 return 0;
230 }
231
232 /* vim: set noet sw=4 ts=4: */
233