CGenerator.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. package com.yahoo.jute.compiler;
  19. import java.util.ArrayList;
  20. import java.io.File;
  21. import java.io.FileWriter;
  22. import java.io.IOException;
  23. import java.util.Iterator;
  24. /**
  25. * C++ Code generator front-end for Hadoop record I/O.
  26. *
  27. * @author Milind Bhandarkar
  28. */
  29. class CGenerator {
  30. private String mFullName;
  31. private String mName;
  32. private ArrayList mInclFiles;
  33. private ArrayList mRecList;
  34. /** Creates a new instance of CppGenerator
  35. *
  36. * @param name possibly full pathname to the file
  37. * @param ilist included files (as JFile)
  38. * @param rlist List of records defined within this file
  39. */
  40. CGenerator(String name, ArrayList ilist, ArrayList rlist) {
  41. mFullName = name;
  42. mName = (new File(name)).getName();
  43. mInclFiles = ilist;
  44. mRecList = rlist;
  45. }
  46. /**
  47. * Generate C++ code. This method only creates the requested file(s)
  48. * and spits-out file-level elements (such as include statements etc.)
  49. * record-level code is generated by JRecord.
  50. */
  51. void genCode() throws IOException {
  52. FileWriter c = new FileWriter(mName+".c");
  53. FileWriter h = new FileWriter(mName+".h");
  54. h.write("#ifndef __"+mName.toUpperCase().replace('.','_')+"__\n");
  55. h.write("#define __"+mName.toUpperCase().replace('.','_')+"__\n");
  56. h.write("#include \"recordio.h\"\n");
  57. for (Iterator i = mInclFiles.iterator(); i.hasNext();) {
  58. JFile f = (JFile) i.next();
  59. h.write("#include \""+f.getName()+".h\"\n");
  60. }
  61. // required for compilation from C++
  62. h.write("\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n");
  63. c.write("#include <stdlib.h>\n"); // need it for calloc() & free()
  64. c.write("#include \""+mName+".h\"\n\n");
  65. for (Iterator i = mRecList.iterator(); i.hasNext();) {
  66. JRecord jr = (JRecord) i.next();
  67. jr.genCCode(h, c);
  68. }
  69. h.write("\n#ifdef __cplusplus\n}\n#endif\n\n");
  70. h.write("#endif //"+mName.toUpperCase().replace('.','_')+"__\n");
  71. h.close();
  72. c.close();
  73. }
  74. }