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-10 14:45:15 +00:00
|
|
|
bool Porygon::ScriptOptions::DefaultModuleExists(const std::string& moduleName) {
|
2019-08-11 14:05:14 +00:00
|
|
|
struct stat buffer;
|
|
|
|
return (stat (moduleName.c_str(), &buffer) == 0);
|
2019-08-10 14:45:15 +00:00
|
|
|
}
|
2019-08-10 09:55:45 +00:00
|
|
|
|
2019-08-10 14:45:15 +00:00
|
|
|
Porygon::Script *Porygon::ScriptOptions::DefaultResolveModule(const std::string& moduleName) {
|
|
|
|
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
|
|
|
|
|
|
|
void SetDefaultModuleExists(bool (*func)(const std::string& moduleName)){
|
|
|
|
Porygon::ScriptOptions::GetDefaultScriptOptions()->SetModuleExistsFunc(func);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetDefaultResolveModule(Porygon::Script * (*func)(const std::string& moduleName)){
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetOptionModuleExistsFunc(Porygon::ScriptOptions* opt, bool (*func)(const std::string& moduleName)){
|
|
|
|
opt->SetModuleExistsFunc(func);
|
|
|
|
}
|
|
|
|
|
2019-08-24 14:11:09 +00:00
|
|
|
void SetOptionResolveModuleFunc(Porygon::ScriptOptions* opt, Porygon::Script* (*func)(const std::string& moduleName)){
|
|
|
|
opt->SetResolveModuleFunc(func);
|
2019-08-24 13:12:09 +00:00
|
|
|
}
|
2019-08-10 09:55:45 +00:00
|
|
|
}
|