Ejemplo n.º 1
0
 public LuaArgs ReadUntilAsync(byte[] buffer, int offset, int count, byte target, LuaContinuation continuation)
 {
     if (m_mode == LuaFileOpenMode.Read)
     {
         return(BufferInputUntilAsync(target, delegate(LuaArgs results) {
             count = Math.Min(count, results.GetInt(0));
             count = DequeueBytes(buffer, offset, count);
             return continuation.Invoke(new LuaArgs(count));
         }));
     }
     return(continuation.Invoke(new LuaArgs(0)));
 }
Ejemplo n.º 2
0
 public LuaArgs ReadByteAsync(LuaContinuation continuation)
 {
     if (m_mode == LuaFileOpenMode.Read)
     {
         return(BufferInputAsync(1, delegate(LuaArgs args)
         {
             var b = DequeueByte();
             return continuation.Invoke(new LuaArgs(b));
         }));
     }
     return(continuation.Invoke(new LuaArgs(-1)));
 }
Ejemplo n.º 3
0
 public LuaArgs ReadAsync(byte[] buffer, int offset, int count, LuaContinuation continuation)
 {
     if (m_mode == LuaFileOpenMode.Read)
     {
         return(BufferInputAsync(count, delegate(LuaArgs args)
         {
             count = DequeueBytes(buffer, offset, count);
             return continuation.Invoke(new LuaArgs(count));
         }));
     }
     return(continuation.Invoke(new LuaArgs(0)));
 }
Ejemplo n.º 4
0
 private LuaArgs WriteOneAsync(LuaValue value, LuaContinuation continuation)
 {
     if (value.IsNumber())
     {
         // Write a number
         var str = value.ToString();
         if (m_contentType == LuaFileContentType.Binary)
         {
             var bytes = Encoding.UTF8.GetBytes(str);
             if (m_stream is LuaFunctionStream)
             {
                 return(((LuaFunctionStream)m_stream).WriteAsync(bytes, 0, bytes.Length, continuation));
             }
             else
             {
                 m_stream.Write(bytes, 0, bytes.Length);
                 return(continuation.Invoke(LuaArgs.Empty));
             }
         }
         else
         {
             m_writer.Write(str);
             return(continuation.Invoke(LuaArgs.Empty));
         }
     }
     else
     {
         // Write a string
         if (m_contentType == LuaFileContentType.Binary)
         {
             var bytes = value.GetByteString();
             if (m_stream is LuaFunctionStream)
             {
                 return(((LuaFunctionStream)m_stream).WriteAsync(bytes, 0, bytes.Length, continuation));
             }
             else
             {
                 m_stream.Write(bytes, 0, bytes.Length);
                 return(continuation.Invoke(LuaArgs.Empty));
             }
         }
         else
         {
             var str = value.GetString();
             m_writer.Write(str.Replace("\n", Environment.NewLine));
             return(continuation.Invoke(LuaArgs.Empty));
         }
     }
 }
Ejemplo n.º 5
0
 public LuaArgs write(LuaArgs args)
 {
     try
     {
         CheckOpen();
         if (m_openMode == LuaFileOpenMode.Write)
         {
             // Write some stuff
             int             i            = 0;
             LuaContinuation continuation = null;
             continuation = delegate {
                 if (!args.IsNil(i))
                 {
                     return(WriteOneAsync(args[i++], continuation));
                 }
                 else
                 {
                     return(new LuaArgs(this));
                 }
             };
             return(continuation.Invoke(LuaArgs.Empty));
         }
         return(new LuaArgs(this));
     }
     catch (IOException e)
     {
         return(new LuaArgs(LuaValue.Nil, e.Message));
     }
 }
Ejemplo n.º 6
0
 public LuaArgs WriteByteAsync(byte value, LuaContinuation continuation)
 {
     if (m_mode == LuaFileOpenMode.Write)
     {
         QueueByte(value);
         return(EmitOutputAsync(continuation));
     }
     return(continuation.Invoke(LuaArgs.Empty));
 }
Ejemplo n.º 7
0
 public LuaArgs WriteAsync(byte[] buffer, int offset, int count, LuaContinuation continuation)
 {
     if (m_mode == LuaFileOpenMode.Write)
     {
         QueueBytes(buffer, offset, count);
         return(EmitOutputAsync(continuation));
     }
     return(continuation.Invoke(LuaArgs.Empty));
 }
