Skip to content

Instantly share code, notes, and snippets.

View shawnfeng0's full-sized avatar
💭
Keep thinking

Shawn Feng shawnfeng0

💭
Keep thinking
View GitHub Profile
#include <ctype.h>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
uintptr_t HexDumpData(const uint8_t *data, size_t length, size_t width = 16,
uintptr_t base_address = 0, bool tail_addr_out = true) {
const uint8_t *data_cur = data;
if (!data || width == 0) return 0;
while (length) {
@shawnfeng0
shawnfeng0 / trans_wrapper_for_zh-CN
Last active February 4, 2020 23:42
Package trans command line translation software to realize automatic translation into Chinese when the input language is English
#!/bin/bash
target_language=$(trans -id ${!#} | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})*)?m//g" | awk '$1=="Code" && $2!="zh-CN" {printf ":zh-CN"}')
trans $target_language "$@"
@shawnfeng0
shawnfeng0 / fifo_power_of_2.h
Last active November 26, 2020 07:42
Efficient fifo queue class implemented by C++. (Reference from kfifo of linux)
// Reference from kfifo of linux
class FifoPowerOfTwo {
#define _is_power_of_2(x) ((x) != 0 && (((x) & ((x)-1)) == 0))
public:
FifoPowerOfTwo(void *buffer, size_t buf_size, size_t element_size = 1)
: data_((unsigned char *)buffer),
element_size_(element_size != 0 ? element_size : 1),
is_allocated_memory_(false) {
size_t num_elements = buf_size / element_size_;
@shawnfeng0
shawnfeng0 / slog.h
Last active August 19, 2020 03:52
Single header log library
#pragma once
#include <stdio.h>
#include <string.h>
// Precompiler define to get only filename;
#if !defined(__FILENAME__)
#define __FILENAME__ \
(strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 \
: strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 \
@shawnfeng0
shawnfeng0 / L_TOKEN.h
Created September 18, 2020 14:34
C++ version of LOGGER_TOKEN
#include <ulog/ulog.h>
namespace ulog {
namespace token {
// void *
inline void print(const char *name, const void *value) {
logger_raw(false, "%s => %p", name ? name : "unnamed", value);
}
void *memcpy(void *dst, void *src, unsigned n) {
if (!dst || !src) {
return nullptr;
}
auto dst_c = (char *) dst;
auto src_c = (char *) src;
if (dst_c < src || dst_c > (src_c + n)) {
while (n--) {
template<typename T>
static inline auto RoundPowOfTwo(T n) -> decltype(n) {
uint64_t value = n;
// Fill 1
value |= value >> 1U;
value |= value >> 2U;
value |= value >> 4U;
value |= value >> 8U;
value |= value >> 16U;
@shawnfeng0
shawnfeng0 / sockaddr_tostr.h
Created December 23, 2020 04:56 — forked from jkomyno/sockaddr_tostr.h
Convert a struct sockaddr address to a string, IPv4 and IPv6
// Convert a struct sockaddr address to a string, IPv4 and IPv6:
char *get_ip_str(const struct sockaddr *sa, char *s, size_t maxlen)
{
switch(sa->sa_family) {
case AF_INET:
inet_ntop(AF_INET, &(((struct sockaddr_in *)sa)->sin_addr),
s, maxlen);
break;
@shawnfeng0
shawnfeng0 / split.h
Created March 2, 2021 05:59
string split function for C++
#include <vector>
#include <string>
inline std::vector<std::string> Split(const std::string &s, const std::string &delimiters = " ") {
std::vector<std::string> tokens;
std::string::size_type start = s.find_first_not_of(delimiters, 0);
std::string::size_type end = s.find_first_of(delimiters, start);
while (std::string::npos != end || std::string::npos != start) {
tokens.emplace_back(s.substr(start, end - start));
start = s.find_first_not_of(delimiters, end);
@shawnfeng0
shawnfeng0 / wpa_ssid_convert_utf8.cc
Last active March 29, 2021 07:53
wpa_ssid_convert_utf8
unsigned char HexNumber(char c) {
if (c >= '0' && c <= '9') {
return c - '0';
} else if (c >= 'a' && c <= 'f') {
return c - 'a' + 10;
} else if (c >= 'A' && c <= 'F') {
return c - 'A' + 10;
} else {
return 0;
}