Skip to content

Commit

Permalink
simplify some headers, better progress indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
moonshadow565 committed Jun 13, 2022
1 parent ce280d9 commit 94b51be
Show file tree
Hide file tree
Showing 25 changed files with 681 additions and 626 deletions.
14 changes: 6 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ add_subdirectory(dep)
add_library(rlib STATIC
lib/rlib/common.hpp
lib/rlib/common.cpp
lib/rlib/error.cpp
lib/rlib/error.hpp
lib/rlib/fbuffer.cpp
lib/rlib/fbuffer.hpp
lib/rlib/iofile.cpp
lib/rlib/iofile.hpp
lib/rlib/rbundle.cpp
Expand All @@ -22,6 +18,8 @@ add_library(rlib STATIC
lib/rlib/rcache.cpp
lib/rlib/rcdn.hpp
lib/rlib/rcdn.cpp
lib/rlib/rchunk.hpp
lib/rlib/rchunk.cpp
lib/rlib/rmanifest.cpp
lib/rlib/rmanifest.hpp
)
Expand All @@ -37,14 +35,14 @@ target_link_libraries(rman-ls PRIVATE rlib)
add_executable(rman-bl src/rman_bl.cpp)
target_link_libraries(rman-bl PRIVATE rlib)

add_executable(rbun-add src/rbun_add.cpp)
target_link_libraries(rbun-add PRIVATE rlib)

add_executable(rbun-chk src/rbun_chk.cpp)
target_link_libraries(rbun-chk PRIVATE rlib)

add_executable(rbun-ls src/rbun_ls.cpp)
target_link_libraries(rbun-ls PRIVATE rlib)

add_executable(rbun-merge src/rbun_merge.cpp)
target_link_libraries(rbun-merge PRIVATE rlib)

add_executable(rbun-usage src/rbun_usage.cpp)
target_link_libraries(rbun-usage PRIVATE rlib)
target_link_libraries(rbun-usage PRIVATE rlib)
81 changes: 60 additions & 21 deletions lib/rlib/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,61 @@

#include <zstd.h>

#include "error.hpp"
#include <cstdarg>
#include <cstdio>
#include <iomanip>
#include <iostream>

using namespace rlib;

auto rlib::progress(char const* banner, std::uint32_t index, std::uint64_t done, std::uint64_t total) noexcept
-> std::string {
constexpr auto MB = 1024.0 * 1024.0;
void rlib::throw_error(char const* from, char const* msg) {
// break point goes here
throw std::runtime_error(std::string(from) + msg);
}

error_stack_t& rlib::error_stack() noexcept {
thread_local error_stack_t instance = {};
return instance;
}

void rlib::push_error_msg(char const* fmt, ...) noexcept {
va_list args;
char buffer[4096];
int result;
va_start(args, fmt);
result = vsnprintf(buffer, 4096, fmt, args);
va_end(args);
if (result >= 0) {
error_stack().push_back({buffer, buffer + result});
}
}

rlib::progress_bar::progress_bar(char const* banner,
bool disabled,
std::uint32_t index,
std::uint64_t done,
std::uint64_t total) noexcept
: banner_(banner), disabled_(disabled), index_(index), done_(done), total_(total), percent_(done_ * 100 / total_) {
this->render();
}

rlib::progress_bar::~progress_bar() noexcept {
this->render();
std::cerr << std::endl;
}

auto rlib::progress_bar::render() const noexcept -> void {
char buffer[128];
sprintf(buffer, "%s #%u: %4.3f / %4.3f", banner, index, done / MB, total / MB);
return buffer;
sprintf(buffer, "\r%s #%u: %.02fMB %u%%", banner_, index_, total_ / MB, (std::uint32_t)percent_);
std::cerr << buffer;
}

auto rlib::progress_bar::update(std::uint64_t done) noexcept -> void {
done_ = done;
auto percent = std::exchange(percent_, done_ * 100 / total_);
if (!disabled_ && percent != percent_) {
this->render();
}
}

auto rlib::to_hex(std::uint64_t id, std::size_t s) noexcept -> std::string {
Expand All @@ -36,25 +81,19 @@ auto rlib::clean_path(std::string path) noexcept -> std::string {

auto rlib::zstd_decompress(std::span<char const> src, std::size_t count) -> std::span<char const> {
thread_local static std::vector<char> buffer = {};
std::size_t size_decompressed = rlib_assert_zstd(ZSTD_findDecompressedSize(src.data(), src.size()));
rlib_assert(size_decompressed == count);
if (buffer.size() < count) {
buffer.clear();
buffer.resize(count);
}
auto result = ZSTD_decompress(buffer.data(), count, src.data(), src.size());
if (ZSTD_isError(result)) {
rlib_error(ZSTD_getErrorName(result));
}
rlib_assert(result == count);
std::size_t result = rlib_assert_zstd(ZSTD_decompress(buffer.data(), count, src.data(), src.size()));
rlib_assert(result == size_decompressed);
return {buffer.data(), count};
}

auto rlib::try_zstd_decompress(std::span<char const> src, std::size_t count) -> std::span<char const> {
thread_local static std::vector<char> buffer = {};
if (buffer.size() < count) {
buffer.resize(count);
}
auto result = ZSTD_decompress(buffer.data(), count, src.data(), src.size());
if (ZSTD_isError(result)) {
return {};
}
return {buffer.data(), result};
auto rlib::zstd_frame_decompress_size(std::span<char const> src) -> std::size_t {
ZSTD_frameHeader header = {};
rlib_assert_zstd(ZSTD_getFrameHeader(&header, src.data(), src.size()));
return header.frameContentSize;
}
87 changes: 83 additions & 4 deletions lib/rlib/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,107 @@
#include <cstddef>
#include <cstring>
#include <functional>
#include <optional>
#include <span>
#include <stdexcept>
#include <string>
#include <string_view>
#include <system_error>
#include <tuple>
#include <type_traits>
#include <vector>

#define rlib_paste_impl(x, y) x##y
#define rlib_paste(x, y) rlib_paste_impl(x, y)

#define rlib_error(msg) ::rlib::throw_error(__func__, msg)

#define rlib_assert(...) \
do { \
if (!(__VA_ARGS__)) [[unlikely]] { \
::rlib::throw_error(__func__, ": " #__VA_ARGS__); \
} \
} while (false)

#define rlib_rethrow(...) \
[&, func = __func__]() -> decltype(auto) { \
try { \
return __VA_ARGS__; \
} catch (std::exception const&) { \
::rlib::throw_error(func, ": " #__VA_ARGS__); \
} \
}()

#define rlib_trace(...) \
::rlib::ErrorTrace rlib_paste(_trace_, __LINE__) { \
[&] { ::rlib::push_error_msg(__VA_ARGS__); } \
}

#define rlib_assert_zstd(...) \
[&, func = __func__]() -> std::size_t { \
if (std::size_t result = __VA_ARGS__; ZSTD_isError(result)) [[unlikely]] { \
throw_error(func, ZSTD_getErrorName(result)); \
} else { \
return result; \
} \
}()

namespace rlib {
[[noreturn]] extern void throw_error(char const* from, char const* msg);

[[noreturn]] inline void throw_error(char const* from, std::error_code const& ec) {
throw_error(from, ec.message().c_str());
}

using error_stack_t = std::vector<std::string>;

extern error_stack_t& error_stack() noexcept;

extern void push_error_msg(char const* fmt, ...) noexcept;

struct progress_bar {
static constexpr auto MB = 1024.0 * 1024.0;

progress_bar(char const* banner,
bool disabled,
std::uint32_t index,
std::uint64_t done,
std::uint64_t total) noexcept;
~progress_bar() noexcept;

auto update(std::uint64_t done) noexcept -> void;

private:
auto render() const noexcept -> void;

char const* banner_;
bool disabled_ = {};
std::uint32_t index_;
std::uint64_t done_;
std::uint64_t total_;
std::uint64_t percent_;
};

template <typename Func>
struct ErrorTrace : Func {
inline ErrorTrace(Func&& func) noexcept : Func(std::move(func)) {}
inline ~ErrorTrace() noexcept {
if (std::uncaught_exceptions()) {
Func::operator()();
}
}
};
extern auto to_hex(std::uint64_t id, std::size_t s = 16) noexcept -> std::string;

template <typename T>
requires(std::is_enum_v<T>)
inline auto to_hex(T id, std::size_t s = 16) noexcept -> std::string { return to_hex((std::uint64_t)id, s); }

extern auto progress(char const* banner, std::uint32_t index, std::uint64_t done, std::uint64_t total) noexcept
-> std::string;

extern auto clean_path(std::string path) noexcept -> std::string;

extern auto zstd_decompress(std::span<char const> src, std::size_t count) -> std::span<char const>;

extern auto try_zstd_decompress(std::span<char const> src, std::size_t count) -> std::span<char const>;
extern auto zstd_frame_decompress_size(std::span<char const> src) -> std::size_t;

template <auto... M>
inline auto sort_by(auto beg, auto end) noexcept -> void {
Expand Down
28 changes: 0 additions & 28 deletions lib/rlib/error.cpp

This file was deleted.

55 changes: 0 additions & 55 deletions lib/rlib/error.hpp

This file was deleted.

1 change: 0 additions & 1 deletion lib/rlib/fbuffer.cpp

This file was deleted.

Loading

0 comments on commit 94b51be

Please sign in to comment.