progress
This commit is contained in:
parent
16e76d6b31
commit
484dbfc9d9
529 changed files with 113694 additions and 0 deletions
232
AspClassic.Scripting/PlatformAdaptationLayer.cs
Normal file
232
AspClassic.Scripting/PlatformAdaptationLayer.cs
Normal file
|
@ -0,0 +1,232 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
using AspClassic.Scripting.Utils;
|
||||
|
||||
namespace AspClassic.Scripting;
|
||||
|
||||
[Serializable]
|
||||
public class PlatformAdaptationLayer
|
||||
{
|
||||
public static readonly PlatformAdaptationLayer Default = new PlatformAdaptationLayer();
|
||||
|
||||
public static readonly bool IsCompactFramework = Environment.OSVersion.Platform == PlatformID.WinCE || Environment.OSVersion.Platform == PlatformID.Xbox;
|
||||
|
||||
private static bool IsSingleRootFileSystem
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Environment.OSVersion.Platform != PlatformID.Unix)
|
||||
{
|
||||
return Environment.OSVersion.Platform == PlatformID.MacOSX;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual StringComparer PathComparer
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Environment.OSVersion.Platform != PlatformID.Unix)
|
||||
{
|
||||
return StringComparer.OrdinalIgnoreCase;
|
||||
}
|
||||
return StringComparer.Ordinal;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual string CurrentDirectory
|
||||
{
|
||||
get
|
||||
{
|
||||
return Directory.GetCurrentDirectory();
|
||||
}
|
||||
set
|
||||
{
|
||||
Directory.SetCurrentDirectory(value);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual Assembly LoadAssembly(string name)
|
||||
{
|
||||
return Assembly.Load(name);
|
||||
}
|
||||
|
||||
public virtual Assembly LoadAssemblyFromPath(string path)
|
||||
{
|
||||
return Assembly.LoadFile(path);
|
||||
}
|
||||
|
||||
public virtual void TerminateScriptExecution(int exitCode)
|
||||
{
|
||||
Environment.Exit(exitCode);
|
||||
}
|
||||
|
||||
public virtual bool FileExists(string path)
|
||||
{
|
||||
return File.Exists(path);
|
||||
}
|
||||
|
||||
public virtual bool DirectoryExists(string path)
|
||||
{
|
||||
return Directory.Exists(path);
|
||||
}
|
||||
|
||||
public virtual Stream OpenInputFileStream(string path, FileMode mode, FileAccess access, FileShare share)
|
||||
{
|
||||
return new FileStream(path, mode, access, share);
|
||||
}
|
||||
|
||||
public virtual Stream OpenInputFileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize)
|
||||
{
|
||||
return new FileStream(path, mode, access, share, bufferSize);
|
||||
}
|
||||
|
||||
public virtual Stream OpenInputFileStream(string path)
|
||||
{
|
||||
return new FileStream(path, FileMode.Open, FileAccess.Read);
|
||||
}
|
||||
|
||||
public virtual Stream OpenOutputFileStream(string path)
|
||||
{
|
||||
return new FileStream(path, FileMode.Create, FileAccess.Write);
|
||||
}
|
||||
|
||||
public virtual void DeleteFile(string path, bool deleteReadOnly)
|
||||
{
|
||||
FileInfo fileInfo = new FileInfo(path);
|
||||
if (deleteReadOnly && fileInfo.IsReadOnly)
|
||||
{
|
||||
fileInfo.IsReadOnly = false;
|
||||
}
|
||||
fileInfo.Delete();
|
||||
}
|
||||
|
||||
[Obsolete("Use GetFileSystemEntries instead")]
|
||||
public virtual string[] GetFiles(string path, string searchPattern)
|
||||
{
|
||||
return Directory.GetFiles(path, searchPattern);
|
||||
}
|
||||
|
||||
[Obsolete("Use GetFileSystemEntries instead")]
|
||||
public virtual string[] GetDirectories(string path, string searchPattern)
|
||||
{
|
||||
return Directory.GetDirectories(path, searchPattern);
|
||||
}
|
||||
|
||||
public string[] GetFileSystemEntries(string path, string searchPattern)
|
||||
{
|
||||
return GetFileSystemEntries(path, searchPattern, includeFiles: true, includeDirectories: true);
|
||||
}
|
||||
|
||||
public virtual string[] GetFileSystemEntries(string path, string searchPattern, bool includeFiles, bool includeDirectories)
|
||||
{
|
||||
if (includeFiles && includeDirectories)
|
||||
{
|
||||
return ArrayUtils.AppendRange(GetDirectories(path, searchPattern), GetFiles(path, searchPattern));
|
||||
}
|
||||
if (includeFiles)
|
||||
{
|
||||
return GetFiles(path, searchPattern);
|
||||
}
|
||||
if (includeDirectories)
|
||||
{
|
||||
return GetDirectories(path, searchPattern);
|
||||
}
|
||||
return ArrayUtils.EmptyStrings;
|
||||
}
|
||||
|
||||
public virtual string GetFullPath(string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Path.GetFullPath(path);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw Error.InvalidPath();
|
||||
}
|
||||
}
|
||||
|
||||
public virtual string CombinePaths(string path1, string path2)
|
||||
{
|
||||
return Path.Combine(path1, path2);
|
||||
}
|
||||
|
||||
public virtual string GetFileName(string path)
|
||||
{
|
||||
return Path.GetFileName(path);
|
||||
}
|
||||
|
||||
public virtual string GetDirectoryName(string path)
|
||||
{
|
||||
return Path.GetDirectoryName(path);
|
||||
}
|
||||
|
||||
public virtual string GetExtension(string path)
|
||||
{
|
||||
return Path.GetExtension(path);
|
||||
}
|
||||
|
||||
public virtual string GetFileNameWithoutExtension(string path)
|
||||
{
|
||||
return Path.GetFileNameWithoutExtension(path);
|
||||
}
|
||||
|
||||
public virtual bool IsAbsolutePath(string path)
|
||||
{
|
||||
if (IsSingleRootFileSystem)
|
||||
{
|
||||
return Path.IsPathRooted(path);
|
||||
}
|
||||
string pathRoot = Path.GetPathRoot(path);
|
||||
if (!pathRoot.EndsWith(":\\"))
|
||||
{
|
||||
return pathRoot.EndsWith(":/");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public virtual void CreateDirectory(string path)
|
||||
{
|
||||
Directory.CreateDirectory(path);
|
||||
}
|
||||
|
||||
public virtual void DeleteDirectory(string path, bool recursive)
|
||||
{
|
||||
Directory.Delete(path, recursive);
|
||||
}
|
||||
|
||||
public virtual void MoveFileSystemEntry(string sourcePath, string destinationPath)
|
||||
{
|
||||
Directory.Move(sourcePath, destinationPath);
|
||||
}
|
||||
|
||||
public virtual string GetEnvironmentVariable(string key)
|
||||
{
|
||||
return Environment.GetEnvironmentVariable(key);
|
||||
}
|
||||
|
||||
public virtual void SetEnvironmentVariable(string key, string value)
|
||||
{
|
||||
if (value != null && value.Length == 0)
|
||||
{
|
||||
if (!NativeMethods.SetEnvironmentVariable(key, value))
|
||||
{
|
||||
throw new ExternalException("SetEnvironmentVariable failed", Marshal.GetLastWin32Error());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Environment.SetEnvironmentVariable(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual IDictionary GetEnvironmentVariables()
|
||||
{
|
||||
return Environment.GetEnvironmentVariables();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue