forked from lava/matplotlib-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatplotlib.h
More file actions
105 lines (90 loc) · 3.31 KB
/
Copy pathmatplotlib.h
File metadata and controls
105 lines (90 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#ifndef _XXX_YYY_ZZZ_MATPLOTLIB_H
#define _XXX_YYY_ZZZ_MATPLOTLIB_H
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#ifdef _WIN32
#define __XXX_YYY_ZZZ_POPEN___ _popen
#define __XXX_YYY_ZZZ_PCLOSE___ _pclose
#else
#define __XXX_YYY_ZZZ_POPEN___ popen
#define __XXX_YYY_ZZZ_PCLOSE___ pclose
#endif
std::string
__XXX_YYY_ZZZ_STRIP__(const std::string &str,
const std::string &whitespace = " \n\r\t\f\v") {
size_t from = str.find_first_not_of(whitespace);
if (from == std::string::npos) {
return "";
}
size_t to = str.find_last_not_of(whitespace);
return str.substr(from, (to - from) + 1);
}
std::string __XXX_YYY_ZZZ_COMMAND__(const std::string &cmd) {
using pipe_ptr = std::unique_ptr<FILE, decltype(__XXX_YYY_ZZZ_PCLOSE___) *>;
pipe_ptr pipe(__XXX_YYY_ZZZ_POPEN___(cmd.c_str(), "r"),
__XXX_YYY_ZZZ_PCLOSE___);
if (pipe == nullptr) {
std::cout << "error: failed to execute: " << cmd << std::endl;
return "";
}
const int BUF_SIZE = 1023;
char buf[BUF_SIZE + 1];
buf[BUF_SIZE] = '\0';
std::stringstream ss;
while (fgets(buf, BUF_SIZE, pipe.get()) != NULL) {
ss << buf;
}
if (__XXX_YYY_ZZZ_PCLOSE___(pipe.release()) != 0) {
return "";
}
return __XXX_YYY_ZZZ_STRIP__(ss.str());
}
static bool __XXX_YYY_ZZZ_INIT__() {
auto myenv = [](const std::string &name) {
const char *value = getenv(name.c_str());
return std::string(value == NULL ? "" : value);
};
const std::string CONDA_PREFIX = myenv("CONDA_PREFIX");
if (CONDA_PREFIX.empty()) {
// do default python config
} else {
#ifdef _WIN32
const std::string PYTHONHOME = CONDA_PREFIX + R"(\lib)";
const std::string PYTHONPATH = PYTHONHOME + ";" + PYTHONHOME +
R"(\site-packages;)" + CONDA_PREFIX +
R"(\DLLs)";
const std::string QT_QPA_PLATFORM_PLUGIN_PATH =
CONDA_PREFIX + R"(\Library\plugins\platforms)";
_putenv_s("PYTHONHOME", PYTHONHOME.c_str());
_putenv_s("PYTHONPATH", PYTHONPATH.c_str());
_putenv_s("QT_QPA_PLATFORM_PLUGIN_PATH",
QT_QPA_PLATFORM_PLUGIN_PATH.c_str());
std::cout << "PYTHONHOME : " << myenv("PYTHONHOME")
<< std::endl;
std::cout << "PYTHONPATH : " << myenv("PYTHONPATH")
<< std::endl;
std::cout << "QT_QPA_PLATFORM_PLUGIN_PATH: "
<< myenv("QT_QPA_PLATFORM_PLUGIN_PATH") << std::endl;
#else
const std::string PYTHON_VERSION = __XXX_YYY_ZZZ_COMMAND__(
"python3 --version | cut -d ' ' -f2 | cut -d '.' -f-2");
const std::string PYTHONHOME =
CONDA_PREFIX + "/lib/python" + PYTHON_VERSION;
const std::string PYTHONPATH = PYTHONHOME + ":" + PYTHONHOME +
"/site-packages:" + PYTHONHOME +
"/lib-dynload";
setenv("PYTHONHOME", PYTHONHOME.c_str(), 1);
setenv("PYTHONPATH", PYTHONPATH.c_str(), 1);
std::cout << "PYTHON_VERSION: " << PYTHON_VERSION << std::endl;
std::cout << "PYTHONHOME : " << myenv("PYTHONHOME") << std::endl;
std::cout << "PYTHONPATH : " << myenv("PYTHONPATH") << std::endl;
#endif
}
return true;
}
static bool __UNUSED_XXX_YYY_ZZZ_INIT__ = __XXX_YYY_ZZZ_INIT__();
#define PY_MAJOR_VERSION 3
#include "matplotlibcpp.h"
#endif