Added constants and conversion methods for enums in the C Run-time Library related to files.

pull/250/head
dahall 2021-06-02 13:08:30 -06:00
parent bc6d5ddff0
commit 370e8a857a
4 changed files with 265 additions and 0 deletions

View File

@ -0,0 +1,124 @@
using System.IO;
namespace Vanara.RunTimeLib
{
/// <summary>Extension methods for CRT enumerations to convert to .NET enumerations.</summary>
public static class ConstantConversionExtensions
{
/// <summary>Converts a <see cref="FileAttributeConstant"/> value to <see cref="FileAccess"/>.</summary>
/// <param name="value">The value.</param>
/// <returns>The result.</returns>
public static FileAccess ToFileAccess(this FileAttributeConstant value)
{
// Note: This is not done in a switch because the order of tests matters.
if ((FileAttributeConstant._A_RDONLY | FileAttributeConstant._A_NORMAL) == (value & (FileAttributeConstant._A_RDONLY | FileAttributeConstant._A_NORMAL)))
return FileAccess.ReadWrite;
else if (0 != (value & FileAttributeConstant._A_RDONLY))
return FileAccess.Read;
else if (0 != (value & FileAttributeConstant._A_NORMAL))
return FileAccess.Write;
else
return FileAccess.Read;
}
/// <summary>Converts a <see cref="FilePermissionConstant"/> value to <see cref="FileAccess"/>.</summary>
/// <param name="value">The value.</param>
/// <returns>The result.</returns>
public static FileAccess ToFileAccess(this FilePermissionConstant value)
{
// Note: This is not done in a switch because the order of tests matters.
if ((value & (FilePermissionConstant._S_IWRITE | FilePermissionConstant._S_IREAD)) != 0)
return FileAccess.ReadWrite;
else if ((value & FilePermissionConstant._S_IWRITE) != 0)
return FileAccess.Write;
else
return FileAccess.Read;
}
/// <summary>Converts a <see cref="FileAttributeConstant"/> value to <see cref="FileAttributes"/>.</summary>
/// <param name="value">The value.</param>
/// <returns>The result.</returns>
public static FileAttributes ToFileAttributes(this FileAttributeConstant value)
{
FileAttributes ret = 0;
if ((value & FileAttributeConstant._A_SUBDIR) != 0)
ret |= FileAttributes.Directory;
if ((value & FileAttributeConstant._A_RDONLY) != 0)
ret |= FileAttributes.ReadOnly;
if ((value & FileAttributeConstant._A_HIDDEN) != 0)
ret |= FileAttributes.Hidden;
if ((value & FileAttributeConstant._A_SYSTEM) != 0)
ret |= FileAttributes.System;
if ((value & FileAttributeConstant._A_ARCH) != 0)
ret |= FileAttributes.Archive;
return ret;
}
/// <summary>Converts a <see cref="FileOpConstant"/> value to <see cref="FileMode"/>.</summary>
/// <param name="value">The value.</param>
/// <returns>The result.</returns>
public static FileMode ToFileMode(this FileOpConstant value)
{
// Note: This is not done in a switch because the order of tests matters.
if ((FileOpConstant._O_CREAT | FileOpConstant._O_EXCL) == (value & (FileOpConstant._O_CREAT | FileOpConstant._O_EXCL)))
return FileMode.CreateNew;
else if ((FileOpConstant._O_CREAT | FileOpConstant._O_TRUNC) == (value & (FileOpConstant._O_CREAT | FileOpConstant._O_TRUNC)))
return FileMode.OpenOrCreate;
else if (0 != (value & FileOpConstant._O_APPEND))
return FileMode.Append;
else if (0 != (value & FileOpConstant._O_CREAT))
return FileMode.Create;
else if (0 != (value & FileOpConstant._O_RDWR))
return FileMode.Open;
else if (0 != (value & FileOpConstant._O_TRUNC))
return FileMode.Truncate;
else
return FileMode.OpenOrCreate; // This seemed the safest way to handled unrecognized types
}
/// <summary>Converts a <see cref="FileOpConstant"/> value to <see cref="FileOptions"/>.</summary>
/// <param name="value">The value.</param>
/// <returns>The result.</returns>
public static FileOptions ToFileOptions(this FileOpConstant value)
{
FileOptions ret = 0;
if ((value & FileOpConstant._O_RANDOM) != 0)
ret |= FileOptions.RandomAccess;
if ((value & FileOpConstant._O_TEMPORARY) != 0)
ret |= FileOptions.DeleteOnClose;
if ((value & FileOpConstant._O_SEQUENTIAL) != 0)
ret |= FileOptions.SequentialScan;
return ret;
}
/// <summary>Converts a <see cref="FileAttributeConstant"/> value to <see cref="FileShare"/>.</summary>
/// <param name="value">The value.</param>
/// <returns>The result.</returns>
public static FileShare ToFileShare(this FileAttributeConstant value)
{
// Note: This is not done in a switch because the order of tests matters.
if ((FileAttributeConstant._A_RDONLY | FileAttributeConstant._A_NORMAL) == (value & (FileAttributeConstant._A_RDONLY | FileAttributeConstant._A_NORMAL)))
return FileShare.ReadWrite;
else if (0 != (value & FileAttributeConstant._A_RDONLY))
return FileShare.Read;
else if (0 != (value & FileAttributeConstant._A_NORMAL))
return FileShare.Write;
else
return FileShare.Read;
}
/// <summary>Converts a <see cref="FilePermissionConstant"/> value to <see cref="FileShare"/>.</summary>
/// <param name="value">The value.</param>
/// <returns>The result.</returns>
public static FileShare ToFileShare(this FilePermissionConstant value)
{
// Note: This is not done in a switch because the order of tests matters.
if ((value & (FilePermissionConstant._S_IWRITE | FilePermissionConstant._S_IREAD)) != 0)
return FileShare.ReadWrite;
else if ((value & FilePermissionConstant._S_IWRITE) != 0)
return FileShare.Write;
else
return FileShare.Read;
}
}
}

