From 70bae3da93cddc1d0aceaf24c8ae263cfe316aea Mon Sep 17 00:00:00 2001 From: Jakcron Date: Sat, 11 Jun 2022 13:17:33 +0800 Subject: [PATCH] Add util functions to CTRTool --- .../visualstudio/CTRTool/CTRTool.vcxproj | 2 + .../CTRTool/CTRTool.vcxproj.filters | 6 +++ ctrtool/src/util.cpp | 52 +++++++++++++++++++ ctrtool/src/util.h | 15 ++++++ 4 files changed, 75 insertions(+) create mode 100644 ctrtool/src/util.cpp create mode 100644 ctrtool/src/util.h diff --git a/ctrtool/build/visualstudio/CTRTool/CTRTool.vcxproj b/ctrtool/build/visualstudio/CTRTool/CTRTool.vcxproj index c8f7dc3..3648b45 100644 --- a/ctrtool/build/visualstudio/CTRTool/CTRTool.vcxproj +++ b/ctrtool/build/visualstudio/CTRTool/CTRTool.vcxproj @@ -153,6 +153,7 @@ + @@ -172,6 +173,7 @@ + diff --git a/ctrtool/build/visualstudio/CTRTool/CTRTool.vcxproj.filters b/ctrtool/build/visualstudio/CTRTool/CTRTool.vcxproj.filters index 91b9ca0..1955240 100644 --- a/ctrtool/build/visualstudio/CTRTool/CTRTool.vcxproj.filters +++ b/ctrtool/build/visualstudio/CTRTool/CTRTool.vcxproj.filters @@ -66,6 +66,9 @@ Header Files + + Header Files + Header Files @@ -119,5 +122,8 @@ Source Files + + Source Files + \ No newline at end of file diff --git a/ctrtool/src/util.cpp b/ctrtool/src/util.cpp new file mode 100644 index 0000000..c729587 --- /dev/null +++ b/ctrtool/src/util.cpp @@ -0,0 +1,52 @@ +#include "util.h" + +#include +#include +#include + +void ctrtool::writeSubStreamToFile(const std::shared_ptr& in_stream, int64_t offset, int64_t length, const tc::io::Path& out_path, tc::ByteData& cache) +{ + writeStreamToStream(std::make_shared(tc::io::SubStream(in_stream, offset, length)), std::make_shared(tc::io::FileStream(out_path, tc::io::FileMode::Create, tc::io::FileAccess::Write)), cache); +} + +void ctrtool::writeSubStreamToFile(const std::shared_ptr& in_stream, int64_t offset, int64_t length, const tc::io::Path& out_path, size_t cache_size) +{ + writeStreamToStream(std::make_shared(tc::io::SubStream(in_stream, offset, length)), std::make_shared(tc::io::FileStream(out_path, tc::io::FileMode::Create, tc::io::FileAccess::Write)), cache_size); +} + +void ctrtool::writeStreamToFile(const std::shared_ptr& in_stream, const tc::io::Path& out_path, tc::ByteData& cache) +{ + writeStreamToStream(in_stream, std::make_shared(tc::io::FileStream(out_path, tc::io::FileMode::Create, tc::io::FileAccess::Write)), cache); +} + +void ctrtool::writeStreamToFile(const std::shared_ptr& in_stream, const tc::io::Path& out_path, size_t cache_size) +{ + writeStreamToStream(in_stream, std::make_shared(tc::io::FileStream(out_path, tc::io::FileMode::Create, tc::io::FileAccess::Write)), cache_size); +} + +void ctrtool::writeStreamToStream(const std::shared_ptr& in_stream, const std::shared_ptr& out_stream, tc::ByteData& cache) +{ + // iterate thru child files + size_t cache_read_len; + + in_stream->seek(0, tc::io::SeekOrigin::Begin); + out_stream->seek(0, tc::io::SeekOrigin::Begin); + for (int64_t remaining_data = in_stream->length(); remaining_data > 0;) + { + cache_read_len = in_stream->read(cache.data(), cache.size()); + if (cache_read_len == 0) + { + throw tc::io::IOException("ctrtool::writeStreamToStream()", "Failed to read from source streeam."); + } + + out_stream->write(cache.data(), cache_read_len); + + remaining_data -= int64_t(cache_read_len); + } +} + +void ctrtool::writeStreamToStream(const std::shared_ptr& in_stream, const std::shared_ptr& out_stream, size_t cache_size) +{ + tc::ByteData cache = tc::ByteData(cache_size); + writeStreamToStream(in_stream, out_stream, cache); +} \ No newline at end of file diff --git a/ctrtool/src/util.h b/ctrtool/src/util.h new file mode 100644 index 0000000..de4071e --- /dev/null +++ b/ctrtool/src/util.h @@ -0,0 +1,15 @@ +#pragma once +#include "types.h" +#include + +namespace ctrtool +{ + +void writeSubStreamToFile(const std::shared_ptr& in_stream, int64_t offset, int64_t length, const tc::io::Path& out_path, tc::ByteData& cache); +void writeSubStreamToFile(const std::shared_ptr& in_stream, int64_t offset, int64_t length, const tc::io::Path& out_path, size_t cache_size = 0x10000); +void writeStreamToFile(const std::shared_ptr& in_stream, const tc::io::Path& out_path, tc::ByteData& cache); +void writeStreamToFile(const std::shared_ptr& in_stream, const tc::io::Path& out_path, size_t cache_size = 0x10000); +void writeStreamToStream(const std::shared_ptr& in_stream, const std::shared_ptr& out_stream, tc::ByteData& cache); +void writeStreamToStream(const std::shared_ptr& in_stream, const std::shared_ptr& out_stream, size_t cache_size = 0x10000); + +} \ No newline at end of file