Skip to content

Commit

Permalink
Vutils
Browse files Browse the repository at this point in the history
  • Loading branch information
vic4key committed Aug 28, 2023
1 parent 78c8bcb commit 2ae0b9d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
14 changes: 8 additions & 6 deletions include/Vutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -617,10 +617,10 @@ bool vuapi write_file_binary_A(const std::string& file_path, const std::vector<b
bool vuapi write_file_binary_W(const std::wstring& file_path, const std::vector<byte>& data);
bool vuapi write_file_binary_A(const std::string& file_path, const byte* data, const size_t size);
bool vuapi write_file_binary_W(const std::wstring& file_path, const byte* data, const size_t size);
std::string vuapi clean_file_name_A(
const std::string& file_name, const bool include_space_char = false, const char replacement_char = '-');
std::wstring vuapi clean_file_name_W(
const std::wstring& file_name, const bool include_space_char = false, const wchar_t replacement_char = L'-');
bool vuapi is_file_name_valid_A(const std::string& file_name);
bool vuapi is_file_name_valid_W(const std::wstring& file_name);
std::string vuapi correct_file_name_A(const std::string& file_name, const char replacement_char = '-');
std::wstring vuapi correct_file_name_W(const std::wstring& file_name, const wchar_t replacement_char = L'-');
std::string vuapi get_file_type_A(const std::string& file_path);
std::wstring vuapi get_file_type_W(const std::wstring& file_path);
std::string vuapi extract_file_directory_A(const std::string& file_path, bool last_slash = true);
Expand Down Expand Up @@ -776,7 +776,8 @@ void vuapi crypt_sha_buffer(
#define is_directory_exists is_directory_exists_W
#define is_file_exists is_file_exists_W
#define get_file_type get_file_type_W
#define clean_file_name clean_file_name_W
#define is_file_name_valid is_file_name_valid_W
#define correct_file_name correct_file_name_W
#define read_file_binary read_file_binary_W
#define write_file_binary write_file_binary_W
#define extract_file_directory extract_file_directory_W
Expand Down Expand Up @@ -848,7 +849,8 @@ void vuapi crypt_sha_buffer(
#define is_directory_exists is_directory_exists_A
#define is_file_exists is_file_exists_A
#define get_file_type get_file_type_A
#define clean_file_name clean_file_name_A
#define is_file_name_valid is_file_name_valid_A
#define correct_file_name correct_file_name_A
#define read_file_binary read_file_binary_A
#define write_file_binary write_file_binary_A
#define extract_file_directory extract_file_directory_A
Expand Down
33 changes: 23 additions & 10 deletions src/details/filedir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <comdef.h>
#include <wbemidl.h>

static const std::wstring FILE_NAME_FORBIDDEN_CHARS = L"\\/:?\"<>|";

namespace vu
{

Expand Down Expand Up @@ -67,27 +69,38 @@ std::wstring vuapi get_file_type_W(const std::wstring& file_path)
return s;
}

std::string vuapi clean_file_name_A(
const std::string& file_name, const bool include_space_char, const char replacement_char)
bool vuapi is_file_name_valid_A(const std::string& file_name)
{
auto file_name_W = to_string_W(file_name);
file_name_W = clean_file_name(file_name_W, include_space_char, wchar_t(replacement_char));
return to_string_A(file_name_W);
return is_file_name_valid_W(file_name_W);
}

std::wstring vuapi clean_file_name_W(
const std::wstring& file_name, const bool include_space_char, const wchar_t replacement_char)
bool vuapi is_file_name_valid_W(const std::wstring& file_name)
{
std::wstring forbidden_chars(L"\\/:?\"<>|");
if (include_space_char)
for (auto c : FILE_NAME_FORBIDDEN_CHARS)
{
forbidden_chars += L" ";
if (file_name.find(c) != std::wstring::npos)
{
return false;
}
}

return true;
}

std::string vuapi correct_file_name_A(const std::string& file_name, const char replacement_char)
{
auto file_name_W = to_string_W(file_name);
file_name_W = correct_file_name_W(file_name_W, wchar_t(replacement_char));
return to_string_A(file_name_W);
}

std::wstring vuapi correct_file_name_W(const std::wstring& file_name, const wchar_t replacement_char)
{
std::wstring tmp = file_name;
std::replace_if(tmp.begin(), tmp.end(), [&](wchar_t c) -> bool
{
return std::wstring::npos != forbidden_chars.find(c);
return std::wstring::npos != FILE_NAME_FORBIDDEN_CHARS.find(c);
}, replacement_char);

return tmp;
Expand Down

0 comments on commit 2ae0b9d

Please sign in to comment.