Ejemplo n.º 8
0
 private LuaArgs EmitOutputAsync(LuaContinuation continuation)
 {
     if (m_function != null && m_buffer.Count > 0)
     {
         var bytes = new byte[m_buffer.Count];
         DequeueBytes(bytes, 0, bytes.Length);
         return(m_function.CallAsync(new LuaArgs(bytes), continuation));
     }
     return(continuation.Invoke(LuaArgs.Empty));
 }
Ejemplo n.º 9
0
        private LuaArgs BufferInputAsync(int bytesNeeded, LuaContinuation continuation)
        {
            LuaContinuation onReturn = null;

            onReturn = delegate(LuaArgs results)
            {
                if (results.IsString(0))
                {
                    var bytes = results.GetByteString(0);
                    if (bytes.Length > 0)
                    {
                        // Read some bytes
                        QueueBytes(bytes, 0, bytes.Length);
                    }
                    else
                    {
                        // EOF
                        m_function = null;
                    }
                }
                else
                {
                    // EOF
                    m_function = null;
                }
                if (m_function != null && m_buffer.Count < bytesNeeded)
                {
                    return(m_function.CallAsync(new LuaArgs(bytesNeeded - m_buffer.Count), onReturn));
                }
                else
                {
                    return(continuation.Invoke(LuaArgs.Empty));
                }
            };
            if (m_function != null && m_buffer.Count < bytesNeeded)
            {
                return(m_function.CallAsync(new LuaArgs(m_buffer.Count - bytesNeeded), onReturn));
            }
            else
            {
                return(continuation.Invoke(LuaArgs.Empty));
            }
        }
Ejemplo n.º 10
0
 public LuaArgs read(LuaArgs args)
 {
     try
     {
         CheckOpen();
         if (m_openMode == LuaFileOpenMode.Read)
         {
             var             results      = new LuaValue[Math.Max(args.Length, 1)];
             int             i            = -1;
             LuaContinuation continuation = null;
             continuation = delegate(LuaArgs args2)
             {
                 if (i >= 0)
                 {
                     results[i] = args2[0];
                     if (results[i].IsNil())
                     {
                         return(new LuaArgs(results).Select(0, i + 1));
                     }
                 }
                 if (i < results.Length - 1)
                 {
                     i = i + 1;
                     var fmt = (i == 0 && args.IsNil(i)) ? new LuaValue("l") : args[i];
                     return(ReadOneAsync(fmt, continuation));
                 }
                 else
                 {
                     return(new LuaArgs(results));
                 }
             };
             return(continuation.Invoke(LuaArgs.Empty));
         }
         return(LuaArgs.Empty);
     }
     catch (IOException e)
     {
         throw new LuaError(e.Message);
     }
 }
