dmolony-DiskBrowser/src/com/bytezone/diskbrowser/visicalc/Sheet.java

477 lines
14 KiB
Java
Raw Normal View History

2016-03-04 03:56:28 +00:00
package com.bytezone.diskbrowser.visicalc;
2015-06-01 09:35:51 +00:00
import java.text.DecimalFormat;
2016-03-08 09:39:35 +00:00
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
2015-06-01 09:35:51 +00:00
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2016-02-24 21:11:14 +00:00
import com.bytezone.diskbrowser.utilities.HexFormatter;
2015-06-01 09:35:51 +00:00
2016-03-06 08:05:32 +00:00
public class Sheet implements Iterable<Cell>
2015-06-01 09:35:51 +00:00
{
2016-03-08 09:39:35 +00:00
private static char[] tokens = { '"', '@', '+' }; // label, function, formula/value
2016-02-04 00:31:44 +00:00
private static final Pattern addressPattern =
2016-03-08 09:39:35 +00:00
Pattern.compile ("([AB]?[A-Z])([0-9]{1,3}):");
2016-03-07 12:16:11 +00:00
// private static final Pattern functionPattern = Pattern
// .compile ("\\(([A-B]?[A-Z])([0-9]{1,3})\\.\\.\\.([A-B]?[A-Z])([0-9]{1,3})\\)?");
// private static final Pattern addressList = Pattern.compile ("\\(([^,]+(,[^,]+)*)\\)");
2015-06-01 09:35:51 +00:00
2016-03-06 08:05:32 +00:00
private final Map<Integer, Cell> sheet = new TreeMap<Integer, Cell> ();
2016-03-11 02:54:31 +00:00
private final List<String> lines = new ArrayList<String> ();
2016-03-08 09:39:35 +00:00
// private final Map<String, Double> functions = new HashMap<String, Double> ();
2015-06-01 09:35:51 +00:00
2016-03-06 08:05:32 +00:00
Cell currentCell = null;
2015-06-01 09:35:51 +00:00
char defaultFormat;
2016-03-04 05:27:14 +00:00
private final Map<Integer, Integer> columnWidths = new TreeMap<Integer, Integer> ();
2016-03-08 09:39:35 +00:00
private int columnWidth = 12;
private char recalculation = ' ';
private char recalculationOrder = ' ';
2016-03-09 10:38:53 +00:00
private int columns;
private int rows;
2016-03-04 05:27:14 +00:00
2015-06-01 09:35:51 +00:00
// Maximum cell = BK254
//Commands:
// /G<address> goto
// /B blank the cell
// /C clear and home
// A-Z label (" to force a label)
// 0-9.+-()*/@# value (+ to force a value)
// /S Storage (ISLDWR)
// /SI Storage init
// /SS Storage Save
// /SL Storage Load
// /SD Storage Delete
// /SW Storage Write (to cassette)
// /SR Storage Read (from cassette)
// /R Replicate
// /G Global (CORF)
// /GF Global Format (DGILR$*)
// /GFI Global Format Integer
// /GF$ Global Format Currency
// /GC Global Column <width>
// /GR Global
2016-03-08 09:39:35 +00:00
// /GRA Recalculation Auto
2015-06-01 09:35:51 +00:00
// /GO Global
2016-03-08 09:39:35 +00:00
// /GOC Calculation Order - Columns first
2015-06-01 09:35:51 +00:00
// /T Titles (HVBN)
// /TH fix Horizontal Titles
// /TV fix Vertical Titles
// /TB fix Both Titles
// /TN fix Neither
// /W Window (HV1SU)
// /WV Window Vertical (split on cursor column)
// /WH Window Horizontal (split on cursor row)
2016-02-04 00:31:44 +00:00
/*
from: Apple II TextFiles
http://www.textfiles.com/apple/
*----------------------*
VISICALC COMMAND CHART
BY: NIGHT HAWK
*----------------------*
/B SET AN ENTRY TO BLANK
/C CLEARS THE SHEET, SETTING ALL ENTRIES TO BLANK
/D DELETES THE ROW(/DR) OR COLUMN(/DC) ON WHICH THE CURSOR
LIES.
/E ALLOWS EDITING OF THE ENTRY CONTENTS OF ANY ENTRY POSITION
BY REDISPLAYING IT ON THE EDIT LINE. USE <- -> KEYS & ESC.
/F SETS THE DISPLAY FORMAT OF AN ENTRY TO ONE OF THE FOLLOWING
FORMATS:
/FG GENERAL
/FI INTEGER
/F$ DOLLAR AND CENTS
/FL LEFT JUSTIFIED
/FR RIGHT JUSTIFIED
2016-03-01 22:51:56 +00:00
/F* GRAPH
/FD DEFAULT
2016-02-04 00:31:44 +00:00
/G GLOBAL COMMANDS. THESE APPLY TO THE ENTIRE SHEET OR WINDOW.
/GC SETS COLUMN WIDTH
/GF SETS THE GLOBAL DEFAULT FORMAT
/GO SETS THE ORDER OF RECALCULATION TO BE DOWN THE
COLUMNS OR ACROSS THE ROWS
/GR SETS RECALCULATION TO BE AUTOMATIC(/GRA) OR MANUAL(/GRM).
/I INSERTS A ROW(/IR) OR A COLUMN(/IC)
/M MOVES AN ENTIRE ROW OR COLUMN TO A NEW POSITION.
/P PRINT COMMAND
/R REPLICATE COMMAND
/S STORAGE COMMANDS ARE AS FOLLOWS:
2016-03-01 22:51:56 +00:00
/SS SAVE
/SL LOAD
/SD DELETES SPECIFIED FILE ON DISK
2016-02-04 00:31:44 +00:00
/SI INITIALIZE A DISK ON SPECIFIED DRIVE
/SQ QUITS VISICALC
/T SETS A HORIZONTAL TITLE AREA(/TH), A VERTICAL TITLE AREA
(/TV), SET BOTH A HORIZONTAL & VERTICAL TITLE AREA(/TB)
OR RESETS THE WINDOWS TO HAVE NO TITLE AREAS(/TN)
/V DISPLAYS VISICALC'S VERSION NUMBER ON THE PROMPT LINE
/W WINDOW CONTROL
/WH HORIZONTAL WINDOW
/WV VERTICAL WINDOW
/W1 RETURNS SCREEN TO ONE WINDOW
/WS SYNCHRONIZED WINDOWS
/WU UNSYNCHRONIZED
/- REPEATING LABEL
*/
2016-03-06 08:05:32 +00:00
public Sheet (byte[] buffer)
2015-06-01 09:35:51 +00:00
{
2016-03-04 05:27:14 +00:00
int last = buffer.length;
while (buffer[--last] == 0)
;
2015-06-01 09:35:51 +00:00
2016-03-04 05:27:14 +00:00
int ptr = 0;
2016-03-06 23:52:46 +00:00
while (ptr < last)
2015-06-01 09:35:51 +00:00
{
2016-03-06 23:52:46 +00:00
int length = getLineLength (buffer, ptr);
String line = HexFormatter.getString (buffer, ptr, length);
lines.add (line);
processLine (line);
ptr += length + 1; // +1 for end-of-line token
2015-06-01 09:35:51 +00:00
}
2016-03-05 21:53:29 +00:00
if (true)
2016-03-04 05:27:14 +00:00
{
2016-03-06 04:41:15 +00:00
System.out.println ();
System.out.println ("Lines:");
2016-03-05 21:53:29 +00:00
for (String line : lines)
System.out.println (line);
System.out.println ();
2016-03-06 04:41:15 +00:00
System.out.println ("Cells:");
2016-03-06 08:05:32 +00:00
for (Cell cell : sheet.values ())
2015-06-01 09:35:51 +00:00
System.out.println (cell);
2016-02-04 00:31:44 +00:00
2016-03-05 21:53:29 +00:00
System.out.println ();
2016-03-06 04:41:15 +00:00
System.out.println ("Column widths:");
2016-03-05 21:53:29 +00:00
System.out.printf ("Default width : %3d%n", columnWidth);
2016-03-04 05:27:14 +00:00
for (Map.Entry<Integer, Integer> entry : columnWidths.entrySet ())
2016-03-05 21:53:29 +00:00
System.out.printf (" column %3d: %3d%n", entry.getKey (), entry.getValue ());
2016-03-04 05:27:14 +00:00
}
}
2016-03-06 23:52:46 +00:00
private int getLineLength (byte[] buffer, int offset)
2016-03-04 05:27:14 +00:00
{
2016-03-06 23:52:46 +00:00
int ptr = offset;
while (buffer[ptr] != (byte) 0x8D) // end-of-line token
2016-03-04 05:27:14 +00:00
ptr++;
2016-03-06 23:52:46 +00:00
return ptr - offset;
2015-06-01 09:35:51 +00:00
}
2016-03-08 09:39:35 +00:00
private void processLine (String line)
2015-06-01 09:35:51 +00:00
{
2016-03-01 22:51:56 +00:00
// NB no closing bracket: [>K11:@SUM(J11...F11]
2016-03-08 09:39:35 +00:00
// >B10:/F$+B4+B7+B8+B9
// >F2:/FR"INCOME
if (line.isEmpty ())
2016-03-06 04:41:15 +00:00
{
System.out.println ("empty command");
return;
}
2016-03-08 09:39:35 +00:00
if (line.startsWith ("/"))
2015-06-01 09:35:51 +00:00
{
2016-03-08 09:39:35 +00:00
switch (line.charAt (1))
2015-06-01 09:35:51 +00:00
{
2016-03-08 09:39:35 +00:00
case 'W':
System.out.printf ("Skipping [%s]%n", line);
2015-06-01 09:35:51 +00:00
break;
2016-03-08 09:39:35 +00:00
case 'G':
switch (line.charAt (2))
2016-02-04 00:31:44 +00:00
{
2016-03-08 09:39:35 +00:00
case 'R':
recalculation = line.charAt (3);
break;
case 'O':
recalculationOrder = line.charAt (3);
break;
case 'P':
System.out.printf ("Skipping [%s]%n", line);
break;
case 'C':
columnWidth = Integer.parseInt (line.substring (3));
break;
2016-02-04 00:31:44 +00:00
}
2015-06-01 09:35:51 +00:00
break;
2016-03-08 09:39:35 +00:00
case 'X':
System.out.printf ("Skipping [%s]%n", line);
2016-03-06 04:41:15 +00:00
break;
2015-06-01 09:35:51 +00:00
default:
2016-03-08 09:39:35 +00:00
System.out.printf ("Skipping [%s]%n", line);
2015-06-01 09:35:51 +00:00
}
2016-03-08 09:39:35 +00:00
return;
2015-06-01 09:35:51 +00:00
}
2016-03-08 09:39:35 +00:00
if (!line.startsWith (">")) // GOTO cell
2015-06-01 09:35:51 +00:00
{
2016-03-08 09:39:35 +00:00
System.out.printf ("Error [%s]%n", line);
return;
2015-06-01 09:35:51 +00:00
}
2016-03-08 09:39:35 +00:00
currentCell = null;
Matcher m = addressPattern.matcher (line);
if (m.find ())
2015-06-01 09:35:51 +00:00
{
2016-03-08 09:39:35 +00:00
Address address = new Address (m.group (1), m.group (2));
currentCell = sheet.get (address.sortValue);
int pos = line.indexOf (':'); // end of cell address
line = line.substring (pos + 1); // remove address from line
if (currentCell == null)
{
currentCell = new Cell (this, address);
if (!line.startsWith ("/G"))
2016-03-09 10:38:53 +00:00
{
2016-03-08 09:39:35 +00:00
sheet.put (currentCell.address.sortValue, currentCell);
2016-03-09 10:38:53 +00:00
if (address.row > rows)
rows = address.row;
if (address.column > columns)
columns = address.column;
}
2016-03-08 09:39:35 +00:00
}
2015-06-01 09:35:51 +00:00
}
2016-03-08 09:39:35 +00:00
else
2015-06-01 09:35:51 +00:00
{
2016-03-08 09:39:35 +00:00
System.out.printf ("Invalid cell address: %s%n", line);
return;
2015-06-01 09:35:51 +00:00
}
2016-03-08 09:39:35 +00:00
assert currentCell != null;
if (line.startsWith ("/G")) // global column widths
2015-06-01 09:35:51 +00:00
{
2016-03-08 09:39:35 +00:00
if (line.charAt (2) == 'C' && line.charAt (3) == 'C')
{
int width = Integer.parseInt (line.substring (4));
columnWidths.put (currentCell.address.column, width);
}
else
System.out.printf ("Unknown Global:[%s]%n", line);
return;
2015-06-01 09:35:51 +00:00
}
2016-03-08 09:39:35 +00:00
// check for formatting commands before a token
String command = "";
if (line.startsWith ("/"))
2015-06-01 09:35:51 +00:00
{
2016-03-08 09:39:35 +00:00
for (char token : tokens)
{
int pos = line.indexOf (token);
if (pos > 0)
{
command = line.substring (0, pos);
line = line.substring (pos);
break;
}
}
if (line.startsWith ("/")) // no token found
{
command = line;
line = "";
}
2015-06-01 09:35:51 +00:00
}
2016-03-08 09:39:35 +00:00
if (true)
System.out.printf ("[%s][%-3s][%s]%n", currentCell.address, command, line);
if (!command.isEmpty ())
currentCell.format (command); // formatting command
if (!line.isEmpty ())
2016-03-11 21:56:02 +00:00
currentCell.setValue (line); // expression
}
Cell getCell (String addressText)
{
Address address = new Address (addressText);
return getCell (address);
2016-03-08 09:39:35 +00:00
}
2015-06-01 09:35:51 +00:00
2016-03-07 04:37:01 +00:00
Cell getCell (Address address)
2015-06-01 09:35:51 +00:00
{
2016-03-09 10:38:53 +00:00
Cell cell = sheet.get (address.sortValue);
if (cell == null)
2016-03-10 02:39:23 +00:00
System.out.printf ("Nonexistent cell requested [%s]%n", address);
2016-03-11 21:56:02 +00:00
2016-03-09 10:38:53 +00:00
return cell;
2015-06-01 09:35:51 +00:00
}
public int size ()
{
return sheet.size ();
}
@Override
2016-03-06 08:05:32 +00:00
public Iterator<Cell> iterator ()
2015-06-01 09:35:51 +00:00
{
return sheet.values ().iterator ();
}
2016-03-11 02:54:31 +00:00
public String getLines ()
{
StringBuilder text = new StringBuilder ();
for (String line : lines)
{
text.append (line);
text.append ("\n");
}
if (text.length () > 0)
text.deleteCharAt (text.length () - 1);
return text.toString ();
}
2015-06-01 09:35:51 +00:00
public String getCells ()
{
StringBuilder text = new StringBuilder ();
2016-03-06 04:41:15 +00:00
String longLine;
if (false)
longLine = "%+++++++++++++++++++++++++++++++++++++++++++++++++++++++"
+ "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
else
longLine = " "
+ " ";
2016-03-11 01:52:22 +00:00
String underline = "---------------------------------------------------------"
+ "-----------------------------------------------------------------";
2015-06-01 09:35:51 +00:00
DecimalFormat nf = new DecimalFormat ("$#####0.00");
// NumberFormat nf = NumberFormat.getCurrencyInstance ();
2016-03-09 10:38:53 +00:00
int lastRow = -1;
2016-03-05 21:53:29 +00:00
int lastColumn = 0;
2015-06-01 09:35:51 +00:00
2016-03-09 10:38:53 +00:00
StringBuilder heading = new StringBuilder (" ");
for (int cellNo = 0; cellNo <= columns; cellNo++)
{
int width = columnWidth;
if (columnWidths.containsKey (cellNo))
width = columnWidths.get (cellNo);
if (width == 1)
heading.append ("=");
2016-03-10 02:39:23 +00:00
else if (width == 2)
heading.append ("==");
2016-03-09 10:38:53 +00:00
else
{
2016-03-10 09:21:47 +00:00
char letter1 = cellNo < 26 ? ' ' : cellNo < 52 ? 'A' : 'B';
2016-03-09 10:38:53 +00:00
char letter2 = (char) ((cellNo % 26) + 'A');
String fmt =
String.format ("%s%s%%%d.%ds", letter1, letter2, (width - 2), (width - 2));
2016-03-11 01:52:22 +00:00
heading.append (String.format (fmt, underline));
2016-03-09 10:38:53 +00:00
}
}
text.append (heading);
2016-03-06 08:05:32 +00:00
for (Cell cell : sheet.values ())
2015-06-01 09:35:51 +00:00
{
while (lastRow < cell.address.row)
{
++lastRow;
2016-03-05 21:53:29 +00:00
lastColumn = 0;
2016-03-09 10:38:53 +00:00
text.append (String.format ("%n%03d:", lastRow + 1));
2015-06-01 09:35:51 +00:00
}
2016-03-05 21:53:29 +00:00
while (lastColumn < cell.address.column)
2015-06-01 09:35:51 +00:00
{
2016-02-04 00:31:44 +00:00
int width = columnWidth;
2016-03-05 21:53:29 +00:00
if (columnWidths.containsKey (lastColumn))
width = columnWidths.get (lastColumn);
2016-02-04 00:31:44 +00:00
text.append (longLine.substring (0, width));
2015-06-01 09:35:51 +00:00
++lastColumn;
}
2016-02-04 00:31:44 +00:00
int colWidth = columnWidth;
if (columnWidths.containsKey (cell.address.column))
colWidth = columnWidths.get (cell.address.column);
2016-03-05 21:53:29 +00:00
++lastColumn;
char format = cell.getFormat ();
if (format == ' ')
format = defaultFormat;
2016-02-04 00:31:44 +00:00
2016-03-09 10:38:53 +00:00
// System.out.println (cell.address);
2015-06-01 09:35:51 +00:00
if (cell.hasValue ())
{
2016-03-05 21:53:29 +00:00
if (format == 'I')
2016-02-04 00:31:44 +00:00
{
2016-03-06 04:41:15 +00:00
String integerFormat = String.format ("%%%d.0f", colWidth);
2016-03-05 21:53:29 +00:00
// System.out.printf ("Integer format:%s%n", integerFormat);
2015-06-01 09:35:51 +00:00
text.append (String.format (integerFormat, cell.getValue ()));
2016-02-04 00:31:44 +00:00
}
2016-03-05 21:53:29 +00:00
else if (format == '$')
2016-02-04 00:31:44 +00:00
{
String currencyFormat = String.format ("%%%d.%ds", colWidth, colWidth);
2016-03-05 21:53:29 +00:00
// System.out.printf ("Currency format:%s%n", currencyFormat);
2015-06-01 09:35:51 +00:00
text.append (String.format (currencyFormat, nf.format (cell.getValue ())));
2016-02-04 00:31:44 +00:00
}
2016-03-06 04:41:15 +00:00
else if (format == '*')
{
String graphFormat = String.format ("%%-%d.%ds", colWidth, colWidth);
text.append (String.format (graphFormat, "********************"));
}
2015-06-01 09:35:51 +00:00
else
2016-02-04 00:31:44 +00:00
{
2016-03-06 04:41:15 +00:00
// this could be improved
String numberFormat = String.format ("%%%d.3f", colWidth + 4);
2016-03-05 21:53:29 +00:00
// System.out.printf ("Number format:%s%n", numberFormat);
2016-03-06 04:41:15 +00:00
String val = String.format (numberFormat, cell.getValue ());
while (val.endsWith ("0"))
val = ' ' + val.substring (0, val.length () - 1);
if (val.endsWith ("."))
val = ' ' + val.substring (0, val.length () - 1);
if (val.length () > colWidth)
val = val.substring (val.length () - colWidth);
text.append (val);
2016-02-04 00:31:44 +00:00
}
2015-06-01 09:35:51 +00:00
}
else
2016-02-04 00:31:44 +00:00
{
2016-03-05 21:53:29 +00:00
if (format == 'R')
{
String labelFormat = String.format ("%%%d.%ds", colWidth, colWidth);
// System.out.printf ("Label format:%s%n", labelFormat);
2016-03-09 10:38:53 +00:00
text.append (String.format (labelFormat, cell.getText ()));
2016-03-05 21:53:29 +00:00
}
else
{
String labelFormat = String.format ("%%-%d.%ds", colWidth, colWidth);
// System.out.printf ("Label format:%s%n", labelFormat);
2016-03-09 10:38:53 +00:00
text.append (String.format (labelFormat, cell.getText ()));
2016-03-05 21:53:29 +00:00
}
2016-02-04 00:31:44 +00:00
}
2015-06-01 09:35:51 +00:00
}
return text.toString ();
}
}