PorygonLang/src/ScriptOptions.cpp

58 lines
2.1 KiB
C++
Raw Normal View History

#include <iostream>
2019-08-10 14:45:15 +00:00
#include <fstream>
2019-08-11 14:05:14 +00:00
#include <sys/stat.h>
#include "ScriptOptions.hpp"
#include "Utilities/StringUtils.hpp"
2019-08-10 14:45:15 +00:00
#include "Script.hpp"
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) {
Porygon::ScriptOptions::GetDefaultScriptOptions()->GetPrintStream() << Porygon::Utilities::StringUtils::FromUTF8(s) << std::endl;
}
bool Porygon::ScriptOptions::DefaultModuleExists(const char* moduleName, size_t size) {
2019-08-11 14:05:14 +00:00
struct stat buffer;
return (stat (moduleName, &buffer) == 0);
2019-08-10 14:45:15 +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);
}
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 char* moduleName, size_t size)){
2019-08-24 13:12:09 +00:00
Porygon::ScriptOptions::GetDefaultScriptOptions()->SetModuleExistsFunc(func);
}
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);
}
void SetOptionModuleExistsFunc(Porygon::ScriptOptions* opt, bool (*func)(const char* moduleName, size_t size)){
2019-08-24 13:12:09 +00:00
opt->SetModuleExistsFunc(func);
}
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
}
}