file library
This commit is contained in:
90
src/modules/file/file.cpp
Normal file
90
src/modules/file/file.cpp
Normal file
@@ -0,0 +1,90 @@
|
||||
#include "file.h"
|
||||
#include "../../defs/defs.h"
|
||||
#include "../../error/error.h"
|
||||
#include <vector>
|
||||
#include <fstream>
|
||||
|
||||
Value modules::file(std::vector<Value> args) {
|
||||
if (args.size() < 2) {
|
||||
error("Not enough args for file module");
|
||||
return Value("Error");
|
||||
}
|
||||
if (args[1].valtype != ValueType::String) {
|
||||
error("Expecting a string to identify file to open");
|
||||
return Value("Error");
|
||||
}
|
||||
if (args[0].valtype == ValueType::Identifier) {
|
||||
if (args[0].string_val == "read") {
|
||||
std::ifstream file(args[1].string_val);
|
||||
if (!file) {
|
||||
error("File " + args[1].string_val + " does not exist (in file module)");
|
||||
return Value("Error");
|
||||
}
|
||||
std::string buf;
|
||||
std::string retval;
|
||||
bool notfirst = false;
|
||||
while (getline(file, buf)) {
|
||||
if (notfirst) {
|
||||
buf += "\n";
|
||||
} else {
|
||||
notfirst = true;
|
||||
}
|
||||
retval += buf;
|
||||
}
|
||||
file.close();
|
||||
return Value(retval);
|
||||
} else if (args[0].string_val == "readlines") {
|
||||
std::ifstream file(args[1].string_val);
|
||||
if (!file) {
|
||||
error("File " + args[1].string_val + " does not exist (in file module)");
|
||||
return Value("Error");
|
||||
}
|
||||
std::string buf;
|
||||
std::vector<Value> retval;
|
||||
while (getline(file, buf)) {
|
||||
retval.push_back(Value(buf));
|
||||
}
|
||||
file.close();
|
||||
return Value(retval);
|
||||
} else if (args[0].string_val == "exists") {
|
||||
if (std::ifstream(args[1].string_val)) {
|
||||
return Value("true");
|
||||
} else {
|
||||
return Value("false");
|
||||
}
|
||||
} else if (args[0].string_val == "write") {
|
||||
if (args.size() < 3) {
|
||||
error("Not enough args for write submodule of file module");
|
||||
return Value("Error");
|
||||
}
|
||||
if (args[2].valtype != ValueType::String) {
|
||||
error("Expecting a string of content to write to file");
|
||||
return Value("Error");
|
||||
}
|
||||
std::ofstream file(args[1].string_val, std::ofstream::trunc);
|
||||
file << args[2].string_val;
|
||||
file.close();
|
||||
return Value("true");
|
||||
} else if (args[0].string_val == "append") {
|
||||
if (args.size() < 3) {
|
||||
error("Not enough args for write submodule of file module");
|
||||
return Value("Error");
|
||||
}
|
||||
if (args[2].valtype != ValueType::String) {
|
||||
error("Expecting a string of content to write to file");
|
||||
return Value("Error");
|
||||
}
|
||||
std::ofstream file(args[1].string_val, std::iostream::app);
|
||||
file << args[2].string_val;
|
||||
file.close();
|
||||
|
||||
} else {
|
||||
error("Unknown submodule of file");
|
||||
return Value("Error");
|
||||
}
|
||||
} else {
|
||||
error("Expecting identifier of submodule in file module");
|
||||
return Value("Error");
|
||||
}
|
||||
return Value("Error");
|
||||
}
|
||||
Reference in New Issue
Block a user