2008年12月20日土曜日

HSPでランタイムを使わずにコンソールを使う

ランタイムを使用せずコンソールを使ってみようと思います。
copenでコンソールを開き
coutputで文字を表示し、
cinputでキーボードから打たれた文字を取得します。
ccloseでコンソールを閉じます。

#module console
#define STD_INPUT_HANDLE (-10)
#define STD_OUTPUT_HANDLE (- 11)
#define STD_ERROR_HANDLE (-12)

#uselib "kernel32.dll"
#func AllocConsole "AllocConsole"
#func FreeConsole "FreeConsole"
#cfunc GetStdHandle "GetStdHandle" int
#func SetStdHandle "SetStdHandle" int,int
#func ReadFile "ReadFile" int,int,int,int,int
#func WriteFile "WriteFile" int,int,int,int,int

#deffunc cset
SetStdHandle STD_OUTPUT_HANDLE,7
SetStdHandle STD_INPUT_HANDLE,3
return
#deffunc copen
AllocConsole

return stat
#deffunc cclose
FreeConsole
return stat
#deffunc coutput str buf_
buf=buf_
b=0
WriteFile GetStdHandle(STD_OUTPUT_HANDLE),varptr(buf),strlen(buf),b,0
stat_=stat
if stat_=0{
return -1
}
return
#deffunc cinput var buf_
d=0
buf_max=64
sdim buf,buf_max
data=0
index=0
repeat
ReadFile GetStdHandle(STD_INPUT_HANDLE),varptr(data),1,varptr(d),0
stat_=stat
if stat_=0{
return -1
}
if data=13||data=10||data=0{
buf_=buf
return
}
poke buf,index,data
index+
if index=buf_max{
sdim buf_d,buf_max
buf_d=buf
buf_max+=64
sdim buf,buf_max
buf=buf_d
}
loop
return
#deffunc cbinput var buf_
d=0
buf_max=64
sdim buf,buf_max
data=0
index=0
repeat
ReadFile GetStdHandle(STD_INPUT_HANDLE),varptr(data),1,varptr(d),0
stat_=stat
if stat_=0{
return -1
}
if data=0{
buf_=buf
return
}
poke buf,index,data
index+
if index=buf_max{
sdim buf_d,buf_max
buf_d=buf
buf_max+=64
sdim buf,buf_max
buf=buf_d
}
loop
return
#global

//////////サンプル///////////
a=""
copen
coutput "hello world"
cinput a
coutput a
wait
cclose
wait
end