Miscellaneous skills for C, C++11/14/17/20, Python2/3, ECMAScript5/6 and Shell Script.
Given a byte count, converts it to human-readable format and returns a string consisting of a value and a units indicator.
Depending on the size of the value, the units part is bytes, KB (kibibytes), MB (mebibytes), GB (gibibytes), TB (tebibytes), or PB (pebibytes)...
Function Prototype
template<typename CharT, typename ByteT>
CharT const* format_bytes(std::basic_string<CharT>& repr // (1)
, ByteT const bytes
, std::size_t const decimal=2u
, std::size_t const reduced_unit=1024u);
template<typename CharT, typename ByteT, typename IndicatorT
, typename = typename std::enable_if<!std::is_integral<IndicatorT>::value>::type>
CharT const* format_bytes(std::basic_string<CharT>& repr // (2)
, ByteT const bytes
, IndicatorT&& indicator
, std::size_t const decimal=2u
, std::size_t const reduced_unit=1024u);
template<typename CharT, typename ByteT, typename InputIt>
CharT const* format_bytes(std::basic_string<CharT>& repr // (3)
, ByteT const bytes
, InputIt first, InputIt last
, std::size_t const decimal=2u
, std::size_t const reduced_unit=1024u);
template<typename CharT, typename ByteT
, typename InputIt, typename IndicatorT>
CharT const* format_bytes(std::basic_string<CharT>& repr // (4)
, ByteT const bytes
, InputIt first, InputIt last
, IndicatorT&& indicator
, std::size_t const decimal=2u
, std::size_t const reduced_unit=1024u);
Usage
using namespace ymh::misc;
std::string s;
std::cout << format_bytes(s, 18446640) << std::endl;
equal to:
std::wstring wcs; // unicode
auto indicators = { "Bytes", "KB", "MB", "GB" };
format_bytes(s, 18446640
, std::begin(indicators), std::end(indicators)
, "MB", 2u, 1024u);
Output
17.60 MB
Save std::error_code, boost.system, GetLastError(), user custom error code and error message to class [w]error_t.
Query error information by dump() or dump_backtrace() from class [w]error_t, it can query error domain, value, message by the numbers of [w]error_t.
Function Prototype
(1) User Custom Error
SET_ERROR_CUSTOM[W](error_t&, domain, value, format_string, ...);
SET_ERROR_MESSAGE[W](error_t&, value, format_string, ...);
SET_ERROR_STRING[W](error_t&, format_string, ...);
(2) std::error_code or boost.system
SET_ERROR_CODE[W](error_t&, error_code);
MAKE_ERROR_CODE[W](error_t&, errc_t);
(3) System Error
SET_SYSTEM_ERROR[W](error_t&, ::GetLastError()); // errno
(4) Catch Exception
ERROR_TRY[W] {
// throw exception
} ERROR_CATCH[W](error_t&)
(5) Print Error Message to Stream
void error_t::dump(std::basic_ostream<charT>&);
void error_t::dump_backtrace(std::basic_ostream<charT>&);
Usage
using namespace ymh;
error_t err;
SET_ERROR_STRING(err, "Open file %s failed", "error.log");
if (err) // return true if error occur
{
SET_SYSTEM_ERROR(err, ::GetLastError());
}
Output
File "xxx.cpp", line ?, in <function>: Open file error.log failed
File "xxx.cpp", line ?, in <function>: Success
Unicode and ANSI (byte string) conversion is supported by class codec
.
Convert wide char to multi bytes by encode<codepage::cp_xxx, bom::nobomb>
and reverse it by decode<codepage::cp_xxx, bom::nobomb>
. It can convert one
multi bytes to another multi bytes by convert<...>
.
Others, it will try convert to local codepage if you use file_text
to read
file's contents. You can save multi bytes to file by save_file_text<...>
.
Attention : may throw std::system_error
exception if failed.
Function Prototype
(1) Multi Bytes to Wide Char
std::wstring wtext = decode<codepage::cp_utf8>(u8"utf-8 string");
wtext = UTF8ToUnicode<bom::nobomb>(u8"utf-8 string");"
(2) Wide Char to Multi Bytes
std::string text = encode<codepage::cp_utf8>(L"wide string");
text = UnicodeToUTF8<bom::bomb>(L"wide string");
(3) Convert between Multi Bytes
std::string ntext = convert<codepage::cp_utf8>("ansi string");
(4) Read/Save File Text
std::wstring wtext = read_file_text(L"demo.txt");
save_file_text<codepage::cp_utf8, bom::bomb>("demo.txt", "bingo");
Usage
using namespace ymh;
try
{
auto text = file_text("demo.txt");
auto wtext = decode(text);
}
catch (std::system_error const& e)
{
}
Measure compression ratio. zlib
, gzip
, bz2
, lzma
is supported.
Usage
usage: mcr.py [-h] [--verbose] [--chunk-size {4,8,16,32,64,128,256,512}]
[--name {gzip,all,zlib,bz2}]
[--level {-2,-1,0,1,2,3,4,5,6,7,8,9}]
file
Measure compression ratio.
positional arguments:
file file or directory
optional arguments:
-h, --help show this help message and exit
--verbose print progress status (default: None)
--chunk-size {4,8,16,32,64,128,256,512}
data chunk's size (metric: KB) (default: 16)
--name {gzip,all,zlib,bz2}
compression algorithm's name (default: all)
--level {-2,-1,0,1,2,3,4,5,6,7,8,9}
controlling the level of compression, all = -2,
default = -1 (default: -2)
Example
$ python3 mcr.py --level=-1 /dev/vda
File: /dev/vda, Length: 40.0 GB, Chunk Size: 16.0 KB
NAME LVL OUTSIZE EXPIRED %SAV IN/ps OUT/ps RATIO %PROG REMAIN
bz2 9 6.61 GB 1.1 h 83.48 10.33 MBps 1.71 MBps 6.05 100.0 0.0 s
zlib -1 8.74 GB 14.93 m 78.15 45.72 MBps 9.99 MBps 4.58 100.0 0.0 s
gzip 9 8.75 GB 50.93 m 78.13 13.4 MBps 2.93 MBps 4.57 100.0 0.0 s
lzma 0 4.43 GB 3.03 h 88.93 3.75 MBps 425.55 KBps 9.03 100.0 0.0 s