forked from MEGIKS/DE_euler_method
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
37 lines (34 loc) · 1.1 KB
/
parser.cpp
File metadata and controls
37 lines (34 loc) · 1.1 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
#include "parser.hpp"
#include <cmath>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <dlfcn.h>
using namespace std;
void safe_write(int fd, const char *data, size_t len) {
size_t writed = 0;
while ((writed += write(fd, data + writed, len - writed)) < len);
}
func_t parse(const string &s) {
char tmpsource[] = "XXXXXX.c";
int fd = mkstemps(tmpsource, 2);
static char prefix[] = "#include <math.h>\ndouble f(double x, double y){return ";
static char suffix[] = ";}\n";
safe_write(fd, prefix, strlen(prefix));
safe_write(fd, s.data(), s.size());
safe_write(fd, suffix, strlen(suffix));
close(fd);
char tmpshared[] = "XXXXXX.so";
close(mkstemps(tmpshared, 3));
if (system((string("gcc -fpic -Ofast -shared -o ") + tmpshared + " " + tmpsource +
" 2> /dev/null").data())) {
unlink(tmpsource);
return nullptr;
}
void *lib = dlopen((string("./") + tmpshared).data(), RTLD_LAZY);
func_t ans = reinterpret_cast<func_t>(dlsym(lib, "f"));
unlink(tmpsource);
unlink(tmpshared);
return ans;
}