예제 #1
0
        public static object OpenIO([NotNull]BlockParam/*!*/ block, RubyClass/*!*/ self, [Optional]MutableString initialString, [Optional]MutableString mode) {
            MutableStringStream stream = new MutableStringStream(initialString ?? MutableString.CreateBinary());
            string ioMode = (mode != null) ? mode.ConvertToString() : "rb+";
            RubyIO io = new StringIO(self.Context, stream, ioMode);

            object result;
            block.Yield(io, out result);
            if (!io.Closed) {
                io.Close();
            }
            return result;
        }
예제 #2
0
 public static void FileControl(StringIO /*!*/ self)
 {
     throw new NotImplementedError();
 }
예제 #3
0
 public static bool Sync(StringIO/*!*/ self) {
     // nop
     return true;
 }
예제 #4
0
 public static StringIO/*!*/ SetBinaryMode(StringIO/*!*/ self) {
     // nop
     return self;
 }
예제 #5
0
        public static RubyArray/*!*/ ReadLines(StringIO/*!*/ self, [DefaultProtocol]MutableString separator) {
            var content = self.GetReadableContent();
            RubyArray result = new RubyArray();

            // no dynamic call, doesn't modify $_ scope variable:
            MutableString line;
            int position = self._position;
            while ((line = ReadLine(content, separator, ref position)) != null) {
                result.Add(line);
                self._lineNumber++;
            }
            self._position = position;
            return result;
        }
예제 #6
0
        public static MutableString Gets(RubyScope/*!*/ scope, StringIO/*!*/ self, [DefaultProtocol]MutableString separator) {
            var content = self.GetReadableContent();

            int position = self._position;
            MutableString result = ReadLine(content, separator, ref position);
            self._position = position;

            scope.GetInnerMostClosureScope().LastInputLine = result;
            self._lineNumber++;

            return result;
        }
예제 #7
0
        public static int ReadChar(StringIO/*!*/ self) {
            var content = self.GetReadableContent();
            int length = content.GetByteCount();

            if (self._position >= length) {
                throw new EOFError("end of file reached");
            }

            return content.GetByte(self._position++);
        }
예제 #8
0
 public static MutableString/*!*/ SystemRead(StringIO/*!*/ self, DynamicNull bytes, [DefaultProtocol, NotNull]MutableString buffer) {
     return Read(self, buffer, true);
 }
예제 #9
0
 public static StringIO /*!*/ Reopen(StringIO /*!*/ self)
 {
     self.SetContent(MutableString.CreateBinary());
     self._mode = IOMode.ReadWrite;
     return(self);
 }
예제 #10
0
 public static StringIO /*!*/ Flush(StringIO /*!*/ self)
 {
     // nop
     return(self);
 }
예제 #11
0
 public static bool IsConsole(StringIO /*!*/ self)
 {
     // nop
     return(false);
 }
예제 #12
0
 public static bool SetSync(StringIO /*!*/ self, bool value)
 {
     // nop
     return(value);
 }
예제 #13
0
 public static bool Sync(StringIO /*!*/ self)
 {
     // nop
     return(true);
 }
예제 #14
0
 public static int FSync(StringIO /*!*/ self)
 {
     // nop
     return(0);
 }
예제 #15
0
 public static object GetDescriptor(StringIO /*!*/ self)
 {
     // nop
     return(null);
 }
예제 #16
0
 public static int Write(ConversionStorage<MutableString>/*!*/ tosConversion, StringIO/*!*/ self, object obj) {
     return Write(self, Protocols.ConvertToString(tosConversion, obj));
 }
예제 #17
0
        public static MutableString Read(StringIO/*!*/ self, [DefaultProtocol]int count, [DefaultProtocol, Optional, NotNull]MutableString buffer) {
            var content = self.GetReadableContent();
            if (count < 0) {
                throw RubyExceptions.CreateArgumentError("negative length -1 given");
            }

            if (buffer != null) {
                buffer.Clear();
            }

            int length = content.GetByteCount();
            if (self._position >= length) {
                return null;
            }

            if (buffer == null) {
                buffer = MutableString.CreateBinary();
            }

            int bytesRead = Math.Min(count, length - self._position);
            buffer.Append(content, self._position, bytesRead).TaintBy(content);
            self._position += bytesRead;
            return buffer;
        }
예제 #18
0
        public static StringIO /*!*/ Reopen(RubyContext /*!*/ context, [NotNull] StringIO /*!*/ self, [NotNull] StringIO /*!*/ other)
        {
            self.SetContent(other._content);
            self._mode       = other._mode;
            self._lineNumber = other._lineNumber;
            self._position   = other._position;

            // TODO: this seems to be MRI bug
            // Shouldn't StringIO's taint be always same as the underlying string's taint?
            context.TaintObjectBy(self, other);
            return(self);
        }
