'string.sbp
'文字列変数の操作用

#ifndef _INC_BASIC_STRING
#define _INC_BASIC_STRING

#require <Classes/System/String.ab>
#require <Classes/System/Text/StringBuilder.ab>

Function StrPtr(s As String) As *StrChar
	StrPtr = s.StrPtr
End Function
'StringBuilder版はClasses/System/Text/StringBuilder.abに定義されている

Function ZeroString(length As Long) As System.Text.StringBuilder
	ZeroString = New System.Text.StringBuilder
	ZeroString.Length = length
End Function

Function MakeStr(psz As PSTR) As String
	Return New String(psz)
End Function

Function MakeStr(psz As PWSTR) As String
	Return New String(psz)
End Function

Dim _System_AllocForConvertedString As *Function(size As SIZE_T) As VoidPtr
_System_AllocForConvertedString = AddressOf (GC_malloc_atomic)

Function GetStr(psz As PSTR, ByRef wcs As PWSTR) As SIZE_T
	If psz <> 0 Then
		Return GetStr(psz, lstrlenA(psz) As SIZE_T, wcs)
	Else
		Return 0
	End If
End Function

Function GetStr(psz As PSTR, len As SIZE_T, ByRef wcs As PWSTR) As SIZE_T
	If psz = 0 Then Return 0
	Dim lenWCS = MultiByteToWideChar(CP_THREAD_ACP, 0, psz, len, 0, 0)
	wcs = _System_AllocForConvertedString(SizeOf (WCHAR) * (lenWCS + 1)) As PWSTR
	GetStr = MultiByteToWideChar(CP_THREAD_ACP, 0, psz, len, wcs, lenWCS)
	wcs[GetStr] = 0
End Function

Function GetStr(psz As PWSTR, ByRef wcs As PWSTR) As SIZE_T
	wcs = psz
	If psz <> 0 Then
		Return lstrlenW(psz) As SIZE_T
	Else
		Return 0
	End If
End Function

Function GetStr(psz As PWSTR, len As SIZE_T, ByRef wcs As PWSTR) As SIZE_T
	wcs = psz
	If psz <> 0 Then
		Return len
	Else
		Return 0
	End If
End Function

Function GetStr(psz As PWSTR, ByRef mbs As PSTR) As SIZE_T
	If psz = 0 Then
		Return 0
	Else
		Return GetStr(psz, lstrlenW(psz) As SIZE_T, mbs)
	End If
End Function

Function GetStr(psz As PWSTR, len As SIZE_T, ByRef mbs As PSTR) As SIZE_T
	If psz = 0 Then Return 0
	Dim lenMBS = WideCharToMultiByte(CP_THREAD_ACP, 0, psz, len, 0, 0, 0, 0)
	mbs = _System_AllocForConvertedString(SizeOf (SByte) * (lenMBS + 1)) As PSTR
	GetStr = WideCharToMultiByte(CP_THREAD_ACP, 0, psz, len, mbs, lenMBS, 0, 0) As SIZE_T
	mbs[GetStr] = 0
End Function

Function GetStr(psz As PSTR, ByRef mbs As PSTR) As SIZE_T
	mbs = psz
	If psz <> 0 Then
		Return lstrlenA(psz) As SIZE_T
	Else
		Return 0
	End If
End Function

Function GetStr(psz As PSTR, len As SIZE_T, ByRef mbs As PSTR) As SIZE_T
	mbs = psz
	If psz <> 0 Then
		Return len
	Else
		Return 0
	End If
End Function

Function GetStr(s As String, ByRef mbs As PSTR) As SIZE_T
	Return GetStr(s.StrPtr, s.Length As SIZE_T, mbs)
End Function

Function GetStr(s As String, ByRef wcs As PWSTR) As SIZE_T
	Return GetStr(s.StrPtr, s.Length As SIZE_T, wcs)
End Function

Function GetWCStr(psz As PSTR, ByRef wcs As PWSTR) As SIZE_T
	Return GetStr(psz, wcs)
End Function

Function GetWCStr(psz As PSTR, len As SIZE_T, ByRef wcs As PWSTR) As SIZE_T
	Return GetStr(psz, len, wcs)
End Function

Function GetWCStr(psz As PWSTR, ByRef wcs As PWSTR) As SIZE_T
	Return GetStr(psz, wcs)
End Function

Function GetWCStr(psz As PWSTR, len As SIZE_T, ByRef wcs As PWSTR) As SIZE_T
	Return GetStr(psz, len, wcs)
End Function

Function GetWCStr(s As String, ByRef wcs As PWSTR) As SIZE_T
	Return GetStr(s.StrPtr, s.Length As SIZE_T, wcs)
End Function

Function GetMBStr(psz As PWSTR, ByRef mbs As PSTR) As SIZE_T
	Return GetStr(psz, mbs)
End Function

Function GetMBStr(psz As PWSTR, len As SIZE_T, ByRef mbs As PSTR) As SIZE_T
	Return GetStr(psz, len, mbs)
