43 return std::filesystem::weakly_canonical(std::filesystem::path(args[0].
string())).string();
49 return std::filesystem::path(args[0].
string()).stem().string();
55 return std::filesystem::path(args[0].
string()).filename().string();
61 return std::filesystem::path(args[0].
string()).extension().string();
67 return std::filesystem::path(args[0].
string()).parent_path().string();
73 return std::filesystem::is_directory(args[0].
string());
79 std::filesystem::path path1 = args[0].string();
80 std::filesystem::path path2 = args[1].string();
81 return (path1 / path2).string();
87 return std::filesystem::file_size(args[0].
string());
93 std::filesystem::path path = args[0].string();
94 std::ifstream file(path);
99 std::stringstream buffer;
100 buffer << file.rdbuf();
107 std::filesystem::path path = args[0].string();
108 i64 lineNum = args[1].asInt();
109 std::ifstream file(path);
112 throw std::runtime_error(
"Could not open file: " + path.string());
114 std::string lineContent;
116 while (std::getline(file, lineContent) && currentLine < lineNum)
126 std::filesystem::path path = args[0].string();
127 i64 lineNum = args[1].asInt();
131 std::ifstream inFile(path);
132 if (!inFile.is_open())
134 throw std::runtime_error(
"Could not open file for reading: " + path.string());
138 std::vector<PhsString> lines;
140 while (std::getline(inFile, line))
142 lines.push_back(line);
147 while (lines.size() <=
static_cast<size_t>(lineNum))
149 lines.emplace_back(
"");
153 lines[lineNum] = content;
156 std::ofstream outFile(path);
157 if (!outFile.is_open())
159 throw std::runtime_error(
"Could not open file for writing: " + path.string());
163 for (
size_t i = 0; i < lines.size(); ++i)
166 if (i != lines.size() - 1)
178 std::filesystem::path path = args[0].string();
179 std::ofstream file(path);
182 throw std::runtime_error(
"Could not open file for writing: " + path.string());
185 file << args[1].string();
192 return std::filesystem::exists(args[0].
string());
198 std::filesystem::path path = args[0].string();
199 std::ofstream file(path, std::ios::app);
202 throw std::runtime_error(
"Could not open file for writing: " + path.string());
205 file << args[1].string();
212 std::filesystem::path path = args[0].string();
213 if (std::filesystem::exists(path))
215 return std::filesystem::remove(path);
223 std::filesystem::path src = args[0].string();
224 std::filesystem::path dest = args[1].string();
226 if (!std::filesystem::exists(src))
230 std::filesystem::rename(src, dest, ec);
239 return std::filesystem::current_path().string();
242 std::filesystem::path dest = args[0].string();
243 if (std::filesystem::exists(dest) && std::filesystem::is_directory(dest))
245 std::filesystem::current_path(dest);
246 return std::filesystem::current_path().string();
255 bool overwrite =
false;
256 if (args.size() <= 3 && args.size() >= 2)
258 overwrite = args[2].asBool();
260 std::filesystem::path src = args[0].string();
261 std::filesystem::path dest = args[1].string();
263 if (!std::filesystem::exists(src))
265 vm->
logerr(
"Source file doesn't exist.");
270 if (std::filesystem::exists(dest) && !overwrite)
272 vm->
logerr(
"Destination file already exists.");
277 std::ifstream source(src, std::ios::binary | std::ios::in);
278 if (!source.is_open())
280 vm->
logerr(
"Failed to open source file.");
285 std::ofstream destination(dest, std::ios::binary | std::ios::out | std::ios::trunc);
286 if (!destination.is_open())
288 vm->
logerr(
"Failed to open destination file.");
293 destination << source.rdbuf();
295 if (source.fail() || destination.fail())
297 vm->
logerr(
"Error during file copy.");
308 std::filesystem::path src = args[0].string();
309 std::filesystem::path dest = args[1].string();
311 status = std::filesystem::copy_file(src, dest);
314 vm->
logerr(
"Failed to copy file during move.");
318 status = std::filesystem::remove(src);
325 if (args[2].isInt() && args[2].asInt() < 0)
327 throw std::runtime_error(
"epoch must be a non-negative integer");
329 std::filesystem::path path = args[0].string();
330 char param = args[1].string()[0];
331 i64 epoch = args[2].asInt();
338 std::filesystem::path path = args[0].string();
339 char param = args[1].string()[0];
346 std::filesystem::path path = args[0].string();
347 std::ofstream file(path);
350 throw std::runtime_error(
"Could not open file: " + path.string());
362 std::vector<Value> entries;
363 for (
const auto &entry : std::filesystem::directory_iterator(path.
str()))
365 entries.push_back(
Value(
PhsString(entry.path().filename().string())));
373 std::filesystem::path path = args[0].string();
374 if (std::filesystem::exists(path))
376 std::filesystem::create_directory(path);
383 std::filesystem::path path = args[0].string();
384 bool recursive = args[1].asBool();
385 if (std::filesystem::exists(path))
389 if (std::filesystem::remove_all(path) > 0)
396 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 PhsString file_filename(const std::vector< Value > &args, VM *vm)
Get the filename.
static void registerFileFunctions(VM *vm)
static PhsString file_stem(const std::vector< Value > &args, VM *vm)
Get the stem of a path.
static PhsString file_join_path(const std::vector< Value > &args, VM *vm)
static bool file_move(const std::vector< Value > &args, VM *vm)
Move file.
static PhsString file_read_line(const std::vector< Value > &args, VM *vm)
Read a line from file.
static bool file_write(const std::vector< Value > &args, VM *vm)
Write to file.
static i64 file_property_get(const std::vector< Value > &args, VM *vm)
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 PhsString file_absolute(const std::vector< Value > &args, VM *vm)
Get full path to relative path.
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 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 bool file_copy(const std::vector< Value > &args, VM *vm)
Copy file.
static bool file_property_edit(const std::vector< Value > &args, VM *vm)
static i64 file_get_size(const std::vector< Value > &args, VM *vm)
static bool file_create_directory(const std::vector< Value > &args, VM *vm)
static bool file_delete(const std::vector< Value > &args, VM *vm)
Delete file.
static PhsString file_extension(const std::vector< Value > &args, VM *vm)
Get the extension of a path.
static PhsString file_parent(const std::vector< Value > &args, VM *vm)
Get the parent of a path.
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.
static Value createArray(std::vector< Value > elements={})
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.