Skip to content

Commit e934b72

Browse files
committed
ported changes from PApplet
1 parent 01ac8a9 commit e934b72

File tree

1 file changed

+157
-35
lines changed

1 file changed

+157
-35
lines changed

core/src/processing/core/PApplet.java

Lines changed: 157 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.io.*;
2828
import java.lang.reflect.*;
2929
import java.net.*;
30+
import java.nio.charset.StandardCharsets;
3031
import java.text.NumberFormat;
3132
import java.util.*;
3233
import java.util.regex.*;
@@ -4062,31 +4063,6 @@ public void run() {
40624063

40634064

40644065

4065-
//////////////////////////////////////////////////////////////
4066-
4067-
// EXTENSIONS
4068-
4069-
4070-
/**
4071-
* Get the compression-free extension for this filename.
4072-
* @param filename The filename to check
4073-
* @return an extension, skipping past .gz if it's present
4074-
*/
4075-
static public String checkExtension(String filename) {
4076-
// Don't consider the .gz as part of the name, createInput()
4077-
// and createOuput() will take care of fixing that up.
4078-
if (filename.toLowerCase().endsWith(".gz")) {
4079-
filename = filename.substring(0, filename.length() - 3);
4080-
}
4081-
int dotIndex = filename.lastIndexOf('.');
4082-
if (dotIndex != -1) {
4083-
return filename.substring(dotIndex + 1).toLowerCase();
4084-
}
4085-
return null;
4086-
}
4087-
4088-
4089-
40904066
//////////////////////////////////////////////////////////////
40914067

40924068
// DATA I/O
@@ -4507,6 +4483,156 @@ public PFont createFont(String name, float size,
45074483
// }
45084484

45094485

4486+
//////////////////////////////////////////////////////////////
4487+
4488+
// LISTING DIRECTORIES
4489+
4490+
4491+
public String[] listPaths(String path, String... options) {
4492+
File[] list = listFiles(path, options);
4493+
4494+
int offset = 0;
4495+
for (String opt : options) {
4496+
if (opt.equals("relative")) {
4497+
if (!path.endsWith(File.pathSeparator)) {
4498+
path += File.pathSeparator;
4499+
}
4500+
offset = path.length();
4501+
break;
4502+
}
4503+
}
4504+
String[] outgoing = new String[list.length];
4505+
for (int i = 0; i < list.length; i++) {
4506+
// as of Java 1.8, substring(0) returns the original object
4507+
outgoing[i] = list[i].getAbsolutePath().substring(offset);
4508+
}
4509+
return outgoing;
4510+
}
4511+
4512+
4513+
public File[] listFiles(String path, String... options) {
4514+
File file = new File(path);
4515+
// if not an absolute path, make it relative to the sketch folder
4516+
if (!file.isAbsolute()) {
4517+
file = sketchFile(path);
4518+
}
4519+
return listFiles(file, options);
4520+
}
4521+
4522+
4523+
// "relative" -> no effect with the Files version, but important for listPaths
4524+
// "recursive"
4525+
// "extension=js" or "extensions=js|csv|txt" (no dot)
4526+
// "directories" -> only directories
4527+
// "files" -> only files
4528+
// "hidden" -> include hidden files (prefixed with .) disabled by default
4529+
static public File[] listFiles(File base, String... options) {
4530+
boolean recursive = false;
4531+
String[] extensions = null;
4532+
boolean directories = true;
4533+
boolean files = true;
4534+
boolean hidden = false;
4535+
4536+
for (String opt : options) {
4537+
if (opt.equals("recursive")) {
4538+
recursive = true;
4539+
} else if (opt.startsWith("extension=")) {
4540+
extensions = new String[] { opt.substring(10) };
4541+
} else if (opt.startsWith("extensions=")) {
4542+
extensions = split(opt.substring(10), ',');
4543+
} else if (opt.equals("files")) {
4544+
directories = false;
4545+
} else if (opt.equals("directories")) {
4546+
files = false;
4547+
} else if (opt.equals("hidden")) {
4548+
hidden = true;
4549+
} else if (opt.equals("relative")) {
4550+
// ignored
4551+
} else {
4552+
throw new RuntimeException(opt + " is not a listFiles() option");
4553+
}
4554+
}
4555+
4556+
if (extensions != null) {
4557+
for (int i = 0; i < extensions.length; i++) {
4558+
extensions[i] = "." + extensions[i];
4559+
}
4560+
}
4561+
4562+
if (!files && !directories) {
4563+
// just make "only files" and "only directories" mean... both
4564+
files = true;
4565+
directories = true;
4566+
}
4567+
4568+
if (!base.canRead()) {
4569+
return null;
4570+
}
4571+
4572+
List<File> outgoing = new ArrayList<>();
4573+
listFilesImpl(base, recursive, extensions, hidden, directories, files, outgoing);
4574+
return outgoing.toArray(new File[0]);
4575+
}
4576+
4577+
4578+
static void listFilesImpl(File folder, boolean recursive,
4579+
String[] extensions, boolean hidden,
4580+
boolean directories, boolean files,
4581+
List<File> list) {
4582+
File[] items = folder.listFiles();
4583+
if (items != null) {
4584+
for (File item : items) {
4585+
String name = item.getName();
4586+
if (!hidden && name.charAt(0) == '.') {
4587+
continue;
4588+
}
4589+
if (item.isDirectory()) {
4590+
if (recursive) {
4591+
listFilesImpl(item, recursive, extensions, hidden, directories, files, list);
4592+
}
4593+
if (directories) {
4594+
list.add(item);
4595+
}
4596+
} else if (files) {
4597+
if (extensions == null) {
4598+
list.add(item);
4599+
} else {
4600+
for (String ext : extensions) {
4601+
if (item.getName().toLowerCase().endsWith(ext)) {
4602+
list.add(item);
4603+
}
4604+
}
4605+
}
4606+
}
4607+
}
4608+
}
4609+
}
4610+
4611+
4612+
4613+
//////////////////////////////////////////////////////////////
4614+
4615+
// EXTENSIONS
4616+
4617+
4618+
/**
4619+
* Get the compression-free extension for this filename.
4620+
* @param filename The filename to check
4621+
* @return an extension, skipping past .gz if it's present
4622+
*/
4623+
static public String checkExtension(String filename) {
4624+
// Don't consider the .gz as part of the name, createInput()
4625+
// and createOuput() will take care of fixing that up.
4626+
if (filename.toLowerCase().endsWith(".gz")) {
4627+
filename = filename.substring(0, filename.length() - 3);
4628+
}
4629+
int dotIndex = filename.lastIndexOf('.');
4630+
if (dotIndex != -1) {
4631+
return filename.substring(dotIndex + 1).toLowerCase();
4632+
}
4633+
return null;
4634+
}
4635+
45104636

45114637
//////////////////////////////////////////////////////////////
45124638

@@ -4566,10 +4692,8 @@ static public BufferedReader createReader(File file) {
45664692
* following lines any more I'm gonna send Sun my medical bills.
45674693
*/
45684694
static public BufferedReader createReader(InputStream input) {
4569-
InputStreamReader isr = null;
4570-
try {
4571-
isr = new InputStreamReader(input, "UTF-8");
4572-
} catch (UnsupportedEncodingException e) { } // not gonna happen
4695+
InputStreamReader isr =
4696+
new InputStreamReader(input, StandardCharsets.UTF_8);
45734697
return new BufferedReader(isr);
45744698
}
45754699

@@ -4612,12 +4736,10 @@ static public PrintWriter createWriter(File file) {
46124736
* It's the JavaSoft API engineers who need to explain themselves.
46134737
*/
46144738
static public PrintWriter createWriter(OutputStream output) {
4615-
try {
4616-
BufferedOutputStream bos = new BufferedOutputStream(output, 8192);
4617-
OutputStreamWriter osw = new OutputStreamWriter(bos, "UTF-8");
4618-
return new PrintWriter(osw);
4619-
} catch (UnsupportedEncodingException e) { } // not gonna happen
4620-
return null;
4739+
BufferedOutputStream bos = new BufferedOutputStream(output, 8192);
4740+
OutputStreamWriter osw =
4741+
new OutputStreamWriter(bos, StandardCharsets.UTF_8);
4742+
return new PrintWriter(osw);
46214743
}
46224744

46234745

0 commit comments

Comments
 (0)