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