/** * Generate C++ code. This method only creates the requested file(s) and spits-out file-level * elements (such as include statements etc.) record-level code is generated by JRecord. */ void genCode() throws IOException { outputDirectory.mkdirs(); FileWriter c = new FileWriter(new File(outputDirectory, mName + ".c")); FileWriter h = new FileWriter(new File(outputDirectory, mName + ".h")); h.write("#ifndef __" + mName.toUpperCase().replace('.', '_') + "__\n"); h.write("#define __" + mName.toUpperCase().replace('.', '_') + "__\n"); h.write("#include \"recordio.h\"\n"); for (Iterator i = mInclFiles.iterator(); i.hasNext(); ) { JFile f = (JFile) i.next(); h.write("#include \"" + f.getName() + ".h\"\n"); } // required for compilation from C++ h.write("\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n"); c.write("#include <stdlib.h>\n"); // need it for calloc() & free() c.write("#include \"" + mName + ".h\"\n\n"); for (Iterator i = mRecList.iterator(); i.hasNext(); ) { JRecord jr = (JRecord) i.next(); jr.genCCode(h, c); } h.write("\n#ifdef __cplusplus\n}\n#endif\n\n"); h.write("#endif //" + mName.toUpperCase().replace('.', '_') + "__\n"); h.close(); c.close(); }
public static void main(String args[]) { String language = "java"; ArrayList recFiles = new ArrayList(); JFile curFile = null; for (int i = 0; i < args.length; i++) { if ("-l".equalsIgnoreCase(args[i]) || "--language".equalsIgnoreCase(args[i])) { language = args[i + 1].toLowerCase(); i++; } else { recFiles.add(args[i]); } } if (!"c++".equals(language) && !"java".equals(language) && !"c".equals(language)) { System.out.println("Cannot recognize language:" + language); System.exit(1); } if (recFiles.size() == 0) { System.out.println("No record files specified. Exiting."); System.exit(1); } for (int i = 0; i < recFiles.size(); i++) { curFileName = (String) recFiles.get(i); File file = new File(curFileName); try { curFile = parseFile(file); } catch (FileNotFoundException e) { System.out.println("File " + (String) recFiles.get(i) + " Not found."); System.exit(1); } catch (ParseException e) { System.out.println(e.toString()); System.exit(1); } System.out.println((String) recFiles.get(i) + " Parsed Successfully"); try { curFile.genCode(language, new File(".")); } catch (IOException e) { System.out.println(e.toString()); System.exit(1); } } }