Debian Codegraph @

arduino-core/src/processing/app/debug/Sizer.java

raw symbols SCIP
1 /* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2
3 /*
4 Sizer - computes the size of a .hex file
5 Part of the Arduino project - http://www.arduino.cc/
6
7 Copyright (c) 2006 David A. Mellis
8 Copyright (c) 2011 Cristian Maglie <[email protected]>
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software Foundation,
22 Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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 // The default Throwable.toString() never returns null, but apparently
84 // some sub-class has overridden it to do so, thus we need to check for
85 // it. See: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1166589459
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 }