Beispiel #1
0
        public static RubyFile/*!*/ Reinitialize(
            ConversionStorage<int?>/*!*/ toInt,
            ConversionStorage<IDictionary<object, object>>/*!*/ toHash,
            ConversionStorage<MutableString>/*!*/ toPath,
            ConversionStorage<MutableString>/*!*/ toStr,
            RubyFile/*!*/ self,
            object descriptorOrPath, 
            [Optional]object optionsOrMode, 
            [Optional]object optionsOrPermissions,
            [DefaultParameterValue(null), DefaultProtocol]IDictionary<object, object> options) {

            var context = self.Context;
            
            Protocols.TryConvertToOptions(toHash, ref options, ref optionsOrMode, ref optionsOrPermissions);
            var toIntSite = toInt.GetSite(TryConvertToFixnumAction.Make(toInt.Context));

            IOInfo info = new IOInfo();
            if (optionsOrMode != Missing.Value) {
                int? m = toIntSite.Target(toIntSite, optionsOrMode);
                info = m.HasValue ? new IOInfo((IOMode)m) : IOInfo.Parse(context, Protocols.CastToString(toStr, optionsOrMode));
            }

            int permissions = 0;
            if (optionsOrPermissions != Missing.Value) {
                int? p = toIntSite.Target(toIntSite, optionsOrPermissions);
                if (!p.HasValue) {
                    throw RubyExceptions.CreateTypeConversionError(context.GetClassName(optionsOrPermissions), "Integer");
                }
                permissions = p.Value;
            }

            if (options != null) {
                info = info.AddOptions(toStr, options);
            }

            // TODO: permissions
            
            // descriptor or path:
            int? descriptor = toIntSite.Target(toIntSite, descriptorOrPath);
            if (descriptor.HasValue) {
                RubyIOOps.Reinitialize(self, descriptor.Value, info);
            } else {
                Reinitialize(self, Protocols.CastToPath(toPath, descriptorOrPath), info, permissions);
            }

            return self;
        }
Beispiel #2
0
        public IOInfo AddOptions(ConversionStorage <MutableString> /*!*/ toStr, IDictionary <object, object> options)
        {
            var context = toStr.Context;

            IOInfo result = this;
            object optionValue;

            if (options.TryGetValue(context.CreateAsciiSymbol("encoding"), out optionValue))
            {
                result = result.AddEncoding(context, Protocols.CastToString(toStr, optionValue));
            }

            if (options.TryGetValue(context.CreateAsciiSymbol("mode"), out optionValue))
            {
                result = result.AddModeAndEncoding(context, Protocols.CastToString(toStr, optionValue));
            }

            return(result);
        }
Beispiel #3
0
        public IOInfo AddModeAndEncoding(RubyContext /*!*/ context, MutableString /*!*/ modeAndEncoding)
        {
            IOInfo info = Parse(context, modeAndEncoding);

            if (_mode.HasValue)
            {
                throw RubyExceptions.CreateArgumentError("mode specified twice");
            }

            if (!HasEncoding)
            {
                return(info);
            }

            if (!info.HasEncoding)
            {
                return(new IOInfo(info.Mode, _externalEncoding, _internalEncoding));
            }

            throw RubyExceptions.CreateArgumentError("encoding specified twice");
        }
Beispiel #4
0
        private static RubyIO/*!*/ Reopen(ConversionStorage<MutableString>/*!*/ toPath, RubyIO/*!*/ io, object pathObj, IOInfo info) {
            MutableString path = Protocols.CastToPath(toPath, pathObj);
            Stream newStream = RubyFile.OpenFileStream(io.Context, path.ToString(path.Encoding.Encoding), info.Mode);
            io.Context.SetStream(io.GetFileDescriptor(), newStream);
            io.SetStream(newStream);
            io.Mode = info.Mode;

            if (info.HasEncoding) {
                io.ExternalEncoding = info.ExternalEncoding;
                io.InternalEncoding = info.InternalEncoding;
            }

            return io;
        }
Beispiel #5
0
        internal static RubyIO/*!*/ Reinitialize(RubyIO/*!*/ io, int descriptor, IOInfo info) {
            io.Mode = info.Mode;
            io.SetStream(GetDescriptorStream(io.Context, descriptor));
            io.SetFileDescriptor(descriptor);

            if (info.HasEncoding) {
                io.ExternalEncoding = info.ExternalEncoding;
                io.InternalEncoding = info.InternalEncoding;
            }

            return io;
        }
Beispiel #6
0
        public static RubyIO/*!*/ Reinitialize(
            ConversionStorage<int?>/*!*/ toInt,
            ConversionStorage<IDictionary<object, object>>/*!*/ toHash,
            ConversionStorage<MutableString>/*!*/ toStr,
            RubyIO/*!*/ self,
            object descriptor,
            [Optional]object optionsOrMode,
            [DefaultParameterValue(null), DefaultProtocol]IDictionary<object, object> options) {

            var context = self.Context;

            object _ = Missing.Value;
            Protocols.TryConvertToOptions(toHash, ref options, ref optionsOrMode, ref _);
            var toIntSite = toInt.GetSite(TryConvertToFixnumAction.Make(toInt.Context));

            IOInfo info = new IOInfo();
            if (optionsOrMode != Missing.Value) {
                int? m = toIntSite.Target(toIntSite, optionsOrMode);
                info = m.HasValue ? new IOInfo((IOMode)m) : IOInfo.Parse(context, Protocols.CastToString(toStr, optionsOrMode));
            }

            if (options != null) {
                info = info.AddOptions(toStr, options);
            }

            int? desc = toIntSite.Target(toIntSite, descriptor);
            if (!desc.HasValue) {
                throw RubyExceptions.CreateTypeConversionError(context.GetClassDisplayName(descriptor), "Fixnum");
            }
            Reinitialize(self, desc.Value, info);

            return self;
        }
Beispiel #7
0
        private static void Reinitialize(RubyFile/*!*/ file, MutableString/*!*/ path, IOInfo info, int permission) {
            var strPath = file.Context.DecodePath(path);
            var stream = RubyFile.OpenFileStream(file.Context, strPath, info.Mode);

            file.Path = strPath;
            file.Mode = info.Mode;
            file.SetStream(stream);
            file.SetFileDescriptor(file.Context.AllocateFileDescriptor(stream));

            if (info.HasEncoding) {
                file.ExternalEncoding = info.ExternalEncoding;
                file.InternalEncoding = info.InternalEncoding;
            }
        }