better pascal TEXT handling

This commit is contained in:
Denis Molony 2022-12-15 20:35:26 +10:00
parent 9fba3893ba
commit bc0993e9d3

View File

@ -4,6 +4,8 @@ package com.bytezone.diskbrowser.applefile;
public class PascalText extends TextFile public class PascalText extends TextFile
// -----------------------------------------------------------------------------------// // -----------------------------------------------------------------------------------//
{ {
private final static int PAGE_SIZE = 1024;
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
public PascalText (String name, byte[] buffer) public PascalText (String name, byte[] buffer)
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
@ -16,28 +18,34 @@ public class PascalText extends TextFile
public String getText () public String getText ()
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
{ {
// Text files are broken up into 1024-byte pages.
// [DLE] [indent] [text] [CR] ... [nulls]
StringBuilder text = new StringBuilder (getHeader ()); StringBuilder text = new StringBuilder (getHeader ());
int ptr = 0x400; int ptr = PAGE_SIZE; // skip text editor header
while (ptr < buffer.length) while (ptr < buffer.length)
{ {
if (buffer[ptr] == 0x00) if (buffer[ptr] == 0x00) // padding to page boundary
{ {
++ptr; ptr = (ptr / PAGE_SIZE + 1) * PAGE_SIZE; // skip to next page
continue; continue;
} }
if (buffer[ptr] == 0x10) if (buffer[ptr] == 0x10) // Data Link Escape code
{ {
int tab = buffer[ptr + 1] - 0x20; int tab = (buffer[ptr + 1] & 0xFF) - 32; // indent amaount
while (tab-- > 0) while (tab-- > 0)
text.append (" "); text.append (" ");
ptr += 2; ptr += 2;
} }
String line = getLine (ptr); while (buffer[ptr] != 0x0D)
text.append (line + "\n"); text.append ((char) buffer[ptr++]);
ptr += line.length () + 1;
text.append ("\n");
ptr++;
} }
if (text.length () > 0) if (text.length () > 0)
@ -45,16 +53,4 @@ public class PascalText extends TextFile
return text.toString (); return text.toString ();
} }
// ---------------------------------------------------------------------------------//
private String getLine (int ptr)
// ---------------------------------------------------------------------------------//
{
StringBuilder line = new StringBuilder ();
while (buffer[ptr] != 0x0D)
line.append ((char) buffer[ptr++]);
return line.toString ();
}
} }