⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aix_init_module.cpp

📁 C++的一个好库。。。现在很流行
💻 CPP
字号:
// Copyright David Abrahams 2002.
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifdef _AIX
#include <cstdio>
#include <cstdlib>

extern "C"
{
#include <sys/stat.h>
#include <unistd.h>
}

# include <string>
# include <boost/python/detail/wrap_python.hpp>
# include <boost/python/errors.hpp>
# include <boost/python/detail/aix_init_module.hpp>
# include <boost/python/module.hpp>

namespace boost { namespace python { namespace detail {

namespace
{
  static PyMethodDef initial_methods[] = { { 0, 0, 0, 0 } };
  extern "C" void initlibboost_python()
  {
      Py_InitModule("libboost_python", initial_methods);
  }

  struct find_and_open_file
  {
      FILE*       fp;
      std::string libpath;  // -- search path
      std::string filename; // -- filename to look for
      std::string fullpath; // -- full path to file
   
      find_and_open_file(
          const std::string& libpath_env
          , const std::string& file);
  };

  find_and_open_file::find_and_open_file(
      const std::string& libpath_env
      , const std::string& file)
      : fp(0)
  {
      char* value = std::getenv(libpath_env.c_str());

      if(value == 0)
          return;
      
      libpath = value;
      
      if (libpath == "")
          return;

      std::string::size_type pos = 0, prev_pos = 0;

      // -- loop through all search paths looking for file 
      while((pos = libpath.find_first_of(":",pos)) != std::string::npos)
      {
          fullpath = libpath.substr(prev_pos,pos - prev_pos) + "/" + file;
          if (::access(fullpath.c_str(), R_OK) == 0)
          {
              struct stat filestat;
              ::stat(fullpath.c_str(), &filestat);
              if (!S_ISDIR(filestat.st_mode))
              { 
                  fp = std::fopen(fullpath.c_str(), "r"); 
                  if (fp)
                  {
                      filename = file;
                  }
                  return;
              }
          }
          prev_pos = ++pos;
      }

      // -- mop up odd path
      if (libpath.find_first_of(":", prev_pos) == std::string::npos)
      {
          fullpath = libpath.substr(prev_pos, libpath.size() - prev_pos) + "/" + file;
          if (::access(fullpath.c_str(), R_OK) == 0)
          {
              struct stat filestat;
              ::stat(fullpath.c_str(),&filestat);
              if (!S_ISDIR(filestat.st_mode))
              { 
                  fp = std::fopen(fullpath.c_str(), "r"); 
                  filename = file;
              }
          }
      }
  }
}

void aix_init_module(
    so_load_function load_dynamic_module
    , char const* module_name
    , void (*init_module)())
{
    static bool initialized;
    if (!initialized)
    {
        char const* const name = "libboost_python.so";
        find_and_open_file dynlib("LIBPATH", name);
        if (dynlib.fp == 0)
        {
            fprintf(stderr, " Error: could not find %s\n", name);
            return;
        }
        
        std::string::size_type pos = pos = dynlib.filename.rfind(".so");
        if (pos != dynlib.filename.size() - 3)
        {
            fprintf(stderr, "dynamic library %s must end with .so\n", dynlib.filename.c_str());
            return;
        }

        PyObject* m = 
            load_dynamic_module(
                const_cast<char*>(dynlib.filename.substr(0,pos).c_str()),
                const_cast<char*>(dynlib.fullpath.c_str()),
                dynlib.fp);

        if (m == 0)
        {
            fprintf(stderr, "failed to load library %s\n", name);
            return;
        }
        Py_DECREF(m);

        initialized = true;
    }
    python::detail::init_module(module_name, init_module);
}

}}} // namespace boost::python
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -