2019-08-10 09:55:45 +00:00
|
|
|
#include <iostream>
|
2019-08-10 14:45:15 +00:00
|
|
|
#include <fstream>
|
2019-08-11 14:05:14 +00:00
|
|
|
#include <sys/stat.h>
|
2019-08-10 09:55:45 +00:00
|
|
|
#include "ScriptOptions.hpp"
|
|
|
|
#include "Utilities/StringUtils.hpp"
|
2019-08-10 14:45:15 +00:00
|
|
|
#include "Script.hpp"
|
2019-08-10 09:55:45 +00:00
|
|
|
|
|
|
|
Porygon::ScriptOptions Porygon::ScriptOptions::DefaultScriptOptions;
|
|
|
|
|
|
|
|
std::streambuf* Porygon::ScriptOptions::_printBuffer = std::cout.rdbuf();
|
|
|
|
std::ostream* Porygon::ScriptOptions::_printStream = new std::ostream(Porygon::ScriptOptions::_printBuffer);
|
|
|
|
|
2019-08-10 14:45:15 +00:00
|
|
|
void Porygon::ScriptOptions::DefaultPrint(const char16_t *s) {
|
2019-08-10 09:55:45 +00:00
|
|
|
Porygon::ScriptOptions::GetDefaultScriptOptions()->GetPrintStream() << Porygon::Utilities::StringUtils::FromUTF8(s) << std::endl;
|
|
|
|
}
|
|
|
|
|
2019-08-24 16:52:11 +00:00
|
|
|
bool Porygon::ScriptOptions::DefaultModuleExists(const char* moduleName, size_t size) {
|
2019-08-11 14:05:14 +00:00
|
|
|
struct stat buffer;
|
2019-08-24 16:52:11 +00:00
|
|
|
return (stat (moduleName, &buffer) == 0);
|
2019-08-10 14:45:15 +00:00
|
|
|
}
|
2019-08-10 09:55:45 +00:00
|
|
|
|
2019-08-24 16:52:11 +00:00
|
|
|
Porygon::Script *Porygon::ScriptOptions::DefaultResolveModule(const char* moduleName, size_t size) {
|
2019-08-10 14:45:15 +00:00
|
|
|
auto stream = std::ifstream(moduleName);
|
|
|
|
std::basic_stringstream<char16_t> stringStream;
|
|
|
|
stringStream << stream.rdbuf();
|
|
|
|
auto str = std::u16string(stringStream.str());
|
|
|
|
return Porygon::Script::Create(str);
|
|
|
|
}
|
2019-08-10 09:55:45 +00:00
|
|
|
|
|
|
|
extern "C"{
|
|
|
|
void SetDefaultPrintFunc(void (*func)(const char16_t*)){
|
|
|
|
Porygon::ScriptOptions::GetDefaultScriptOptions()->SetPrintFunc(func);
|
|
|
|
}
|
2019-08-24 13:12:09 +00:00
|
|
|
|
2019-08-24 16:52:11 +00:00
|
|
|
void SetDefaultModuleExists(bool (*func)(const char* moduleName, size_t size)){
|
2019-08-24 13:12:09 +00:00
|
|
|
Porygon::ScriptOptions::GetDefaultScriptOptions()->SetModuleExistsFunc(func);
|
|
|
|
}
|
|
|
|
|
2019-08-24 16:52:11 +00:00
|
|
|
void SetDefaultResolveModule(Porygon::Script * (*func)(const char* moduleName, size_t size)){
|
2019-08-24 13:12:09 +00:00
|
|
|
Porygon::ScriptOptions::GetDefaultScriptOptions()->SetResolveModuleFunc(func);
|
|
|
|
}
|
|
|
|
|
|
|
|
Porygon::ScriptOptions* CreateOptions(){
|
|
|
|
return new Porygon::ScriptOptions();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetOptionPrintFunc(Porygon::ScriptOptions* opt, void (*func)(const char16_t*)){
|
|
|
|
opt->SetPrintFunc(func);
|
|
|
|
}
|
|
|
|
|
2019-08-24 16:52:11 +00:00
|
|
|
void SetOptionModuleExistsFunc(Porygon::ScriptOptions* opt, bool (*func)(const char* moduleName, size_t size)){
|
2019-08-24 13:12:09 +00:00
|
|
|
opt->SetModuleExistsFunc(func);
|
|
|
|
}
|
|
|
|
|
2019-08-24 16:52:11 +00:00
|
|
|
void SetOptionResolveModuleFunc(Porygon::ScriptOptions* opt, Porygon::Script* (*func)(const char* moduleName, size_t size)){
|
2019-08-24 14:11:09 +00:00
|
|
|
opt->SetResolveModuleFunc(func);
|
2019-08-24 13:12:09 +00:00
|
|
|
}
|
2019-08-10 09:55:45 +00:00
|
|
|
}
|