public void ResetCurrentStream() { string file = CurrentFileName.ToString(); Stream stream = RubyFile.OpenFileStream(_context, file, _defaultMode); SingletonStream = new RubyIO(_context, stream, _defaultMode); }
public static void ForEach(BlockParam block, RubyClass /*!*/ self, [DefaultProtocol, NotNull] MutableString /*!*/ path, [DefaultProtocol] MutableString separator, [DefaultProtocol, DefaultParameterValue(-1)] int limit) { using (RubyIO io = new RubyIO(self.Context, File.OpenRead(path.ConvertToString()), IOMode.ReadOnly)) { Each(self.Context, block, io, separator, limit); } }
public static RubyArray /*!*/ ReadLines(RubyClass /*!*/ self, [DefaultProtocol, NotNull] MutableString path, [DefaultProtocol] MutableString separator, [DefaultProtocol, DefaultParameterValue(-1)] int limit) { using (RubyIO io = new RubyIO(self.Context, File.OpenRead(path.ConvertToString()), IOMode.ReadOnly)) { return(ReadLines(self.Context, io, separator, limit)); } }
public static object Load(RubyScope /*!*/ scope, RubyModule /*!*/ self, [NotNull] RubyIO /*!*/ source, [Optional] Proc proc) { BinaryReader reader = source.GetBinaryReader(); MarshalReader loader = new MarshalReader(reader, scope.RubyContext, scope.GlobalScope, proc); return(loader.Load()); }
public static object Each(RubyContext /*!*/ context, BlockParam block, RubyIO /*!*/ self, [DefaultProtocol] MutableString separator) { self.RequireReadable(); MutableString line; while ((line = self.ReadLineOrParagraph(separator)) != null) { if (block == null) { throw RubyExceptions.NoBlockGiven(); } line.IsTainted = true; context.InputProvider.LastInputLineNumber = ++self.LineNumber; object result; if (block.Yield(line, out result)) { return(result); } } return(self); }
public static bool IsAtty(RubyIO /*!*/ self) { ConsoleStreamType?console = self.ConsoleStreamType; if (console == null) { return(self.GetStream().BaseStream == Stream.Null); } int fd = GetStdHandleFd(console.Value); switch (Environment.OSVersion.Platform) { case PlatformID.Win32NT: case PlatformID.Win32S: case PlatformID.Win32Windows: case PlatformID.WinCE: IntPtr handle = GetStdHandle(fd); if (handle == IntPtr.Zero) { throw new Win32Exception(); } return(GetFileType(handle) == FILE_TYPE_CHAR); default: return(isatty(fd) == 1); } }
private static void Seek(RubyIO /*!*/ self, long pos, int seekOrigin) { if (seekOrigin < 0 || seekOrigin > 2) { throw RubyExceptions.CreateArgumentError("Invalid argument"); } if (self.IsConsoleDescriptor()) { throw new Errno.BadFileDescriptorError(); } // TODO: make sure we assert stream is not actually closed if (self.Closed) { throw RubyExceptions.CreateArgumentError("trying to seek on a non-existent stream?"); } SeekOrigin origin = SeekOrigin.Current; if (seekOrigin == SEEK_SET) { origin = SeekOrigin.Begin; } else if (seekOrigin == SEEK_END) { origin = SeekOrigin.End; } self.Seek(pos, origin); }
private void Test_Read1() { string s; string crlf = "\r\n"; var stream = new TestStream(false, B( "ab\r\r\n" + "e" + (s = "fgh" + crlf + "ijkl" + crlf + "mnop" + crlf + crlf + crlf + crlf + "qrst") + crlf + "!" )); int s_crlf_count = 6; var io = new RubyIO(Context, stream, "r"); Assert(io.PeekByte() == (byte)'a'); var buffer = MutableString.CreateBinary(B("foo:")); Assert(io.AppendBytes(buffer, 4) == 4); Assert(buffer.ToString() == "foo:ab\r\n"); buffer = MutableString.CreateBinary(); Assert(io.AppendBytes(buffer, 1) == 1); Assert(buffer.ToString() == "e"); buffer = MutableString.CreateMutable("x:"); int c = s.Length - s_crlf_count - 2; Assert(io.AppendBytes(buffer, c) == c); Assert(buffer.ToString() == "x:" + s.Replace(crlf, "\n").Substring(0, c)); buffer = MutableString.CreateBinary(); Assert(io.AppendBytes(buffer, 10) == 4); Assert(buffer.ToString() == "st\n!"); buffer = MutableString.CreateBinary(); Assert(io.AppendBytes(buffer, 10) == 0); Assert(buffer.ToString() == ""); }
public static MutableString /*!*/ ReadFile(RubyClass /*!*/ self, [DefaultProtocol, NotNull] MutableString /*!*/ path) { using (RubyIO io = OpenFileForRead(self.Context, path)) { return(Read(io)); } }
public static object Getc(RubyIO /*!*/ self) { self.AssertOpenedForReading(); int c = self.ReadByteNormalizeEoln(); return((c != -1) ? ScriptingRuntimeHelpers.Int32ToObject(c) : null); }
public static RubyArray /*!*/ ReadLines(RubyClass /*!*/ self, [DefaultProtocol, NotNull] MutableString path, [DefaultProtocol] MutableString separator) { using (RubyIO io = new RubyIO(self.Context, File.OpenRead(path.ConvertToString()), "r")) { return(ReadLines(self.Context, io, separator)); } }
public static MutableString /*!*/ Read(RubyIO /*!*/ self) { var buffer = MutableString.CreateBinary(); self.AppendBytes(buffer, Int32.MaxValue); return(buffer); }
public static RubyIO /*!*/ Reinitialize(RubyIO /*!*/ self, [DefaultProtocol] int descriptor, int mode) { self.Mode = (IOMode)mode; self.SetStream(GetDescriptorStream(self.Context, descriptor)); self.SetFileDescriptor(descriptor); return(self); }
//write_nonblock #endregion #region read, read_nonblock private static byte[] ReadAllBytes(RubyIO /*!*/ io) { var fixedBuffer = new byte[io.Length]; io.ReadBytes(fixedBuffer, 0, (int)io.Length); return(fixedBuffer); }
public static RubyIO /*!*/ Reopen(RubyIO /*!*/ self, [NotNull] RubyIO /*!*/ source) { self.Context.RedirectFileDescriptor(self.GetFileDescriptor(), source.GetFileDescriptor()); self.SetStream(source.GetStream()); self.Mode = source.Mode; return(self); }
public static RubyIO /*!*/ Binmode(RubyIO /*!*/ self) { if (!self.Closed && self.Position == 0) { self.PreserveEndOfLines = true; } return(self); }
public static object Dump(RubyModule /*!*/ self, object obj, [NotNull] RubyIO /*!*/ io, [Optional] int?limit) { BinaryWriter writer = io.GetBinaryWriter(); MarshalWriter dumper = new MarshalWriter(writer, self.Context, limit); dumper.Dump(obj); return(io); }
public static void CreateIO(RubyIO/*!*/ self, [DefaultProtocol]int fileDescriptor, [DefaultProtocol, NotNull, Optional]MutableString modeString) { // TODO: if (modeString != null) { self.ResetIOMode(modeString.ConvertToString()); } }
public static void CloseWriter(RubyIO /*!*/ self) { if (self.Closed) { throw RubyExceptions.CreateIOError("closed stream"); } self.CloseWriter(); }
public static object /*!*/ Pos(RubyIO /*!*/ self) { if (self.Position <= Int32.MaxValue) { return((int)self.Position); } return((BigInteger)self.Position); }
public static RubyIO /*!*/ Reopen(RubyIO /*!*/ self, [DefaultProtocol, NotNull] MutableString /*!*/ path, int mode) { Stream newStream = RubyFile.OpenFileStream(self.Context, path.ConvertToString(), (IOMode)mode); self.Context.SetStream(self.GetFileDescriptor(), newStream); self.SetStream(newStream); self.Mode = (IOMode)mode; return(self); }
public static RubyIO/*!*/ InitializeCopy(RubyIO/*!*/ self, [NotNull]RubyIO/*!*/ source) { Stream stream = source.GetStream(); int descriptor = self.Context.DuplicateFileDescriptor(source.GetFileDescriptor()); self.SetStream(stream); self.SetFileDescriptor(descriptor); self.Mode = source.Mode; return self; }
public static void Pos(RubyIO /*!*/ self, [DefaultProtocol] int value) { if (self.IsConsoleDescriptor()) { throw new Errno.BadFileDescriptorError(); } self.Seek(value, SeekOrigin.Begin); }
private static RubyIO /*!*/ ToIo(RubyContext /*!*/ context, object obj) { RubyIO io = obj as RubyIO; if (io == null) { throw RubyExceptions.CreateTypeConversionError(RubyUtils.GetClassName(context, obj), "IO"); } return(io); }
public static int Write(RubyIO /*!*/ self, [NotNull] MutableString /*!*/ val) { int bytesWritten = val.IsEmpty ? 0 : self.WriteBytes(val, 0, val.GetByteCount()); if (self.AutoFlush) { self.Flush(); } return(bytesWritten); }
public static RubyIO /*!*/ InitializeCopy(RubyIO /*!*/ self, [NotNull] RubyIO /*!*/ source) { Stream stream = source.GetStream(); int descriptor = self.Context.DuplicateFileDescriptor(source.GetFileDescriptor()); self.SetStream(stream); self.SetFileDescriptor(descriptor); self.Mode = source.Mode; return(self); }
public static int Write(RubyIO /*!*/ self, [NotNull] MutableString /*!*/ val) { self.AssertOpenedForWriting(); int bytesWritten = self.Write(val); if (self.AutoFlush) { self.Flush(); } return(bytesWritten); }
public static int Seek(RubyIO /*!*/ self, [NotNull] BigInteger /*!*/ pos, [DefaultProtocol, DefaultParameterValue(SEEK_SET)] int seekOrigin) { long longPos; if (!pos.AsInt64(out longPos)) { throw RubyExceptions.CreateRangeError("bignum too big to convert into `long'"); } Seek(self, longPos, seekOrigin); return(0); }
public static int WriteNoBlock(RubyIO /*!*/ self, [NotNull] MutableString /*!*/ val) { var stream = self.GetWritableStream(); try { stream.WriteTimeout = 0; } catch (InvalidOperationException) { throw RubyExceptions.CreateEBADF(); } return(Write(self, val)); }
public static MutableString Gets(RubyScope /*!*/ scope, RubyIO /*!*/ self, [DefaultProtocol] MutableString separator) { MutableString result = self.ReadLineOrParagraph(separator); KernelOps.Taint(scope.RubyContext, result); scope.GetInnerMostClosureScope().LastInputLine = result; scope.RubyContext.InputProvider.IncrementLastInputLineNumber(); return(result); }
public static RubyIO /*!*/ CreateIO(RubyClass /*!*/ self, [DefaultProtocol] int fileDescriptor, [DefaultProtocol, NotNull, Optional] MutableString modeString) { // TODO: a new RubyIO should be created here RubyIO result = self.Context.GetDescriptor(fileDescriptor); if (modeString != null) { result.ResetIOMode(modeString.ConvertToString()); } return(result); }
public static int ReadChar(RubyIO /*!*/ self) { self.AssertOpenedForReading(); int c = self.ReadByteNormalizeEoln(); if (c == -1) { throw new EOFError("end of file reached"); } return(c); }
public static MutableString /*!*/ ReadLine(RubyScope /*!*/ scope, RubyIO /*!*/ self, [DefaultProtocol] MutableString separator) { // no dynamic call, modifies $_ scope variable: MutableString result = Gets(scope, self); if (result == null) { throw new EOFError("end of file reached"); } return(result); }
public Request(HttpRequestBase request) { ContractUtils.RequiresNotNull(request, "request"); // http or https this.scheme = request.Url.Scheme; // move headers to a Ruby Hash this.headers = new Hash(RubyEngine.Context); foreach (string key in request.Headers.AllKeys) { string value = request.Headers.Get(key); if (string.IsNullOrEmpty(value)) continue; headers.Add(key, value); } this.queryString = request.QueryString.ToString(); this.body = new RubyIO(RubyEngine.Context, request.InputStream, IOMode.ReadOnly); // Save the origional request incase it's needed. OrigionalRequest = request; }
public static int WriteNoBlock(ConversionStorage<MutableString>/*!*/ tosConversion, RubyIO/*!*/ self, object obj) { return Write(self, Protocols.ConvertToString(tosConversion, obj)); }
public static int WriteNoBlock(RubyIO/*!*/ self, [NotNull]MutableString/*!*/ val) { var stream = self.GetWritableStream(); int result = -1; self.NonBlockingOperation(() => result = Write(self, val), false); return result; }
public static RubyArray/*!*/ ReadLines(RubyContext/*!*/ context, RubyIO/*!*/ self, [DefaultProtocol]MutableString separator, [DefaultProtocol]int limit) { RubyArray result = new RubyArray(); // no dynamic call, doesn't modify $_ scope variable: MutableString line; while ((line = self.ReadLineOrParagraph(separator, limit)) != null) { result.Add(line); } self.LineNumber += result.Count; context.InputProvider.LastInputLineNumber = self.LineNumber; return result; }
public static RubyArray/*!*/ ReadLines(RubyContext/*!*/ context, RubyIO/*!*/ self, DynamicNull separator) { return ReadLines(context, self, null, -1); }
public static MutableString/*!*/ ReadLine(RubyScope/*!*/ scope, RubyIO/*!*/ self, [DefaultProtocol]MutableString separator, [DefaultProtocol]int limit) { // no dynamic call, modifies $_ scope variable: MutableString result = Gets(scope, self, separator, limit); if (result == null) { throw new EOFError("end of file reached"); } return result; }
public static MutableString/*!*/ ReadLine(RubyScope/*!*/ scope, RubyIO/*!*/ self, DynamicNull separator) { return ReadLine(scope, self, null, -1); }
public static int ReadChar(RubyIO/*!*/ self) { self.RequireReadable(); int c = self.ReadByteNormalizeEoln(); if (c == -1) { throw new EOFError("end of file reached"); } return c; }
public static MutableString/*!*/ SystemRead(RubyIO/*!*/ self, [DefaultProtocol]int bytes, [DefaultProtocol, Optional]MutableString buffer) { var stream = self.GetReadableStream(); if (stream.DataBuffered) { throw RubyExceptions.CreateIOError("sysread for buffered IO"); } // We use Flush to simulate non-buffered IO. // A better approach would be to create a parallel FileStream with // System.IO.FileOptions.WriteThrough (which corresponds to FILE_FLAG_NO_BUFFERING), and also maybe // System.IO.FileOptions.SequentialScan (FILE_FLAG_SEQUENTIAL_SCAN). // TODO: sysopen does that? stream.Flush(); var result = Read(self, bytes, buffer); if (result == null) { throw new EOFError("end of file reached"); } return result; }
public static MutableString/*!*/ Read(RubyIO/*!*/ self, DynamicNull bytes, [DefaultProtocol, Optional]MutableString buffer) { buffer = PrepareReadBuffer(self, buffer); self.AppendBytes(buffer, Int32.MaxValue); return buffer; }
private static MutableString PrepareReadBuffer(RubyIO/*!*/ io, MutableString buffer) { if (buffer == null) { buffer = MutableString.CreateBinary(); } else { buffer.Clear(); } #if TODO var internalEncoding = io.InternalEncoding ?? io.ExternalEncoding; if (buffer != null) { buffer.Clear(); buffer.ForceEncoding(internalEncoding); } else if (io.ExternalEncoding == RubyEncoding.Binary && internalEncoding == RubyEncoding.Binary) { buffer = MutableString.CreateBinary(); } else { buffer = MutableString.CreateMutable(internalEncoding); } #endif return buffer; }
public static MutableString/*!*/ Read(RubyIO/*!*/ self) { return Read(self, null, null); }
public static object SysSeek(RubyIO/*!*/ self, [DefaultProtocol]IntegerValue pos, [DefaultProtocol, DefaultParameterValue(SEEK_SET)]int seekOrigin) { self.Flush(); self.Seek(pos.ToInt64(), RubyIO.ToSeekOrigin(seekOrigin)); return pos.ToObject(); }
public static MutableString Read(RubyIO/*!*/ self, [DefaultProtocol]int bytes, [DefaultProtocol, Optional]MutableString buffer) { self.RequireReadable(); if (bytes < 0) { throw RubyExceptions.CreateArgumentError("negative length -1 given"); } buffer = PrepareReadBuffer(self, buffer); int bytesRead = self.AppendBytes(buffer, bytes); return (bytesRead == 0 && bytes != 0) ? null : buffer; }
public static object/*!*/ Pos(RubyIO/*!*/ self) { if (self.Position <= Int32.MaxValue) { return (int)self.Position; } return (BigInteger)self.Position; }
public static MutableString ReadNoBlock(RubyIO/*!*/ self, [DefaultProtocol]int bytes, [DefaultProtocol, Optional]MutableString buffer) { var stream = self.GetReadableStream(); MutableString result = null; self.NonBlockingOperation(() => result = Read(self, bytes, buffer), true); return result; }
public static void Pos(RubyIO/*!*/ self, [DefaultProtocol]IntegerValue pos) { self.Seek(pos.ToInt64(), SeekOrigin.Begin); }
public static MutableString/*!*/ ReadLine(RubyScope/*!*/ scope, RubyIO/*!*/ self) { return ReadLine(scope, self, scope.RubyContext.InputSeparator, -1); }
public static int GetLineNumber(RubyIO/*!*/ self) { self.RequireOpen(); return self.LineNumber; }
public static MutableString/*!*/ ReadLine(RubyScope/*!*/ scope, RubyIO/*!*/ self, [DefaultProtocol, NotNull]Union<MutableString, int> separatorOrLimit) { if (separatorOrLimit.IsFixnum()) { return ReadLine(scope, self, scope.RubyContext.InputSeparator, separatorOrLimit.Fixnum()); } else { return ReadLine(scope, self, separatorOrLimit.String(), -1); } }
public static void SetLineNumber(RubyContext/*!*/ context, RubyIO/*!*/ self, [DefaultProtocol]int value) { self.RequireOpen(); self.LineNumber = value; }
public static RubyArray/*!*/ ReadLines(RubyContext/*!*/ context, RubyIO/*!*/ self) { return ReadLines(context, self, context.InputSeparator, -1); }
public static int Write(RubyIO/*!*/ self, [NotNull]MutableString/*!*/ val) { int bytesWritten = val.IsEmpty ? 0 : self.WriteBytes(val, 0, val.GetByteCount()); if (self.AutoFlush) { self.Flush(); } return bytesWritten; }
public static RubyArray/*!*/ ReadLines(RubyContext/*!*/ context, RubyIO/*!*/ self, [DefaultProtocol, NotNull]Union<MutableString, int> separatorOrLimit) { if (separatorOrLimit.IsFixnum()) { return ReadLines(context, self, context.InputSeparator, separatorOrLimit.Fixnum()); } else { return ReadLines(context, self, separatorOrLimit.String(), -1); } }
public static int SysWrite(BinaryOpStorage/*!*/ writeStorage, ConversionStorage<MutableString>/*!*/ tosConversion, RubyContext/*!*/ context, RubyIO/*!*/ self, [NotNull]MutableString/*!*/ val) { RubyBufferedStream stream = self.GetWritableStream(); if (stream.DataBuffered) { PrintOps.ReportWarning(writeStorage, tosConversion, MutableString.CreateAscii("syswrite for buffered IO")); } int bytes = Write(self, val); self.Flush(); return bytes; }
public static RubyArray/*!*/ ReadLines(RubyClass/*!*/ self, [DefaultProtocol, NotNull]MutableString path, [DefaultProtocol]MutableString separator, [DefaultProtocol, DefaultParameterValue(-1)]int limit) { using (RubyIO io = new RubyIO(self.Context, File.OpenRead(path.ConvertToString()), IOMode.ReadOnly)) { return ReadLines(self.Context, io, separator, limit); } }
public static int SysWrite(BinaryOpStorage/*!*/ writeStorage, ConversionStorage<MutableString>/*!*/ tosConversion, RubyContext/*!*/ context, RubyIO/*!*/ self, object obj) { return SysWrite(writeStorage, tosConversion, context, self, Protocols.ConvertToString(tosConversion, obj)); }