예제 #19
0
        public static object GetByte(StringIO/*!*/ self) {
            var content = self.GetReadableContent();

            if (self._position >= content.GetByteCount()) {
                return null;
            }

            return ScriptingRuntimeHelpers.Int32ToObject(content.GetByte(self._position++));
        }
예제 #20
0
 public static StringIO /*!*/ Reopen(StringIO /*!*/ self, [NotNull] MutableString /*!*/ content)
 {
     return(Reopen(self, content, null));
 }
예제 #21
0
 public static void SetLineNo(StringIO/*!*/ self, [DefaultProtocol]int value) {
     self._lineNumber = value;
 }
예제 #22
0
 public static void Close(StringIO /*!*/ self)
 {
     self.GetContent();
     self.Close();
 }
예제 #23
0
        public static MutableString/*!*/ ReadLine(RubyScope/*!*/ scope, StringIO/*!*/ self, [DefaultProtocol]MutableString separator) {
            // no dynamic call, modifies $_ scope variable:
            MutableString result = Gets(scope, self, separator);
            if (result == null) {
                throw new EOFError("end of file reached");
            }

            return result;
        }
예제 #24
0
 public static void CloseRead(StringIO /*!*/ self)
 {
     self.GetReadableContent();
     self._mode = self._mode.CloseRead();
 }
예제 #25
0
 public static object EachLine(BlockParam block, StringIO/*!*/ self, [DefaultProtocol]MutableString separator) {
     // TODO: improve MSOps.EachLine
     var content = self.GetReadableContent();
     var result = MutableStringOps.EachLine(block, content, separator, self._position);
     return ReferenceEquals(result, content) ? self : result;
 }
예제 #26
0
 public static void CloseWrite(StringIO /*!*/ self)
 {
     self.GetWritableContent();
     self._mode = self._mode.CloseWrite();
 }
예제 #27
0
 public static object GetDescriptor(StringIO/*!*/ self) {
     // nop
     return null;
 }
예제 #28
0
 public static bool IsClosed(StringIO /*!*/ self)
 {
     return(self._mode.IsClosed());
 }
예제 #29
0
 public static bool IsConsole(StringIO/*!*/ self) {
     // nop
     return false;
 }
예제 #30
0
 public static bool IsClosedRead(StringIO /*!*/ self)
 {
     return(!self._mode.CanRead());
 }
예제 #31
0
        public static int Write(StringIO/*!*/ self, [NotNull]MutableString/*!*/ value) {
            var content = self.GetWritableContent();
            int length = content.GetByteCount();
            var bytesWritten = value.GetByteCount();
            int pos;

            if ((self._mode & IOMode.WriteAppends) != 0) {
                pos = length;
            } else {
                pos = self._position;
            }

            try {
                content.WriteBytes(pos, value, 0, bytesWritten);
            } catch (InvalidOperationException) {
                throw RubyExceptions.CreateIOError("not modifiable string");
            }

            content.TaintBy(value);
            self._position = pos + bytesWritten;
            return bytesWritten;
        }
예제 #32
0
 public static bool IsClosedWrite(StringIO /*!*/ self)
 {
     return(!self._mode.CanWrite());
 }
예제 #33
0
        public static MutableString/*!*/ Read(StringIO/*!*/ self, MutableString buffer, bool eofError) {
            var content = self.GetReadableContent();
            int start = self._position;
            int length = content.GetByteCount();

            if (buffer != null) {
                buffer.Clear();
            } else {
                buffer = MutableString.CreateBinary();
            }

            if (start < length) {
                self._position = length;
                buffer.Append(content, start, length - start).TaintBy(content);
            } else if (eofError) {
                throw new EOFError("end of file reached");
            }

            return buffer;
        }
예제 #34
0
 public static int GetLength(StringIO /*!*/ self)
 {
     return(self.GetContent().GetByteCount());
 }
예제 #35
0
 public static MutableString/*!*/ SystemRead(StringIO/*!*/ self, [Optional]DynamicNull bytes) {
     return Read(self, null, true);
 }
예제 #36
0
 public static int GetPosition(StringIO /*!*/ self)
 {
     return(self._position);
 }
예제 #37
0
 public static MutableString/*!*/ SystemRead(StringIO/*!*/ self, [DefaultProtocol]int bytes, [DefaultProtocol, Optional, NotNull]MutableString buffer) {
     var result = Read(self, bytes, buffer);
     if (result == null) {
         throw new EOFError("end of file reached");
     }
     return result;
 }
예제 #38
0
 public static void Pos(StringIO /*!*/ self, [DefaultProtocol] int pos)
 {
     self.SetPosition(pos);
 }
