Kaynağa Gözat

BRL.Platform. Initial Import.

woollybah 6 yıl önce
ebeveyn
işleme
e10d0157dc

+ 6 - 0
platform.mod/doc/osversion.bmx

@@ -0,0 +1,6 @@
+SuperStrict
+
+Framework BRL.StandardIO
+Import BRL.Platform
+
+Print OSVersion()

+ 116 - 0
platform.mod/platform.bmx

@@ -0,0 +1,116 @@
+' Copyright (c) 2019 Bruce A Henderson
+'
+' This software is provided 'as-is', without any express or implied
+' warranty. In no event will the authors be held liable for any damages
+' arising from the use of this software.
+' 
+' Permission is granted to anyone to use this software for any purpose,
+' including commercial applications, and to alter it and redistribute it
+' freely, subject to the following restrictions:
+' 
+'    1. The origin of this software must not be misrepresented; you must not
+'    claim that you wrote the original software. If you use this software
+'    in a product, an acknowledgment in the product documentation would be
+'    appreciated but is not required.
+' 
+'    2. Altered source versions must be plainly marked as such, and must not be
+'    misrepresented as being the original software.
+' 
+'    3. This notice may not be removed or altered from any source
+'    distribution.
+' 
+SuperStrict
+
+Rem
+bbdoc: Platform utils
+End Rem
+Module BRL.Platform
+
+Import BRL.TextStream
+Import Pub.macos
+
+?win32
+Import "win32_glue.c"
+?
+
+Private
+Global _version:String
+Public
+
+Rem
+bbdoc: Returns a #String representation of the OS version.
+about: On Linux, this will return something like "Ubuntu" or "Fedora".
+On Windows and macOS, this will return the version number, such as "10.11"
+End Rem
+Function OSVersion:String()
+	If _version Then
+		Return _version
+	End If
+?linux
+	Return LinuxVersion()
+?macos
+	Return MacOSVersion()
+?win32
+	Return WindowsVersion()
+?
+End Function
+
+Function LinuxVersion:String()
+?Not linux
+	Return ""
+?linux
+	If _version Then
+		Return _version
+	End If
+	
+	Local rel:String[] = LoadText("/etc/os-release").Split("~n")
+	For Local line:String = EachIn rel
+		If line.StartsWith("NAME") Then
+			Local parts:String[] = line.Split("=")
+			If parts.length > 1 Then
+				_version = parts[1].Replace("~q", "").Trim()
+				Return _version
+			End If
+		End If
+	Next
+?
+End Function
+
+Function MacOSVersion:String()
+?Not macos
+	Return ""
+?macos
+	If _version Then
+		Return _version
+	End If
+
+	Local major:Int, minor:Int, patch:Int
+	NSOSVersion(major, minor, patch)
+	_version = major + "." + minor + "." + patch
+	Return _version
+?
+End Function
+
+Private
+?win32
+Extern
+	Function bmx_os_getwindowsversion(major:Int Var, minor:Int Var)
+End Extern
+?
+Public
+
+Function WindowsVersion:String()
+?Not win32
+	Return ""
+?win32
+	If _version Then
+		Return _version
+	End If
+
+	Local major:Int
+	Local minor:Int
+	bmx_os_getwindowsversion(major, minor)
+	_version = major + "." + minor
+	Return _version
+?
+End Function

+ 85 - 0
platform.mod/win32_glue.c

@@ -0,0 +1,85 @@
+/*
+ Copyright (c) 2019 Bruce A Henderson
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+ 
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+ 
+    1. The origin of this software must not be misrepresented; you must not
+    claim that you wrote the original software. If you use this software
+    in a product, an acknowledgment in the product documentation would be
+    appreciated but is not required.
+ 
+    2. Altered source versions must be plainly marked as such, and must not be
+    misrepresented as being the original software.
+ 
+    3. This notice may not be removed or altered from any source
+    distribution.
+*/
+#include "windows.h"
+
+bmx_os_getwindowsversion(int * major, int * minor) {
+
+	OSVERSIONINFOEX versionInfo = {0};
+	versionInfo.dwOSVersionInfoSize = sizeof(versionInfo);
+
+	HINSTANCE nt = LoadLibrary("ntdll.dll");
+
+	if (nt != NULL) {
+
+		NTSTATUS (WINAPI *rtlGetVersion)(PRTL_OSVERSIONINFOW lpVersionInformation) = (NTSTATUS (WINAPI *)(PRTL_OSVERSIONINFOW)) GetProcAddress (nt, "RtlGetVersion");
+		
+		if (rtlGetVersion != NULL) {
+			rtlGetVersion(&versionInfo);
+		} else {
+			GetVersionEx(&versionInfo);
+		}
+
+		int _major = versionInfo.dwMajorVersion;
+		int _minor = versionInfo.dwMinorVersion;
+
+		switch (_major) {
+			case 5:
+				if (_minor == 1 || _minor == 2) {
+					*major = _major;
+					*minor = _minor;
+					return;	
+				}
+				break;
+				
+			case 6:
+				switch (_minor) {
+					case 0:
+						*major = _major;
+						*minor = _minor;
+						return;
+					case 1:
+						*major = 7;
+						*minor = 0;
+						return;
+					case 2:
+						*major = 8;
+						*minor = 0;
+						return;
+					case 3:
+						*major = 8;
+						*minor = 1;
+						return;
+				}
+				break;
+				
+			case 10:
+				*major = _major;
+				*minor = 0;
+				return;
+		}
+	}
+
+	// don't know what version this is...
+	*major = 0;
+	*minor = 0;
+}