End Function

Function GetMBStr(psz As PSTR, ByRef mbs As PSTR) As SIZE_T
	Return GetStr(psz, mbs)
End Function

Function GetMBStr(psz As PSTR, len As SIZE_T, ByRef mbs As PSTR) As SIZE_T
	Return GetStr(psz, len, mbs)
End Function

Function GetMBStr(s As String, ByRef mbs As PSTR) As SIZE_T
	Return GetStr(s.StrPtr, s.Length As SIZE_T, mbs)
End Function

Function GetTCStr(psz As PSTR, ByRef tcs As PCTSTR) As SIZE_T
	Return GetStr(psz, tcs)
End Function

Function GetTCStr(psz As PSTR, len As SIZE_T, ByRef tcs As PCTSTR) As SIZE_T
	Return GetStr(psz, len, tcs)
End Function

Function GetTCStr(psz As PWSTR, ByRef tcs As PCTSTR) As SIZE_T
	Return GetStr(psz, tcs)
End Function

Function GetTCStr(psz As PWSTR, len As SIZE_T, ByRef tcs As PCTSTR) As SIZE_T
	Return GetStr(psz, len, tcs)
End Function

Function GetTCStr(s As String, ByRef tcs As PCTSTR) As SIZE_T
	Return GetStr(s.StrPtr, s.Length As SIZE_T, tcs)
End Function

Function GetSCStr(psz As PSTR, ByRef ss As *StrChar) As SIZE_T
	Return GetStr(psz, ss)
End Function

Function GetSCStr(psz As PSTR, len As SIZE_T, ByRef ss As *StrChar) As SIZE_T
	Return GetStr(psz, len, ss)
End Function

Function GetSCStr(psz As PWSTR, ByRef ss As *StrChar) As SIZE_T
	Return GetStr(psz, ss)
End Function

Function GetSCStr(psz As PWSTR, len As SIZE_T, ByRef ss As *StrChar) As SIZE_T
	Return GetStr(psz, len, ss)
End Function

Function GetSCStr(s As String, ByRef ss As *StrChar) As SIZE_T
	Return GetStr(s.StrPtr, s.Length As SIZE_T, ss)
End Function

Function ToWCStr(psz As PSTR) As PWSTR
	GetStr(psz, ToWCStr)
End Function

Function ToWCStr(psz As PSTR, len As SIZE_T) As PWSTR
	GetStr(psz, len, ToWCStr)
End Function

Function ToWCStr(psz As PWSTR) As PWSTR
	GetStr(psz, ToWCStr)
End Function

Function ToWCStr(psz As PWSTR, len As SIZE_T) As PWSTR
	GetStr(psz, len, ToWCStr)
End Function

Function ToWCStr(s As String) As PWSTR
	GetStr(s.StrPtr, s.Length As SIZE_T, ToWCStr)
End Function

Function ToMBStr(psz As PSTR) As PSTR
	GetStr(psz, ToMBStr)
End Function

Function ToMBStr(psz As PSTR, len As SIZE_T) As PSTR
	GetStr(psz, len, ToMBStr)
End Function

Function ToMBStr(psz As PWSTR) As PSTR
	GetStr(psz, ToMBStr)
End Function

Function ToMBStr(psz As PWSTR, len As SIZE_T) As PSTR
	GetStr(psz, len, ToMBStr)
End Function

Function ToMBStr(s As String) As PSTR
	GetStr(s.StrPtr, s.Length As SIZE_T, ToMBStr)
End Function

Function ToTCStr(psz As PSTR) As PCTSTR
	GetStr(psz, ToTCStr)
End Function

Function ToTCStr(psz As PSTR, len As SIZE_T) As PCTSTR
	GetStr(psz, len, ToTCStr)
End Function

Function ToTCStr(psz As PWSTR) As PCTSTR
	GetStr(psz, ToTCStr)
End Function

Function ToTCStr(psz As PWSTR, len As SIZE_T) As PCTSTR
	GetStr(psz, len, ToTCStr)
End Function

Function ToTCStr(s As String) As PCTSTR
	GetStr(s.StrPtr, s.Length As SIZE_T, ToTCStr)
End Function

Function ToSCStr(psz As PSTR) As *StrChar
	GetStr(psz, ToSCStr)
End Function

Function ToSCStr(psz As PSTR, len As SIZE_T) As *StrChar
	GetStr(psz, len, ToSCStr)
End Function

Function ToSCStr(psz As PWSTR) As *StrChar
	GetStr(psz, ToSCStr)
End Function

Function ToSCStr(psz As PWSTR, len As SIZE_T) As *StrChar
	GetStr(psz, len, ToSCStr)
End Function

Function ToSCStr(s As String) As *StrChar
	GetStr(s.StrPtr, s.Length As SIZE_T, ToSCStr)
End Function

#endif '_INC_BASIC_STRING