'dos_console.sbp
'このファイルには、コンソール アプリケーション用のサポート プログラムが記載されます。


#ifndef _INC_DOS_CONSOLE
#define _INC_DOS_CONSOLE


Dim _System_hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE)
Dim _System_hConsoleIn = GetStdHandle(STD_INPUT_HANDLE)

'---------- command.sbp内で定義済み ----------
'Dim _System_InputDataPtr[_System_MAX_PARMSNUM] As VoidPtr
'Dim _System_InputDataType[_System_MAX_PARMSNUM] As DWord
'---------------------------------------------
Sub INPUT_FromPrompt(ShowStr As String)
	Dim InputBuf[1023] As TCHAR
	Dim dwAccessBytes As DWord

*InputReStart

	PRINT_ToPrompt(ShowStr)

	'入力
	ReadConsole(_System_hConsoleIn, InputBuf, Len(InputBuf), dwAccessBytes, 0)
	If InputBuf[dwAccessBytes-2] = &h0d And InputBuf[dwAccessBytes-1] = &h0a Then
		InputBuf[dwAccessBytes-2] = 0
		dwAccessBytes -= 2
	End If

	If dwAccessBytes = 0 Then Goto *InputReStart

	'データを変数に格納
	Const comma = &h2c As StrChar 'Asc(",")
	Dim broken = ActiveBasic.Strings.Detail.Split(New String(InputBuf, dwAccessBytes As Long), comma)
	Dim i As Long
	For i = 0 To ELM(broken.Count)
		If _System_InputDataPtr[i] = 0 Then
			PRINT_ToPrompt(Ex"入力データの個数が多すぎます\r\n")
			Goto *InputReStart
		End If
		_System_Input_SetArgument(_System_InputDataPtr[i], _System_InputDataType[i], broken[i].ToString)
	Next

	If _System_InputDataPtr[i]<>0 Then
		PRINT_ToPrompt(Ex"入力データの個数が足りません\r\n")
		Goto *InputReStart
	End If
End Sub

Macro LOCATE(x As Long, y As Long)
	SetConsoleCursorPosition(_System_hConsoleOut, MAKELONG(x, y))
End Macro

Sub PRINT_ToPrompt(buf As String)
	Dim dwAccessBytes As DWord
#ifdef __STRING_UNICODE_WINDOWS_ANSI
'	Debug
	Dim oldAlloc = _System_AllocForConvertedString
	_System_AllocForConvertedString = AddressOf (_System_malloc)
	Dim pszOut As PCSTR
	Dim len = GetStr(buf, pszOut)
	_System_AllocForConvertedString = oldAlloc
	WriteConsole(_System_hConsoleOut, pszOut, len, dwAccessBytes, 0)
	_System_free(pszOut)
#else
	WriteConsole(_System_hConsoleOut, buf.StrPtr, buf.Length, dwAccessBytes, 0)
#endif
End Sub
/* TODO: _System_GetUsingFormatを用意して実装する
Sub PRINTUSING_ToPrompt(UsingStr As String)
	PRINT_ToPrompt(_System_GetUsingFormat(UsingStr))
End Sub
*/

#endif '_INC_DOS_CONSOLE
