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; }
public static void FileControl(StringIO /*!*/ self) { throw new NotImplementedError(); }
public static bool Sync(StringIO/*!*/ self) { // nop return true; }
public static StringIO/*!*/ SetBinaryMode(StringIO/*!*/ self) { // nop return self; }
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; }
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; }
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++); }
public static MutableString/*!*/ SystemRead(StringIO/*!*/ self, DynamicNull bytes, [DefaultProtocol, NotNull]MutableString buffer) { return Read(self, buffer, true); }
public static StringIO /*!*/ Reopen(StringIO /*!*/ self) { self.SetContent(MutableString.CreateBinary()); self._mode = IOMode.ReadWrite; return(self); }
public static StringIO /*!*/ Flush(StringIO /*!*/ self) { // nop return(self); }
public static bool IsConsole(StringIO /*!*/ self) { // nop return(false); }
public static bool SetSync(StringIO /*!*/ self, bool value) { // nop return(value); }
public static bool Sync(StringIO /*!*/ self) { // nop return(true); }
public static int FSync(StringIO /*!*/ self) { // nop return(0); }
public static object GetDescriptor(StringIO /*!*/ self) { // nop return(null); }
public static int Write(ConversionStorage<MutableString>/*!*/ tosConversion, StringIO/*!*/ self, object obj) { return Write(self, Protocols.ConvertToString(tosConversion, obj)); }
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; }
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); }
public static object GetByte(StringIO/*!*/ self) { var content = self.GetReadableContent(); if (self._position >= content.GetByteCount()) { return null; } return ScriptingRuntimeHelpers.Int32ToObject(content.GetByte(self._position++)); }
public static StringIO /*!*/ Reopen(StringIO /*!*/ self, [NotNull] MutableString /*!*/ content) { return(Reopen(self, content, null)); }
public static void SetLineNo(StringIO/*!*/ self, [DefaultProtocol]int value) { self._lineNumber = value; }
public static void Close(StringIO /*!*/ self) { self.GetContent(); self.Close(); }
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; }
public static void CloseRead(StringIO /*!*/ self) { self.GetReadableContent(); self._mode = self._mode.CloseRead(); }
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; }
public static void CloseWrite(StringIO /*!*/ self) { self.GetWritableContent(); self._mode = self._mode.CloseWrite(); }
public static object GetDescriptor(StringIO/*!*/ self) { // nop return null; }
public static bool IsClosed(StringIO /*!*/ self) { return(self._mode.IsClosed()); }
public static bool IsConsole(StringIO/*!*/ self) { // nop return false; }
public static bool IsClosedRead(StringIO /*!*/ self) { return(!self._mode.CanRead()); }
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; }
public static bool IsClosedWrite(StringIO /*!*/ self) { return(!self._mode.CanWrite()); }
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; }
public static int GetLength(StringIO /*!*/ self) { return(self.GetContent().GetByteCount()); }
public static MutableString/*!*/ SystemRead(StringIO/*!*/ self, [Optional]DynamicNull bytes) { return Read(self, null, true); }
public static int GetPosition(StringIO /*!*/ self) { return(self._position); }
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; }
public static void Pos(StringIO /*!*/ self, [DefaultProtocol] int pos) { self.SetPosition(pos); }
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"); } } }
public static bool Eof(StringIO /*!*/ self) { var context = self.GetReadableContent(); return(self._position >= context.GetByteCount()); }
public static int GetLineNo(StringIO/*!*/ self) { return self._lineNumber; }
public static MutableString /*!*/ GetString(StringIO /*!*/ self) { return(self._content); }
public static MutableString Gets(RubyScope/*!*/ scope, StringIO/*!*/ self) { return Gets(scope, self, scope.RubyContext.InputSeparator); }
public static MutableString /*!*/ SetString(StringIO /*!*/ self, [DefaultProtocol, NotNull] MutableString /*!*/ str) { self.SetContent(str); return(str); }
public static MutableString/*!*/ ReadLine(RubyScope/*!*/ scope, StringIO/*!*/ self) { return ReadLine(scope, self, scope.RubyContext.InputSeparator); }
public static int Write(ConversionStorage <MutableString> /*!*/ tosConversion, StringIO /*!*/ self, object obj) { return(Write(self, Protocols.ConvertToString(tosConversion, obj))); }
public static RubyArray/*!*/ ReadLines(RubyContext/*!*/ context, StringIO/*!*/ self) { return ReadLines(self, context.InputSeparator); }
public static MutableString /*!*/ SystemRead(StringIO /*!*/ self, [Optional] DynamicNull bytes) { return(Read(self, null, true)); }
public static object EachLine(RubyContext/*!*/ context, BlockParam block, StringIO/*!*/ self) { return EachLine(block, self, context.InputSeparator); }
public static MutableString /*!*/ SystemRead(StringIO /*!*/ self, DynamicNull bytes, [DefaultProtocol, NotNull] MutableString buffer) { return(Read(self, buffer, true)); }
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; }
public static int GetLineNo(StringIO /*!*/ self) { return(self._lineNumber); }
public static void FileControl(StringIO/*!*/ self) { throw new NotImplementedError(); }
public static void SetLineNo(StringIO /*!*/ self, [DefaultProtocol] int value) { self._lineNumber = value; }
public static int FSync(StringIO/*!*/ self) { // nop return 0; }
public static MutableString Gets(RubyScope /*!*/ scope, StringIO /*!*/ self) { return(Gets(scope, self, scope.RubyContext.InputSeparator)); }
public static bool SetSync(StringIO/*!*/ self, bool value) { // nop return value; }
public static MutableString /*!*/ ReadLine(RubyScope /*!*/ scope, StringIO /*!*/ self) { return(ReadLine(scope, self, scope.RubyContext.InputSeparator)); }
public static StringIO/*!*/ Flush(StringIO/*!*/ self) { // nop return self; }
public static StringIO /*!*/ SetBinaryMode(StringIO /*!*/ self) { // nop return(self); }