;============================================================
; 2007/xx/xx
; S.Programs HHX
; 汎用ライブラリ (HSP 3.0)
; http://sprocket.babyblue.jp/
;============================================================
;============================================================
; Windows Timer
#module
; winapi
#uselib "user32"
#func SetTimer "SetTimer" int, int, int, int
#func KillTimer "KillTimer" int, int
;--------------------------------------------------
#define global timer_setlabel(%1) oncmd gosub %1, 0x113
;--------------------------------------------------
#deffunc timer_start
SetTimer hwnd, 333, 33, 0
return
;--------------------------------------------------
#deffunc timer_stop
KillTimer hwnd, 333
return
#global
;============================================================
; wstrlen
; ダブルバイト対応 文字数カウント
;#uselib "kernel32"
;#cfunc wstrlen "lstrlenW" wptr
#module
; winapi
#uselib "kernel32"
#cfunc MultiByteToWideChar "MultiByteToWideChar" int, int, var, int, int, int
;--------------------------------------------------
#defcfunc wstrlen var v
return (MultiByteToWideChar(0, 0, v, -1, 0, 0) - 1)
#global
;============================================================
; BENCH macro
#uselib "winmm"
#cfunc global d3timer "timeGetTime"
#define global B_ST(%1="test") bench_ot = d3timer() : bench_titl = %1
#define global B_EN dialog bench_titl + " - " + (d3timer() - bench_ot) + "ms"
#define global B_I1 bench_i = 0
#define global B_I2 bench_iot = d3timer()
#define global B_I3 bench_i += d3timer() - bench_iot
#define global B_I4 dialog "B_I - " + bench_i + "ms"
;============================================================
; プロセス間 メモリ共有 モジュール
; 仕様
; ・保護モード 読み書き可
; ・最大サイズ 4KB
#module
; winapi
#uselib "kernel32"
#cfunc CreateFileMapping "CreateFileMappingA" int, int, int, int, int, str
#cfunc GetLastError "GetLastError"
#cfunc MapViewOfFile "MapViewOfFile" int, int, int, int, int
#func UnmapViewOfFile "UnmapViewOfFile" sptr
#func CloseHandle "CloseHandle" int
;--------------------------------------------------
#deffunc filemap_create str inp
hMapobj = CreateFileMapping(-1, 0, 4, 0,4096, inp)
lasterr = GetLastError()
mapptr = MapViewOfFile(hMapobj, 2, 0, 0, 0)
return
;--------------------------------------------------
#deffunc filemap_close
UnmapViewOfFile mapptr
CloseHandle hMapobj
return
;--------------------------------------------------
#defcfunc filemap_viewptr
return mapptr
;--------------------------------------------------
#defcfunc filemap_already_exists
; ERROR_ALREADY_EXISTS 183
return (lasterr = 183)
#global
;============================================================
; ステータスバー モジュール
#module
; winapi
#uselib "comctl32"
#func InitCommonControlsEx "InitCommonControlsEx" var
#uselib "user32"
#func GetWindowRect "GetWindowRect" int, var
#func DestroyIcon "DestroyIcon" int
#uselib "shell32"
#func ExtractIconEx "ExtractIconExA" sptr, sptr, sptr, sptr, sptr
; const
#define ctype objhwnd(%1) objinfo(%1, 2)
#define STAT_INITED 0xAA55
#const WS_CHILD 0x40000000
#const WS_VISIBLE 0x10000000
#define RECT_LEFT 0
#define RECT_TOP 1
#define RECT_RIGHT 2
#define RECT_BOTTOM 3
;--------------------------------------------------
#deffunc status_mes int i, str inp
if inited = STAT_INITED {
a = inp
sendmsg objhwnd(id_status), 0x0401, i, varptr(a) ; SB_SETTEXT
; アイコン消去
if icon.i {
sendmsg objhwnd(id_status), 0x040F, i, 0 ; SB_SETICON
DestroyIcon icon.i
icon.i = 0
}
}
return
;--------------------------------------------------
#deffunc status_icon int i, str fn, int id
if inited = STAT_INITED {
; アイコン設定
if icon.i : DestroyIcon icon.i : icon.i = 0
ExtractIconEx fn, id, 0, varptr(icon.i), 1
if icon.i : sendmsg objhwnd(id_status), 0x040F, i, icon.i ; SB_SETICON
}
return
;--------------------------------------------------
#deffunc status_resize
if inited = STAT_INITED {
a = ginfo_winx*60/100, ginfo_winx*80/100, -1 ; パーツ位置 (60%, 80%, 100% 固定)
sendmsg objhwnd(id_status), 0x0404, 3, varptr(a) ; SB_SETPARTS
sendmsg objhwnd(id_status), 0x0005, 0, 0 ; WM_SIZE
}
return
;--------------------------------------------------
#defcfunc status_height
dim a, 4 ; RECT
if inited = STAT_INITED {
GetWindowRect objhwnd(id_status), a
}
return (a.RECT_BOTTOM - a.RECT_TOP)
;--------------------------------------------------
#deffunc status_create
; init (4.70 以上の comctl32.dll が必要)
a = 8, 0x00000004 ; ICC_BAR_CLASSES
InitCommonControlsEx a
winobj "msctls_statusbar32", "", 0, WS_CHILD | WS_VISIBLE, 0, 0, 0, 0
id_status = stat
dim icon, 3 ; 各パーツのアイコンハンドル格納
inited = STAT_INITED ; 初期化されフラグ
status_resize ; パーツ初期化
return
#global
; icon sample
; status_icon 0, "explorer.exe", 7 ; (i)
; status_icon 0, "explorer.exe", 8 ;
; status_icon 0, "explorer.exe", 9 ; (X)
; status_icon 0, "shell32.dll", 22 ; ○━
; status_icon 0, "shell32.dll", 23 ; (?)
;============================================================
; 文字列キャッシュモジュール (str)key, (str)value
; 参照の古いラインから上書き
#module
; config
#define CACHE_LINES 16
;--------------------------------------------------
#deffunc cache_reset
sdim slot_key, 64, CACHE_LINES ; キー文字列
sdim slot_val, 64, CACHE_LINES ; データ
dim slot_ref, CACHE_LINES ; 参照カウンタ
dim hits, 2 ; パフォーマンスカウンタ
return
;--------------------------------------------------
#deffunc cache_write str key, var v
; 参照カウンタが最小のライン a を求める
m = slot_ref.0
a = 0
repeat CACHE_LINES
if slot_ref.cnt < m {
m = slot_ref.cnt
a = cnt
}
loop
; ライン a にデータ書き込み
slot_key.a = key
slot_val.a = v
slot_ref.a = pc : pc++ ; 参照カウンタ設定
return
;--------------------------------------------------
#deffunc cache_read str key, var v
a = -1
repeat CACHE_LINES
if slot_key.cnt = key {
a = cnt
break
}
loop
if a >= 0 {
; ライン a からデータ読み込み
v = slot_val.a
slot_ref.a = pc : pc++ ; 参照カウンタ設定
} else {
v = ""
dialog "無効なキーを参照 : " + key
}
return
;--------------------------------------------------
#defcfunc cache_exist str key
ret = 0
repeat CACHE_LINES
if slot_key.cnt = key {
ret = 1
break
}
loop
hits.ret++ ; performance counter
return ret
;--------------------------------------------------
#defcfunc cache_hitrate
if (hits.0 + hits.1) = 0 {
return "-"
} else {
return "hitrate=" + (hits.1 * 100 / (hits.0 + hits.1)) + "%, hit=" + hits.1 + ", miss=" + hits.0
}
#global
cache_reset
;============================================================
; HTML 用 文字列関連モジュール
; HTML/URL Enc/Dec (shift-jis)
; Single quotation Escape
; URL auto-link
#module
;--------------------------------------------------
#defcfunc URLEnc str inp
src = inp
dest = ""
memexpand dest, strlen(src) * 3 + 1 ; sdim
p = 0
repeat strlen(src)
c = peek(src, cnt)
if (c>='a' & c<='z') {
poke dest, p, c : p++
} else:if (c>='A' & c<='Z') {
poke dest, p, c : p++
} else:if (c>='0' & c<='9') | c='_' {
poke dest, p, c : p++
} else:if c = ' ' {
poke dest, p, '+' : p++
} else {
a = c >> 4
b = c & 15
if a >= 10 : a += 55 : else : a += '0'
if b >= 10 : b += 55 : else : b += '0'
poke dest, p, '%' : p++
poke dest, p, a : p++
poke dest, p, b : p++
}
loop
poke dest, p
return dest
;--------------------------------------------------
#defcfunc URLDec str inp
src = inp
sdim dest, strlen(src) + 1
p = 0
repeat
c = peek(src, p) : p++
if c = 0 : break
if c = '+' {
c = ' '
} else:if c = '%' {
a = peek(src, p) : p++
b = peek(src, p) : p++
if a >= 'A' : a -= 55 : else : a -= '0'
if b >= 'A' : b -= 55 : else : b -= '0'
c = a << 4 | b
}
poke dest, cnt, c
loop
return dest
;--------------------------------------------------
#defcfunc HTMLEnc str inp, int sw
dest = inp
; speedup patch (99%)
if sw = 0 {
if instr(dest,0,"&")<0 & instr(dest,0,"<")<0 & instr(dest,0,">")<0 {
return dest
}
}
; HTMLEnc init
if he_inited = 0 {
sdim he_ssrc, 16, 6
sdim he_sdst, 16, 6
he_ssrc = "&", "<", ">", "\"", " ", "\n"
he_sdst = "&", "<", ">", """, " ", "
"
he_inited = 1
}
; find and destroy
repeat 3 + sw
ssrc = he_ssrc(cnt)
sdst = he_sdst(cnt)
src = dest
; 汎用文字列置換ルーチン (src, dest, ssrc, sdst)
p = 0
dest = ""
repeat
a = instr(src, p, ssrc)
if a = -1 {
dest += strmid(src, p, strlen(src))
break
}
dest += strmid(src, p, a) + sdst
p += a + strlen(ssrc)
loop
loop
return dest
;--------------------------------------------------
#define global ctype ExHTMLEnc(%1) HTMLEnc(%1, 3)
;--------------------------------------------------
#defcfunc QuoteEsc str inp
src = inp
sdim dest, strlen(src) * 2 + 1
p = 0
repeat strlen(src)
c = peek(src, cnt)
if c = 39 { ; '
dest += "\\\'"
p += 2
} else:if c = 92 { ; \\
dest += "\\\\"
p += 2
} else:if c = 13 { ; \n
dest += "\\n"
p += 2
} else:if c = 10 {
} else:if (c>=0x81 & c<=0x9F) | (c>=0xE0 & c<=0xFC) { ; SJIS patch
wpoke dest, p, wpeek(src, cnt)
p += 2
continue cnt + 2
} else {
poke dest, p, c : p++
}
loop
return dest
/*
;--------------------------------------------------
#defcfunc HTMLALink str inp
src = inp
dest = ""
p = 0
repeat
a = instr(src, p, "http://")
if a = -1 {
dest += strmid(src, p, strlen(src))
break
}
dest += strmid(src, p, a)
p += a
getstr buf, src, p, '<'
; URL 区切り文字
if instr(buf,0," ") > 0 : buf = strmid(buf, 0, instr(buf,0," "))
if instr(buf,0,"&") > 0 : buf = strmid(buf, 0, instr(buf,0,"&"))
p += strlen(buf)
dest += "" + buf + ""
loop
return dest
*/
#global
;============================================================
; HTML 中のアクティブスクリプトに関するキーワードを禁止
#module
; winapi
#uselib "user32"
#func CharLower "CharLowerA" var
;--------------------------------------------------
#deffunc DoubtAngel var astr
sdim dtable, 16, 99
dtable = "