improved Range

This commit is contained in:
Denis Molony 2016-03-02 09:02:52 +11:00
parent adcd9cffda
commit 7c883cf5a1

View File

@ -2,12 +2,7 @@ package com.bytezone.diskbrowser.applefile;
import java.security.InvalidParameterException; import java.security.InvalidParameterException;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -21,7 +16,8 @@ public class VisicalcSpreadsheet implements Iterable<VisicalcCell>
private static final Pattern cellContents = private static final Pattern cellContents =
Pattern.compile ("([-+/*]?)(([A-Z]{1,2}[0-9]{1,3})|([0-9.]+)|(@[^-+/*]+))"); Pattern.compile ("([-+/*]?)(([A-Z]{1,2}[0-9]{1,3})|([0-9.]+)|(@[^-+/*]+))");
private static final Pattern functionPattern = Pattern private static final Pattern functionPattern = Pattern
.compile ("\\(([A-B]?[A-Z])([0-9]{1,3})\\.\\.\\.([A-B]?[A-Z])([0-9]{1,3})\\)"); .compile ("\\(([A-B]?[A-Z])([0-9]{1,3})\\.\\.\\.([A-B]?[A-Z])([0-9]{1,3})\\)?");
private static final Pattern addressList = Pattern.compile ("\\(([^,]+(,[^,]+)*)\\)");
private final Map<Integer, VisicalcCell> sheet = new TreeMap<Integer, VisicalcCell> (); private final Map<Integer, VisicalcCell> sheet = new TreeMap<Integer, VisicalcCell> ();
private final Map<String, Double> functions = new HashMap<String, Double> (); private final Map<String, Double> functions = new HashMap<String, Double> ();
@ -141,13 +137,17 @@ public class VisicalcSpreadsheet implements Iterable<VisicalcCell>
int ptr = 0; int ptr = 0;
int last = buffer.length - 1; int last = buffer.length - 1;
// System.out.println (HexFormatter.format (buffer));
while (buffer[last] == 0) while (buffer[last] == 0)
last--; last--;
while (ptr <= last) while (ptr <= last)
{ {
int endPtr = findEndPtr (buffer, ptr); int endPtr = findEndPtr (buffer, ptr);
add (HexFormatter.getString (buffer, ptr, endPtr - ptr)); String s = HexFormatter.getString (buffer, ptr, endPtr - ptr);
// System.out.println (s);
add (s);
ptr = endPtr + 1; ptr = endPtr + 1;
} }
@ -159,20 +159,23 @@ public class VisicalcSpreadsheet implements Iterable<VisicalcCell>
System.out.printf ("Width of column %3d: %d%n", entry.getKey (), entry.getValue ()); System.out.printf ("Width of column %3d: %d%n", entry.getKey (), entry.getValue ());
} }
public void add (String command) private void add (String command)
{ {
// [>K11:@SUM(J11...F11]
// System.out.printf ("Adding command [%s]%n", command);
lines.add (command); lines.add (command);
String data; String data;
if (command.startsWith (">")) // GOTO cell if (command.startsWith (">")) // GOTO cell
{ {
int pos = command.indexOf (':'); // end of cell address int pos = command.indexOf (':'); // end of cell address
Matcher m = addressPattern.matcher (command); Matcher m = addressPattern.matcher (command);
if (m.find ()) if (m.find ())
{ {
Address address = new Address (m.group (1), m.group (2)); Address address = new Address (m.group (1), m.group (2));
VisicalcCell cell = sheet.get (address.sortValue); VisicalcCell cell = sheet.get (address.sortValue);
command = command.substring (pos + 1); command = command.substring (pos + 1);
// System.out.printf ("%s %s%n", address, command);
if (cell == null) if (cell == null)
{ {
@ -187,7 +190,7 @@ public class VisicalcSpreadsheet implements Iterable<VisicalcCell>
System.out.printf ("Invalid cell address: %s%n", command); System.out.printf ("Invalid cell address: %s%n", command);
} }
if (command.startsWith ("/")) // command if (command.startsWith ("/")) // command
{ {
// System.out.printf ("Cmd: %s%n", command); // System.out.printf ("Cmd: %s%n", command);
data = command.substring (1); data = command.substring (1);
@ -277,6 +280,21 @@ public class VisicalcSpreadsheet implements Iterable<VisicalcCell>
double result = 0; double result = 0;
if (range == null)
{
m = addressList.matcher (function);
while (m.find ())
{
String[] cells = m.group (1).split (",");
range = new Range (cells);
}
if (range == null)
{
System.out.println ("null range : " + function);
return result;
}
}
if (function.startsWith ("@SUM")) if (function.startsWith ("@SUM"))
{ {
for (Address address : range) for (Address address : range)
@ -454,13 +472,13 @@ public class VisicalcSpreadsheet implements Iterable<VisicalcCell>
{ {
if (command.startsWith ("/")) if (command.startsWith ("/"))
{ {
if (command.charAt (1) == 'F') // format cell if (command.charAt (1) == 'F') // format cell
{ {
format = command.charAt (2); format = command.charAt (2);
if (command.length () > 3 && command.charAt (3) == '"') if (command.length () > 3 && command.charAt (3) == '"')
label = command.substring (4); label = command.substring (4);
} }
else if (command.charAt (1) == '-') // repeating label else if (command.charAt (1) == '-') // repeating label
{ {
repeatingChar = command.charAt (2); repeatingChar = command.charAt (2);
for (int i = 0; i < 20; i++) for (int i = 0; i < 20; i++)
@ -474,7 +492,10 @@ public class VisicalcSpreadsheet implements Iterable<VisicalcCell>
else if (command.matches ("^[0-9.]+$")) // contains only numbers or . else if (command.matches ("^[0-9.]+$")) // contains only numbers or .
this.value = Float.parseFloat (command); this.value = Float.parseFloat (command);
else else
{
formula = command; formula = command;
// System.out.printf ("Formula=[%s]%n", formula);
}
} }
public boolean hasValue () public boolean hasValue ()
@ -490,15 +511,19 @@ public class VisicalcSpreadsheet implements Iterable<VisicalcCell>
double result = 0.0; double result = 0.0;
double interim = 0.0; double interim = 0.0;
// System.out.printf ("In getValue with [%s]%n", formula);
Matcher m = cellContents.matcher (formula); Matcher m = cellContents.matcher (formula);
while (m.find ()) while (m.find ())
{ {
// for (int i = 0; i <= m.groupCount (); i++)
// System.out.printf ("%d %s%n", i, m.group (i));
valid = true; valid = true;
char operator = m.group (1).isEmpty () ? '+' : m.group (1).charAt (0); char operator = m.group (1).isEmpty () ? '+' : m.group (1).charAt (0);
if (m.group (3) != null) // address if (m.group (3) != null) // address
interim = parent.getValue (m.group (3)); interim = parent.getValue (m.group (3));
else if (m.group (4) != null) // constant else if (m.group (4) != null) // constant
try try
{ {
interim = Double.parseDouble (m.group (4)); interim = Double.parseDouble (m.group (4));
@ -598,9 +623,27 @@ public class VisicalcSpreadsheet implements Iterable<VisicalcCell>
throw new InvalidParameterException (); throw new InvalidParameterException ();
} }
public Range (String[] cells)
{
for (String s : cells)
{
Address address = new Address (s);
range.add (address);
}
}
@Override @Override
public String toString () public String toString ()
{ {
if (from == null || to == null)
{
StringBuilder text = new StringBuilder ();
for (Address address : range)
text.append (address.text + ",");
if (text.length () > 0)
text.deleteCharAt (text.length () - 1);
return text.toString ();
}
return String.format (" %s -> %s", from.text, to.text); return String.format (" %s -> %s", from.text, to.text);
} }