Skip to content

Instantly share code, notes, and snippets.

@alenstarx
alenstarx / qt5-overloaded.cpp
Created March 26, 2018 07:42
Connecting overloaded signals and slots in Qt 5
struct Foo {
void overloadedFunction();
void overloadedFunction(int, QString);
};
// requires C++14
qOverload<>(&Foo:overloadedFunction)
qOverload<int, QString>(&Foo:overloadedFunction)
// same, with C++11
@alenstarx
alenstarx / fork.c
Created December 17, 2017 06:54
fork
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
struct process_info {
char* name;
char* path;
@alenstarx
alenstarx / youdao.py
Last active September 3, 2017 13:29
youdao translation (python3 implementation)
#! /usr/bin/python3
# coding = utf-8
# youdao.py
# To-do: get meaning of a word from youdao.com
# Author: alen
# Date: 2017/08/05
import urllib.parse
import urllib.request
@alenstarx
alenstarx / websocket.js
Created May 27, 2017 08:59
websocket client for javascript (ES6)
const WsStateDisconnected = 0;
const WsStateDisconnecting = 1;
const WsStateConnected = 2;
const WsStateConnecting = 3;
class SimpleWSocket {
constructor(url) {
this.wsState = WsStateDisconnected;
this.timer = null;
this.url = url;
this.ws = null;
@alenstarx
alenstarx / callmethod.go
Created May 27, 2017 08:23
callmethod by reflect (golang)
package main
// copy from
// https://stackoverflow.com/questions/14116840/dynamically-call-method-on-interface-regardless-of-receiver-type
import (
"fmt"
"reflect"
)
type Test struct {
Start string
@alenstarx
alenstarx / sqlfile.go
Last active May 27, 2017 07:41
executor for sql file 执行sql文件中的sql语句
package main
import (
"bufio"
"database/sql"
"github.com/alenstar/nanoweb/log"
_ "github.com/go-sql-driver/mysql"
"os"
"strings"
)
@alenstarx
alenstarx / postjson.js
Created May 5, 2017 15:19
Post application/json in gjs
const Lang = imports.lang;
const Soup = imports.gi.Soup;
let _session = new Soup.SessionAsync();
function POSTJSON(url, params, callback) {
log('post started');
let request = Soup.Message.new('POST', url);
log('req created');
let _params = JSON.stringify(params)
@alenstarx
alenstarx / post.js
Created May 5, 2017 15:18
POST requests in gjs
const Lang = imports.lang;
const Soup = imports.gi.Soup;
let _session = new Soup.SessionAsync();
function POST(url, params, callback) {
let request = Soup.Message.new('POST', url);
let _params = Soup.form_encode_hash(params);
request.set_request('application/x-www-form-urlencoded',
@alenstarx
alenstarx / get.js
Created May 5, 2017 15:18
GET requests in gjs with libsoup
const Lang = imports.lang;
const Soup = imports.gi.Soup;
let _session = new Soup.SessionAsync();
function GET(url, callback) {
let request = Soup.Message.new('GET', url);
_session.queue_message(request, Lang.bind(this,
function(session, message) {
#ifndef UTIL_STRING_UTILS_HPP
#define UTIL_STRING_UTILS_HPP
// trim from start
static inline std::string &string_ltrim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
return s;
}
// trim from end