-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompiler.cpp
More file actions
195 lines (182 loc) · 6.28 KB
/
Compiler.cpp
File metadata and controls
195 lines (182 loc) · 6.28 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "Compiler.h"
#include <juce_core/juce_core.h>
#include <cstdio>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <array>
#include <vector>
#include <sstream>
#include "PreferencesData.h"
#if WIN32
#define Popen _popen
#define PClose _pclose
#else
#define Popen popen
#define PClose pclose
#endif
namespace
{
std::string __compiler_executable;
}
namespace
{
class CompilerException : public std::exception {
public:
CompilerException(const std::string &what) : _what(what) {}
virtual const char* what() const throw()
{
return _what.c_str();
}
private:
const std::string _what;
};
std::string exec(const std::string & cmd, const std::vector<std::string> &arguments) {
std::stringstream ss;
ss << cmd;
for (auto const& arg : arguments)
{
ss << " " << arg;
}
std::array<char, 128> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&PClose)> pipe(Popen(ss.str().c_str(), "r"), PClose);
if (!pipe)
{
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
{
result += buffer.data();
}
return result.substr(0, result.length() - 1);
}
const juce::var & get(const juce::var &json, const std::string &key, bool expected = true)
{
auto &result = json[key.c_str()];
if (expected && result.isVoid())
{
throw std::runtime_error("unexpected compiler response");
}
return result;
}
void checkForErrors(const juce::var& jsonResult, const std::string &, const std::string&)
{
const auto& errorMessage = get(jsonResult, "errorMessage", false);
if (errorMessage.isVoid())
{
return;
}
const auto& sourceFile = get(jsonResult, "sourceFile", false);
const auto& position = get(jsonResult, "positionBegin", false);
std::stringstream ss;
if (!sourceFile.isVoid())
{
ss << "in file \""<< sourceFile.toString() << "\"";
}
if (!position.isVoid())
{
ss << ": position " << position.toString();
}
ss << "\n" << errorMessage.toString();
throw CompilerException(ss.str());
}
}
CompiledSheetPtr Compiler::compile(const std::string& sheetPath)
{
auto compilerExe = compilerExecutable();
logger.log(LogLambda(log << "sheetc" << " \"" << sheetPath << "\""));
try
{
CompiledSheetPtr result = std::make_shared<CompiledSheet>();
auto stringResult = exec(compilerExe, { sheetPath, "--mode=json" });
auto jsonResult = juce::JSON::parse(stringResult);
checkForErrors(jsonResult, compilerExe, sheetPath);
const auto& midiInfo = get(jsonResult, "midi");
// midi data
const auto base64MidiData = get(midiInfo, "midiData").toString();
double estimatedByteSize = (base64MidiData.length() * (3.0 / 4.0) + 10);
juce::MemoryOutputStream midiByteStream((size_t)estimatedByteSize);
juce::Base64::convertFromBase64(midiByteStream, base64MidiData);
result->midiData.resize(midiByteStream.getDataSize());
::memcpy(result->midiData.data(), midiByteStream.getData(), result->midiData.size());
logger.log(LogLambda(log << "MIDI data created: " << result->midiData.size() << " Bytes"));
// sources
const auto& sources = get(midiInfo, "sources");
for (int i = 0; i < sources.size(); ++i)
{
const auto& sourceId = get(sources[i], "sourceId");
const auto& path = get(sources[i], "path");
result->sources.push_back({ sourceId.toString().toStdString(), path.toString().toStdString() });
}
// document event infos
const auto& eventInfos = get(jsonResult, "eventInfos");
for (int i = 0; i < eventInfos.size(); ++i)
{
const auto& sheetEventInfos = get(eventInfos[i], "sheetEventInfos");
for(int j = 0; j < sheetEventInfos.size(); ++j)
{
DocumentEventInfo docEventInfo;
const auto &sheetEventInfo = sheetEventInfos[j];
docEventInfo.sourceId = (juce::int64)get(sheetEventInfo, "sourceId");
docEventInfo.beginPosition = (juce::int64)get(sheetEventInfo, "beginPosition");
auto endp = get(sheetEventInfo, "endPosition", false);
if (!endp.isVoid())
{
docEventInfo.endPosition = (juce::int64)get(sheetEventInfo, "endPosition", false);
} else {
docEventInfo.beginPosition;
}
docEventInfo.beginTime = (double)get(sheetEventInfo, "beginTime");
docEventInfo.endTime = (double)get(sheetEventInfo, "endTime");
EventPositionSet value = {docEventInfo};
result->eventInfos += std::make_pair(TimelineIntervalType::right_open(docEventInfo.beginTime, docEventInfo.endTime), value);
}
}
return result;
}
catch (const CompilerException& ex)
{
logger.error(LogLambda(log << ex.what()));
}
catch (const std::exception& ex)
{
logger.error(LogLambda(log << "FAILED: " << ex.what()));
}
catch (...)
{
logger.error(LogLambda(log << "FAILED: unkown error"));
}
return nullptr;
}
std::string Compiler::getVersionStr()
{
return exec(compilerExecutable(), {"--version"});
}
std::string Compiler::compilerExecutable() const
{
if (__compiler_executable.empty())
{
auto preferencesData = readPreferencesData();
if (!preferencesData.binPath.empty())
{
__compiler_executable = (juce::File::addTrailingSeparator(preferencesData.binPath) + "sheetc").toStdString();
return __compiler_executable;
}
#ifdef WIN32
__compiler_executable = "sheetc";
#else
__compiler_executable = exec("which", {"sheetc"});
if (__compiler_executable.empty())
{
__compiler_executable = "/usr/local/bin/sheetc";
}
#endif
}
return __compiler_executable;
}
void Compiler::resetExecutablePath()
{
__compiler_executable.clear();
}