diff --git a/Core/RunTimeLib/ConstConvExt.cs b/Core/RunTimeLib/ConstConvExt.cs new file mode 100644 index 00000000..3f0c4fec --- /dev/null +++ b/Core/RunTimeLib/ConstConvExt.cs @@ -0,0 +1,124 @@ +using System.IO; + +namespace Vanara.RunTimeLib +{ + /// Extension methods for CRT enumerations to convert to .NET enumerations. + public static class ConstantConversionExtensions + { + /// Converts a value to . + /// The value. + /// The result. + 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; + } + + /// Converts a value to . + /// The value. + /// The result. + 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; + } + + /// Converts a value to . + /// The value. + /// The result. + 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; + } + + /// Converts a value to . + /// The value. + /// The result. + 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 + } + + /// Converts a value to . + /// The value. + /// The result. + 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; + } + + /// Converts a value to . + /// The value. + /// The result. + 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; + } + + /// Converts a value to . + /// The value. + /// The result. + 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; + } + } +} \ No newline at end of file diff --git a/Core/RunTimeLib/fcntl.cs b/Core/RunTimeLib/fcntl.cs new file mode 100644 index 00000000..45044719 --- /dev/null +++ b/Core/RunTimeLib/fcntl.cs @@ -0,0 +1,78 @@ +using System; + +namespace Vanara.RunTimeLib +{ + /// + /// 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. + /// + [Flags] + public enum FileOpConstant : int + { + /// Opens file for reading only; if this flag is given, neither _O_RDWR nor _O_WRONLY can be given. + _O_RDONLY = 0x0000, + + /// Opens file for writing only; if this flag is given, neither _O_RDONLY nor _O_RDWR can be given. + _O_WRONLY = 0x0001, + + /// Opens file for both reading and writing; if this flag is given, neither _O_RDONLY nor _O_WRONLY can be given. + _O_RDWR = 0x0002, + + /// Repositions the file pointer to the end of the file before every write operation. + _O_APPEND = 0x0008, + + /// Creates and opens a new file for writing; this has no effect if the file specified by filename exists. + _O_CREAT = 0x0100, + + /// + /// 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. + /// + _O_TRUNC = 0x0200, + + /// Returns an error value if the file specified by filename exists. Only applies when used with _O_CREAT. + _O_EXCL = 0x0400, + + /// Opens a file in text (translated) mode. (For more information, see Text and Binary Mode File I/O and fopen.) + _O_TEXT = 0x4000, + + /// Opens the file in binary (untranslated) mode. (See fopen for a description of binary mode.) + _O_BINARY = 0x8000, + + /// Opens a file in Unicode mode. + _O_WTEXT = 0x10000, + + /// Opens a file in Unicode UTF-16 mode. + _O_U16TEXT = 0x20000, + + /// Opens a file in Unicode UTF-8 mode. + _O_U8TEXT = 0x40000, + + // macro to translate the C 2.0 name used to force binary mode for files + /// + _O_RAW = _O_BINARY, + + /// Prevents creation of a shared file descriptor. + _O_NOINHERIT = 0x0080, + + /// + /// 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. + /// + _O_TEMPORARY = 0x0040, + + /// + /// Creates a file as temporary and if possible does not flush to disk. The pmode argument is required when _O_CREAT is specified. + /// + _O_SHORT_LIVED = 0x1000, + + /// get information about a directory + _O_OBTAIN_DIR = 0x2000, + + /// Specifies that caching is optimized for, but not restricted to, sequential access from disk. + _O_SEQUENTIAL = 0x0020, + + /// Specifies that caching is optimized for, but not restricted to, random access from disk. + _O_RANDOM = 0x0010, + } +} \ No newline at end of file diff --git a/Core/RunTimeLib/io.cs b/Core/RunTimeLib/io.cs new file mode 100644 index 00000000..9cd1e44b --- /dev/null +++ b/Core/RunTimeLib/io.cs @@ -0,0 +1,30 @@ +using System; + +namespace Vanara.RunTimeLib +{ + /// These constants specify the current attributes of the file or directory specified by the function. + [Flags] + public enum FileAttributeConstant : int + { + /// Normal. File can be read or written to without restriction. + _A_NORMAL = 0x00, + + /// Read-only. File cannot be opened for writing, and a file with the same name cannot be created. + _A_RDONLY = 0x01, + + /// + /// 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. + /// + _A_HIDDEN = 0x02, + + /// System file. Not normally seen with the DIR command, unless the /AS option is used. + _A_SYSTEM = 0x04, + + /// Subdirectory. + _A_SUBDIR = 0x10, + + /// Archive. Set whenever the file is changed, and cleared by the BACKUP command. + _A_ARCH = 0x20, + } +} \ No newline at end of file diff --git a/Core/RunTimeLib/stat.cs b/Core/RunTimeLib/stat.cs new file mode 100644 index 00000000..d86e9e67 --- /dev/null +++ b/Core/RunTimeLib/stat.cs @@ -0,0 +1,33 @@ +using System; + +namespace Vanara.RunTimeLib +{ + /// These constants are used to indicate file type in the st_mode field of the _stat structure. + [Flags] + public enum FilePermissionConstant : int + { + /// File type mask + _S_IFMT = 0xF000, + + /// Directory + _S_IFDIR = 0x4000, + + /// Character special + _S_IFCHR = 0x2000, + + /// Pipe + _S_IFIFO = 0x1000, + + /// Regular + _S_IFREG = 0x8000, + + /// Read permission, owner + _S_IREAD = 0x0100, + + /// Write permission, owner + _S_IWRITE = 0x0080, + + /// Execute/search permission, owner + _S_IEXEC = 0x0040, + } +} \ No newline at end of file