Support std::filesystem::path

This commit is contained in:
Federico Cecchetto 2022-06-10 20:27:23 +02:00
parent 97e39635e0
commit 7cd34d8c91
4 changed files with 73 additions and 7 deletions

View File

@ -50,6 +50,20 @@ namespace io
gsc::function::add("directoryisempty", utils::io::directory_is_empty); gsc::function::add("directoryisempty", utils::io::directory_is_empty);
gsc::function::add("listfiles", utils::io::list_files); gsc::function::add("listfiles", utils::io::list_files);
gsc::function::add("removefile", utils::io::remove_file); gsc::function::add("removefile", utils::io::remove_file);
gsc::function::add("removedirectory", [](const std::filesystem::path& src, const scripting::variadic_args& va)
{
bool recursive = false;
if (va.size() > 0)
{
recursive = va[0];
}
utils::io::remove_directory(src, recursive);
});
gsc::function::add("copyfolder", utils::io::copy_folder);
gsc::function::add("copydirectory", utils::io::copy_folder);
gsc::function::add("readfile", static_cast<std::string(*)(const std::string&)>(utils::io::read_file)); gsc::function::add("readfile", static_cast<std::string(*)(const std::string&)>(utils::io::read_file));
} }
}; };

View File

@ -58,7 +58,6 @@ namespace scripting
return typenames[type_]; return typenames[type_];
} }
printf("UNKNOWN TYPE %i\n", type_);
return "unknown"; return "unknown";
} }
@ -134,8 +133,52 @@ namespace scripting
script_value(const vector& value); script_value(const vector& value);
private:
template <typename T> template <typename T>
bool is() const; T get() const
{
if (std::is_constructible<T, std::string>::value && this->is<std::string>()) \
{
return T(this->as<std::string>());
}
throw std::runtime_error("Invalid type");
}
public:
#define ADD_TYPE(type) \
template <> \
bool is<type>() const; \
private: \
template <> \
type get<>() const; \
public: \
template <typename T>
bool is() const
{
if (std::is_constructible<T, std::string>::value && this->is<std::string>()) \
{
return true;
}
return false;
}
ADD_TYPE(bool)
ADD_TYPE(int)
ADD_TYPE(unsigned int)
ADD_TYPE(float)
ADD_TYPE(double)
ADD_TYPE(const char*)
ADD_TYPE(std::string)
ADD_TYPE(vector)
ADD_TYPE(array)
ADD_TYPE(object)
ADD_TYPE(function)
ADD_TYPE(entity)
ADD_TYPE(script_value)
template <typename T> template <typename T>
T as() const T as() const
@ -184,10 +227,6 @@ namespace scripting
variable_value value_{}; variable_value value_{};
private:
template <typename T>
T get() const;
}; };
class function_argument; class function_argument;

View File

@ -120,4 +120,16 @@ namespace utils::io
{ {
std::filesystem::copy(src, target, std::filesystem::copy_options::overwrite_existing | std::filesystem::copy_options::recursive); std::filesystem::copy(src, target, std::filesystem::copy_options::overwrite_existing | std::filesystem::copy_options::recursive);
} }
}
size_t remove_directory(const std::filesystem::path& src, bool recursive)
{
if (recursive)
{
return static_cast<size_t>(std::filesystem::remove_all(src));
}
else
{
return static_cast<size_t>(std::filesystem::remove(src));
}
}
}

View File

@ -18,4 +18,5 @@ namespace utils::io
bool directory_is_empty(const std::string& directory); bool directory_is_empty(const std::string& directory);
std::vector<std::string> list_files(const std::string& directory); std::vector<std::string> list_files(const std::string& directory);
void copy_folder(const std::filesystem::path& src, const std::filesystem::path& target); void copy_folder(const std::filesystem::path& src, const std::filesystem::path& target);
size_t remove_directory(const std::filesystem::path& src, bool recursive = false);
} }