C64Lib.Core.D64Drive.open_close_d64_file C# (CSharp) Method

open_close_d64_file() private method

private open_close_d64_file ( string d64name, Stream fileStream ) : void
d64name string
fileStream Stream
return void
        private void open_close_d64_file(string d64name, Stream fileStream)
        {
            long size;
            byte[] magic = new byte[4];

            // Close old .d64, if open
            if (the_file != null)
            {
                close_all_channels();
                the_file.Dispose();
                the_file = null;
            }

            if (null == d64name || d64name.Length < 1)
            {
                return;
            }

            if (null != fileStream)
            {
                the_file = fileStream;
            }
            else
            {
                try
                {
                    the_file = new FileStream(d64name, FileMode.Open, FileAccess.Read);
                }
                catch (DirectoryNotFoundException)
                {
                    the_file = null;
                    return;
                }
            }

            // Open new .d64 file
            // Check length
            size = the_file.Length;

            // Check length
            if (size < NUM_SECTORS * 256)
            {
                the_file.Dispose();
                the_file = null;
                return;
            }

            // x64 image?
            the_file.Read(magic, 0, 4);
            if (magic[0] == 0x43 && magic[1] == 0x15 && magic[2] == 0x41 && magic[3] == 0x64)
                image_header = 64;
            else
                image_header = 0;

            // Preset error info (all sectors no error)
            Array.Clear(error_info, 0, error_info.Length);

            // Load sector error info from .d64 file, if present
            if (image_header == 0 && size == NUM_SECTORS * 257)
            {
                the_file.Seek(NUM_SECTORS * 256, SeekOrigin.Begin);
                the_file.Read(error_info, 0, NUM_SECTORS);
            }
        }