From 0e5c5bcf7b4cd4088348bd7c31c8201472a49951 Mon Sep 17 00:00:00 2001 From: Dave Goodwin Date: Wed, 6 Aug 2003 22:46:11 +0000 Subject: [PATCH] *** empty log message *** --- Cantera/src/converters/ct2ctml.cpp | 26 ------ Cantera/src/ct2ctml.cpp | 122 +++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+), 26 deletions(-) delete mode 100644 Cantera/src/converters/ct2ctml.cpp create mode 100644 Cantera/src/ct2ctml.cpp diff --git a/Cantera/src/converters/ct2ctml.cpp b/Cantera/src/converters/ct2ctml.cpp deleted file mode 100644 index bef3293d0..000000000 --- a/Cantera/src/converters/ct2ctml.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include "ct_defs.h" -#include -#include -#include - -namespace Cantera { -void ct2ctml(char* file) { - ofstream f(".cttmp.py"); - f << "from Cantera import *\n" - << "from Cantera.ctml_writer import *\n" - << "import sys, os, os.path\n" - << "file = " << file << endl - << "base = os.path.basename(file)\n" - << "root, ext = os.path.splitext(base)\n" - << "dataset(root)\n" - << "execfile(file)\n" - << "write()\n"; - f.close(); - string PY_CMD = getenv("PYTHON_CMD"); - if (!PY_CMD) PY_CMD = "python"; - int ierr = system(PY_CMD+" .cttmp.py"); - if (ierr != 0) - throw CanteraError("ct2ctml", "could not convert input file to CTML."); -} - -} diff --git a/Cantera/src/ct2ctml.cpp b/Cantera/src/ct2ctml.cpp new file mode 100644 index 000000000..559cfed6d --- /dev/null +++ b/Cantera/src/ct2ctml.cpp @@ -0,0 +1,122 @@ +#include "ct_defs.h" +#include "ctexceptions.h" +#include +#include +#include +#include "ctml.h" +#include "../../include/pypath.h" + +using namespace Cantera; + +namespace ctml { + + static string pypath() { + string s = "python"; +#ifdef PYTHON_EXE + s = string(PYTHON_EXE); + return s; +#else + const char* py = getenv("PYTHON_CMD"); + if (py) s = string(py); + return s; +#endif + } + + static bool checkPython() { + string path = tmpDir() + "/check.py"; + ofstream f(path.c_str()); + if (!f) { + throw CanteraError("checkPython","cannot write to "+tmpDir()); + } + f << "from Cantera import *\n"; + f.close(); + string cmd = pypath() + " " + path + " &> " + tmpDir() + "/log"; + int ierr = system(cmd.c_str()); + if (ierr != 0) { + string msg; + msg = cmd + "\n\n########################################################################\n\n" + "The Cantera Python interface is required in order to process\n" + "Cantera input files, but it does not seem to be correctly installed.\n\n" + "Check that you can invoke the Python interpreter with \n" + "the command \"python\", and that typing \"from Cantera import *\"\n" + "at the Python prompt does not produce an error. If Python on your system\n" + "is invoked with some other command, set environment variable PYTHON_CMD\n" + "to the full path to the Python interpreter. \n\n" + "#########################################################################\n\n"; + writelog(msg); + return false; + } + return true; + } + + + void ct2ctml(const char* file) { + string path = tmpDir()+"/.cttmp.py"; + ofstream f(path.c_str()); + if (!f) { + throw CanteraError("ct2ctml","cannot write to "+tmpDir()); + } + f << "from Cantera import *\n" + << "from Cantera.ctml_writer import *\n" + << "import sys, os, os.path\n" + << "file = \"" << file << "\"\n" + << "base = os.path.basename(file)\n" + << "root, ext = os.path.splitext(base)\n" + << "dataset(root)\n" + << "execfile(file)\n" + << "write()\n"; + f.close(); + string cmd = pypath() + " " + path + " &> ct2ctml.log"; + int ierr = system(cmd.c_str()); + if (ierr != 0) { + string msg = cmd; + ifstream ferr("ct2ctml.log"); + if (ferr) { + char line[80]; + while (!ferr.eof()) { + //msg += "\n"; + ferr.getline(line, 80); + writelog(string(line)+"\n"); + } + ferr.close(); + } + bool pyok = checkPython(); + if (!pyok) + msg += "\nError in Python installation."; + else + msg += "\nCheck error messages above for syntax errors."; + throw CanteraError("ct2ctml", "could not convert input file to CTML\n command line was: "+msg); + } + } + + + /** + * Get an CTML tree from a file, possibly preprocessing the file + * first. + */ + void get_CTML_Tree(XML_Node* rootPtr, string file) { + string ff; + + // find the input file on the Cantera search path + string inname = findInputFile(file); + if (inname == "") + throw CanteraError("get_CTML_tree", "file "+file+" not found"); + + /** + * Check whether or not the file is XML. If not, it will be first + * processed with the preprocessor. + */ + int idot = file.find('.'); + string ext = file.substr(idot,file.size()); + if (ext != ".xml" && ext != ".ctml") { + ct2ctml(file.c_str()); + ff = file.substr(0,idot)+".xml"; + } + else + ff = file; + + ifstream fin(ff.c_str()); + rootPtr->build(fin); + fin.close(); + } +}