| 30 | #include <unistd.h> |
| 31 | |
| 32 | bool dnnc::cppCodeGen::write() { |
| 33 | |
| 34 | inferDataType typeInference(_graph); |
| 35 | typeInference.main(); |
| 36 | |
| 37 | std::string code = ""; |
| 38 | |
| 39 | for (dnnParameters param : _graph.parameters()) { |
| 40 | code += write(param); |
| 41 | } |
| 42 | size_t argv_index = 1; |
| 43 | for (ioNode *term : _graph.inputs()) { |
| 44 | code += write(*term, argv_index); |
| 45 | } |
| 46 | |
| 47 | for (node *n : _graph) { |
| 48 | if (n->ntype() == node::OPERATOR) |
| 49 | code += write(*dynamic_cast<opNode *>(n)); |
| 50 | } |
| 51 | |
| 52 | // OUTPUTs are written with operators. |
| 53 | |
| 54 | std::ofstream out(_bundleDir.size() |
| 55 | ? (_bundleDir + FS_PATH_SEPARATOR + _outFile) |
| 56 | : _outFile); |
| 57 | if (!out.is_open() || out.fail()) { |
| 58 | std::cerr << "ERROR (CODEGEN): could not open file " + _outFile + |
| 59 | " to write.\n"; |
| 60 | return false; |
| 61 | } |
| 62 | out << writeIncludes() << "\n"; |
| 63 | out << writeUsageFunction() << "\n"; |
| 64 | out << writeMainFunction(code) << "\n"; |
| 65 | out.close(); |
| 66 | return code.length(); |
| 67 | } |
| 68 | |
| 69 | // \brief replace all characters that can't appear |
| 70 | // in C++ variable name. |