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 1a39fdf commit 78c8bcb
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 33 deletions.
10 changes: 10 additions & 0 deletions include/Vutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -609,10 +609,18 @@ bool vuapi is_directory_exists_A(const std::string& directory);
bool vuapi is_directory_exists_W(const std::wstring& directory);
bool vuapi is_file_exists_A(const std::string& file_path);
bool vuapi is_file_exists_W(const std::wstring& file_path);
std::unique_ptr<std::vector<vu::byte>> vuapi read_file_binary_A(const std::string& file_path);
std::unique_ptr<std::vector<vu::byte>> vuapi read_file_binary_W(const std::wstring& file_path);
bool vuapi read_file_binary_A(const std::string& file_path, std::vector<byte>& data);
bool vuapi read_file_binary_W(const std::wstring& file_path, std::vector<byte>& data);
bool vuapi write_file_binary_A(const std::string& file_path, const std::vector<byte>& data);
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'-');
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 @@ -768,6 +776,7 @@ 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 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 @@ -839,6 +848,7 @@ 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 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
92 changes: 59 additions & 33 deletions src/details/filedir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,35 +67,42 @@ std::wstring vuapi get_file_type_W(const std::wstring& file_path)
return s;
}

bool vuapi read_file_binary_A(const std::string& file_path, std::vector<byte>& data)
std::string vuapi clean_file_name_A(
const std::string& file_name, const bool include_space_char, const char replacement_char)
{
FILE* file = nullptr;
fopen_s(&file, file_path.c_str(), "rb");
if (file == nullptr)
{
return false;
}

fseek(file, 0, SEEK_END);
size_t file_size = ftell(file);
fseek(file, 0, SEEK_SET);
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);
}

if (data.empty() || data.size() > file_size)
std::wstring vuapi clean_file_name_W(
const std::wstring& file_name, const bool include_space_char, const wchar_t replacement_char)
{
std::wstring forbidden_chars(L"\\/:?\"<>|");
if (include_space_char)
{
data.resize(file_size);
forbidden_chars += L" ";
}

std::fill(data.begin(), data.end(), 0);

size_t read_bytes = fread(&data[0], 1, data.size(), file);
std::wstring tmp = file_name;
std::replace_if(tmp.begin(), tmp.end(), [&](wchar_t c) -> bool
{
return std::wstring::npos != forbidden_chars.find(c);
}, replacement_char);

fclose(file);
return tmp;
}

return read_bytes == data.size();
bool vuapi read_file_binary_A(const std::string& file_path, std::vector<byte>& data)
{
auto file_path_W = to_string_W(file_path);
return read_file_binary_W(file_path_W, data);
}

bool vuapi read_file_binary_W(const std::wstring& file_path, std::vector<byte>& data)
{
data.clear();

FILE* file = nullptr;
_wfopen_s(&file, file_path.c_str(), L"rb");
if (file == nullptr)
Expand All @@ -121,32 +128,51 @@ bool vuapi read_file_binary_W(const std::wstring& file_path, std::vector<byte>&
return read_bytes == data.size();
}

bool vuapi write_file_binary_A(const std::string& file_path, const std::vector<byte>& data)
std::unique_ptr<std::vector<vu::byte>> vuapi read_file_binary_A(const std::string& file_path)
{
auto file_path_W = to_string_W(file_path);
return read_file_binary_W(file_path_W);
}

std::unique_ptr<std::vector<vu::byte>> vuapi read_file_binary_W(const std::wstring& file_path)
{
std::vector<vu::byte> data;
read_file_binary_W(file_path, data);
if (data.empty())
{
return false;
nullptr;
}

FILE* file = nullptr;
fopen_s(&file, file_path.c_str(), "wb");
if (file == nullptr)
std::unique_ptr<std::vector<vu::byte>> ptr(new std::vector<vu::byte>);
ptr->swap(data);
return ptr;
}

bool vuapi write_file_binary_A(const std::string& file_path, const byte* data, const size_t size)
{
auto file_path_W = to_string_W(file_path);
return write_file_binary_W(file_path_W, data, size);
}

bool vuapi write_file_binary_W(const std::wstring& file_path, const std::vector<byte>& data)
{
if (data.empty())
{
return false;
}

fseek(file, 0, SEEK_END);

size_t written_bytes = fwrite(&data[0], 1, data.size(), file);

fclose(file);
return write_file_binary_W(file_path , &data[0], data.size());
}

return written_bytes == data.size();
bool vuapi write_file_binary_A(const std::string& file_path, const std::vector<byte>& data)
{
auto file_path_W = to_string_W(file_path);
return write_file_binary_W(file_path_W, data);
}

bool vuapi write_file_binary_W(const std::wstring& file_path, const std::vector<byte>& data)
bool vuapi write_file_binary_W(const std::wstring& file_path, const byte* data, const size_t size)
{
if (data.empty())
if (data == nullptr || size == 0)
{
return false;
}
Expand All @@ -160,11 +186,11 @@ bool vuapi write_file_binary_W(const std::wstring& file_path, const std::vector<

fseek(file, 0, SEEK_END);

size_t written_bytes = fwrite(&data[0], 1, data.size(), file);
size_t written_bytes = fwrite(data, 1, size, file);

fclose(file);

return written_bytes == data.size();
return written_bytes == size;
}

bool vuapi is_file_exists_A(const std::string& file_path)
Expand Down

0 comments on commit 78c8bcb

Please sign in to comment.