dmolony-DiskBrowser/src/com/bytezone/diskbrowser/applefile/MerlinSource.java

113 lines
3.5 KiB
Java
Raw Permalink Normal View History

2015-06-01 09:35:51 +00:00
package com.bytezone.diskbrowser.applefile;
2020-09-14 07:59:15 +00:00
// -----------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
public class MerlinSource extends AbstractFile
2020-09-14 07:59:15 +00:00
// -----------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
{
int ptr;
private static int[] tabs = { 12, 19, 35 };
private static int TAB_POS = tabs[2];
private final int recordLength;
private final int eof;
2017-02-01 21:15:30 +00:00
private boolean prodosFile;
2015-06-01 09:35:51 +00:00
// Source : Prodos text file
2020-09-14 07:59:15 +00:00
// ---------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
public MerlinSource (String name, byte[] buffer, int recordLength, int eof)
2020-09-14 07:59:15 +00:00
// ---------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
{
super (name, buffer);
this.eof = eof;
this.recordLength = recordLength;
2017-02-01 21:15:30 +00:00
prodosFile = true;
2015-06-01 09:35:51 +00:00
}
// Source : Dos binary file
2020-09-14 07:59:15 +00:00
// ---------------------------------------------------------------------------------//
2017-11-26 06:16:56 +00:00
public MerlinSource (String name, byte[] buffer, int loadAddress)
2020-09-14 07:59:15 +00:00
// ---------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
{
super (name, buffer);
this.eof = 0;
this.recordLength = 0;
2017-11-26 06:16:56 +00:00
this.loadAddress = loadAddress;
2015-06-01 09:35:51 +00:00
}
2020-09-14 07:59:15 +00:00
// ---------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
@Override
public String getText ()
2020-09-14 07:59:15 +00:00
// ---------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
{
StringBuilder text = new StringBuilder ();
2017-02-01 21:15:30 +00:00
text.append ("Merlin source : " + name + "\n");
if (prodosFile)
2015-06-01 09:35:51 +00:00
{
text.append (String.format ("Record length : %,8d%n", recordLength));
2017-02-01 21:15:30 +00:00
text.append (String.format ("EOF (aux) : %,8d%n", eof));
2015-06-01 09:35:51 +00:00
}
else
2017-11-26 06:16:56 +00:00
{
text.append (String.format ("Buffer size : %04X %<,8d%n", buffer.length));
text.append (String.format ("Load address : %04X %<,8d%n", loadAddress));
}
2015-06-01 09:35:51 +00:00
text.append ("\n");
ptr = 0;
while (ptr < buffer.length && buffer[ptr] != 0)
text.append (getLine () + "\n");
if (text.length () > 0)
text.deleteCharAt (text.length () - 1);
return text.toString ();
}
2020-09-14 07:59:15 +00:00
// ---------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
private String getLine ()
2020-09-14 07:59:15 +00:00
// ---------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
{
StringBuilder line = new StringBuilder ();
boolean comment = false;
boolean string = false;
while (ptr < buffer.length)
{
int val = buffer[ptr++] & 0x7F;
if (val == 0x0D)
break;
if (val == '*' && line.length () == 0)
comment = true;
if (val == '"')
string = !string;
if (val == ';' && !comment)
{
comment = true;
while (line.length () < TAB_POS)
line.append (' ');
}
if (val == ' ' && !comment && !string)
{
line = tab (line);
if (line.length () >= tabs[2])
comment = true;
}
else
line.append ((char) val);
}
return line.toString ();
}
2020-09-14 07:59:15 +00:00
// ---------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
private StringBuilder tab (StringBuilder text)
2020-09-14 07:59:15 +00:00
// ---------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
{
int nextTab = 0;
for (int tab : tabs)
if (text.length () < tab)
{
nextTab = tab;
break;
}
while (text.length () < nextTab)
text.append (' ');
return text;
}
}