Skip to content

Commit 8238ad2

Browse files
committed
rewrite Util.listFiles() because it wasn't working properly
1 parent ddeff12 commit 8238ad2

2 files changed

Lines changed: 38 additions & 44 deletions

File tree

app/src/processing/app/Util.java

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.io.*;
2626
import java.nio.file.Files;
2727
import java.util.Enumeration;
28-
import java.util.Vector;
2928
import java.util.zip.*;
3029

3130
import processing.core.PApplet;
@@ -379,61 +378,55 @@ static public long calcFolderSize(File folder) {
379378
}
380379

381380

382-
// /**
383-
// * Recursively creates a list of all files within the specified folder,
384-
// * and returns a list of their relative paths.
385-
// * Ignores any files/folders prefixed with a dot.
386-
// */
387-
// static public String[] listFiles(String path, boolean relative) {
388-
// return listFiles(new File(path), relative);
389-
// }
390-
391-
381+
/**
382+
* Recursively creates a list of all files within the specified folder,
383+
* and returns a list of their relative paths.
384+
* Ignores any files/folders prefixed with a dot.
385+
* @param relative true return relative paths instead of absolute paths
386+
*/
392387
static public String[] listFiles(File folder, boolean relative) {
393-
String path = folder.getAbsolutePath();
394-
Vector<String> vector = new Vector<String>();
395-
listFiles(relative ? (path + File.separator) : "", path, null, vector);
396-
String outgoing[] = new String[vector.size()];
397-
vector.copyInto(outgoing);
398-
return outgoing;
388+
return listFiles(folder, relative, null);
399389
}
400390

401391

402-
static public String[] listFiles(File folder, boolean relative, String extension) {
403-
String path = folder.getAbsolutePath();
404-
Vector<String> vector = new Vector<String>();
392+
static public String[] listFiles(File folder, boolean relative,
393+
String extension) {
405394
if (extension != null) {
406395
if (!extension.startsWith(".")) {
407396
extension = "." + extension;
408397
}
409398
}
410-
listFiles(relative ? (path + File.separator) : "", path, extension, vector);
411-
String outgoing[] = new String[vector.size()];
412-
vector.copyInto(outgoing);
413-
return outgoing;
399+
400+
StringList list = new StringList();
401+
listFilesImpl(folder, relative, extension, list);
402+
403+
if (relative) {
404+
String[] outgoing = new String[list.size()];
405+
// remove the slash (or backslash) as well
406+
int prefixLength = folder.getAbsolutePath().length() + 1;
407+
for (int i = 0; i < outgoing.length; i++) {
408+
outgoing[i] = list.get(i).substring(prefixLength);
409+
}
410+
return outgoing;
411+
}
412+
return list.array();
414413
}
415414

416415

417-
static protected void listFiles(String basePath,
418-
String path, String extension,
419-
Vector<String> vector) {
420-
File folder = new File(path);
421-
String[] list = folder.list();
422-
if (list != null) {
423-
for (String item : list) {
424-
if (item.charAt(0) == '.') continue;
425-
if (extension == null || item.toLowerCase().endsWith(extension)) {
426-
File file = new File(path, item);
427-
String newPath = file.getAbsolutePath();
428-
if (newPath.startsWith(basePath)) {
429-
newPath = newPath.substring(basePath.length());
430-
}
431-
// only add if no ext or match
432-
if (extension == null || item.toLowerCase().endsWith(extension)) {
433-
vector.add(newPath);
434-
}
435-
if (file.isDirectory()) { // use absolute path
436-
listFiles(basePath, file.getAbsolutePath(), extension, vector);
416+
static void listFilesImpl(File folder, boolean relative,
417+
String extension, StringList list) {
418+
File[] items = folder.listFiles();
419+
if (items != null) {
420+
for (File item : items) {
421+
String name = item.getName();
422+
if (name.charAt(0) != '.') {
423+
if (item.isDirectory()) {
424+
listFilesImpl(item, relative, extension, list);
425+
426+
} else { // a file
427+
if (extension == null || name.endsWith(extension)) {
428+
list.append(item.getAbsolutePath());
429+
}
437430
}
438431
}
439432
}

todo.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ X https://github.com/processing/processing/issues/4128
1010
X https://github.com/processing/processing/issues/4503
1111
X see if CLASSPATH can be set to screw up p5
1212
X works fine on OS X, couldn't reproduce on Linux
13+
X rewrite Util.listFiles() because it wasn't working recursively
1314

1415
mode work
1516
X add template support for Modes

0 commit comments

Comments
 (0)