' Classes/System/OperatingSystem.ab

#require <Classes/System/Version.ab>

Namespace System

Class OperatingSystem
	' Inherits ICloneable', ISerializable
Public
	' Constractor
	Sub OperatingSystem(platform As PlatformID, version As Version)
		pf = platform
		ver = version
		sp = ""
	End Sub

	Sub OperatingSystem(ByRef vi As OSVERSIONINFOA)
		pf = ToPlatformID(vi.dwPlatformId)
		ver = New Version(vi.dwMajorVersion, vi.dwMinorVersion, vi.dwBuildNumber)
		sp = New String(vi.szCSDVersion As PCSTR)
	End Sub

	Sub OperatingSystem(ByRef vi As OSVERSIONINFOW)
		pf = ToPlatformID(vi.dwPlatformId)
		ver = New Version(vi.dwMajorVersion, vi.dwMinorVersion, vi.dwBuildNumber)
		sp = New String(vi.szCSDVersion As PCWSTR)
	End Sub

	' Properties
	Const Function Platform() As PlatformID
		Return pf
	End Function

	Const Function Version() As Version
		Return ver
	End Function

	Const Function ServicePack() As String
		Return sp
	End Function

	Const Function VersionString() As String
		Dim sb = New Text.StringBuilder

		Select Case pf
			Case PlatformID.Win32S
				sb.Append("Microsoft Win32S ")
			Case PlatformID.Win32Windows
				sb.Append("Microsoft Windows ")
			Case PlatformID.Win32NT
				sb.Append("Microsoft Windows NT ")
			Case PlatformID.WinCE
				sb.Append("Microsoft Windows CE ")
			Case PlatformID.Unix
				sb.Append("<unknown> ")
		End Select
		sb.Append(ver)
		If String.IsNullOrEmpty(sp) <> False Then
			sb.Append(" ").Append(sp)
		End If
		VersionString = sb.ToString()
	End Function

	' Methods
	Override Function ToString() As String
		Return VersionString
	End Function

	Override Function GetHashCode() As Long
		Return pf.GetHashCode Xor ver.GetHashCode Xor sp.GetHashCode
	End Function

Private
	pf As PlatformID
	ver As Version
	sp As String

	Static Function ToPlatformID(platformId As DWord) As PlatformID
		Select Case platformId
			Case VER_PLATFORM_WIN32s
				Return PlatformID.Win32S
			Case VER_PLATFORM_WIN32_WINDOWS
				Return PlatformID.Win32Windows
			Case VER_PLATFORM_WIN32_NT
				Return PlatformID.Win32NT
			Case VER_PLATFORM_WIN32_CE
				Return PlatformID.WinCE
			Case Else
				Return PlatformID.Unix
		End Select
	End Function
End Class

Enum PlatformID
	Win32S = VER_PLATFORM_WIN32s '0
	Win32Windows = VER_PLATFORM_WIN32_WINDOWS '1
	Win32NT = VER_PLATFORM_WIN32_NT '2
	WinCE = VER_PLATFORM_WIN32_CE '3
	Unix = 4
End Enum

End Namespace
