forked from ground/ground
Organisation, documentation, mathlib
This commit is contained in:
43
extlibs/file/file.cpp
Normal file
43
extlibs/file/file.cpp
Normal 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()
|
Reference in New Issue
Block a user