Replaced platform code with dotnet's environment version.

This commit is contained in:
Eugene Wang
2021-04-27 21:36:17 -04:00
parent 2a38b5f0c4
commit b86ecb3c1d
6 changed files with 295 additions and 419 deletions

View File

@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NTwain
{
/// <summary>
/// Contains info on current platform, process, and TWAIN stuff.
/// </summary>
public static class PlatformInfo
{
/// <summary>
/// Whether the current OS is windows.
/// </summary>
public static bool IsWindows { get; }
/// <summary>
/// Whether the current OS is linux.
/// </summary>
public static bool IsLinux { get; }
/// <summary>
/// Whether the current OS is MacOSX.
/// </summary>
public static bool IsMacOSX { get; }
/// <summary>
/// Whether the application is running in 64-bit.
/// </summary>
public static bool IsApp64Bit { get; set; }
static PlatformInfo()
{
var platform = Environment.OSVersion.Platform;
IsWindows = platform == PlatformID.Win32NT;
IsLinux = platform == PlatformID.Unix;
IsMacOSX = platform == PlatformID.MacOSX;
IsApp64Bit = IntPtr.Size == 8;
}
}
}

View File

@@ -1,166 +0,0 @@
///////////////////////////////////////////////////////////////////////////////////////
//
// TwainWorkingGroup.TWAIN
//
// These are the definitions for TWAIN. They're essentially the C/C++
// TWAIN.H file contents translated to C#, with modifications that
// recognize the differences between Windows, Linux and Mac OS X.
//
///////////////////////////////////////////////////////////////////////////////////////
// Author Date TWAIN Comment
// M.McLaughlin 13-Mar-2019 2.4.0.3 Add language code page support for strings
// M.McLaughlin 13-Nov-2015 2.4.0.0 Updated to latest spec
// M.McLaughlin 13-Sep-2015 2.3.1.2 DsmMem bug fixes
// M.McLaughlin 26-Aug-2015 2.3.1.1 Log fix and sync with TWAIN Direct
// M.McLaughlin 13-Mar-2015 2.3.1.0 Numerous fixes
// M.McLaughlin 13-Oct-2014 2.3.0.4 Added logging
// M.McLaughlin 24-Jun-2014 2.3.0.3 Stability fixes
// M.McLaughlin 21-May-2014 2.3.0.2 64-Bit Linux
// M.McLaughlin 27-Feb-2014 2.3.0.1 AnyCPU support
// M.McLaughlin 21-Oct-2013 2.3.0.0 Initial Release
///////////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2013-2020 Kodak Alaris Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
///////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TWAINWorkingGroup
{
static partial class PlatformTools
{
/// <summary>
/// The platform we're running on...
/// </summary>
private static Platform ms_platform;
private static Processor ms_processor;
private static bool ms_blFirstPassGetPlatform = true;
/// <summary>
/// 32-bit or 64-bit...
/// </summary>
/// <returns>Number of bits in the machine word for this process</returns>
public static int GetMachineWordBitSize()
{
return ((IntPtr.Size == 4) ? 32 : 64);
}
/// <summary>
/// Quick access to our platform id...
/// </summary>
/// <returns></returns>
public static Platform GetPlatform()
{
// First pass...
if (ms_blFirstPassGetPlatform)
{
// Dont'c come in here again...
ms_blFirstPassGetPlatform = false;
// We're Windows...
if (Environment.OSVersion.ToString().Contains("Microsoft Windows"))
{
ms_platform = Platform.WINDOWS;
ms_processor = (GetMachineWordBitSize() == 64) ? Processor.X86_64 : Processor.X86;
}
// We're Mac OS X (this has to come before LINUX!!!)...
else if (Directory.Exists("/Library/Application Support"))
{
ms_platform = Platform.MACOSX;
ms_processor = (GetMachineWordBitSize() == 64) ? Processor.X86_64 : Processor.X86;
}
// We're Linux...
else if (Environment.OSVersion.ToString().Contains("Unix"))
{
string szProcessor = "";
ms_platform = Platform.LINUX;
try
{
using (Process process = new Process
{
StartInfo = new ProcessStartInfo()
{
FileName = "uname",
Arguments = "-m",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
})
{
process.Start();
process.WaitForExit();
szProcessor = process.StandardOutput.ReadToEnd();
process.Close();
}
}
catch
{
Console.Out.WriteLine("Oh dear, this isn't good...where's uname?");
}
if (szProcessor.Contains("mips64"))
{
ms_processor = Processor.MIPS64EL;
}
else
{
ms_processor = (GetMachineWordBitSize() == 64) ? Processor.X86_64 : Processor.X86;
}
}
// We have a problem, Log will throw for us...
else
{
ms_platform = Platform.UNKNOWN;
ms_processor = Processor.UNKNOWN;
Log.Assert("Unsupported platform..." + ms_platform);
}
}
// All done...
return (ms_platform);
}
/// <summary>
/// Quick access to our processor id...
/// </summary>
/// <returns></returns>
public static Processor GetProcessor()
{
// First pass...
if (ms_blFirstPassGetPlatform)
{
GetPlatform();
}
// All done...
return (ms_processor);
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -40,6 +40,7 @@
// DEALINGS IN THE SOFTWARE.
///////////////////////////////////////////////////////////////////////////////////////
using NTwain;
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
@@ -89,27 +90,27 @@ namespace TWAINWorkingGroup
******************************************************************************/
/// <summary>
/// Our supported platforms...
/// </summary>
public enum Platform
{
UNKNOWN,
WINDOWS,
LINUX,
MACOSX
};
///// <summary>
///// Our supported platforms...
///// </summary>
//public enum Platform
//{
// UNKNOWN,
// WINDOWS,
// LINUX,
// MACOSX
//};
/// <summary>
/// Our supported processors...
/// </summary>
public enum Processor
{
UNKNOWN,
X86,
X86_64,
MIPS64EL
};
///// <summary>
///// Our supported processors...
///// </summary>
//public enum Processor
//{
// UNKNOWN,
// X86,
// X86_64,
// MIPS64EL
//};
/// <summary>
/// Used for strings that go up to 32-bytes...
@@ -198,7 +199,7 @@ namespace TWAINWorkingGroup
}
// If we're running on a Mac, take off the prefix 'byte'...
if (a_blMayHavePrefix && (PlatformTools.GetPlatform() == Platform.MACOSX))
if (a_blMayHavePrefix && (PlatformInfo.IsMacOSX))
{
sz = sz.Remove(0, 1);
}
@@ -244,7 +245,7 @@ namespace TWAINWorkingGroup
{
a_sz = "";
}
else if (a_blMayHavePrefix && (PlatformTools.GetPlatform() == Platform.MACOSX))
else if (a_blMayHavePrefix && (PlatformInfo.IsMacOSX))
{
a_sz = (char)a_sz.Length + a_sz;
}
@@ -374,7 +375,7 @@ namespace TWAINWorkingGroup
}
// If we're running on a Mac, take off the prefix 'byte'...
if (a_blMayHavePrefix && (PlatformTools.GetPlatform() == Platform.MACOSX))
if (a_blMayHavePrefix && (PlatformInfo.IsMacOSX))
{
sz = sz.Remove(0, 1);
}
@@ -420,7 +421,7 @@ namespace TWAINWorkingGroup
{
a_sz = "";
}
else if (a_blMayHavePrefix && (PlatformTools.GetPlatform() == Platform.MACOSX))
else if (a_blMayHavePrefix && (PlatformInfo.IsMacOSX))
{
a_sz = (char)a_sz.Length + a_sz;
}
@@ -589,7 +590,7 @@ namespace TWAINWorkingGroup
}
// If we're running on a Mac, take off the prefix 'byte'...
if (a_blMayHavePrefix && (PlatformTools.GetPlatform() == Platform.MACOSX))
if (a_blMayHavePrefix && (PlatformInfo.IsMacOSX))
{
sz = sz.Remove(0, 1);
}
@@ -635,7 +636,7 @@ namespace TWAINWorkingGroup
{
a_sz = "";
}
else if (a_blMayHavePrefix && (PlatformTools.GetPlatform() == Platform.MACOSX))
else if (a_blMayHavePrefix && (PlatformInfo.IsMacOSX))
{
a_sz = (char)a_sz.Length + a_sz;
}
@@ -884,7 +885,7 @@ namespace TWAINWorkingGroup
}
// If we're running on a Mac, take off the prefix 'byte'...
if (a_blMayHavePrefix && (PlatformTools.GetPlatform() == Platform.MACOSX))
if (a_blMayHavePrefix && (PlatformInfo.IsMacOSX))
{
sz = sz.Remove(0, 1);
}
@@ -930,7 +931,7 @@ namespace TWAINWorkingGroup
{
a_sz = "";
}
else if (a_blMayHavePrefix && (PlatformTools.GetPlatform() == Platform.MACOSX))
else if (a_blMayHavePrefix && (PlatformInfo.IsMacOSX))
{
a_sz = (char)a_sz.Length + a_sz;
}

