Files
ground_fork/extlibs/file/file.cpp

44 lines
961 B
C++
Raw Normal View History

2025-08-25 18:29:45 +10:00
#include "ground_lib.h"
#include <fstream>
#include <filesystem>
GroundValue readFile(GroundValue* args, int arg_count) {
VALIDATE_ARGS_1(GROUND_STRING);
std::ifstream ffile(GET_STRING(args[0]));
std::string tmp;
std::string out;
while (std::getline(ffile, tmp)) {
out += tmp + "\n";
}
return ground_string_val(out);
}
GroundValue writeFile(GroundValue* args, int arg_count) {
VALIDATE_ARGS_2(GROUND_STRING, GROUND_STRING);
std::ofstream file(GET_STRING(args[0]));
if (file.good()) {
file << GET_STRING(args[1]);
} else {
std::cout << "File isn't good for writing in" << std::endl;
return GROUND_BOOL_VAL(false);
}
return GROUND_BOOL_VAL(true);
}
GROUND_LIBRARY_INTERFACE()
GROUND_LIBRARY_INIT()
REGISTER_GROUND_FUNCTION(readFile);
REGISTER_GROUND_FUNCTION(writeFile);
GROUND_LIBRARY_INIT_END()
GROUND_LIBRARY_CLEANUP()
GROUND_LIBRARY_CLEANUP_END()