Ejemplo n.º 11
0
        private LuaArgs ReadOneAsync(LuaValue fmt, LuaContinuation continuation)
        {
            if (fmt.IsNumber())
            {
                // Number
                var count = fmt.GetInt();
                if (m_contentType == LuaFileContentType.Binary)
                {
                    // Read some bytes
                    var bytes = new byte[count];
                    if (m_stream is LuaFunctionStream)
                    {
                        return(((LuaFunctionStream)m_stream).ReadAsync(
                                   bytes, 0, count,
                                   delegate(LuaArgs args) {
                            int bytesRead = args.GetInt(0);
                            if (bytesRead > 0)
                            {
                                if (bytesRead != count)
                                {
                                    Array.Resize(ref bytes, bytesRead);
                                }
                                return continuation.Invoke(new LuaArgs(bytes));
                            }
                            else
                            {
                                return continuation.Invoke(LuaArgs.Nil);
                            }
                        }
                                   ));
                    }
                    else
                    {
                        int bytesRead = m_stream.Read(bytes, 0, count);
                        if (bytesRead > 0)
                        {
                            if (bytesRead != count)
                            {
                                Array.Resize(ref bytes, bytesRead);
                            }
                            return(continuation.Invoke(new LuaArgs(bytes)));
                        }
                        else
                        {
                            return(continuation.Invoke(LuaArgs.Nil));
                        }
                    }
                }
                else
                {
                    // Read some chars
                    var chars     = new char[count];
                    int charsRead = m_reader.Read(chars, 0, count);
                    if (charsRead > 0)
                    {
                        var str = new string(chars, 0, charsRead);
                        return(continuation.Invoke(new LuaArgs(str)));
                    }
                    else
                    {
                        return(continuation.Invoke(LuaArgs.Nil));
                    }
                }
            }
            else
            {
                // Format string
                var fmtStr = fmt.GetString();
                if (fmtStr == "*n" || fmtStr == "n")
                {
                    // Read a number
                    if (m_contentType == LuaFileContentType.Binary)
                    {
                        throw new NotImplementedException();
                    }
                    else
                    {
                        var    num = m_reader.ReadLine();
                        long   lresult;
                        double dresult;
                        if (long.TryParse(num, out lresult))
                        {
                            return(continuation.Invoke(new LuaArgs(lresult)));
                        }
                        else if (double.TryParse(num, out dresult))
                        {
                            return(continuation.Invoke(new LuaArgs(dresult)));
                        }
                        else
                        {
                            return(continuation.Invoke(LuaArgs.Nil));
                        }
                    }
                }
                else if (fmtStr == "*a" || fmtStr == "a")
                {
                    // Read to end
                    if (m_contentType == LuaFileContentType.Binary)
                    {
                        byte[] buffer       = new byte[4096];
                        var    memoryStream = new MemoryStream();
                        if (m_stream is LuaFunctionStream)
                        {
                            var             luaStream = (LuaFunctionStream)m_stream;
                            LuaContinuation onReturn  = null;
                            onReturn = delegate(LuaArgs args)
                            {
                                var bytesRead = args.GetInt(0);
                                if (bytesRead > 0)
                                {
                                    memoryStream.Write(buffer, 0, bytesRead);
                                    return(luaStream.ReadAsync(buffer, 0, buffer.Length, onReturn));
                                }
                                else
                                {
                                    var bytes = memoryStream.ToArray();
                                    return(continuation.Invoke(new LuaArgs(bytes)));
                                }
                            };
                            return(luaStream.ReadAsync(buffer, 0, buffer.Length, onReturn));
                        }
                        else
                        {
                            int bytesRead;
                            while ((bytesRead = m_stream.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                memoryStream.Write(buffer, 0, bytesRead);
                            }
                            var bytes = memoryStream.ToArray();
                            return(continuation.Invoke(new LuaArgs(bytes)));
                        }
                    }
                    else
                    {
                        var str = m_reader.ReadToEnd();
                        return(continuation.Invoke(new LuaArgs(str)));
                    }
                }
                else if (fmtStr == "*l" || fmtStr == "l")
                {
                    // Read a line
                    if (m_contentType == LuaFileContentType.Binary)
                    {
                        var memoryStream = new MemoryStream();
                        if (m_stream is LuaFunctionStream)
                        {
                            var             luaStream = (LuaFunctionStream)m_stream;
                            byte[]          buffer    = new byte[4096];
                            LuaContinuation onReturn  = null;
                            onReturn = delegate(LuaArgs args)
                            {
                                var bytesRead = args.GetInt(0);
                                if (bytesRead < buffer.Length || buffer[bytesRead - 1] == '\n')
                                {
                                    // Found newline or EOF
                                    memoryStream.Write(buffer, 0, bytesRead);
                                    if (memoryStream.Length == 0)
                                    {
                                        return(continuation.Invoke(LuaArgs.Nil));
                                    }
                                    else
                                    {
                                        // Trim newline
                                        var results = memoryStream.GetBuffer();
                                        var len     = memoryStream.Length;
                                        if (len > 0 && results[len - 1] == '\n')
                                        {
                                            len--;
                                        }
                                        if (len > 0 && results[len - 1] == '\r')
                                        {
                                            len--;
                                        }

                                        // Return bytes
                                        var bytes = new byte[len];
                                        Array.Copy(results, bytes, len);
                                        return(continuation.Invoke(new LuaArgs(bytes)));
                                    }
                                }
                                else
                                {
                                    // Get some more bytes
                                    memoryStream.Write(buffer, 0, bytesRead);
                                    return(luaStream.ReadUntilAsync(buffer, 0, buffer.Length, (byte)'\n', onReturn));
                                }
                            };
                            return(luaStream.ReadUntilAsync(buffer, 0, buffer.Length, (byte)'\n', onReturn));
                        }
                        else
                        {
                            var b = m_stream.ReadByte();
                            if (b < 0)
                            {
                                return(continuation.Invoke(LuaArgs.Nil));
                            }
                            else
                            {
                                while (true)
                                {
                                    if (b < 0 || b == '\n')
                                    {
                                        break;
                                    }
                                    else if (b != '\r')
                                    {
                                        memoryStream.WriteByte((byte)b);
                                    }
                                    b = m_stream.ReadByte();
                                }
                                var bytes = memoryStream.ToArray();
                                return(continuation.Invoke(new LuaArgs(bytes)));
                            }
                        }
                    }
                    else
                    {
                        var str = m_reader.ReadLine();
                        if (str != null)
                        {
                            return(continuation.Invoke(new LuaArgs(str)));
                        }
                        else
                        {
                            return(continuation.Invoke(LuaArgs.Nil));
                        }
                    }
                }
                else if (fmt == "*L" || fmt == "L")
                {
                    // Read a line including the line ending
                    if (m_contentType == LuaFileContentType.Binary)
                    {
                        var memoryStream = new MemoryStream();
                        if (m_stream is LuaFunctionStream)
                        {
                            var             luaStream = (LuaFunctionStream)m_stream;
                            byte[]          buffer    = new byte[4096];
                            LuaContinuation onReturn  = null;
                            onReturn = delegate(LuaArgs args)
                            {
                                var bytesRead = args.GetInt(0);
                                if (bytesRead < buffer.Length || buffer[bytesRead - 1] == '\n')
                                {
                                    // Found newline or EOFF
                                    memoryStream.Write(buffer, 0, bytesRead);
                                    if (memoryStream.Length == 0)
                                    {
                                        return(continuation.Invoke(LuaArgs.Nil));
                                    }
                                    else
                                    {
                                        // Return bytess
                                        var bytes = memoryStream.ToArray();
                                        return(continuation.Invoke(new LuaArgs(bytes)));
                                    }
                                }
                                else
                                {
                                    // Get some more bytes
                                    memoryStream.Write(buffer, 0, bytesRead);
                                    return(luaStream.ReadUntilAsync(buffer, 0, buffer.Length, (byte)'\n', onReturn));
                                }
                            };
                            return(luaStream.ReadUntilAsync(buffer, 0, buffer.Length, (byte)'\n', onReturn));
                        }
                        else
                        {
                            var b = m_stream.ReadByte();
                            if (b < 0)
                            {
                                return(continuation.Invoke(LuaArgs.Nil));
                            }
                            else
                            {
                                while (true)
                                {
                                    if (b < 0)
                                    {
                                        break;
                                    }
                                    else if (b == '\n')
                                    {
                                        memoryStream.WriteByte((byte)b);
                                        break;
                                    }
                                    else
                                    {
                                        memoryStream.WriteByte((byte)b);
                                    }
                                    b = m_stream.ReadByte();
                                }
                                var bytes = memoryStream.ToArray();
                                return(continuation.Invoke(new LuaArgs(bytes)));
                            }
                        }
                    }
                    else
                    {
                        var str = m_reader.ReadLine();
                        if (str != null)
                        {
                            str = str + '\n';
                            return(continuation.Invoke(new LuaArgs(str)));
                        }
                        else
                        {
                            return(continuation.Invoke(LuaArgs.Nil));
                        }
                    }
                }
                else
                {
                    throw new LuaError("Invalid format: " + fmt);
                }
            }
        }
Ejemplo n.º 12
0
        private LuaArgs BufferInputUntilAsync(byte target, LuaContinuation continuation)
        {
            int i = 0;

            foreach (var b in m_buffer)
            {
                if (b == target)
                {
                    return(continuation.Invoke(new LuaArgs(i + 1)));
                }
                else
                {
                    ++i;
                }
            }

            LuaContinuation onReturn = null;

            onReturn = delegate(LuaArgs results)
            {
                if (results.IsString(0))
                {
                    var bytes = results.GetByteString(0);
                    if (bytes.Length > 0)
                    {
                        // Read some bytes
                        QueueBytes(bytes, 0, bytes.Length);
                        for (int j = 0; j < bytes.Length; ++j)
                        {
                            if (bytes[j] == target)
                            {
                                return(continuation.Invoke(new LuaArgs(i + j + 1)));
                            }
                        }
                        i += bytes.Length;
                    }
                    else
                    {
                        // EOF
                        m_function = null;
                    }
                }
                else
                {
                    // EOF
                    m_function = null;
                }
                if (m_function != null)
                {
                    return(m_function.CallAsync(new LuaArgs(1), onReturn));
                }
                else
                {
                    return(continuation.Invoke(new LuaArgs(m_buffer.Count)));
                }
            };
            if (m_function != null)
            {
                return(m_function.CallAsync(new LuaArgs(1), onReturn));
            }
            else
            {
                return(continuation.Invoke(new LuaArgs(m_buffer.Count)));
            }
        }