Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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");
                }
            }
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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++)));
        }
Ejemplo n.º 6
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++));
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
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);
        }
Ejemplo n.º 10
0
 public static void CloseRead(StringIO/*!*/ self) {
     self.GetReadableContent();
     self._mode = self._mode.CloseRead();
 }
Ejemplo n.º 11
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;
        }
Ejemplo n.º 12
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;
        }
Ejemplo n.º 13
0
 public static bool Eof(StringIO/*!*/ self) {
     var context = self.GetReadableContent();
     return self._position >= context.GetByteCount();
 }
Ejemplo n.º 14
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++));
        }
Ejemplo n.º 15
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;
        }
Ejemplo n.º 16
0
 public static void CloseRead(StringIO /*!*/ self)
 {
     self.GetReadableContent();
     self._mode = self._mode.CloseRead();
 }
Ejemplo n.º 17
0
        public static bool Eof(StringIO /*!*/ self)
        {
            var context = self.GetReadableContent();

            return(self._position >= context.GetByteCount());
        }
Ejemplo n.º 18
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;
        }
Ejemplo n.º 19
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;
 }
Ejemplo n.º 20
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");
                }
            }
        }
Ejemplo n.º 21
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;
        }
Ejemplo n.º 22
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++);
        }