View File

@@ -50,7 +50,7 @@ namespace NTwain
{
TWTY itemType;
// Mac has a level of indirection and a different structure (ick)...
if (PlatformTools.GetPlatform() == Platform.MACOSX)
if (PlatformInfo.IsMacOSX)
{
// Crack the container...
var onevalue = MarshalTo<TW_ONEVALUE_MACOSX>(lockedPtr);
@@ -83,12 +83,11 @@ namespace NTwain
try
{
var platform = PlatformTools.GetPlatform();
TWTY itemType;
int count = 0;
// Mac has a level of indirection and a different structure (ick)...
if (platform == Platform.MACOSX)
if (PlatformInfo.IsMacOSX)
{
// Crack the container...
var twenumerationmacosx = MarshalTo<TW_ENUMERATION_MACOSX>(lockedPtr);
@@ -99,7 +98,7 @@ namespace NTwain
lockedPtr += Marshal.SizeOf(twenumerationmacosx);
}
// Windows or the 2.4+ Linux DSM...
else if ((platform == Platform.WINDOWS) || ((twain.m_blFoundLatestDsm || twain.m_blFoundLatestDsm64) && (twain.m_linuxdsm == TWAIN.LinuxDsm.IsLatestDsm)))
else if ((PlatformInfo.IsWindows) || ((twain.m_blFoundLatestDsm || twain.m_blFoundLatestDsm64) && (twain.m_linuxdsm == TWAIN.LinuxDsm.IsLatestDsm)))
{
// Crack the container...
var twenumeration = MarshalTo<TW_ENUMERATION>(lockedPtr);
@@ -153,7 +152,7 @@ namespace NTwain
uint count;
// Mac has a level of indirection and a different structure (ick)...
if (PlatformTools.GetPlatform() == Platform.MACOSX)
if (PlatformInfo.IsMacOSX)
{
// Crack the container...
var twarraymacosx = MarshalTo<TW_ARRAY_MACOSX>(lockedPtr);
@@ -195,10 +194,9 @@ namespace NTwain
{
TW_RANGE twrange = default;
TW_RANGE_FIX32 twrangefix32 = default;
var platform = PlatformTools.GetPlatform();
// Mac has a level of indirection and a different structure (ick)...
if (platform == Platform.MACOSX)
if (PlatformInfo.IsMacOSX)
{
var twrangemacosx = MarshalTo<TW_RANGE_MACOSX>(lockedPtr);
var twrangefix32macosx = MarshalTo<TW_RANGE_FIX32_MACOSX>(lockedPtr);
@@ -216,7 +214,7 @@ namespace NTwain
twrangefix32.CurrentValue = twrangefix32macosx.CurrentValue;
}
// Windows or the 2.4+ Linux DSM...
else if ((platform == Platform.WINDOWS) || (twain.m_linuxdsm == TWAIN.LinuxDsm.IsLatestDsm) ||
else if ((PlatformInfo.IsWindows) || (twain.m_linuxdsm == TWAIN.LinuxDsm.IsLatestDsm) ||
((twain.m_blFoundLatestDsm || twain.m_blFoundLatestDsm64) && (twain.m_linuxdsm == TWAIN.LinuxDsm.IsLatestDsm)))
{
twrange = MarshalTo<TW_RANGE>(lockedPtr);
@@ -296,7 +294,7 @@ namespace NTwain
{
TWTY itemType;
// Mac has a level of indirection and a different structure (ick)...
if (PlatformTools.GetPlatform() == Platform.MACOSX)
if (PlatformInfo.IsMacOSX)
{
// Crack the container...
var onevalue = MarshalTo<TW_ONEVALUE_MACOSX>(lockedPtr);

View File

@@ -71,7 +71,7 @@ namespace NTwain
TWTY itemType = GetItemType<TValue>();
// Allocate the container (go for worst case, which is TW_STR255)...
if (PlatformTools.GetPlatform() == Platform.MACOSX)
if (PlatformInfo.IsMacOSX)
{
twCap.hContainer = twain.DsmMemAlloc((uint)(Marshal.SizeOf(default(TW_ONEVALUE_MACOSX)) + Marshal.SizeOf(default(TW_STR255))));
lockedPtr = twain.DsmMemLock(twCap.hContainer);
@@ -112,7 +112,7 @@ namespace NTwain
TWTY itemType = GetItemType<TValue>();
// Allocate the container (go for worst case, which is TW_STR255)...
if (PlatformTools.GetPlatform() == Platform.MACOSX)
if (PlatformInfo.IsMacOSX)
{
// Allocate...
twCap.hContainer = twain.DsmMemAlloc((uint)(Marshal.SizeOf(default(TW_ARRAY_MACOSX)) + ((values.Length + 1) * Marshal.SizeOf(default(TW_STR255)))));
@@ -163,10 +163,9 @@ namespace NTwain
if (twCap.hContainer != IntPtr.Zero) twain.DsmMemFree(ref twCap.hContainer);
TWTY itemType = GetItemType<TValue>();
var platform = PlatformTools.GetPlatform();
// Allocate the container (go for worst case, which is TW_STR255)...
if (platform == Platform.MACOSX)
if (PlatformInfo.IsMacOSX)
{
// Allocate...
twCap.hContainer = twain.DsmMemAlloc((uint)(Marshal.SizeOf(default(TW_ENUMERATION_MACOSX)) + ((value.Items.Length + 1) * Marshal.SizeOf(default(TW_STR255)))));
@@ -184,7 +183,7 @@ namespace NTwain
lockedPtr += Marshal.SizeOf(twenumerationmacosx);
}
// Windows or the 2.4+ Linux DSM...
else if ((platform == Platform.WINDOWS) ||
else if ((PlatformInfo.IsWindows) ||
(twain.m_linuxdsm == TWAIN.LinuxDsm.IsLatestDsm) ||
((twain.m_blFoundLatestDsm || twain.m_blFoundLatestDsm64) && (twain.m_linuxdsm == TWAIN.LinuxDsm.IsLatestDsm)))
{
@@ -242,17 +241,16 @@ namespace NTwain
if (twCap.hContainer != IntPtr.Zero) twain.DsmMemFree(ref twCap.hContainer);
TWTY itemType = GetItemType<TValue>();
var platform = PlatformTools.GetPlatform();
// Allocate the container (go for worst case, which is TW_STR255)...
if (platform == Platform.MACOSX)
if (PlatformInfo.IsMacOSX)
{
// Allocate...
twCap.hContainer = twain.DsmMemAlloc((uint)(Marshal.SizeOf(default(TW_RANGE_MACOSX))));
lockedPtr = twain.DsmMemLock(twCap.hContainer);
}
// Windows or the 2.4+ Linux DSM...
else if ((platform == Platform.WINDOWS) ||
else if ((PlatformInfo.IsWindows) ||
(twain.m_linuxdsm == TWAIN.LinuxDsm.IsLatestDsm) ||
((twain.m_blFoundLatestDsm || twain.m_blFoundLatestDsm64) && (twain.m_linuxdsm == TWAIN.LinuxDsm.IsLatestDsm)))
{
@@ -281,8 +279,6 @@ namespace NTwain
{
// TODO: reduce this later
var platform = PlatformTools.GetPlatform();
TW_RANGE twrange = default;
TW_RANGE_MACOSX twrangemacosx = default;
TW_RANGE_LINUX64 twrangelinux64 = default;
@@ -292,7 +288,7 @@ namespace NTwain
default:
throw new NotSupportedException($"{itemType} is not supported for range.");
case TWTY.INT8:
if (platform == Platform.MACOSX)
if (PlatformInfo.IsMacOSX)
{
twrangemacosx.ItemType = (uint)itemType;
twrangemacosx.MinValue = (uint)Convert.ToSByte(value.MinValue);
@@ -324,7 +320,7 @@ namespace NTwain
}
break;
case TWTY.UINT8:
if (platform == Platform.MACOSX)
if (PlatformInfo.IsMacOSX)
{
twrangemacosx.ItemType = (uint)itemType;
twrangemacosx.MinValue = Convert.ToByte(value.MinValue);
@@ -356,7 +352,7 @@ namespace NTwain
}
break;
case TWTY.INT16:
if (platform == Platform.MACOSX)
if (PlatformInfo.IsMacOSX)
{
twrangemacosx.ItemType = (uint)itemType;
twrangemacosx.MinValue = (uint)Convert.ToInt16(value.MinValue);
@@ -389,7 +385,7 @@ namespace NTwain
break;
case TWTY.BOOL:
case TWTY.UINT16:
if (platform == Platform.MACOSX)
if (PlatformInfo.IsMacOSX)
{
twrangemacosx.ItemType = (uint)itemType;
twrangemacosx.MinValue = Convert.ToUInt16(value.MinValue);
@@ -421,7 +417,7 @@ namespace NTwain
}
break;
case TWTY.INT32:
if (platform == Platform.MACOSX)
if (PlatformInfo.IsMacOSX)
{
twrangemacosx.ItemType = (uint)itemType;
twrangemacosx.MinValue = (uint)Convert.ToInt32(value.MinValue);
@@ -453,7 +449,7 @@ namespace NTwain
}
break;
case TWTY.UINT32:
if (platform == Platform.MACOSX)
if (PlatformInfo.IsMacOSX)
{
twrangemacosx.ItemType = (uint)itemType;
twrangemacosx.MinValue = Convert.ToUInt32(value.MinValue);
@@ -490,7 +486,7 @@ namespace NTwain
double step = Convert.ToDouble(value.StepSize);
double def = Convert.ToDouble(value.DefaultValue);
double current = Convert.ToDouble(value.CurrentValue);
if (platform == Platform.MACOSX)
if (PlatformInfo.IsMacOSX)
{
TW_RANGE_FIX32_MACOSX twrangefix32macosx = default;
twrangefix32macosx.ItemType = (uint)itemType;