Exemple #1
0
        /// <summary>
        /// Save the program to stream stream in defined mode.
        /// </summary>
        /// <param name="stream">Stream to which to save</param>
        /// <param name="mode">Mode in which to save program</param>
        public void SaveTo(Stream stream, ProgramMode mode)
        {
            if (mode != ProgramMode.Protected && _protected)
            {
                throw new ReplRuntimeException(ReplExceptionCode.IllegalFunctionCall);
            }

            var current = Bytecode.Position;

            // skip first \x00 in bytecode
            Bytecode.Seek(1, SeekOrigin.Begin);
            switch (mode)
            {
            case ProgramMode.Binary:
                Bytecode.CopyTo(stream);
                break;

            case ProgramMode.Protected:
                ProtectedProgramEncoder.Encode(Bytecode).CopyTo(stream);
                break;

            case ProgramMode.Ascii:
                while (true)
                {
                    var output = Tokeniser.DetokeniseLine(Bytecode);
                    if (output.LineNumber == -1 || output.LineNumber > MaxLineNumber)
                    {
                        break;
                    }

                    stream.Write(output.Text);
                }
                break;
            }

            Bytecode.Seek(current, SeekOrigin.Begin);
        }