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