using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text;
using Iot.Device.Arduino;
namespace ArduinoCsCompiler.Runtime
{
[ArduinoReplacement(typeof(System.IO.Path))]
internal static partial class MiniPath
{
[ArduinoImplementation]
public static char[] GetInvalidFileNameChars() => new char[] { '\0', '/' };
[ArduinoImplementation]
public static char[] GetInvalidPathChars() => new char[] { '\0' };
[ArduinoImplementation]
private static string RemoveLongPathPrefix(string path)
{
return path;
}
[ArduinoImplementation]
public static string GetTempPath()
{
const string TempEnvVar = "TMPDIR";
const string DefaultTempPath = "/tmp/";
string? path = Environment.GetEnvironmentVariable(TempEnvVar);
return
string.IsNullOrEmpty(path) ? DefaultTempPath :
MiniPathInternal.IsDirectorySeparator(path[path.Length - 1]) ? path :
path + MiniPathInternal.DirectorySeparatorChar;
}
[ArduinoImplementation]
public static string GetTempFileName()
{
const string Suffix = ".tmp";
const int SuffixByteLength = 4;
string template = GetTempPath() + "tmpXXXXXX" + Suffix + "\0";
byte[] name = Encoding.UTF8.GetBytes(template);
IntPtr fd = MiniInterop.CheckIo(MiniInterop.Sys.MksTemps(name, SuffixByteLength));
MiniInterop.Sys.Close(fd);
return Encoding.UTF8.GetString(name, 0, name.Length - 1);
}
[ArduinoImplementation]
public static bool IsPathRooted(
#if NET5_0_OR_GREATER
[NotNullWhen(true)]
#endif
string? path)
{
if (path == null)
{
return false;
}
return IsPathRooted(path.AsSpan());
}
[ArduinoImplementation]
public static bool IsPathRooted(ReadOnlySpan<char> path)
{
return path.Length > 0 && path[0] == MiniPathInternal.DirectorySeparatorChar;
}
[ArduinoImplementation]
public static string? GetPathRoot(string? path)
{
if (MiniPathInternal_File.IsEffectivelyEmpty(path))
{
return null;
}
return IsPathRooted(path) ? MiniPathInternal.DirectorySeparatorCharAsString : string.Empty;
}
[ArduinoImplementation]
public static ReadOnlySpan<char> GetPathRoot(ReadOnlySpan<char> path)
{
return IsPathRooted(path) ? MiniPathInternal.DirectorySeparatorCharAsString.AsSpan() : ReadOnlySpan<char>.Empty;
}
[ArduinoImplementation]
public static string GetFullPath(string path)
{
if (path == null)
{
throw new ArgumentNullException(nameof(path));
}
if (path.Length == 0)
{
throw new ArgumentException("SR.Arg_PathEmpty", nameof(path));
}
if (path.Contains('\0'))
{
throw new ArgumentException("SR.Argument_InvalidPathChars", nameof(path));
}
if (!IsPathRooted(path))
{
path = Path.Combine(MiniInterop.Sys.GetCwd(), path);
}
string collapsedString = MiniPathInternal.RemoveRelativeSegments(path, MiniPathInternal.GetRootLength(path));
string result = collapsedString.Length == 0 ? MiniPathInternal.DirectorySeparatorCharAsString : collapsedString;
return result;
}
internal static bool IsCaseSensitive
{
[ArduinoImplementation]
get
{
return true;
}
}
}
}