| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
package processing.app.debug; |
| 26 |
|
| 27 |
import java.util.regex.Matcher; |
| 28 |
import java.util.regex.Pattern; |
| 29 |
|
| 30 |
import processing.app.helpers.PreferencesMap; |
| 31 |
import processing.app.helpers.ProcessUtils; |
| 32 |
import processing.app.helpers.StringReplacer; |
| 33 |
|
| 34 |
public class Sizer implements MessageConsumer { |
| 35 |
private long textSize; |
| 36 |
private long dataSize; |
| 37 |
private long eepromSize; |
| 38 |
private RunnerException exception; |
| 39 |
private PreferencesMap prefs; |
| 40 |
private String firstLine; |
| 41 |
private Pattern textPattern; |
| 42 |
private Pattern dataPattern; |
| 43 |
private Pattern eepromPattern; |
| 44 |
|
| 45 |
public Sizer(PreferencesMap _prefs) { |
| 46 |
prefs = _prefs; |
| 47 |
textPattern = Pattern.compile(prefs.get("recipe.size.regex")); |
| 48 |
dataPattern = null; |
| 49 |
String pref = prefs.get("recipe.size.regex.data"); |
| 50 |
if (pref != null) |
| 51 |
dataPattern = Pattern.compile(pref); |
| 52 |
eepromPattern = null; |
| 53 |
pref = prefs.get("recipe.size.regex.eeprom"); |
| 54 |
if (pref != null) |
| 55 |
eepromPattern = Pattern.compile(pref); |
| 56 |
} |
| 57 |
|
| 58 |
public long[] computeSize() throws RunnerException { |
| 59 |
|
| 60 |
int r = 0; |
| 61 |
try { |
| 62 |
String pattern = prefs.get("recipe.size.pattern"); |
| 63 |
String cmd[] = StringReplacer.formatAndSplit(pattern, prefs); |
| 64 |
|
| 65 |
exception = null; |
| 66 |
textSize = -1; |
| 67 |
dataSize = -1; |
| 68 |
eepromSize = -1; |
| 69 |
Process process = ProcessUtils.exec(cmd); |
| 70 |
MessageSiphon in = new MessageSiphon(process.getInputStream(), this); |
| 71 |
MessageSiphon err = new MessageSiphon(process.getErrorStream(), this); |
| 72 |
|
| 73 |
boolean running = true; |
| 74 |
while(running) { |
| 75 |
try { |
| 76 |
in.join(); |
| 77 |
err.join(); |
| 78 |
r = process.waitFor(); |
| 79 |
running = false; |
| 80 |
} catch (InterruptedException intExc) { } |
| 81 |
} |
| 82 |
} catch (Exception e) { |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
exception = new RunnerException( |
| 87 |
(e.toString() == null) ? e.getClass().getName() + r : e.toString() + r); |
| 88 |
} |
| 89 |
|
| 90 |
if (exception != null) |
| 91 |
throw exception; |
| 92 |
|
| 93 |
if (textSize == -1) |
| 94 |
throw new RunnerException(firstLine); |
| 95 |
|
| 96 |
return new long[] { textSize, dataSize, eepromSize }; |
| 97 |
} |
| 98 |
|
| 99 |
@Override |
| 100 |
public void message(String s) { |
| 101 |
if (firstLine == null) |
| 102 |
firstLine = s; |
| 103 |
Matcher textMatcher = textPattern.matcher(s.trim()); |
| 104 |
if (textMatcher.matches()) { |
| 105 |
if (textSize < 0) |
| 106 |
textSize = 0; |
| 107 |
textSize += Long.parseLong(textMatcher.group(1)); |
| 108 |
} |
| 109 |
if(dataPattern != null) { |
| 110 |
Matcher dataMatcher = dataPattern.matcher(s.trim()); |
| 111 |
if (dataMatcher.matches()) { |
| 112 |
if (dataSize < 0) |
| 113 |
dataSize = 0; |
| 114 |
dataSize += Long.parseLong(dataMatcher.group(1)); |
| 115 |
} |
| 116 |
} |
| 117 |
if(eepromPattern != null) { |
| 118 |
Matcher eepromMatcher = eepromPattern.matcher(s.trim()); |
| 119 |
if (eepromMatcher.matches()) { |
| 120 |
if (eepromSize < 0) |
| 121 |
eepromSize = 0; |
| 122 |
eepromSize += Long.parseLong(eepromMatcher.group(1)); |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
} |