Skip to content

Instantly share code, notes, and snippets.

View benwei's full-sized avatar

ben6 benwei

View GitHub Profile
@benwei
benwei / makefile
Created February 3, 2012 02:57
membuddy.c & makefile
#http://en.wikipedia.org/wiki/Buddy_memory_allocation
#This is a dummy code for Buddy memory allocation implementation.
CFLAGS=-DHAVE_DEV_SYS
LDFLAGS=
CFILES=membuddy.c
OBJS=$(CFILES:.c=.o)
BINNAME=membuddy
all: $(BINNAME)
@benwei
benwei / closure.py
Created July 3, 2012 15:29
closure sample h4
# reference:
# http://stackoverflow.com/questions/3190706/nonlocal-keyword-in-python-2-x
# http://en.wikipedia.org/wiki/Closure_(computer_science)#Example
#
def counter():
d = {'x': 0} # tested with python 2.7.2
def increment(y=1):
# nonlocal only work for python3
#nonlocal x
d['x'] += y
@benwei
benwei / yd
Created November 27, 2012 10:19
yd script is a simple wrapper while cache read word for ydict
#!/bin/sh
YD_HIST_DIR="${HOME}/.ydict_history"
mkdir -p "$YD_HIST_DIR/_relation/"
echo "$@" >> "${YD_HIST_DIR}"/_words.txt
WordCacheFile="${YD_HIST_DIR}"/"$@".txt
WordRelationFile="${YD_HIST_DIR}"/_relation/"$@".txt
if [ ! -f "$WordCacheFile" ]; then
ydict -u -w "$@" | sed -e "s/'/'/g" -e "s/\(.\[36m\)[ ]*/\1 /g" > "$WordCacheFile"
@benwei
benwei / regex_fun.js
Created November 27, 2012 11:11
convert function to that.func (javascript)
// regexp
// convert
// function a (a1,a2)
// that.a = function (a1, a2)
cat jsfile.js | sed -e 's/^\([ \t]*\)function[ \t]*\([a-zA-Z]*\)(/\1that.\2 = function (/g'
@benwei
benwei / BWSpentTime.js
Created December 7, 2012 09:59
SpentTime class for node js
function SpentTime () {
this.reset = function () {
start = new Date();
}
this.diff = function () {
var now = new Date();
return (now.getTime() - start.getTime())
}
var start = new Date();
}
@benwei
benwei / git_over_ssh.md
Created April 1, 2013 10:59
git over ssh note

how to git over ssh

create a base repository

 $ mkdir -p testprj
 $ cd testprj
 $ git init --bare
//
// SOIPAddress.c
//
/*
Copyright (c) 2013, Ben Wei
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
@benwei
benwei / blink_by_serial_input_char_a.ino
Last active December 16, 2015 18:21
blink arduino's led (pin 13) by serial input with char 'a'
@benwei
benwei / cwc_l.sh
Created July 16, 2013 10:43
one line to list line count of c file
#!/bin/sh
find -name "*.c" | while read fn ; do wc -l $fn ; done
@benwei
benwei / vimtips_regex01
Last active August 29, 2015 13:59
vim tips
replace
test-keyworkd : anything ;
to
“test-keyworkd” :“anything”,
regex:
s/^\([ \t]*\)\([a-z-]*\):[ \t]*\(.*\);/\1"\2":"\3",/g