/* * AppleCommander - An Apple ][ image utility. * Copyright (C) 2002-2022 by Robert Greene * robgreene at users.sourceforge.net * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package com.webcodepro.applecommander.storage.os.dos33; import java.text.NumberFormat; import java.util.ArrayList; import java.util.List; import com.webcodepro.applecommander.storage.Disk; import com.webcodepro.applecommander.storage.DiskFullException; import com.webcodepro.applecommander.storage.FileEntry; import com.webcodepro.applecommander.storage.FileFilter; import com.webcodepro.applecommander.storage.FormattedDisk; import com.webcodepro.applecommander.storage.StorageBundle; import com.webcodepro.applecommander.storage.filters.ApplesoftFileFilter; import com.webcodepro.applecommander.storage.filters.AssemblySourceFileFilter; import com.webcodepro.applecommander.storage.filters.BinaryFileFilter; import com.webcodepro.applecommander.storage.filters.GraphicsFileFilter; import com.webcodepro.applecommander.storage.filters.IntegerBasicFileFilter; import com.webcodepro.applecommander.storage.filters.TextFileFilter; import com.webcodepro.applecommander.util.AppleUtil; import com.webcodepro.applecommander.util.TextBundle; /** * Represents a DOS file entry on disk. *
* Date created: Oct 4, 2002 5:15:25 PM
* @author Rob Greene
*/
public class DosFileEntry implements FileEntry {
private TextBundle textBundle = StorageBundle.getInstance();
/**
* Indicates the length in bytes of the DOS file entry field.
*/
public static final int FILE_DESCRIPTIVE_ENTRY_LENGTH = 35;
/**
* Holds the disk the FileEntry is attached to.
*/
private DosFormatDisk disk;
/**
* Track of the FileEntry location.
*/
private int track;
/**
* Sector of the FileEntry location.
*/
private int sector;
/**
* Offset into sector of FileEntry location.
*/
private int offset;
/**
* Temporary location to store the DOS address (which is stored with
* the contents of the data file).
*/
private Integer address;
/**
* Constructor for DosFileEntry.
*/
public DosFileEntry(DosFormatDisk disk, int track, int sector, int offset) {
super();
this.disk = disk;
this.track = track;
this.sector = sector;
this.offset = offset;
}
/**
* Read the FileEntry from the disk image.
*/
protected byte[] readFileEntry() {
byte[] sectorData = disk.readSector(track, sector);
byte[] fileEntry = new byte[FILE_DESCRIPTIVE_ENTRY_LENGTH];
System.arraycopy(sectorData, offset, fileEntry, 0, fileEntry.length);
return fileEntry;
}
/**
* Write the FileEntry to the disk image.
*/
protected void writeFileEntry(byte[] fileEntry) {
if (fileEntry.length != FILE_DESCRIPTIVE_ENTRY_LENGTH) {
throw new IllegalArgumentException(textBundle.
format("DosFileEntry.DosFileEntryLengthError", //$NON-NLS-1$
FILE_DESCRIPTIVE_ENTRY_LENGTH));
}
byte[] sectorData = disk.readSector(track, sector);
System.arraycopy(fileEntry, 0, sectorData, offset, fileEntry.length);
disk.writeSector(track, sector, sectorData);
}
/**
* Return the maximum filename length.
*/
public int getMaximumFilenameLength() {
return 30;
}
/**
* Return the name of this file.
* @see com.webcodepro.applecommander.storage.FileEntry#getFilename()
*/
public String getFilename() {
return AppleUtil.getString(readFileEntry(), 3, 30).trim();
}
/**
* Set the name of this file.
*/
public void setFilename(String filename) {
byte[] data = readFileEntry();
AppleUtil.setString(data, 3, filename.toUpperCase(), 30);
writeFileEntry(data);
}
/**
* Return the filetype of this file.
* @see com.webcodepro.applecommander.storage.FileEntry#getFiletype()
*/
public String getFiletype() {
int filetype = (AppleUtil.getUnsignedByte(readFileEntry()[2]) & 0x7f);
if (filetype == 0x00) return "T"; //$NON-NLS-1$
// the "^" operator is exclusive or - used to ensure only that
// bit was turned on. if others are turned on, fall through and
// return a "?" as the file type
if ((filetype ^ 0x01) == 0) return "I"; //$NON-NLS-1$
if ((filetype ^ 0x02) == 0) return "A"; //$NON-NLS-1$
if ((filetype ^ 0x04) == 0) return "B"; //$NON-NLS-1$
if ((filetype ^ 0x08) == 0) return "S"; //$NON-NLS-1$
if ((filetype ^ 0x10) == 0) return "R"; //$NON-NLS-1$
if ((filetype ^ 0x20) == 0) return "a"; //$NON-NLS-1$
if ((filetype ^ 0x40) == 0) return "b"; //$NON-NLS-1$
return "?"; // should never occur (read the code!) //$NON-NLS-1$
}
/**
* Set the filetype. This contains some ProDOS file type translation to support
* translation from an AppleSingle archive.
*/
public void setFiletype(String filetype) {
byte[] data = readFileEntry();
int type = 0x04; // assume binary
if ("T".equals(filetype) || "TXT".equals(filetype)) type = 0x00; //$NON-NLS-1$
if ("I".equals(filetype)) type = 0x01; //$NON-NLS-1$
if ("A".equals(filetype) || "BAS".equals(filetype)) type = 0x02; //$NON-NLS-1$
if ("B".equals(filetype)) type = 0x04; //$NON-NLS-1$
if ("S".equals(filetype)) type = 0x08; //$NON-NLS-1$
if ("R".equals(filetype)) type = 0x10; //$NON-NLS-1$
if ("a".equals(filetype)) type = 0x20; //$NON-NLS-1$
if ("b".equals(filetype)) type = 0x40; //$NON-NLS-1$
type = (type | (data[2] & 0x80));
data[2] = (byte) type;
writeFileEntry(data);
}
/**
* Identify if this file is locked.
* @see com.webcodepro.applecommander.storage.FileEntry#isLocked()
*/
public boolean isLocked() {
return (readFileEntry()[2] & 0x80) != 0;
}
/**
* Set the lock indicator.
*/
public void setLocked(boolean lock) {
byte[] data = readFileEntry();
if (lock) {
data[2] = (byte)(AppleUtil.getUnsignedByte(data[2]) | 0x80);
} else {
data[2] = (byte)(AppleUtil.getUnsignedByte(data[2]) & 0x7f);
}
writeFileEntry(data);
}
/**
* Compute the size of this file (in bytes).
* @see com.webcodepro.applecommander.storage.FileEntry#getSize()
*/
public int getSize() {
byte[] rawdata = null;
if (!isDeleted()) {
rawdata = disk.getFileData(this);
}
// default to nothing special, just compute from number of sectors
int size = (getSectorsUsed()-1) * Disk.SECTOR_SIZE;
if (size < 1) size = 0; // we assume a T/S block is included (may not be)
if (rawdata != null) {
if ("B".equals(getFiletype())) { //$NON-NLS-1$
// binary
return AppleUtil.getWordValue(rawdata, 2);
} else if ("A".equals(getFiletype()) || "I".equals(getFiletype())) { //$NON-NLS-1$ //$NON-NLS-2$
// applesoft, integer basic
return AppleUtil.getWordValue(rawdata, 0);
}
}
return size;
}
/**
* Compute the number of sectors used.
*/
public int getSectorsUsed() {
return AppleUtil.getWordValue(readFileEntry(), 0x21);
}
/**
* Set the number of sectors used.
*/
public void setSectorsUsed(int sectorsUsed) {
byte[] data = readFileEntry();
AppleUtil.setWordValue(data, 0x21, sectorsUsed);
writeFileEntry(data);
}
/**
* Identify if this is a directory file.
* @see com.webcodepro.applecommander.storage.FileEntry#isDirectory()
*/
public boolean isDirectory() {
return false;
}
/**
* Identify if this file has been deleted.
* @see com.webcodepro.applecommander.storage.FileEntry#isDeleted()
*/
public boolean isDeleted() {
return AppleUtil.getUnsignedByte(readFileEntry()[0]) == 0xff;
}
/**
* Delete this file.
*/
public void delete() {
disk.freeSectors(this);
byte[] fileEntry = readFileEntry();
fileEntry[0x20] = fileEntry[0x00];
fileEntry[0x00] = (byte)0xff;
writeFileEntry(fileEntry);
}
/**
* Get the standard file column header information.
* This default implementation is intended only for standard mode.
* displayMode is specified in FormattedDisk.
*/
public List