78
Core/RunTimeLib/fcntl.cs Normal file
View File

@ -0,0 +1,78 @@
using System;
namespace Vanara.RunTimeLib
{
/// <summary>
/// The integer expression formed from one or more of these constants determines the type of reading or writing operations permitted. It
/// is formed by combining one or more constants with a translation-mode constant.
/// </summary>
[Flags]
public enum FileOpConstant : int
{
/// <summary>Opens file for reading only; if this flag is given, neither _O_RDWR nor _O_WRONLY can be given.</summary>
_O_RDONLY = 0x0000,
/// <summary>Opens file for writing only; if this flag is given, neither _O_RDONLY nor _O_RDWR can be given.</summary>
_O_WRONLY = 0x0001,
/// <summary>Opens file for both reading and writing; if this flag is given, neither _O_RDONLY nor _O_WRONLY can be given.</summary>
_O_RDWR = 0x0002,
/// <summary>Repositions the file pointer to the end of the file before every write operation.</summary>
_O_APPEND = 0x0008,
/// <summary>Creates and opens a new file for writing; this has no effect if the file specified by filename exists.</summary>
_O_CREAT = 0x0100,
/// <summary>
/// Opens and truncates an existing file to zero length; the file must have write permission. The contents of the file are
/// destroyed. If this flag is given, you cannot specify _O_RDONLY.
/// </summary>
_O_TRUNC = 0x0200,
/// <summary>Returns an error value if the file specified by filename exists. Only applies when used with _O_CREAT.</summary>
_O_EXCL = 0x0400,
/// <summary>Opens a file in text (translated) mode. (For more information, see Text and Binary Mode File I/O and fopen.)</summary>
_O_TEXT = 0x4000,
/// <summary>Opens the file in binary (untranslated) mode. (See fopen for a description of binary mode.)</summary>
_O_BINARY = 0x8000,
/// <summary>Opens a file in Unicode mode.</summary>
_O_WTEXT = 0x10000,
/// <summary>Opens a file in Unicode UTF-16 mode.</summary>
_O_U16TEXT = 0x20000,
/// <summary>Opens a file in Unicode UTF-8 mode.</summary>
_O_U8TEXT = 0x40000,
// macro to translate the C 2.0 name used to force binary mode for files
/// <summary></summary>
_O_RAW = _O_BINARY,
/// <summary>Prevents creation of a shared file descriptor.</summary>
_O_NOINHERIT = 0x0080,
/// <summary>
/// Creates a file as temporary; the file is deleted when the last file descriptor is closed. The pmode argument is required when
/// _O_CREAT is specified.
/// </summary>
_O_TEMPORARY = 0x0040,
/// <summary>
/// Creates a file as temporary and if possible does not flush to disk. The pmode argument is required when _O_CREAT is specified.
/// </summary>
_O_SHORT_LIVED = 0x1000,
/// <summary>get information about a directory</summary>
_O_OBTAIN_DIR = 0x2000,
/// <summary>Specifies that caching is optimized for, but not restricted to, sequential access from disk.</summary>
_O_SEQUENTIAL = 0x0020,
/// <summary>Specifies that caching is optimized for, but not restricted to, random access from disk.</summary>
_O_RANDOM = 0x0010,
}
}

30
Core/RunTimeLib/io.cs Normal file
View File

@ -0,0 +1,30 @@
using System;
namespace Vanara.RunTimeLib
{
/// <summary>These constants specify the current attributes of the file or directory specified by the function.</summary>
[Flags]
public enum FileAttributeConstant : int
{
/// <summary>Normal. File can be read or written to without restriction.</summary>
_A_NORMAL = 0x00,
/// <summary>Read-only. File cannot be opened for writing, and a file with the same name cannot be created.</summary>
_A_RDONLY = 0x01,
/// <summary>
/// Hidden file. Not normally seen with the DIR command, unless the /AH option is used. Returns information about normal files as
/// well as files with this attribute.
/// </summary>
_A_HIDDEN = 0x02,
/// <summary>System file. Not normally seen with the DIR command, unless the /AS option is used.</summary>
_A_SYSTEM = 0x04,
/// <summary>Subdirectory.</summary>
_A_SUBDIR = 0x10,
/// <summary>Archive. Set whenever the file is changed, and cleared by the BACKUP command.</summary>
_A_ARCH = 0x20,
}
}

33
Core/RunTimeLib/stat.cs Normal file
View File

@ -0,0 +1,33 @@
using System;
namespace Vanara.RunTimeLib
{
/// <summary>These constants are used to indicate file type in the st_mode field of the _stat structure.</summary>
[Flags]
public enum FilePermissionConstant : int
{
/// <summary>File type mask</summary>
_S_IFMT = 0xF000,
/// <summary>Directory</summary>
_S_IFDIR = 0x4000,
/// <summary>Character special</summary>
_S_IFCHR = 0x2000,
/// <summary>Pipe</summary>
_S_IFIFO = 0x1000,
/// <summary>Regular</summary>
_S_IFREG = 0x8000,
/// <summary>Read permission, owner</summary>
_S_IREAD = 0x0100,
/// <summary>Write permission, owner</summary>
_S_IWRITE = 0x0080,
/// <summary>Execute/search permission, owner</summary>
_S_IEXEC = 0x0040,
}
}