Skip to content

Instantly share code, notes, and snippets.

@kevinlynx
kevinlynx / poster.rb
Last active September 12, 2017 13:59
#!/usr/bin/env ruby
#
# I write this script to post my personl blog codemacro.com posts to my cppblog.com/kevinlynx
# Kevin Lynx 8.7.2012
#
require 'nokogiri'
require 'open-uri'
require 'xmlrpc/client'
require 'yaml'
@kevinlynx
kevinlynx / gist:3413212
Created August 21, 2012 07:47
chat server
class Entity {
public:
enum { CLIENT, SERVER };
Entity(const CGUID &id, int type, const std::string &brief);
// send text to this entity, this function will preprocess `text`
bool Send(const std::string &text);
private:
@kevinlynx
kevinlynx / start_match.cpp
Created August 12, 2013 13:24
match a string using `*' operator i.e: *abc, a*bc
// test.cpp : 定义控制台应用程序的入口点。
//
#include <stdio.h>
#include <vector>
#include <string>
#include <assert.h>
struct OpCode {
/* 0: indicate `*' operator, 1: string match */
#include <assert.h>
typedef unsigned __int64 uint64;
typedef unsigned char byte;
void setBit(byte *data, int pos) {
byte bits[] = {
0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01
};
/* note the position starts from 1, not 0 */
#include <assert.h>
char find(const char *str) {
int cnt = 1;
char c = *str++;
for ( ; *str; ++str) {
c = cnt == 0 ? *str : c;
cnt = cnt + (*str == c ? 1 : (cnt - 1 < 0 ? -cnt : -1));
}
return cnt > 0 ? c : 0;
@kevinlynx
kevinlynx / gen_runjar.bat
Last active December 23, 2015 19:39
create a shell script to run jar specified some jars as class path
echo off
REM
REM Usage: put jars into lib/ext directory, put config file in ext directory
REM gen_runjar.bat
REM Kevin Lynx 09/24/2013
REM
if "%1"=="" goto usage
if not "%1"=="clear" goto dist
del /Q ext\*.*
@kevinlynx
kevinlynx / implicit.scala
Last active December 24, 2015 21:39
implicit parameter
// `s' and `b' are both implicit parameters
def implFn(i: Int)(implicit s: String, b: Int) = {
println(s)
i + b
}
def main(args: Array[String]) {
// otherS will match parameter `s' by type, **not by name**
implicit val otherS = "hello"
// error, two variables with the same type
@kevinlynx
kevinlynx / function-literal.scala
Last active December 24, 2015 21:39
function literal in scala
object Test2 {
//Also: def callFn(a: Int, f: (Int) => Int) = {
def callFn(a: Int, f: Int => Int) = {
a + f(a)
}
// 2 parameters
def callFnMult(a: Int, f: (Int, Int) => Int) = {
a + f(a, a)
@kevinlynx
kevinlynx / scala-jna.scala
Created October 10, 2013 13:10
JNA used in scala
package test
import com.sun.jna._
import com.sun.jna.ptr._
trait Kernel32 extends Library {
def GetDiskFreeSpaceA(lpRootPathName: String,
lpSectorsPerCluster: IntByReference, lpBytesPerSector: IntByReference,
lpNumberOfFreeClusters: IntByReference, lpTotalNumberOfClusters: IntByReference): Boolean
def GetComputerNameW(outName: Array[Char], size: IntByReference): Boolean
package squtest
import org.squeryl.PrimitiveTypeMode._
import org.squeryl.Schema
import org.squeryl.annotations.Column
import org.squeryl.SessionFactory
import org.squeryl.Session
import org.squeryl.KeyedEntity
import org.squeryl.adapters.H2Adapter
import java.util.Date