38 std::filesystem::path path = args[0].asString();
39 std::ifstream file(path);
44 std::stringstream buffer;
45 buffer << file.rdbuf();
52 std::filesystem::path path = args[0].asString();
53 int64_t lineNum = args[1].asInt();
54 std::ifstream file(path);
57 throw std::runtime_error(
"Could not open file: " + path.string());
59 std::string lineContent;
61 while (std::getline(file, lineContent) && currentLine < lineNum)
71 std::filesystem::path path = args[0].asString();
72 int64_t lineNum = args[1].asInt();
73 std::string content = args[2].asString();
76 std::ifstream inFile(path);
77 if (!inFile.is_open())
79 throw std::runtime_error(
"Could not open file for reading: " + path.string());
82 std::vector<std::string> lines;
84 while (std::getline(inFile, line))
86 lines.push_back(line);
91 while (lines.size() <=
static_cast<size_t>(lineNum))
93 lines.emplace_back(
"");
97 lines[lineNum] = content;
100 std::ofstream outFile(path);
101 if (!outFile.is_open())
103 throw std::runtime_error(
"Could not open file for writing: " + path.string());
106 for (
size_t i = 0; i < lines.size(); ++i)
109 if (i != lines.size() - 1)
121 std::filesystem::path path = args[0].asString();
122 std::ofstream file(path);
125 throw std::runtime_error(
"Could not open file for writing: " + path.string());
127 file << args[1].asString();
134 return std::filesystem::exists(args[0].asString());
140 std::filesystem::path path = args[0].asString();
141 std::ofstream file(path, std::ios::app);
144 throw std::runtime_error(
"Could not open file for writing: " + path.string());
146 file << args[1].asString();
153 std::filesystem::path path = args[0].asString();
154 if (std::filesystem::exists(path))
156 std::filesystem::remove(path);
165 std::filesystem::path src = args[0].asString();
166 std::string dest = args[1].asString();
167 if (std::filesystem::exists(src))
169 std::filesystem::rename(src, dest);
180 return std::filesystem::current_path().string();
183 std::filesystem::path dest = args[0].asString();
184 if (std::filesystem::exists(dest) && std::filesystem::is_directory(dest))
186 std::filesystem::current_path(dest);
187 return std::filesystem::current_path().string();
196 bool overwrite =
false;
197 if (args.size() <= 3 && args.size() >= 2)
199 overwrite = args[2].asBool();
201 std::filesystem::path src = args[0].asString();
202 std::filesystem::path dest = args[1].asString();
204 if (!std::filesystem::exists(src))
206 std::cerr <<
"Source file doesn't exist." << std::endl;
210 if (!std::filesystem::exists(dest) && !overwrite)
212 std::cerr <<
"Source file doesn't exist." << std::endl;
216 std::ifstream source(src, std::ios::binary | std::ios::in);
217 if (!source.is_open())
219 std::cerr <<
"Failed to open source file." << std::endl;
223 std::ofstream destination(dest, std::ios::binary | std::ios::out | std::ios::trunc);
224 if (!destination.is_open())
226 std::cerr <<
"Failed to open destination file." << std::endl;
230 destination << source.rdbuf();
232 if (source.fail() || destination.fail())
234 std::cerr <<
"Error during file copy." << std::endl;
244 std::filesystem::path src = args[0].asString();
245 std::filesystem::path dest = args[1].asString();
246 std::filesystem::copy_file(src, dest);
247 std::filesystem::remove(src);
254 if (args[2].isInt() && args[2].asInt() < 0)
256 throw std::runtime_error(
"epoch must be a non-negative integer");
258 std::filesystem::path path = args[0].asString();
259 char param = args[1].asString()[0];
260 int64_t epoch = args[2].asInt();
267 std::filesystem::path path = args[0].asString();
268 char param = args[1].asString()[0];
275 std::filesystem::path path = args[0].asString();
276 std::ofstream file(path);
279 throw std::runtime_error(
"Could not open file: " + path.string());
288 std::string path = args[0].asString();
292 for (
const auto &entry : std::filesystem::directory_iterator(path))
296 result += entry.path().filename().string();
300 catch (
const std::exception &e)
302 return Value(e.what());
309 std::string path = args[0].asString();
316 auto status = std::filesystem::status(path);
317 auto perms = status.permissions();
326 if (std::filesystem::is_directory(status))
330 else if (std::filesystem::is_regular_file(status))
334 else if (std::filesystem::is_symlink(status))
344 mode |= ((perms & std::filesystem::perms::owner_read) != std::filesystem::perms::none) ? 0x100 : 0;
345 mode |= ((perms & std::filesystem::perms::owner_write) != std::filesystem::perms::none) ? 0x80 : 0;
346 mode |= ((perms & std::filesystem::perms::owner_exec) != std::filesystem::perms::none) ? 0x40 : 0;
347 mode |= ((perms & std::filesystem::perms::group_read) != std::filesystem::perms::none) ? 0x20 : 0;
348 mode |= ((perms & std::filesystem::perms::group_write) != std::filesystem::perms::none) ? 0x10 : 0;
349 mode |= ((perms & std::filesystem::perms::group_exec) != std::filesystem::perms::none) ? 0x8 : 0;
350 mode |= ((perms & std::filesystem::perms::others_read) != std::filesystem::perms::none) ? 0x4 : 0;
351 mode |= ((perms & std::filesystem::perms::others_write) != std::filesystem::perms::none) ? 0x2 : 0;
352 mode |= ((perms & std::filesystem::perms::others_exec) != std::filesystem::perms::none) ? 0x1 : 0;
355 stat.
fields[
"mode"] =
Value(
static_cast<int64_t
>(mode));
356 stat.
fields[
"nlink"] =
Value(
static_cast<int64_t
>(nlink));
357 stat.
fields[
"uid"] =
Value(
static_cast<int64_t
>(uid));
358 stat.
fields[
"gid"] =
Value(
static_cast<int64_t
>(gid));
359 stat.
fields[
"size"] =
Value(
static_cast<int64_t
>(std::filesystem::file_size(path)));
361 return Value(std::make_shared<Value::StructInstance>(std::move(stat)));
363 catch (
const std::exception &e)
366 std::cerr <<
"fstat error: " << e.what() << std::endl;
374 std::filesystem::path path = args[0].asString();
375 if (std::filesystem::exists(path))
377 std::filesystem::create_directory(path);
384 std::filesystem::path path = args[0].asString();
385 if (std::filesystem::exists(path))
387 std::filesystem::remove(path);
static Value file_current_directory(const std::vector< Value > &args, VM *vm)
Get/set working directory.
static Value file_write_line(const std::vector< Value > &args, VM *vm)
Write a line to file.
static Value file_read_line(const std::vector< Value > &args, VM *vm)
Read a line from file.
static Value file_remove_directory(const std::vector< Value > &args, VM *vm)
static Value file_rename(const std::vector< Value > &args, VM *vm)
Rename file.
static Value file_property_get(const std::vector< Value > &args, VM *vm)
static Value file_exists(const std::vector< Value > &args, VM *vm)
Check if file exists.
static void checkArgCount(const std::vector< Value > &args, size_t minimumArguments, const std::string &name, bool allowMoreArguments=false)
static Value file_read_directory(const std::vector< Value > &args, VM *vm)
static Value file_property_edit(const std::vector< Value > &args, VM *vm)
static Value file_read(const std::vector< Value > &args, VM *vm)
Read file.
static Value file_delete(const std::vector< Value > &args, VM *vm)
Delete file.
static Value file_statistics(const std::vector< Value > &args, VM *vm)
static Value file_copy(const std::vector< Value > &args, VM *vm)
Copy file.
static Value file_create_directory(const std::vector< Value > &args, VM *vm)
static Value file_write(const std::vector< Value > &args, VM *vm)
Write to file.
static Value file_create(const std::vector< Value > &args, VM *vm)
static Value file_append(const std::vector< Value > &args, VM *vm)
Append to file.
static Value file_move(const std::vector< Value > &args, VM *vm)
Move file.
static Value registerFileFunctions(const std::vector< Value > &args, VM *vm)
void registerNativeFunction(const std::string &name, NativeFunction fn)
Register a native function.
A value in the Phasor VM.
nlink_t file_get_links_count(const char *path)
Retrieves the number of hard links to a file.
int64_t file_get_properties(char *path, char param)
Get file metadata time property.
bool file_get_owner_id(const char *path, uid_t *uid, gid_t *gid)
Retrieves the owner identifier of a file.
bool file_set_properties(char *path, char param, int64_t epoch)
Set file metadata time property.
The Phasor Programming Language and Runtime.
std::unordered_map< std::string, Value > fields