Pchp.Library.Streams.StreamWrapper.ParseMode C# (CSharp) Method

ParseMode() public method

Parse the mode argument passed to fopen() and make the appropriate FileMode and FileAccess combination. Integrate the relevant options from StreamOpenOptions too.
public ParseMode ( string mode, StreamOpenOptions options, FileMode &fileMode, FileAccess &fileAccess, StreamAccessOptions &accessOptions ) : bool
mode string Mode as passed to fopen().
options StreamOpenOptions The passed to fopen().
fileMode FileMode Resulting specifying opening mode.
fileAccess FileAccess Resulting specifying read/write access options.
accessOptions StreamAccessOptions Resulting giving /// additional information to the stream opener.
return bool
        public bool ParseMode(string mode, StreamOpenOptions options, out FileMode fileMode, out FileAccess fileAccess, out StreamAccessOptions accessOptions)
        {
            accessOptions = StreamAccessOptions.Empty;
            bool forceBinary = false; // The user requested a text stream
            bool forceText = false; // Use text access to the stream (default is binary)

            // First check for relevant options in StreamOpenOptions:

            // Search for the file only if mode=='[ra]*' and use_include_path==true.
            // StreamAccessOptions findFile = 0;
            if ((options & StreamOpenOptions.UseIncludePath) > 0)
            {
                // findFile = StreamAccessOptions.FindFile;
                accessOptions |= StreamAccessOptions.FindFile;
            }

            // Copy the AutoRemove option.
            if ((options & StreamOpenOptions.Temporary) > 0)
            {
                accessOptions |= StreamAccessOptions.Temporary;
            }

            // Now do the actual mode parsing:
            fileMode = FileMode.Open;
            fileAccess = FileAccess.Write;
            if (String.IsNullOrEmpty(mode))
            {
                PhpException.Throw(PhpError.Warning, ErrResources.empty_file_mode);
                return false;
            }

            switch (mode[0])
            {
                case 'r':
                    // flags = 0;
                    // fileMode is already set to Open
                    fileAccess = FileAccess.Read;
                    //accessOptions |= findFile;
                    break;

                case 'w':
                    // flags = O_TRUNC|O_CREAT;
                    // fileAccess is set to Write
                    fileMode = FileMode.Create;
                    //accessOptions |= findFile;
                    // EX: Note that use_include_path is applicable to all access methods.
                    // Create truncates the existing file to zero length
                    break;

                case 'a':
                    // flags = O_CREAT|O_APPEND;
                    // fileAccess is set to Write
                    fileMode = FileMode.Append;
                    //accessOptions |= findFile;
                    // Note: .NET does not support the "a+" mode, use "r+" and Seek()
                    break;

                case 'x':
                    // flags = O_CREAT|O_EXCL;
                    // fileAccess is set to Write
                    fileMode = FileMode.CreateNew;
                    accessOptions |= StreamAccessOptions.Exclusive;
                    break;

                default:
                    PhpException.Throw(PhpError.Warning, ErrResources.invalid_file_mode, mode);
                    return false;
            }

            if (mode.IndexOf('+') > -1)
            {
                // flags |= O_RDWR;
                fileAccess = FileAccess.ReadWrite;
            }

            if ((fileMode == FileMode.Append) && (fileAccess == FileAccess.ReadWrite))
            {
                // Note: .NET does not support the "a+" mode, use "r+" and Seek()
                fileMode = FileMode.OpenOrCreate;
                fileAccess = FileAccess.ReadWrite;
                accessOptions |= StreamAccessOptions.SeekEnd;
            }

            if (mode.IndexOf('b') > -1)
            {
                // flags |= O_BINARY;
                forceBinary = true;
            }
            if (mode.IndexOf('t') > -1)
            {
                // flags |= _O_TEXT;
                forceText = true;
            }

            // Exactly one of these options is required.
            if ((forceBinary && forceText) || (!forceBinary && !forceText))
            {
                //LocalConfiguration config = Configuration.Local;

                //// checks whether default mode is applicable:
                //if (config.FileSystem.DefaultFileOpenMode == "b")
                //{
                //    forceBinary = true;
                //}
                //else if (config.FileSystem.DefaultFileOpenMode == "t")
                //{
                //    forceText = true;
                //}
                //else
                //{
                //    PhpException.Throw(PhpError.Warning, ErrResources.ambiguous_file_mode, mode);
                //}
                throw new NotImplementedException("Configuration.FileSystem.DefaultFileOpenMode");

                // Binary mode is assumed
            }
            else if (forceText)
            {
                // Default mode is binary (unless the text mode is specified).
                accessOptions |= StreamAccessOptions.UseText;
            }

            // Store the two file-access flags into the access options too.
            accessOptions |= (StreamAccessOptions)fileAccess;

            return true;
        }

Same methods

StreamWrapper::ParseMode ( string mode, StreamOpenOptions options, StreamAccessOptions &accessOptions ) : bool