private IodineObject Writeln(VirtualMachine vm, IodineObject self, IodineObject[] args)
            {
                IodineStream thisObj = self as IodineStream;

                if (thisObj.Closed)
                {
                    vm.RaiseException(new IodineIOException("Stream has been closed!"));
                    return(null);
                }

                if (!thisObj.CanWrite)
                {
                    vm.RaiseException(new IodineIOException("Can not write to stream!"));
                    return(null);
                }

                foreach (IodineObject obj in args)
                {
                    if (!thisObj.Write(obj))
                    {
                        vm.RaiseException(new IodineNotSupportedException(
                                              "The requested type is not supported"
                                              ));
                        return(null);
                    }
                    foreach (byte b in Environment.NewLine)
                    {
                        thisObj.File.WriteByte(b);
                    }
                }
                return(null);
            }
            private IodineObject Tell(VirtualMachine vm, IodineObject self, IodineObject[] args)
            {
                IodineStream thisObj = self as IodineStream;

                if (thisObj.Closed)
                {
                    vm.RaiseException("Stream has been closed!");
                    return(null);
                }
                return(new IodineInteger(thisObj.File.Position));
            }
            private IodineObject Flush(VirtualMachine vm, IodineObject self, IodineObject[] args)
            {
                IodineStream thisObj = self as IodineStream;

                if (thisObj.Closed)
                {
                    vm.RaiseException("Stream has been closed!");
                    return(null);
                }
                thisObj.File.Flush();
                return(null);
            }
            private IodineObject Readln(VirtualMachine vm, IodineObject self, IodineObject[] argss)
            {
                IodineStream thisObj = self as IodineStream;

                if (thisObj.Closed)
                {
                    vm.RaiseException("Stream has been closed!");
                    return(null);
                }

                if (!thisObj.CanRead)
                {
                    vm.RaiseException("Stream is not open for reading!");
                    return(null);
                }

                return(thisObj.ReadLine());
            }
Example #5
0
        private static byte[] PreformHash(VirtualMachine vm, IodineObject obj, HashAlgorithm algol)
        {
            if (obj is IodineString || obj is IodineBytes)
            {
                byte[] data = GetBytes(obj);

                return(algol.ComputeHash(data));
            }

            IodineStream stream = obj as IodineStream;

            if (obj == null)
            {
                vm.RaiseException(new IodineTypeException("Str"));
                return(null);
            }

            return(algol.ComputeHash(stream.File));
        }
            private IodineObject Write(VirtualMachine vm, IodineObject self, IodineObject[] args)
            {
                IodineStream thisObj = self as IodineStream;

                if (thisObj.Closed)
                {
                    vm.RaiseException(new IodineIOException("Stream has been closed!"));
                    return(null);
                }

                if (!thisObj.CanWrite)
                {
                    vm.RaiseException(new IodineIOException("Can not write to stream!"));
                    return(null);
                }

                foreach (IodineObject obj in args)
                {
                    thisObj.Write(obj);
                }
                return(null);
            }
            private IodineObject Read(VirtualMachine vm, IodineObject self, IodineObject[] args)
            {
                IodineStream thisObj = self as IodineStream;

                if (thisObj.Closed)
                {
                    vm.RaiseException("Stream has been closed!");
                    return(null);
                }

                if (!thisObj.CanRead)
                {
                    vm.RaiseException("Stream is not open for reading!");
                    return(null);
                }

                if (args.Length > 0)
                {
                    IodineInteger intv = args [0] as IodineInteger;

                    if (intv == null)
                    {
                        vm.RaiseException(new IodineTypeException("Int"));
                        return(null);
                    }

                    byte[] buf = new byte[(int)intv.Value];
                    thisObj.File.Read(buf, 0, buf.Length);
                    return(new IodineString(Encoding.UTF8.GetString(buf)));
                }
                else
                {
                    vm.RaiseException(new IodineArgumentException(1));
                    return(null);
                }
            }
            private IodineObject ReadAll(VirtualMachine vm, IodineObject self, IodineObject[] args)
            {
                IodineStream thisObj = self as IodineStream;

                if (thisObj.Closed)
                {
                    vm.RaiseException("Stream has been closed!");
                    return(null);
                }

                List <byte> bytes = new List <byte> ();
                int         ch    = 0;

                while ((ch = thisObj.File.ReadByte()) != -1)
                {
                    bytes.Add((byte)ch);
                }

                if (thisObj.BinaryMode)
                {
                    return(new IodineBytes(bytes.ToArray()));
                }
                return(new IodineString(Encoding.UTF8.GetString(bytes.ToArray())));
            }
            private IodineObject Seek(VirtualMachine vm, IodineObject self, IodineObject[] args)
            {
                IodineStream thisObj = self as IodineStream;

                if (args.Length == 0)
                {
                    vm.RaiseException(new IodineArgumentException(1));
                    return(null);
                }

                if (thisObj.Closed)
                {
                    vm.RaiseException(new IodineException("The underlying stream has been closed!"));
                    return(null);
                }

                if (!thisObj.File.CanSeek)
                {
                    vm.RaiseException(new IodineIOException("The stream does not support seek"));
                    return(null);
                }

                IodineInteger offsetObj = args [0] as IodineInteger;
                int           whence    = SEEK_SET;
                long          offset    = offsetObj.Value;

                if (offsetObj == null)
                {
                    vm.RaiseException(new IodineTypeException("Int"));
                    return(null);
                }

                if (args.Length > 1)
                {
                    IodineInteger whenceObj = args [1] as IodineInteger;

                    if (whenceObj == null)
                    {
                        vm.RaiseException(new IodineTypeException("Int"));
                        return(null);
                    }

                    whence = (int)whenceObj.Value;
                }

                switch (whence)
                {
                case SEEK_SET:
                    thisObj.File.Position = offset;
                    break;

                case SEEK_CUR:
                    thisObj.File.Seek(offset, SeekOrigin.Current);
                    break;

                case SEEK_END:
                    thisObj.File.Seek(offset, SeekOrigin.End);
                    break;

                default:
                    vm.RaiseException(new IodineNotSupportedException());
                    return(null);
                }
                return(null);
            }