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

open_directory() private method

private open_directory ( byte pattern ) : byte
pattern byte
return byte
        byte open_directory(byte[] pattern)
        {
            int i, j, n, m;
            byte* p, q;
            DirEntry* de;
            byte c;
            byte* tmppat;

            // Special treatment for "$0"
            if (pattern[0] == '0' && pattern[1] == 0)
                pattern += 1;

            // Skip everything before the ':' in the pattern
            if ((tmppat = CharFunctions.strchr(pattern, ':')) != null)
                pattern = tmppat + 1;

            AllocateChannelBuffer(0, 8192);

            p = buf_ptr[0] = chan_buf[0];

            chan_mode[0] = ChannelMode.CHMOD_DIRECTORY;

            // Create directory title
            *p++ = 0x01;	// Load address $0401 (from PET days :-)
            *p++ = 0x04;
            *p++ = 0x01;	// Dummy line link
            *p++ = 0x01;
            *p++ = 0;		// Drive number (0) as line number
            *p++ = 0;
            *p++ = 0x12;	// RVS ON
            *p++ = (byte)'\"';

            q = bam->disk_name;
            for (i = 0; i < 23; i++)
            {
                if ((c = *q++) == 0xa0)
                    *p++ = (byte)' ';		// Replace 0xa0 by space
                else
                    *p++ = c;
            }
            *(p - 7) = (byte)'\"';
            *p++ = 0;

            // Scan all directory blocks
            dir.next_track = bam->dir_track;
            dir.next_sector = bam->dir_sector;

            fixed (Directory* dd = &dir)
            {

                while (dir.next_track != 0x00)
                {
                    if (!read_sector(dir.next_track, dir.next_sector, &dd->next_track))
                        return (byte)C64StatusCode.ST_OK;

                    DirEntry* ade = (DirEntry*)dd->entry;
                    // Scan all 8 entries of a block
                    for (j = 0; j < 8; j++)
                    {
                        de = &ade[j];

                        if (de->type != 0 && match(pattern, de->name))
                        {
                            *p++ = 0x01; // Dummy line link
                            *p++ = 0x01;

                            *p++ = de->num_blocks_l; // Line number
                            *p++ = de->num_blocks_h;

                            *p++ = (byte)' ';
                            n = (de->num_blocks_h << 8) + de->num_blocks_l;
                            if (n < 10) *p++ = (byte)' ';
                            if (n < 100) *p++ = (byte)' ';

                            *p++ = (byte)'\"';
                            q = de->name;
                            m = 0;
                            for (i = 0; i < 16; i++)
                            {
                                if ((c = *q++) == 0xa0)
                                {
                                    if (m != 0)
                                        *p++ = (byte)' ';		// Replace all 0xa0 by spaces
                                    else
                                        m = *p++ = (byte)'\"';	// But the first by a '"'
                                }
                                else
                                    *p++ = c;
                            }
                            if (m != 0)
                                *p++ = (byte)' ';
                            else
                                *p++ = (byte)'\"';			// No 0xa0, then append a space

                            if ((de->type & 0x80) != 0)
                                *p++ = (byte)' ';
                            else
                                *p++ = (byte)'*';

                            *p++ = type_char_1[de->type & 0x0f];
                            *p++ = type_char_2[de->type & 0x0f];
                            *p++ = type_char_3[de->type & 0x0f];

                            if ((de->type & 0x40) != 0)
                                *p++ = (byte)'<';
                            else
                                *p++ = (byte)' ';

                            *p++ = (byte)' ';
                            if (n >= 10) *p++ = (byte)' ';
                            if (n >= 100) *p++ = (byte)' ';
                            *p++ = 0;
                        }
                    }
                }

            }
            // Final line
            q = p;
            for (i = 0; i < 29; i++)
                *q++ = (byte)' ';

            n = 0;
            for (i = 0; i < 35; i++)
                n += bam->bitmap[i * 4];

            *p++ = 0x01;		// Dummy line link
            *p++ = 0x01;
            *p++ = (byte)(n & 0xff);	// Number of free blocks as line number
            *p++ = (byte)((n >> 8) & 0xff);

            *p++ = (byte)'B';
            *p++ = (byte)'L';
            *p++ = (byte)'O';
            *p++ = (byte)'C';
            *p++ = (byte)'K';
            *p++ = (byte)'S';
            *p++ = (byte)' ';
            *p++ = (byte)'F';
            *p++ = (byte)'R';
            *p++ = (byte)'E';
            *p++ = (byte)'E';
            *p++ = (byte)'.';

            p = q;
            *p++ = 0;
            *p++ = 0;
            *p++ = 0;

            buf_len[0] = (int)(p - chan_buf[0]);

            return (byte)C64StatusCode.ST_OK;
        }