42 return std::filesystem::weakly_canonical(std::filesystem::path(args[0].asString())).string();
48 return std::filesystem::path(args[0].asString()).stem().string();
54 return std::filesystem::path(args[0].asString()).filename().string();
60 return std::filesystem::path(args[0].asString()).extension().string();
66 return std::filesystem::path(args[0].asString()).parent_path().string();
72 return std::filesystem::is_directory(args[0].asString());
78 std::filesystem::path path1 = args[0].asString();
79 std::filesystem::path path2 = args[1].asString();
80 return (path1 / path2).string();
86 return std::filesystem::file_size(args[0].asString());
92 std::filesystem::path path = args[0].asString();
93 std::ifstream file(path);
98 std::stringstream buffer;
99 buffer << file.rdbuf();
106 std::filesystem::path path = args[0].asString();
107 int64_t lineNum = args[1].asInt();
108 std::ifstream file(path);
111 throw std::runtime_error(
"Could not open file: " + path.string());
113 std::string lineContent;
115 while (std::getline(file, lineContent) && currentLine < lineNum)
125 std::filesystem::path path = args[0].asString();
126 int64_t lineNum = args[1].asInt();
127 std::string content = args[2].asString();
130 std::ifstream inFile(path);
131 if (!inFile.is_open())
133 throw std::runtime_error(
"Could not open file for reading: " + path.string());
137 std::vector<std::string> lines;
139 while (std::getline(inFile, line))
141 lines.push_back(line);
146 while (lines.size() <=
static_cast<size_t>(lineNum))
148 lines.emplace_back(
"");
152 lines[lineNum] = content;
155 std::ofstream outFile(path);
156 if (!outFile.is_open())
158 throw std::runtime_error(
"Could not open file for writing: " + path.string());
162 for (
size_t i = 0; i < lines.size(); ++i)
165 if (i != lines.size() - 1)
177 std::filesystem::path path = args[0].asString();
178 std::ofstream file(path);
181 throw std::runtime_error(
"Could not open file for writing: " + path.string());
184 file << args[1].asString();
191 return std::filesystem::exists(args[0].asString());
197 std::filesystem::path path = args[0].asString();
198 std::ofstream file(path, std::ios::app);
201 throw std::runtime_error(
"Could not open file for writing: " + path.string());
204 file << args[1].asString();
211 std::filesystem::path path = args[0].asString();
212 if (std::filesystem::exists(path))
214 return std::filesystem::remove(path);
222 std::filesystem::path src = args[0].asString();
223 std::filesystem::path dest = args[1].asString();
225 if (!std::filesystem::exists(src))
229 std::filesystem::rename(src, dest, ec);
238 return std::filesystem::current_path().string();
241 std::filesystem::path dest = args[0].asString();
242 if (std::filesystem::exists(dest) && std::filesystem::is_directory(dest))
244 std::filesystem::current_path(dest);
245 return std::filesystem::current_path().string();
254 bool overwrite =
false;
255 if (args.size() <= 3 && args.size() >= 2)
257 overwrite = args[2].asBool();
259 std::filesystem::path src = args[0].asString();
260 std::filesystem::path dest = args[1].asString();
262 if (!std::filesystem::exists(src))
264 vm->
logerr(
"Source file doesn't exist.");
269 if (std::filesystem::exists(dest) && !overwrite)
271 vm->
logerr(
"Destination file already exists.");
276 std::ifstream source(src, std::ios::binary | std::ios::in);
277 if (!source.is_open())
279 vm->
logerr(
"Failed to open source file.");
284 std::ofstream destination(dest, std::ios::binary | std::ios::out | std::ios::trunc);
285 if (!destination.is_open())
287 vm->
logerr(
"Failed to open destination file.");
292 destination << source.rdbuf();
294 if (source.fail() || destination.fail())
296 vm->
logerr(
"Error during file copy.");
307 std::filesystem::path src = args[0].asString();
308 std::filesystem::path dest = args[1].asString();
310 status = std::filesystem::copy_file(src, dest);
313 vm->
logerr(
"Failed to copy file during move.");
317 status = std::filesystem::remove(src);
324 if (args[2].isInt() && args[2].asInt() < 0)
326 throw std::runtime_error(
"epoch must be a non-negative integer");
328 std::filesystem::path path = args[0].asString();
329 char param = args[1].asString()[0];
330 int64_t epoch = args[2].asInt();
337 std::filesystem::path path = args[0].asString();
338 char param = args[1].asString()[0];
345 std::filesystem::path path = args[0].asString();
346 std::ofstream file(path);
349 throw std::runtime_error(
"Could not open file: " + path.string());
359 std::string path = args[0].asString();
361 for (
const auto &entry : std::filesystem::directory_iterator(path))
365 result += entry.path().filename().string();
375 std::filesystem::path path = args[0].asString();
376 if (std::filesystem::exists(path))
378 std::filesystem::create_directory(path);
385 std::filesystem::path path = args[0].asString();
386 bool recursive = args[1].asBool();
387 if (std::filesystem::exists(path))
391 if (std::filesystem::remove_all(path) > 0)
398 return std::filesystem::remove(path);
static bool file_remove_directory(const std::vector< Value > &args, VM *vm)
static Value file_current_directory(const std::vector< Value > &args, VM *vm)
Get/set working directory.
static bool file_rename(const std::vector< Value > &args, VM *vm)
Rename file.
static std::string file_absolute(const std::vector< Value > &args, VM *vm)
Get full path to relative path.
static int64_t file_get_size(const std::vector< Value > &args, VM *vm)
static void registerFileFunctions(VM *vm)
static bool file_move(const std::vector< Value > &args, VM *vm)
Move file.
static bool file_write(const std::vector< Value > &args, VM *vm)
Write to file.
static std::string file_read_line(const std::vector< Value > &args, VM *vm)
Read a line from file.
static bool file_append(const std::vector< Value > &args, VM *vm)
Append to file.
static bool file_is_directory(const std::vector< Value > &args, VM *vm)
Check if path is directory.
static bool 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 bool file_create(const std::vector< Value > &args, VM *vm)
static std::string file_extension(const std::vector< Value > &args, VM *vm)
Get the extension of a path.
static Value file_read_directory(const std::vector< Value > &args, VM *vm)
static bool file_write_line(const std::vector< Value > &args, VM *vm)
Write a line to file.
static Value file_read(const std::vector< Value > &args, VM *vm)
Read file.
static int64_t file_property_get(const std::vector< Value > &args, VM *vm)
static bool file_copy(const std::vector< Value > &args, VM *vm)
Copy file.
static std::string file_stem(const std::vector< Value > &args, VM *vm)
Get the stem of a path.
static bool file_property_edit(const std::vector< Value > &args, VM *vm)
static std::string file_filename(const std::vector< Value > &args, VM *vm)
Get the filename.
static bool file_create_directory(const std::vector< Value > &args, VM *vm)
static std::string file_parent(const std::vector< Value > &args, VM *vm)
Get the parent of a path.
static bool file_delete(const std::vector< Value > &args, VM *vm)
Delete file.
static std::string file_join_path(const std::vector< Value > &args, VM *vm)
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.
int64_t PHASORstd_file_getProperties(char *path, char param)
Get file metadata time property.
bool PHASORstd_file_setProperties(char *path, char param, int64_t epoch)
Set file metadata time property.
The Phasor Programming Language and Runtime.