[120] | 1 | /** |
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
| 3 | * or more contributor license agreements. See the NOTICE file |
---|
| 4 | * distributed with this work for additional information |
---|
| 5 | * regarding copyright ownership. The ASF licenses this file |
---|
| 6 | * to you under the Apache License, Version 2.0 (the |
---|
| 7 | * "License"); you may not use this file except in compliance |
---|
| 8 | * with the License. You may obtain a copy of the License at |
---|
| 9 | * |
---|
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 11 | * |
---|
| 12 | * Unless required by applicable law or agreed to in writing, software |
---|
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 15 | * See the License for the specific language governing permissions and |
---|
| 16 | * limitations under the License. |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | package org.apache.hadoop.record.compiler; |
---|
| 20 | |
---|
| 21 | import java.util.ArrayList; |
---|
| 22 | import java.io.File; |
---|
| 23 | import java.io.FileWriter; |
---|
| 24 | import java.io.IOException; |
---|
| 25 | import java.util.Iterator; |
---|
| 26 | |
---|
| 27 | /** |
---|
| 28 | * C++ Code generator front-end for Hadoop record I/O. |
---|
| 29 | */ |
---|
| 30 | class CppGenerator extends CodeGenerator { |
---|
| 31 | |
---|
| 32 | CppGenerator() { |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | /** |
---|
| 36 | * Generate C++ code. This method only creates the requested file(s) |
---|
| 37 | * and spits-out file-level elements (such as include statements etc.) |
---|
| 38 | * record-level code is generated by JRecord. |
---|
| 39 | */ |
---|
| 40 | void genCode(String name, ArrayList<JFile> ilist, |
---|
| 41 | ArrayList<JRecord> rlist, String destDir, ArrayList<String> options) |
---|
| 42 | throws IOException { |
---|
| 43 | name = new File(destDir, (new File(name)).getName()).getAbsolutePath(); |
---|
| 44 | |
---|
| 45 | FileWriter cc = new FileWriter(name+".cc"); |
---|
| 46 | try { |
---|
| 47 | FileWriter hh = new FileWriter(name+".hh"); |
---|
| 48 | |
---|
| 49 | try { |
---|
| 50 | String fileName = (new File(name)).getName(); |
---|
| 51 | hh.write("#ifndef __"+fileName.toUpperCase().replace('.','_')+"__\n"); |
---|
| 52 | hh.write("#define __"+fileName.toUpperCase().replace('.','_')+"__\n"); |
---|
| 53 | hh.write("#include \"recordio.hh\"\n"); |
---|
| 54 | hh.write("#include \"recordTypeInfo.hh\"\n"); |
---|
| 55 | for (Iterator<JFile> iter = ilist.iterator(); iter.hasNext();) { |
---|
| 56 | hh.write("#include \""+iter.next().getName()+".hh\"\n"); |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | cc.write("#include \""+fileName+".hh\"\n"); |
---|
| 60 | cc.write("#include \"utils.hh\"\n"); |
---|
| 61 | |
---|
| 62 | for (Iterator<JRecord> iter = rlist.iterator(); iter.hasNext();) { |
---|
| 63 | iter.next().genCppCode(hh, cc, options); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | hh.write("#endif //"+fileName.toUpperCase().replace('.','_')+"__\n"); |
---|
| 67 | } finally { |
---|
| 68 | hh.close(); |
---|
| 69 | } |
---|
| 70 | } finally { |
---|
| 71 | cc.close(); |
---|
| 72 | } |
---|
| 73 | } |
---|
| 74 | } |
---|