-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMaximaChain.h
125 lines (77 loc) · 2.91 KB
/
MaximaChain.h
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#ifndef MAXIMACHAIN_H_INCLUDED
#define MAXIMACHAIN_H_INCLUDED
#include <cstddef>
#include <string>
#include <deque>
#include "boost/process.hpp"
#include "boost/regex.hpp"
#include "boost/filesystem.hpp"
#include "boost/scoped_ptr.hpp"
#include "boost/shared_ptr.hpp"
namespace bp = ::boost::process;
namespace fs = ::boost::filesystem;
using std::size_t;
namespace Maxima
{
class MaximaChain
{
public:
MaximaChain(const std::string &maximaPath, // Full path to the Maxima executable
const std::string &workingDir = ".", // Working directory for output files
const std::string &utilsDir = "."); // Utils directory that contains display.lisp
~MaximaChain();
std::string executeCommand(const std::string &command);
std::string executeCommandList(const std::string &command);
// Command must end with ';' or '$'. In annother case we append ';'
void sendCommand(std::string command);
bp::process::id_type getId() const;
const fs::path &getWorkingDirectory() const;
class MaximaIOHook
{
public:
virtual ~MaximaIOHook();
virtual std::string handle(const std::string &first,
const std::string &second) = 0;
};
void setMaximaIOHook(const std::string &hookRegex, MaximaIOHook *hook);
private:
struct Reply
{
typedef std::deque<char> RawReply;
typedef RawReply::iterator It;
typedef std::pair<It, It> Range;
RawReply reply;
Range prompt;
std::deque<Range> outs;
std::deque<Range> betweens;
Reply(std::istream &in);
std::string concatenateParts();
// True if prompt is valid (%i\d+)
bool CheckPrompt() const { return validPrompt; }
size_t getPromptId() const { return promptId; }
bool isInterrupted() const;
private:
Reply(const Reply &);
void operator=(const Reply &);
bool validPrompt;
size_t promptId;
};
typedef boost::shared_ptr<MaximaChain::Reply> ReplyPtr;
// Returns the number of bytes read
static size_t readData(std::istream &in, Reply::RawReply &reply);
// Sends command and get reply in Maxima style
ReplyPtr crudeExecute(const std::string &command);
// Check if the input expression valid
int checkInput(char nextChar, int checkerState) const;
ReplyPtr readReply();
void getPid();
boost::scoped_ptr<bp::child> process;
MaximaIOHook* maximaIOHook;
boost::regex maximaIOHookRegex;
fs::path workingDirectory;
fs::path utilsDirectory;
size_t lastPromptId;
size_t pid;
};
} // namespace Maxima
#endif // MAXIMACHAIN_H_INCLUDED