예제 #39
0
        public static void SetPreviousByte(StringIO/*!*/ self, [DefaultProtocol]int b) {
            // MRI: this checks if the IO is readable although it actually modifies the string:
            MutableString content = self.GetReadableContent();

            int pos = self._position - 1;
            if (pos >= 0) {
                int length = content.GetByteCount();
                try {
                    if (pos >= length) {
                        content.Append(0, pos - length);
                        content.Append(unchecked((byte)b));
                    } else {
                        content.SetByte(pos, unchecked((byte)b));
                    }
                    self._position = pos;
                } catch (InvalidOperationException) {
                    throw RubyExceptions.CreateIOError("not modifiable string");
                }
            }
        }
예제 #40
0
        public static bool Eof(StringIO /*!*/ self)
        {
            var context = self.GetReadableContent();

            return(self._position >= context.GetByteCount());
        }
예제 #41
0
 public static int GetLineNo(StringIO/*!*/ self) {
     return self._lineNumber;
 }
예제 #42
0
 public static MutableString /*!*/ GetString(StringIO /*!*/ self)
 {
     return(self._content);
 }
예제 #43
0
 public static MutableString Gets(RubyScope/*!*/ scope, StringIO/*!*/ self) {
     return Gets(scope, self, scope.RubyContext.InputSeparator);
 }
예제 #44
0
 public static MutableString /*!*/ SetString(StringIO /*!*/ self, [DefaultProtocol, NotNull] MutableString /*!*/ str)
 {
     self.SetContent(str);
     return(str);
 }
예제 #45
0
 public static MutableString/*!*/ ReadLine(RubyScope/*!*/ scope, StringIO/*!*/ self) {
     return ReadLine(scope, self, scope.RubyContext.InputSeparator);
 }
예제 #46
0
 public static int Write(ConversionStorage <MutableString> /*!*/ tosConversion, StringIO /*!*/ self, object obj)
 {
     return(Write(self, Protocols.ConvertToString(tosConversion, obj)));
 }
예제 #47
0
 public static RubyArray/*!*/ ReadLines(RubyContext/*!*/ context, StringIO/*!*/ self) {
     return ReadLines(self, context.InputSeparator);
 }
예제 #48
0
 public static MutableString /*!*/ SystemRead(StringIO /*!*/ self, [Optional] DynamicNull bytes)
 {
     return(Read(self, null, true));
 }
예제 #49
0
 public static object EachLine(RubyContext/*!*/ context, BlockParam block, StringIO/*!*/ self) {
     return EachLine(block, self, context.InputSeparator);
 }
예제 #50
0
 public static MutableString /*!*/ SystemRead(StringIO /*!*/ self, DynamicNull bytes, [DefaultProtocol, NotNull] MutableString buffer)
 {
     return(Read(self, buffer, true));
 }
예제 #51
0
        public static object EachByte(BlockParam block, StringIO/*!*/ self) {
            MutableString content;
            int pos;
            while ((pos = self._position) < (content = self.GetReadableContent()).GetByteCount()) {
                if (block == null) {
                    throw RubyExceptions.NoBlockGiven();
                }

                self._position++;

                object result;
                if (block.Yield(ScriptingRuntimeHelpers.Int32ToObject(content.GetByte(pos)), out result)) {
                    return result;
                }
            }
            return null;
        }
예제 #52
0
 public static int GetLineNo(StringIO /*!*/ self)
 {
     return(self._lineNumber);
 }
예제 #53
0
 public static void FileControl(StringIO/*!*/ self) {
     throw new NotImplementedError();
 }
예제 #54
0
 public static void SetLineNo(StringIO /*!*/ self, [DefaultProtocol] int value)
 {
     self._lineNumber = value;
 }
예제 #55
0
 public static int FSync(StringIO/*!*/ self) {
     // nop
     return 0;
 }
예제 #56
0
 public static MutableString Gets(RubyScope /*!*/ scope, StringIO /*!*/ self)
 {
     return(Gets(scope, self, scope.RubyContext.InputSeparator));
 }
예제 #57
0
 public static bool SetSync(StringIO/*!*/ self, bool value) {
     // nop
     return value;
 }
예제 #58
0
 public static MutableString /*!*/ ReadLine(RubyScope /*!*/ scope, StringIO /*!*/ self)
 {
     return(ReadLine(scope, self, scope.RubyContext.InputSeparator));
 }
예제 #59
0
 public static StringIO/*!*/ Flush(StringIO/*!*/ self) {
     // nop
     return self;
 }
예제 #60
0
 public static StringIO /*!*/ SetBinaryMode(StringIO /*!*/ self)
 {
     // nop
     return(self);
 }