Simple file and request libraries

This commit is contained in:
2025-08-25 18:29:45 +10:00
parent e56e6de911
commit 1c5ca8d201
3 changed files with 281 additions and 0 deletions

43
extlibs/file.cpp Normal file
View File

@@ -0,0 +1,43 @@
#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()