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 CGenerator extends CodeGenerator {
31 
CGenerator()32   CGenerator() {
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    */
genCode(String name, ArrayList<JFile> ilist, ArrayList<JRecord> rlist, String destDir, ArrayList<String> options)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     FileWriter cc = new FileWriter(name+".c");
45     try {
46       FileWriter hh = new FileWriter(name+".h");
47       try {
48         hh.write("#ifndef __"+name.toUpperCase().replace('.','_')+"__\n");
49         hh.write("#define __"+name.toUpperCase().replace('.','_')+"__\n");
50         hh.write("#include \"recordio.h\"\n");
51         for (Iterator<JFile> iter = ilist.iterator(); iter.hasNext();) {
52           hh.write("#include \""+iter.next().getName()+".h\"\n");
53         }
54 
55         cc.write("#include \""+name+".h\"\n");
56 
57         /*
58         for (Iterator<JRecord> iter = rlist.iterator(); iter.hasNext();) {
59         iter.next().genCppCode(hh, cc);
60         }
61          */
62 
63         hh.write("#endif //"+name.toUpperCase().replace('.','_')+"__\n");
64       } finally {
65         hh.close();
66       }
67     } finally {
68       cc.close();
69     }
70   }
71 }
72