Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support -output_path when use unpack #373

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions basisu_tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2552,6 +2552,14 @@ static bool unpack_and_validate_mode(command_line_params &opts)
std::string base_filename;
string_split_path(pInput_filename, nullptr, nullptr, &base_filename, nullptr);

if (opts.m_output_path.size()) {
if (!create_directory_if_not_exists(opts.m_output_path)) {
return false;
}

string_combine_path(base_filename, opts.m_output_path.c_str(), base_filename.c_str());
}

uint8_vec file_data;
if (!basisu::read_file_to_vec(pInput_filename, file_data))
{
Expand Down
31 changes: 30 additions & 1 deletion encoder/basisu_enc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
#include <windows.h>
#endif

#include <filesystem>
#if defined(_WIN32)
namespace fs = std::filesystem;
#else
namespace fs = std::__fs::filesystem;
#endif

namespace basisu
{
uint64_t interval_timer::g_init_ticks, interval_timer::g_freq;
Expand Down Expand Up @@ -505,7 +512,29 @@ namespace basisu

return status;
}


bool create_directory_if_not_exists(std::string &dir) {
fs::path dir_path(dir);

try {
if (!fs::exists(dir_path)) {
if (fs::create_directories(dir_path)) {
// Directory created
} else {
error_printf("Failed to create directory: \"%s\"\n", dir.c_str());
return false;
}
} else if (!fs::is_directory(dir_path)) {
error_printf("Path exists but is not a directory: \"%s\"\n", dir.c_str());
return false;
}
return true;
} catch (const fs::filesystem_error& e) {
error_printf("Filesystem error: \"%s\"\n", e.what());
return false;
}
}

bool read_file_to_vec(const char* pFilename, uint8_vec& data)
{
FILE* pFile = nullptr;
Expand Down
4 changes: 3 additions & 1 deletion encoder/basisu_enc.h
Original file line number Diff line number Diff line change
Expand Up @@ -3274,7 +3274,9 @@ namespace basisu

bool save_png(const char* pFilename, const image& img, uint32_t image_save_flags = 0, uint32_t grayscale_comp = 0);
inline bool save_png(const std::string &filename, const image &img, uint32_t image_save_flags = 0, uint32_t grayscale_comp = 0) { return save_png(filename.c_str(), img, image_save_flags, grayscale_comp); }


bool create_directory_if_not_exists(std::string &dir);

bool read_file_to_vec(const char* pFilename, uint8_vec& data);

bool write_data_to_file(const char* pFilename, const void* pData, size_t len);
Expand Down