public static RubyTopLevelScope/*!*/ CreateMainTopLevelScope(LocalsDictionary/*!*/ locals, Scope/*!*/ globalScope, LanguageContext/*!*/ language, out object self, out RuntimeFlowControl/*!*/ rfc, string dataPath, int dataOffset) { Assert.NotNull(locals, globalScope, language); GlobalScopeExtension rubyGlobalScope = (GlobalScopeExtension)language.EnsureScopeExtension(globalScope); RubyTopLevelScope scope = new RubyTopLevelScope(rubyGlobalScope, null, locals); scope.Initialize(new RuntimeFlowControl(), RubyMethodAttributes.PrivateInstance, rubyGlobalScope.MainObject); scope.SetDebugName(rubyGlobalScope.IsHosted ? "top-primary-hosted" : "top-primary"); // define TOPLEVEL_BINDING constant: if (!rubyGlobalScope.IsHosted) { var objectClass = rubyGlobalScope.Context.ObjectClass; objectClass.SetConstant("TOPLEVEL_BINDING", new Binding(scope)); if (dataOffset >= 0) { RubyFile dataFile; if (File.Exists(dataPath)) { dataFile = new RubyFile(rubyGlobalScope.Context, dataPath, RubyFileMode.RDONLY); dataFile.Seek(dataOffset, SeekOrigin.Begin); } else { dataFile = null; } objectClass.SetConstant("DATA", dataFile); } } self = scope.SelfObject; rfc = scope.RuntimeFlowControl; return scope; }
public static MutableString /*!*/ Read(ConversionStorage <int> /*!*/ fixnumCast, RubyClass /*!*/ self, [DefaultProtocol, NotNull] MutableString /*!*/ path, [DefaultParameterValue(null)] object lengthObj, [DefaultParameterValue(0)] object offsetObj) { var site = fixnumCast.GetSite(ConvertToFixnumAction.Make(fixnumCast.Context)); int length = (lengthObj != null) ? site.Target(site, lengthObj) : 0; int offset = (offsetObj != null) ? site.Target(site, offsetObj) : 0; if (offset < 0) { throw RubyExceptions.CreateEINVAL(); } if (length < 0) { throw RubyExceptions.CreateArgumentError("negative length {0} given", length); } using (RubyIO io = new RubyFile(self.Context, path.ConvertToString(), IOMode.ReadOnly)) { if (offset > 0) { io.Seek(offset, SeekOrigin.Begin); } if (lengthObj == null) { return(Read(io)); } else { return(Read(io, length, null)); } } }
public static RubyTopLevelScope/*!*/ CreateMainTopLevelScope(LocalsDictionary/*!*/ locals, Scope/*!*/ globalScope, LanguageContext/*!*/ language, out object self, out RuntimeFlowControl/*!*/ rfc, string dataPath, int dataOffset) { Assert.NotNull(locals, globalScope, language); RubyContext context = (RubyContext)language; RubyGlobalScope rubyGlobalScope = context.InitializeGlobalScope(globalScope, false); RubyTopLevelScope scope = new RubyTopLevelScope(rubyGlobalScope, null, locals); scope.Initialize(new RuntimeFlowControl(), RubyMethodAttributes.PrivateInstance, rubyGlobalScope.MainObject); scope.SetDebugName("top-main"); var objectClass = context.ObjectClass; objectClass.SetConstant("TOPLEVEL_BINDING", new Binding(scope)); if (dataOffset >= 0) { RubyFile dataFile; if (context.DomainManager.Platform.FileExists(dataPath)) { dataFile = new RubyFile(context, dataPath, RubyFileMode.RDONLY); dataFile.Seek(dataOffset, SeekOrigin.Begin); } else { dataFile = null; } objectClass.SetConstant("DATA", dataFile); } self = scope.SelfObject; rfc = scope.RuntimeFlowControl; return scope; }
public void ResetCurrentStream() { string file = CurrentFileName.ToString(); Stream stream = RubyFile.OpenFileStream(_context, file, _defaultMode); SingletonStream = new RubyIO(_context, stream, _defaultMode); }
public static int ChangeOwner(RubyContext /*!*/ context, RubyFile /*!*/ self, object owner, object group) { if ((owner == null || owner is int) && (group == null || group is int)) { return(0); } throw RubyExceptions.CreateUnexpectedTypeError(context, owner, "Fixnum"); }
public static MutableString /*!*/ Inspect(RubyContext /*!*/ context, RubyFile /*!*/ self) { return(MutableString.CreateMutable(context.GetPathEncoding()). Append("#<File:"). Append(self.Path). Append(self.Closed ? " (closed)" : ""). Append('>')); }
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 int Truncate(RubyFile /*!*/ self, [DefaultProtocol] int size) { if (size < 0) { throw new InvalidError(); } self.Length = size; return(0); }
public static int Truncate(RubyClass /*!*/ self, [DefaultProtocol, NotNull] MutableString /*!*/ path, [DefaultProtocol] int size) { if (size < 0) { throw new InvalidError(); } using (RubyFile f = new RubyFile(self.Context, path, IOMode.ReadWrite)) { f.Length = size; } return(0); }
public static void SetDataConstant(RubyScope/*!*/ scope, string/*!*/ dataPath, int dataOffset) { Debug.Assert(dataOffset >= 0); RubyFile dataFile; RubyContext context = scope.RubyContext; if (context.DomainManager.Platform.FileExists(dataPath)) { dataFile = new RubyFile(context, dataPath, RubyFileMode.RDONLY); dataFile.Seek(dataOffset, SeekOrigin.Begin); } else { dataFile = null; } context.ObjectClass.SetConstant("DATA", dataFile); }
public static int SysOpen(RubyClass /*!*/ self, [NotNull] MutableString path, [Optional] MutableString mode, [Optional] int perm) { if (RubyFileOps.DirectoryExists(self.Context, path)) { // TODO: What file descriptor should be returned for a directory? return(-1); } RubyIO io = new RubyFile(self.Context, path.ToString(), IOModeEnum.Parse(mode)); int fileDesc = io.GetFileDescriptor(); io.Close(); return(fileDesc); }
public void ResetCurrentStream() { string file = CurrentFileName.ToString(); if (file == "-") { SingletonStream = this.Context.StandardInput as RubyIO; } else { Stream stream = RubyFile.OpenFileStream(_context, file, _defaultMode); SingletonStream = new RubyIO(_context, stream, _defaultMode); } }
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; }
public static RubyFile/*!*/ Reinitialize(RubyFile/*!*/ self, [DefaultProtocol, NotNull]Union<int, MutableString> descriptorOrPath, int mode, [Optional]int permission) { // TODO: remove duplicity (constructors vs initializers) if (descriptorOrPath.IsFixnum()) { RubyIOOps.Reinitialize(self, descriptorOrPath.Fixnum(), mode); } else { var path = self.Context.DecodePath(descriptorOrPath.Second); var stream = RubyFile.OpenFileStream(self.Context, path, (IOMode)mode); self.Path = path; self.Mode = (IOMode)mode; self.SetStream(stream); self.SetFileDescriptor(self.Context.AllocateFileDescriptor(stream)); } // TODO: permission return self; }
public static MutableString /*!*/ Read( ConversionStorage <IDictionary <object, object> > /*!*/ toHash, ConversionStorage <int> /*!*/ fixnumCast, ConversionStorage <MutableString> /*!*/ toPath, RubyClass /*!*/ self, object path, [Optional] object optionsOrLength, [Optional] object optionsOrOffset, [DefaultParameterValue(null), DefaultProtocol] IDictionary <object, object> options) { TryConvertToOptions(toHash, ref options, ref optionsOrLength, ref optionsOrOffset); var site = fixnumCast.GetSite(ConvertToFixnumAction.Make(fixnumCast.Context)); int length = (optionsOrLength != Missing.Value && optionsOrLength != null) ? site.Target(site, optionsOrLength) : 0; int offset = (optionsOrOffset != Missing.Value && optionsOrOffset != null) ? site.Target(site, optionsOrOffset) : 0; if (offset < 0) { throw RubyExceptions.CreateEINVAL(); } if (length < 0) { throw RubyExceptions.CreateArgumentError("negative length {0} given", length); } using (RubyIO io = new RubyFile(self.Context, Protocols.CastToPath(toPath, path), IOMode.ReadOnly)) { if (offset > 0) { io.Seek(offset, SeekOrigin.Begin); } if (optionsOrLength != Missing.Value && optionsOrLength != null) { return(Read(io, length, null)); } else { return(Read(io)); } } }
public static MutableString /*!*/ GetPath(RubyFile /*!*/ self) { return(MutableString.Create(self.Path)); }
public static DateTime ModifiedTime(RubyContext /*!*/ context, RubyFile /*!*/ self) { return(RubyStatOps.ModifiedTime(RubyStatOps.Create(context, self.Path))); }
public static MutableString/*!*/ Read(ConversionStorage<int>/*!*/ fixnumCast, RubyClass/*!*/ self, [DefaultProtocol, NotNull]MutableString/*!*/ path, [DefaultParameterValue(null)]object lengthObj, [DefaultParameterValue(0)]object offsetObj) { var site = fixnumCast.GetSite(ConvertToFixnumAction.Make(fixnumCast.Context)); int length = (lengthObj != null) ? site.Target(site, lengthObj) : 0; int offset = (offsetObj != null) ? site.Target(site, offsetObj) : 0; if (offset < 0) { throw RubyExceptions.CreateEINVAL(); } if (length < 0) { throw RubyExceptions.CreateArgumentError("negative length {0} given", length); } using (RubyIO io = new RubyFile(self.Context, path.ConvertToString(), IOMode.ReadOnly)) { if (offset > 0) { io.Seek(offset, SeekOrigin.Begin); } if (lengthObj == null) { return Read(io); } else { return Read(io, length, null); } } }
public static RubyIO/*!*/ Open( RubyContext/*!*/ context, object self, [DefaultProtocol, NotNull]MutableString/*!*/ path, int mode, [DefaultProtocol, DefaultParameterValue(RubyFileOps.ReadWriteMode)]int permission) { string fileName = path.ConvertToString(); if (fileName.Length > 0 && fileName[0] == '|') { throw new NotImplementedError(); } RubyIO file = new RubyFile(context, fileName, (RubyFileMode)mode); SetPermission(context, fileName, permission); return file; }
public static object Open(BlockParam block, RubyClass/*!*/ self, [DefaultProtocol, NotNull]MutableString/*!*/ path) { var strPath = self.Context.DecodePath(path); var file = new RubyFile(self.Context, strPath, IOMode.ReadOnly); var gzipReader = Create(self, file); return gzipReader.Do(block); }
public static MutableString GetPath(RubyFile/*!*/ self) { self.RequireInitialized(); return self.Path != null ? self.Context.EncodePath(self.Path) : null; }
public static FileSystemInfo Stat(RubyFile/*!*/ self) { return RubyStatOps.Create(self); }
public static object ParseFile(RubyModule/*!*/ self, [DefaultProtocol, NotNull]MutableString/*!*/ path) { using (Stream stream = new RubyFile(self.Context, path.ConvertToString(), IOMode.Default).GetReadableStream()) { foreach (Node obj in MakeComposer(self.Context, stream)) { return obj; } } return ScriptingRuntimeHelpers.False; }
public static object LoadFile(RubyScope/*!*/ scope, RubyModule/*!*/ self, [DefaultProtocol, NotNull]MutableString/*!*/ path) { using (RubyFile file = new RubyFile(self.Context, path.ConvertToString(), IOMode.Default)) { foreach (object obj in MakeConstructor(scope.GlobalScope, file.GetReadableStream())) { return obj; } } return null; }
public static MutableString GetPath(RubyFile/*!*/ self) { return self.Path != null ? self.Context.EncodePath(self.Path) : null; }
public static MutableString/*!*/ Inspect(RubyContext/*!*/ context, RubyFile/*!*/ self) { return MutableString.CreateMutable(context.GetPathEncoding()). Append("#<File:"). Append(self.Path). Append(self.Closed ? " (closed)" : ""). Append('>'); }
public static FileSystemInfo Stat(RubyContext/*!*/ context, RubyFile/*!*/ self) { return RubyStatOps.Create(context, self.Path); }
public static RubyTime CreateTime(RubyContext/*!*/ context, RubyFile/*!*/ self) { return RubyStatOps.CreateTime(RubyStatOps.Create(context, self.Path)); }
public static MutableString GetPath(RubyFile /*!*/ self) { return(self.Path != null?self.Context.EncodePath(self.Path) : null); }
public static int ChangeOwner(RubyFile /*!*/ self, [DefaultProtocol] int owner, [DefaultProtocol] int group) { return(0); }
public static RubyTime ModifiedTime(RubyContext/*!*/ context, RubyFile/*!*/ self) { return RubyStatOps.ModifiedTime(RubyStatOps.Create(self)); }
public static int SysOpen(RubyClass/*!*/ self, [NotNull]MutableString path, [Optional]MutableString mode, [Optional]int perm) { if (FileTest.DirectoryExists(self.Context, path)) { // TODO: What file descriptor should be returned for a directory? return -1; } RubyIO io = new RubyFile(self.Context, path.ToString(), IOModeEnum.Parse(mode)); int fileDesc = io.GetFileDescriptor(); io.Close(); return fileDesc; }
public static MutableString/*!*/ Inspect(RubyFile/*!*/ self) { return MutableString.CreateMutable(self.Context.GetPathEncoding()). Append("#<"). Append(self.Context.GetClassOf(self).GetName(self.Context)). Append(':'). Append(self.Path). Append(self.Closed ? " (closed)" : ""). Append('>'); }
public static int Chmod(RubyFile /*!*/ self, [DefaultProtocol] int permission) { Chmod(self.Path, permission); return(0); }
// TODO: should work for IO and files w/o paths: internal static FileSystemInfo/*!*/ Create(RubyFile/*!*/ file) { file.RequireInitialized(); if (file.Path == null) { throw new NotSupportedException("TODO: cannot get file info for files without path"); } return Create(file.Context, file.Path); }
public static int ChangeOwner(RubyFile/*!*/ self, [DefaultProtocol]int owner, [DefaultProtocol]int group) { return 0; }
public static object Open(BlockParam block, RubyClass/*!*/ self, [DefaultProtocol, NotNull]MutableString/*!*/ path, [DefaultParameterValue(0)]int level, [DefaultParameterValue(DEFAULT_STRATEGY)]int strategy) { var strPath = self.Context.DecodePath(path); var file = new RubyFile(self.Context, strPath, IOMode.WriteOnly | IOMode.Truncate | IOMode.CreateIfNotExists); var gzipWriter = Create(self, file, level, strategy); return gzipWriter.Do(block); }
public static int ChangeOwner(RubyContext/*!*/ context, RubyFile/*!*/ self, object owner, object group) { if ((owner == null || owner is int) && (group == null || group is int)) { return 0; } throw RubyExceptions.CreateUnexpectedTypeError(context, owner, "Fixnum"); }
public static MutableString/*!*/ Binread(RubyClass/*!*/ self, [DefaultProtocol, NotNull]MutableString/*!*/ path, [DefaultProtocol, Optional]int? length, [DefaultProtocol, Optional]int? offset) { if (offset.HasValue && offset.Value < 0) { throw RubyExceptions.CreateEINVAL(); } if (length.HasValue && length.Value < 0) { throw RubyExceptions.CreateArgumentError("negative length {0} given", length); } using (RubyIO io = new RubyFile(self.Context, path.ToString(), IOMode.ReadOnly)) { if (offset.HasValue && offset.Value > 0) { io.Seek(offset.Value, SeekOrigin.Begin); } return (length.HasValue) ? Read(io, length.Value, null) : Read(io); } }
public static int Truncate(RubyFile/*!*/ self, [DefaultProtocol]int size) { if (size < 0) { throw new InvalidError(); } self.Length = size; return 0; }
public static FileSystemInfo Stat(RubyContext /*!*/ context, RubyFile /*!*/ self) { return(RubyStatOps.Create(context, self.Path)); }
public static int Truncate(ConversionStorage<MutableString>/*!*/ toPath, RubyClass/*!*/ self, object path, [DefaultProtocol]int size) { if (size < 0) { throw new InvalidError(); } using (RubyFile f = new RubyFile(self.Context, self.Context.DecodePath(Protocols.CastToPath(toPath, path)), IOMode.ReadWrite)) { f.Length = size; } return 0; }
public static MutableString /*!*/ Inspect(RubyContext /*!*/ context, RubyFile /*!*/ self) { return(MutableString.CreateMutable("#<File:").Append(self.Path).Append('>')); }
public static RubyTime AccessTime(RubyContext/*!*/ context, RubyFile/*!*/ self) { return RubyStatOps.AccessTime(RubyStatOps.Create(self)); }
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; } }
public static RubyTime AccessTime(RubyContext /*!*/ context, RubyFile /*!*/ self) { return(RubyStatOps.AccessTime(RubyStatOps.Create(context, self.Path))); }
public static MutableString/*!*/ Read( ConversionStorage<IDictionary<object, object>>/*!*/ toHash, ConversionStorage<int>/*!*/ fixnumCast, ConversionStorage<MutableString>/*!*/ toPath, RubyClass/*!*/ self, object path, [Optional]object optionsOrLength, [Optional]object optionsOrOffset, [DefaultParameterValue(null), DefaultProtocol]IDictionary<object, object> options) { Protocols.TryConvertToOptions(toHash, ref options, ref optionsOrLength, ref optionsOrOffset); var site = fixnumCast.GetSite(ConvertToFixnumAction.Make(fixnumCast.Context)); int length = (optionsOrLength != Missing.Value && optionsOrLength != null) ? site.Target(site, optionsOrLength) : 0; int offset = (optionsOrOffset != Missing.Value && optionsOrOffset != null) ? site.Target(site, optionsOrOffset) : 0; if (offset < 0) { throw RubyExceptions.CreateEINVAL(); } if (length < 0) { throw RubyExceptions.CreateArgumentError("negative length {0} given", length); } // TODO: options using (RubyIO io = new RubyFile(self.Context, self.Context.DecodePath(Protocols.CastToPath(toPath, path)), IOMode.ReadOnly)) { if (offset > 0) { io.Seek(offset, SeekOrigin.Begin); } if (optionsOrLength != Missing.Value && optionsOrLength != null) { return Read(io, length, null); } else { return Read(io); } } }
public static int Chmod(RubyFile/*!*/ self, [DefaultProtocol]int permission) { self.RequireInitialized(); // TODO: if (self.Path == null) { throw new NotSupportedException("TODO: cannot chmod for files without path"); } Chmod(self.Path, permission); return 0; }