36 std::filesystem::path path = args[0].asString();
37 std::filesystem::path fullPath = std::filesystem::weakly_canonical(path);
38 return fullPath.string();
44 std::filesystem::path path = args[0].asString();
45 std::ifstream file(path);
50 std::stringstream buffer;
51 buffer << file.rdbuf();
58 std::filesystem::path path = args[0].asString();
59 int64_t lineNum = args[1].asInt();
60 std::ifstream file(path);
63 throw std::runtime_error(
"Could not open file: " + path.string());
65 std::string lineContent;
67 while (std::getline(file, lineContent) && currentLine < lineNum)
77 std::filesystem::path path = args[0].asString();
78 int64_t lineNum = args[1].asInt();
79 std::string content = args[2].asString();
82 std::ifstream inFile(path);
83 if (!inFile.is_open())
85 throw std::runtime_error(
"Could not open file for reading: " + path.string());
88 std::vector<std::string> lines;
90 while (std::getline(inFile, line))
92 lines.push_back(line);
97 while (lines.size() <=
static_cast<size_t>(lineNum))
99 lines.emplace_back(
"");
103 lines[lineNum] = content;
106 std::ofstream outFile(path);
107 if (!outFile.is_open())
109 throw std::runtime_error(
"Could not open file for writing: " + path.string());
112 for (
size_t i = 0; i < lines.size(); ++i)
115 if (i != lines.size() - 1)
127 std::filesystem::path path = args[0].asString();
128 std::ofstream file(path);
131 throw std::runtime_error(
"Could not open file for writing: " + path.string());
133 file << args[1].asString();
140 return std::filesystem::exists(args[0].asString());
146 std::filesystem::path path = args[0].asString();
147 std::ofstream file(path, std::ios::app);
150 throw std::runtime_error(
"Could not open file for writing: " + path.string());
152 file << args[1].asString();
159 std::filesystem::path path = args[0].asString();
160 if (std::filesystem::exists(path))
162 std::filesystem::remove(path);
171 std::filesystem::path src = args[0].asString();
172 std::string dest = args[1].asString();
173 if (std::filesystem::exists(src))
175 std::filesystem::rename(src, dest);
186 return std::filesystem::current_path().string();
189 std::filesystem::path dest = args[0].asString();
190 if (std::filesystem::exists(dest) && std::filesystem::is_directory(dest))
192 std::filesystem::current_path(dest);
193 return std::filesystem::current_path().string();
202 bool overwrite =
false;
203 if (args.size() <= 3 && args.size() >= 2)
205 overwrite = args[2].asBool();
207 std::filesystem::path src = args[0].asString();
208 std::filesystem::path dest = args[1].asString();
210 if (!std::filesystem::exists(src))
212 vm->
logerr(
"Source file doesn't exist.");
217 if (std::filesystem::exists(dest) && !overwrite)
219 vm->
logerr(
"Destination file already exists.");
224 std::ifstream source(src, std::ios::binary | std::ios::in);
225 if (!source.is_open())
227 vm->
logerr(
"Failed to open source file.");
232 std::ofstream destination(dest, std::ios::binary | std::ios::out | std::ios::trunc);
233 if (!destination.is_open())
235 vm->
logerr(
"Failed to open destination file.");
240 destination << source.rdbuf();
242 if (source.fail() || destination.fail())
244 vm->
logerr(
"Error during file copy.");
255 std::filesystem::path src = args[0].asString();
256 std::filesystem::path dest = args[1].asString();
258 status = std::filesystem::copy_file(src, dest);
261 vm->
logerr(
"Failed to copy file during move.");
265 status = std::filesystem::remove(src);
272 if (args[2].isInt() && args[2].asInt() < 0)
274 throw std::runtime_error(
"epoch must be a non-negative integer");
276 std::filesystem::path path = args[0].asString();
277 char param = args[1].asString()[0];
278 int64_t epoch = args[2].asInt();
285 std::filesystem::path path = args[0].asString();
286 char param = args[1].asString()[0];
293 std::filesystem::path path = args[0].asString();
294 std::ofstream file(path);
297 throw std::runtime_error(
"Could not open file: " + path.string());
306 std::string path = args[0].asString();
310 for (
const auto &entry : std::filesystem::directory_iterator(path))
314 result += entry.path().filename().string();
318 catch (
const std::exception &e)
320 return Value(e.what());
327 std::string path = args[0].asString();
334 auto status = std::filesystem::status(path);
335 auto perms = status.permissions();
344 if (std::filesystem::is_directory(status))
348 else if (std::filesystem::is_symlink(status))
358 mode |= ((perms & std::filesystem::perms::owner_read) != std::filesystem::perms::none) ? 0x100 : 0;
359 mode |= ((perms & std::filesystem::perms::owner_write) != std::filesystem::perms::none) ? 0x80 : 0;
360 mode |= ((perms & std::filesystem::perms::owner_exec) != std::filesystem::perms::none) ? 0x40 : 0;
361 mode |= ((perms & std::filesystem::perms::group_read) != std::filesystem::perms::none) ? 0x20 : 0;
362 mode |= ((perms & std::filesystem::perms::group_write) != std::filesystem::perms::none) ? 0x10 : 0;
363 mode |= ((perms & std::filesystem::perms::group_exec) != std::filesystem::perms::none) ? 0x8 : 0;
364 mode |= ((perms & std::filesystem::perms::others_read) != std::filesystem::perms::none) ? 0x4 : 0;
365 mode |= ((perms & std::filesystem::perms::others_write) != std::filesystem::perms::none) ? 0x2 : 0;
366 mode |= ((perms & std::filesystem::perms::others_exec) != std::filesystem::perms::none) ? 0x1 : 0;
369 stat.
fields[
"mode"] =
Value(
static_cast<int64_t
>(mode));
370 stat.
fields[
"nlink"] =
Value(
static_cast<int64_t
>(nlink));
371 stat.
fields[
"uid"] =
Value(
static_cast<int64_t
>(uid));
372 stat.
fields[
"gid"] =
Value(
static_cast<int64_t
>(gid));
373 stat.
fields[
"size"] =
Value(
static_cast<int64_t
>(std::filesystem::file_size(path)));
375 return Value(std::make_shared<Value::StructInstance>(std::move(stat)));
377 catch (
const std::exception &e)
379 vm->
logerr(std::format(
"fstat error: {}", e.what()));
388 std::filesystem::path path = args[0].asString();
389 if (std::filesystem::exists(path))
391 std::filesystem::create_directory(path);
398 std::filesystem::path path = args[0].asString();
399 bool recursive = args[1].asBool();
400 if (std::filesystem::exists(path))
404 if (std::filesystem::remove_all(path) > 0)
409 return 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 void registerFileFunctions(VM *vm)
static Value file_absolute(const std::vector< Value > &args, VM *vm)
Get full path to relative path.
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.
void logerr(const Value &msg)
Log a Value to stderr.
void registerNativeFunction(const std::string &name, NativeFunction fn)
Register a native function.
void flusherr()
Flush stderr.
A value in the Phasor VM.
nlink_t PHASORstd_file_getLinksCount(const char *path)
Retrieves the number of hard links to a file.
int64_t PHASORstd_file_getProperties(char *path, char param)
Get file metadata time property.
bool PHASORstd_file_getOwnerId(const char *path, uid_t *uid, gid_t *gid)
Retrieves the owner identifier of a file.
bool PHASORstd_file_setProperties(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