]> www.ginac.de Git - ginac.git/blobdiff - ginac/excompiler.cpp
Happy New Year!
[ginac.git] / ginac / excompiler.cpp
index 1b4e73d6b33082301b393a3611909d10d7e4b73d..c54c4659c297d0f242b816c328315134ba1e68ab 100644 (file)
@@ -6,7 +6,7 @@
  */
 
 /*
- *  GiNaC Copyright (C) 1999-2008 Johannes Gutenberg University Mainz, Germany
+ *  GiNaC Copyright (C) 1999-2019 Johannes Gutenberg University Mainz, Germany
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
 
 #include "excompiler.h"
 
-#include <stdexcept>
-#include <ios>
-#include <fstream>
-#include <sstream>
-#include <string>
-#include <vector>
-
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 
-#ifdef HAVE_LIBDL
-#include <dlfcn.h>
-#endif // def HAVE_LIBDL
-
 #include "ex.h"
 #include "lst.h"
 #include "operators.h"
 #include "relational.h"
 #include "symbol.h"
 
+#ifdef HAVE_LIBDL
+# include <dlfcn.h>
+#endif // def HAVE_LIBDL
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#else
+# ifdef _MSC_VER
+#  include <io.h>  // for close(3)
+# endif // def _MSC_VER
+#endif // def HAVE_UNISTD_H
+#include <cstdlib>
+#include <fstream>
+#include <ios>
+#include <sstream>
+#include <stdexcept>
+#include <string>
+#include <vector>
+
 namespace GiNaC {
 
 #ifdef HAVE_LIBDL
-       
+
 /**
  * Small class that manages modules opened by libdl. It is used by compile_ex
  * and link_ex in order to have a clean-up of opened modules and their
@@ -73,11 +80,11 @@ class excompiler
        std::vector<filedesc> filelist; /**< List of all opened modules */
 public:
        /**
-        * Complete clean-up of opend modules is done on destruction.
+        * Complete clean-up of opened modules is done on destruction.
         */
        ~excompiler()
        {
-               for (std::vector<filedesc>::const_iterator it = filelist.begin(); it != filelist.end(); ++it) {
+               for (auto it = filelist.begin(); it != filelist.end(); ++it) {
                        clean_up(it);
                }
        }
@@ -113,13 +120,15 @@ public:
                        const char* filename_pattern = "./GiNaCXXXXXX";
                        char* new_filename = new char[strlen(filename_pattern)+1];
                        strcpy(new_filename, filename_pattern);
-                       if (!mktemp(new_filename)) {
-                               delete new_filename;
-                               throw std::runtime_error("mktemp failed");
+                       int fd = mkstemp(new_filename);
+                       if (fd == -1) {
+                               delete[] new_filename;
+                               throw std::runtime_error("mkstemp failed");
                        }
                        filename = std::string(new_filename);
                        ofs.open(new_filename, std::ios::out);
-                       delete new_filename;
+                       close(fd);
+                       delete[] new_filename;
                } else {
                        // use parameter as filename
                        ofs.open(filename.c_str(), std::ios::out);
@@ -141,7 +150,7 @@ public:
         */
        void compile_src_file(const std::string filename, bool clean_up)
        {
-               std::string strcompile = "ginac-excompiler " + filename;
+               std::string strcompile = LIBEXECDIR "ginac-excompiler " + filename;
                if (system(strcompile.c_str())) {
                        throw std::runtime_error("excompiler::compile_src_file: error compiling source file!");
                }
@@ -154,9 +163,9 @@ public:
         */
        void* link_so_file(const std::string filename, bool clean_up)
        {
-               void* module = NULL;
+               void* module = nullptr;
                module = dlopen(filename.c_str(), RTLD_NOW);
-               if (module == NULL)     {
+               if (module == nullptr) {
                        throw std::runtime_error("excompiler::link_so_file: could not open compiled module!");
                }
 
@@ -170,10 +179,10 @@ public:
         */
        void unlink(const std::string filename)
        {
-               for (std::vector<filedesc>::iterator it = filelist.begin(); it != filelist.end();) {
+               for (auto it = filelist.begin(); it != filelist.end();) {
                        if (it->name == filename) {
                                clean_up(it);
-                               filelist.erase(it);
+                               it = filelist.erase(it);
                        } else {
                                ++it;
                        }
@@ -182,7 +191,7 @@ public:
 };
 
 /**
- * This static object manages the modules opened by the complile_ex and link_ex
+ * This static object manages the modules opened by the compile_ex and link_ex
  * functions. On program termination its dtor is called and all open modules
  * are closed. The associated source and so-files are eventually deleted then
  * as well.
@@ -196,7 +205,7 @@ static excompiler global_excompiler;
 void compile_ex(const ex& expr, const symbol& sym, FUNCP_1P& fp, const std::string filename)
 {
        symbol x("x");
-       ex expr_with_x = expr.subs(lst(sym==x));
+       ex expr_with_x = expr.subs(lst{sym==x});
 
        std::ofstream ofs;
        std::string unique_filename = filename;
@@ -221,7 +230,7 @@ void compile_ex(const ex& expr, const symbol& sym, FUNCP_1P& fp, const std::stri
 void compile_ex(const ex& expr, const symbol& sym1, const symbol& sym2, FUNCP_2P& fp, const std::string filename)
 {
        symbol x("x"), y("y");
-       ex expr_with_xy = expr.subs(lst(sym1==x, sym2==y));
+       ex expr_with_xy = expr.subs(lst{sym1==x, sym2==y});
 
        std::ofstream ofs;
        std::string unique_filename = filename;
@@ -246,14 +255,14 @@ void compile_ex(const ex& expr, const symbol& sym1, const symbol& sym2, FUNCP_2P
 void compile_ex(const lst& exprs, const lst& syms, FUNCP_CUBA& fp, const std::string filename)
 {
        lst replacements;
-       for (int count=0; count<syms.nops(); ++count) {
+       for (std::size_t count=0; count<syms.nops(); ++count) {
                std::ostringstream s;
                s << "a[" << count << "]";
                replacements.append(syms.op(count) == symbol(s.str()));
        }
 
        std::vector<ex> expr_with_cname;
-       for (int count=0; count<exprs.nops(); ++count) {
+       for (std::size_t count=0; count<exprs.nops(); ++count) {
                expr_with_cname.push_back(exprs.op(count).subs(replacements));
        }
 
@@ -263,7 +272,7 @@ void compile_ex(const lst& exprs, const lst& syms, FUNCP_CUBA& fp, const std::st
 
        ofs << "void compiled_ex(const int* an, const double a[], const int* fn, double f[])" << std::endl;
        ofs << "{" << std::endl;
-       for (int count=0; count<exprs.nops(); ++count) {
+       for (std::size_t count=0; count<exprs.nops(); ++count) {
                ofs << "f[" << count << "] = ";
                expr_with_cname[count].print(GiNaC::print_csrc_double(ofs));
                ofs << ";" << std::endl;