dlang-sqlite inital
This commit is contained in:
commit
ce0a6c08e4
160 files changed
+362540
No files matched your search
@@ -0,0 +1,85 @@
|
||||
#ifndef DLANG_LIBS_IO_HPP
|
||||
#define DLANG_LIBS_IO_HPP
|
||||
|
||||
#include <fstream>
|
||||
#include "../vm.hpp"
|
||||
#include "../helpers.hpp"
|
||||
|
||||
static void NativeRead(DlangVM* vm)
|
||||
{
|
||||
bool binary = false;
|
||||
if (dlang_isboolean(vm))
|
||||
binary = dlang_toboolean(vm);
|
||||
std::string path = dlang_tostring(vm);
|
||||
|
||||
std::ifstream stream(path, (binary ? std::ios::binary : std::ios::in));
|
||||
if (!stream.is_open())
|
||||
{
|
||||
dlang_pushstring(vm, "Failed to open file!");
|
||||
return;
|
||||
}
|
||||
|
||||
std::string content(
|
||||
(std::istreambuf_iterator<char>(stream)),
|
||||
std::istreambuf_iterator<char>()
|
||||
);
|
||||
|
||||
stream.close();
|
||||
|
||||
dlang_pushstring(vm, content);
|
||||
}
|
||||
|
||||
static void NativeWrite(DlangVM* vm)
|
||||
{
|
||||
bool binary = false;
|
||||
if (dlang_isboolean(vm))
|
||||
binary = dlang_toboolean(vm);
|
||||
|
||||
std::string content = dlang_tostring(vm);
|
||||
std::string path = dlang_tostring(vm);
|
||||
|
||||
std::ofstream stream(path, (binary ? std::ios::binary : std::ios::out));
|
||||
if (!stream.is_open())
|
||||
{
|
||||
dlang_pushboolean(vm, false);
|
||||
return;
|
||||
}
|
||||
|
||||
stream.write(reinterpret_cast<const char*>(content.data()), content.size());
|
||||
stream.close();
|
||||
|
||||
dlang_pushboolean(vm, true);
|
||||
}
|
||||
|
||||
static void NativeInput(DlangVM* vm)
|
||||
{
|
||||
if (dlang_isstring(vm))
|
||||
{
|
||||
auto promp = dlang_tostring(vm);
|
||||
printf("%s", promp.c_str());
|
||||
}
|
||||
|
||||
std::string inputLine;
|
||||
if (std::getline(std::cin, inputLine))
|
||||
dlang_pushstring(vm, inputLine);
|
||||
else
|
||||
dlang_pushstring(vm, "");
|
||||
}
|
||||
|
||||
inline void openlib_io(DlangVM* vm)
|
||||
{
|
||||
dlang_newtable(vm);
|
||||
|
||||
dlang_pushcfunction(vm, NativeRead);
|
||||
dlang_setfield(vm, "read");
|
||||
|
||||
dlang_pushcfunction(vm, NativeWrite);
|
||||
dlang_setfield(vm, "write");
|
||||
|
||||
dlang_pushcfunction(vm, NativeInput);
|
||||
dlang_setfield(vm, "input");
|
||||
|
||||
dlang_setglobal(vm, "io");
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user