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