Skip to content

Commit 8bece7c

Browse files
Simplify Sketch.load
By using FileUtils.listFiles, this function can be greatly simplified, without changing functionality in any way.
1 parent 83d0a5e commit 8bece7c

File tree

1 file changed

+9
-39
lines changed

1 file changed

+9
-39
lines changed

app/src/processing/app/Sketch.java

+9-39
Original file line numberDiff line numberDiff line change
@@ -161,50 +161,20 @@ protected void load() throws IOException {
161161
codeFolder = new File(folder, "code");
162162
dataFolder = new File(folder, "data");
163163

164-
// get list of files in the sketch folder
165-
String list[] = folder.list();
166-
167-
// reset these because load() may be called after an
168-
// external editor event. (fix for 0099)
169-
codeCount = 0;
170-
171-
code = new SketchCode[list.length];
172-
173-
List<String> extensions = getExtensions();
174-
175-
for (String filename : list) {
176-
// Ignoring the dot prefix files is especially important to avoid files
177-
// with the ._ prefix on Mac OS X. (You'll see this with Mac files on
178-
// non-HFS drives, i.e. a thumb drive formatted FAT32.)
179-
if (filename.startsWith(".")) continue;
180-
181-
// Don't let some wacko name a directory blah.pde or bling.java.
182-
if (new File(folder, filename).isDirectory()) continue;
183-
184-
// figure out the name without any extension
185-
String base = filename;
186-
// now strip off the .pde and .java extensions
187-
for (String extension : extensions) {
188-
if (base.toLowerCase().endsWith("." + extension)) {
189-
base = base.substring(0, base.length() - (extension.length() + 1));
190-
191-
// Don't allow people to use files with invalid names, since on load,
192-
// it would be otherwise possible to sneak in nasty filenames. [0116]
193-
if (Sketch.isSanitaryName(base)) {
194-
code[codeCount++] =
195-
new SketchCode(new File(folder, filename));
196-
} else {
197-
editor.console.message(I18n.format("File name {0} is invalid: ignored", filename), true, false);
198-
}
199-
}
164+
List<SketchCode> tmpcode = new ArrayList<SketchCode>();
165+
for (File file : FileUtils.listFiles(folder, false, getExtensions())) {
166+
if (Sketch.isSanitaryName(file.getName())) {
167+
tmpcode.add(new SketchCode(file));
168+
} else {
169+
editor.console.message(I18n.format("File name {0} is invalid: ignored", file.getName()), true, false);
200170
}
201171
}
202172

203-
if (codeCount == 0)
173+
if (tmpcode.isEmpty())
204174
throw new IOException(_("No valid code files found"));
205175

206-
// Remove any code that wasn't proper
207-
code = (SketchCode[]) PApplet.subset(code, 0, codeCount);
176+
codeCount = tmpcode.size();
177+
code = tmpcode.toArray(new SketchCode[codeCount]);
208178

209179
// move the main class to the first tab
210180
// start at 1, if it's at zero, don't bother

0 commit comments

Comments
 (0)