Adding support for GS/OS mixed case names. #84

This commit is contained in:
Rob Greene 2022-06-25 20:04:27 -05:00
parent a4d9c5e746
commit 06c76ed1c3
2 changed files with 159 additions and 70 deletions

View File

@ -24,6 +24,7 @@ import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.function.Function;
import com.webcodepro.applecommander.storage.DiskFullException; import com.webcodepro.applecommander.storage.DiskFullException;
import com.webcodepro.applecommander.storage.FileEntry; import com.webcodepro.applecommander.storage.FileEntry;
@ -61,13 +62,14 @@ public class ProdosFileEntry extends ProdosCommonEntry implements FileEntry {
/** /**
* Return the name of this file. * Return the name of this file.
* This handles normal files, deleted files, and AppleWorks files - which use * This handles normal files, deleted files, AppleWorks files - which use
* the AUXTYPE attribute to indicate upper/lowercase in the filename. * the AUXTYPE attribute to indicate upper/lowercase in the filename, and
* GS/OS casing which is in the ProDOS version flags.
* Note that we don't do mixed case for deleted files.
*/ */
public String getFilename() { public String getFilename() {
String fileName;
if (isDeleted()) { if (isDeleted()) {
fileName = AppleUtil.getString(readFileEntry(), 1, 15); String fileName = AppleUtil.getString(readFileEntry(), 1, 15);
StringBuffer buf = new StringBuffer(); StringBuffer buf = new StringBuffer();
for (int i=0; i<fileName.length(); i++) { for (int i=0; i<fileName.length(); i++) {
char ch = fileName.charAt(i); char ch = fileName.charAt(i);
@ -77,55 +79,53 @@ public class ProdosFileEntry extends ProdosCommonEntry implements FileEntry {
break; break;
} }
} }
fileName = buf.toString(); return buf.toString();
} else { }
fileName = AppleUtil.getProdosString(readFileEntry(), 0); else {
} String fileName = AppleUtil.getProdosString(readFileEntry(), 0);
if (isAppleWorksFile()) { if (isAppleWorksFile()) {
int auxtype = getAuxiliaryType(); int auxtype = getAuxiliaryType();
StringBuffer mixedCase = new StringBuffer(fileName); fileName = getMixedCaseName(fileName, auxtype, ProdosFileEntry::calculateAppleWorksBit);
// the highest bit of the least significant byte is the first }
// character through the 2nd bit of the most significant byte // GS/OS upper/lower case mix
// being the 15th character. Bit is on indicates lower-case or else if ((getMinimumProdosVersion() & 0x80) != 0) {
// a space if a "." is present. int flags = getMinimumProdosVersion() << 8 | getProdosVersion();
for (int i=0; i<16 && i<fileName.length(); i++) { fileName = getMixedCaseName(fileName, flags, ProdosFileEntry::calculateGSOSBit);
boolean lowerCase; }
if (i < 8) { return fileName;
lowerCase = AppleUtil.isBitSet((byte)auxtype, 7-i); }
} else { }
lowerCase = AppleUtil.isBitSet((byte)(auxtype >> 8), 7-(i%8)); /**
} * the highest bit of the least significant byte is the first
if (lowerCase) { * character through the 2nd bit of the most significant byte
char ch = mixedCase.charAt(i); * being the 15th character. Bit is on indicates lower-case or
if (ch == '.') { * a space if a "." is present.
mixedCase.setCharAt(i, ' '); */
} else { static int calculateAppleWorksBit(int pos) {
mixedCase.setCharAt(i, Character.toLowerCase(ch)); final int[] bitTest = { 7, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11, 10, 9 };
} return 1 << bitTest[pos];
} }
} /**
fileName = mixedCase.toString(); * 1st bit in 16 bit number is enablement flag, so skipping it
} * flags = e01234567 89ABCDEF (e = enablement flag; hex digits = character index)
// GS/OS upper/lower case mix */
if ((getMinimumProdosVersion() & 0x80) != 0) { static int calculateGSOSBit(int pos) {
int flags = getMinimumProdosVersion() << 8 | getProdosVersion(); return 1 << (14-pos);
int bit = 1 << 14; // 1st bit in 16 bit number is enablement flag, so skipping it }
StringBuffer mixedCase = new StringBuffer(fileName); static String getMixedCaseName(final String original, final int bits,
for (int i=0; i<16 && i<fileName.length(); i++) { final Function<Integer,Integer> bitPosition) {
boolean lowerCase = (flags & bit) != 0; StringBuffer mixedCase = new StringBuffer(original);
if (lowerCase) { for (int i=0; i<mixedCase.length(); i++) {
char ch = mixedCase.charAt(i); if ((bits & bitPosition.apply(i)) != 0) {
if (ch == '.') { char ch = mixedCase.charAt(i);
mixedCase.setCharAt(i, ' '); if (ch == '.') {
} else { mixedCase.setCharAt(i, ' ');
mixedCase.setCharAt(i, Character.toLowerCase(ch)); } else {
} mixedCase.setCharAt(i, Character.toLowerCase(ch));
} }
bit >>= 1; }
} }
fileName = mixedCase.toString(); return mixedCase.toString();
}
return fileName;
} }
/** /**
@ -137,31 +137,51 @@ public class ProdosFileEntry extends ProdosCommonEntry implements FileEntry {
/** /**
* Set the name of this file. * Set the name of this file.
* This handles normal files, deleted files, AppleWorks files - which use
* the AUXTYPE attribute to indicate upper/lowercase in the filename, and
* GS/OS casing which is in the ProDOS version flags.
*/ */
public void setFilename(String filename) { public void setFilename(String filename) {
byte[] fileEntry = readFileEntry(); // 1. Set the upper/lowercase flags
if (isDeleted()) {
AppleUtil.setString(fileEntry, 1, filename.toUpperCase(), 15);
} else {
AppleUtil.setProdosString(fileEntry, 0, filename.toUpperCase(), 15);
}
if (isAppleWorksFile()) { if (isAppleWorksFile()) {
byte lowByte = 0; // Since we are ALWAYS doing the GSOS filename, we just have a temp holder here.
byte highByte = 0; StringBuilder tempFilename = new StringBuilder(filename);
for (int i=0; i<filename.length(); i++) { int auxtype = setMixedCaseFlags(tempFilename, ProdosFileEntry::calculateAppleWorksBit);
if (Character.isLowerCase(filename.charAt(i))) { setAuxiliaryType(auxtype);
if (i < 8) {
lowByte = AppleUtil.setBit(lowByte, 7-i);
} else if (i < 16) {
highByte = AppleUtil.setBit(highByte, 7-(i%8));
}
}
}
setAuxiliaryType(fileEntry, lowByte, highByte);
} }
StringBuilder alternateFilename = new StringBuilder(filename);
int flags = setMixedCaseFlags(alternateFilename, ProdosFileEntry::calculateGSOSBit);
if (flags != 0) {
// high bit signals mixed case
flags |= 0x8000;
}
filename = alternateFilename.toString();
setMinimumProdosVersion(flags >> 8);
setProdosVersion(flags);
// 2. Set the filename details as the result should be all uppercase.
byte[] fileEntry = readFileEntry();
if (isDeleted()) {
AppleUtil.setString(fileEntry, 1, filename.toUpperCase(), 15);
} else {
AppleUtil.setProdosString(fileEntry, 0, filename.toUpperCase(), 15);
}
writeFileEntry(fileEntry); writeFileEntry(fileEntry);
} }
static int setMixedCaseFlags(StringBuilder filename, Function<Integer,Integer> bitPosition) {
int flags = 0;
for (int i=0; i<filename.length(); i++) {
char ch = filename.charAt(i);
if (Character.isLowerCase(ch) || ch == ' ') {
flags |= bitPosition.apply(i);
if (ch == ' ') ch = '.';
filename.setCharAt(i, Character.toUpperCase(ch));
}
}
return flags;
}
/** /**
* Copy GEOS-specific metadata to the directory entry verbatim: * Copy GEOS-specific metadata to the directory entry verbatim:
* Bytes $00-$10 ($11 bytes) * Bytes $00-$10 ($11 bytes)

View File

@ -0,0 +1,69 @@
/*
* AppleCommander - An Apple ][ image utility.
* Copyright (C) 2002-2022 by Robert Greene and others
* 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.prodos;
import org.junit.Test;
import static org.junit.Assert.*;
public class ProdosFileEntryTest {
@Test
public void testGetMixedCaseName_GSOS() {
final String originalName = "DESK.ACCS";
final int flags = 0xb9c0;
final String expectedName = "Desk.Accs";
assertEquals(expectedName, ProdosFileEntry.getMixedCaseName(originalName, flags,
ProdosFileEntry::calculateGSOSBit));
}
@Test
public void testSetMixedCaseName_GSOS() {
final String originalName = "Desk.Accs";
final int expectedFlags = 0xb9c0 & 0x7fff; // the flag is set outside this method
final String expectedName = "DESK.ACCS";
StringBuilder sb = new StringBuilder(originalName);
int actualFlags = ProdosFileEntry.setMixedCaseFlags(sb, ProdosFileEntry::calculateGSOSBit);
assertEquals(expectedName, sb.toString());
assertEquals(expectedFlags, actualFlags);
}
@Test
public void testGetMixedCaseName_AppleWorks() {
final String originalName = "MOUSEFIXER.DOCS";
final int auxtype = 0xee7b;
final String expectedName = "MouseFixer Docs";
assertEquals(expectedName, ProdosFileEntry.getMixedCaseName(originalName, auxtype,
ProdosFileEntry::calculateAppleWorksBit));
}
@Test
public void testSetMixedCaseName_AppleWorks() {
final String originalName = "MouseFixer Docs";
final int expectedFlags = 0xee7b;
final String expectedName = "MOUSEFIXER.DOCS";
StringBuilder sb = new StringBuilder(originalName);
int actualFlags = ProdosFileEntry.setMixedCaseFlags(sb, ProdosFileEntry::calculateAppleWorksBit);
assertEquals(expectedName, sb.toString());
assertEquals(expectedFlags, actualFlags);
}
}