
com.webcodepro.applecommander.storage.os.cpm.CpmFormatDisk Maven / Gradle / Ivy
Show all versions of AppleCommander Show documentation
/*
* AppleCommander - An Apple ][ image utility.
* Copyright (C) 2003-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.cpm;
import com.webcodepro.applecommander.storage.*;
import com.webcodepro.applecommander.storage.physical.ImageOrder;
import com.webcodepro.applecommander.util.AppleUtil;
import com.webcodepro.applecommander.util.TextBundle;
import java.util.*;
/**
* Manages a disk that is in the Apple CP/M format.
*
* @author Rob Greene
*/
public class CpmFormatDisk extends FormattedDisk {
private TextBundle textBundle = StorageBundle.getInstance();
/**
* The size of the CP/M sector. Assumed to be 128.
*/
public static final int CPM_SECTORSIZE = 128;
/**
* The size of a CP/M block. Assumed to be 1K.
*/
public static final int CPM_BLOCKSIZE = 1024;
/**
* The number of CP/M sectors per CP/M block.
*/
public static final int CPM_SECTORS_PER_CPM_BLOCK =
CPM_BLOCKSIZE / CPM_SECTORSIZE;
/**
* The number of CP/M blocks per physical track.
*/
public static final int CPM_BLOCKS_PER_TRACK = 4;
/**
* The number of physical sectors per CP/M block.
*/
public static final int PHYSICAL_SECTORS_PER_BLOCK = 4;
/**
* The track number which CP/M block #0 resides at.
* (The other tracks are boot-related and not available.)
*/
public static final int PHYSICAL_BLOCK_TRACK_START = 3;
/**
* The sector skew of the CP/M disk image.
*/
public static final int[] sectorSkew = {
0x0, 0x6, 0xc, 0x3, 0x9, 0xf, 0xe, 0x5,
0xb, 0x2, 0x8, 0x7, 0xd, 0x4, 0xa, 0x1 };
/**
* Manage CP/M disk usage.
*/
public class CpmDiskUsage implements DiskUsage {
int block = -1;
boolean[] usage = null;
public CpmDiskUsage(boolean[] usage) {
this.usage = usage;
}
public boolean hasNext() {
return block < usage.length-1;
}
public void next() {
block++;
}
public boolean isFree() {
return !isUsed();
}
public boolean isUsed() {
return usage[block];
}
}
/**
* Construct a CP/M formatted disk.
*/
public CpmFormatDisk(String filename, ImageOrder imageOrder) {
super(filename, imageOrder);
}
/**
* Create a CpmFormatDisk. All CP/M disk images are expected to
* be 140K in size.
*/
public static CpmFormatDisk[] create(String filename, ImageOrder imageOrder) {
CpmFormatDisk disk = new CpmFormatDisk(filename, imageOrder);
disk.format();
return new CpmFormatDisk[] { disk };
}
/**
* There apparently is no corresponding CP/M disk name.
* @see com.webcodepro.applecommander.storage.FormattedDisk#getDiskName()
*/
public String getDiskName() {
return textBundle.get("CpmFormatDisk.DiskName"); //$NON-NLS-1$
}
/**
* Identify the operating system format of this disk.
* @see com.webcodepro.applecommander.storage.FormattedDisk#getFormat()
*/
public String getFormat() {
return textBundle.get("CpmFormatDisk.Cpm"); //$NON-NLS-1$
}
/**
* Return the amount of free space in bytes.
* @see com.webcodepro.applecommander.storage.FormattedDisk#getFreeSpace()
*/
public int getFreeSpace() {
return getPhysicalSize() - getUsedSpace();
}
/**
* Return the amount of used space in bytes.
* @see com.webcodepro.applecommander.storage.FormattedDisk#getUsedSpace()
*/
public int getUsedSpace() {
int blocksUsed = getBlocksUsed();
return blocksUsed * CPM_BLOCKSIZE +
PHYSICAL_BLOCK_TRACK_START * CPM_BLOCKS_PER_TRACK * CPM_BLOCKSIZE;
}
/**
* Compute the number of CP/M blocks that are currently used.
*/
public int getBlocksUsed() {
List files = getFiles();
int blocksUsed = 0;
for (int i=0; i files = getFiles();
for (int i=0; i 0) {
byte[] block = readCpmBlock(blockNumber);
System.arraycopy(block, 0,
data, i * CPM_BLOCKSIZE, CPM_BLOCKSIZE);
}
}
return data;
}
/**
* Format the disk. Simply wipes the disk to all 0xE5 - this seems to
* be the standard (or a requirement).
*
* Note: Assumes that this is a 140K CP/M disk of 35 tracks and
* 16 physical sectors.
* @see com.webcodepro.applecommander.storage.FormattedDisk#format()
*/
public void format() {
getImageOrder().format();
byte[] sectorData = new byte[SECTOR_SIZE];
for (int i=0; i 0 && !Character.isLetter(filetype.charAt(0))) {
newType.append('A');
}
int i=0;
while (newType.length() < 3 && i getFiles() {
List files = new ArrayList<>();
Map index = new HashMap<>();
for (int i=0; i<64; i++) {
int offset = i*CpmFileEntry.ENTRY_LENGTH;
CpmFileEntry fileEntry = new CpmFileEntry(this, offset);
if (!fileEntry.isEmpty()) {
// Files are unique by name, type, and user number.
String key = fileEntry.getFilename().trim() + "." //$NON-NLS-1$
+ fileEntry.getFiletype().trim() + ":" //$NON-NLS-1$
+ fileEntry.getUserNumber(0);
if (index.containsKey(key)) {
fileEntry = (CpmFileEntry) index.get(key);
fileEntry.addOffset(offset);
} else {
files.add(fileEntry);
index.put(key, fileEntry);
}
}
}
return files;
}
/**
* Create a new FileEntry.
* @see com.webcodepro.applecommander.storage.DirectoryEntry#createFile()
*/
public FileEntry createFile() throws DiskFullException {
throw new DiskFullException(
textBundle.get("FileCreationNotSupported") //$NON-NLS-1$
, this.getFilename());
}
/**
* Identify if additional directories can be created. CP/M doesn't
* support directories.
* @see com.webcodepro.applecommander.storage.DirectoryEntry#canCreateDirectories()
*/
public boolean canCreateDirectories() {
return false;
}
/**
* Indicates if this disk image can create a file.
* @see com.webcodepro.applecommander.storage.DirectoryEntry#canCreateFile()
*/
public boolean canCreateFile() {
return false;
}
/**
* Read a CP/M block (1K in size).
*/
public byte[] readCpmBlock(int block) {
byte[] data = new byte[CPM_BLOCKSIZE];
int track = computeTrack(block);
int sector = computeSector(block);
for (int i=0; i getFileColumnHeaders(int displayMode) {
List list = new ArrayList<>();
switch (displayMode) {
case FILE_DISPLAY_NATIVE:
list.add(new FileColumnHeader(textBundle.get("Name"), 8,
FileColumnHeader.ALIGN_LEFT, "name"));
list.add(new FileColumnHeader(textBundle.get("Type"), 3,
FileColumnHeader.ALIGN_LEFT, "type"));
break;
case FILE_DISPLAY_DETAIL:
list.add(new FileColumnHeader(textBundle.get("Name"), 8,
FileColumnHeader.ALIGN_LEFT, "name"));
list.add(new FileColumnHeader(textBundle.get("Type"), 3,
FileColumnHeader.ALIGN_LEFT, "type"));
list.add(new FileColumnHeader(textBundle.get("SizeInBytes"), 6,
FileColumnHeader.ALIGN_RIGHT, "sizeInBytes"));
list.add(new FileColumnHeader(textBundle.get("CpmFormatDisk.UserNumber"), 4,
FileColumnHeader.ALIGN_RIGHT, "user"));
list.add(new FileColumnHeader(textBundle.get("DeletedQ"), 7,
FileColumnHeader.ALIGN_CENTER, "deleted"));
list.add(new FileColumnHeader(textBundle.get("LockedQ"), 6,
FileColumnHeader.ALIGN_CENTER, "locked"));
break;
default: // FILE_DISPLAY_STANDARD
list.addAll(super.getFileColumnHeaders(displayMode));
break;
}
return list;
}
/**
* Indicates if this FormattedDisk supports a disk map.
*/
public boolean supportsDiskMap() {
return true;
}
/**
* Change to a different ImageOrder. Remains in CP/M format but
* the underlying order can change.
* @see ImageOrder
*/
public void changeImageOrder(ImageOrder imageOrder) {
AppleUtil.changeImageOrderByTrackAndSector(getImageOrder(), imageOrder);
setImageOrder(imageOrder);
}
/**
* Writes the raw bytes into the file. This bypasses any special formatting
* of the data (such as prepending the data with a length and/or an address).
* Typically, the FileEntry.setFileData method should be used.
*/
public void setFileData(FileEntry fileEntry, byte[] fileData) throws DiskFullException {
// TODO implement setFileData
}
/**
* Create a new DirectoryEntry.
* @see com.webcodepro.applecommander.storage.DirectoryEntry#createDirectory(String)
*/
public DirectoryEntry createDirectory(String name) throws DiskFullException {
throw new UnsupportedOperationException(textBundle.get("DirectoryCreationNotSupported")); //$NON-NLS-1$
}
/**
* Gives an indication on how this disk's geometry should be handled.
*/
public DiskGeometry getDiskGeometry() {
return DiskGeometry.TRACK_SECTOR;
}
/**
* Provides conversation from a given ProDOS file type since as it is common across
* many archiving tools.
*/
@Override
public String fromProdosFiletype(String prodosFiletype) {
if ("TXT".equalsIgnoreCase(prodosFiletype)) {
return "TXT";
}
return "BIN";
}
/**
* Provides conversation to a given ProDOS file type since as it is common across
* many archiving tools.
*/
@Override
public String toProdosFiletype(String nativeFiletype) {
for (String textFiletype : CpmFileEntry.TEXT_FILETYPES) {
if (textFiletype.equalsIgnoreCase(nativeFiletype)) {
return "TXT";
}
}
return "BIN";
}
}