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
@shawnfeng0
shawnfeng0 / temp_file_write.c
Last active July 26, 2024 14:09
temp_file_write function
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <sys/file.h>
#include <time.h>
static inline long long get_uptime_ms() {
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
#pragma once
#include <tuple>
#include <type_traits>
template <typename... DestroyFuncs>
class DestroyGuard {
public:
~DestroyGuard() {
call_all_destroy_callbacks(
#include <archive.h>
#include <archive_entry.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <string>
#include <vector>
#include <chrono>
class Timer {
public:
Timer() { start_ = std::chrono::high_resolution_clock::now(); }
~Timer() = default;
template <typename T>
uint64_t elapsed() {
auto now = std::chrono::high_resolution_clock::now();
@shawnfeng0
shawnfeng0 / current_utc_time.c
Created April 27, 2023 18:20 — forked from jbenet/current_utc_time.c
work around lack of clock_gettime in os x
/*
author: jbenet
os x, compile with: gcc -o testo test.c
linux, compile with: gcc -o testo test.c -lrt
*/
#include <time.h>
#include <sys/time.h>
#include <stdio.h>
int main(void) {
std::string raw = "{\"test\": 1}";
Json::Value root;// starts as "null"; will contain the root value after parsing
Json::Reader reader;
reader.parse(raw, root, false);
int test_a = root["test"].asInt();
DEBUG_TOKEN(test_a);
@shawnfeng0
shawnfeng0 / delay_truth.h
Last active August 3, 2022 13:17
DelayTruth: Delay setting true variable. TimeoutTruth: Set true to false after timeout
#include <chrono>
/*
* Delay setting true variable
* test code:
DelayTruth state(2000);
EXPECT_FALSE(state);
state.mark(true);
EXPECT_FALSE(state);
sleep(1);
@shawnfeng0
shawnfeng0 / busybox_httpd_indexcgi.c
Last active July 1, 2022 09:05
Modified the file index graphical interface to be similar to the nginx interface, Original reference: networking/httpd_indexcgi.c
/*
* Copyright (c) 2007 Denys Vlasenko <[email protected]>
*
* Licensed under GPLv2, see file LICENSE in this source tree.
*/
/*
* This program is a CGI application. It outputs directory index page.
* Put it into cgi-bin/index.cgi and chmod 0755.
*/
@shawnfeng0
shawnfeng0 / enum_with_string.h
Last active November 15, 2021 09:40
Define an enum that can be converted to a string
//
// Created by shawnfeng([email protected]) on 2021/11/1.
//
#pragma once
#include <map>
#include <string>
#include <vector>
// reference:
#include <iostream>
#define REFRESH_OUTPUT_LINE(token) \
std::cout << "\r"; \
std::cout << #token << " => " << (token); \
std::cout << "\x1b[K"; \
std::cout.flush();