mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-19 09:42:11 +03:00
Split IDE into 2 projects.
BEWARE: HIGHLY EXPERIMENTAL BRANCH
This commit is contained in:
@ -0,0 +1,38 @@
|
||||
package processing.app.helpers;
|
||||
|
||||
import static processing.app.I18n._;
|
||||
|
||||
public class BasicUserNotifier extends UserNotifier {
|
||||
|
||||
/**
|
||||
* Show an error message that's actually fatal to the program.
|
||||
* This is an error that can't be recovered. Use showWarning()
|
||||
* for errors that allow P5 to continue running.
|
||||
*/
|
||||
public void showError(String title, String message, Throwable e, int exit_code) {
|
||||
if (title == null) title = _("Error");
|
||||
|
||||
System.err.println(title + ": " + message);
|
||||
|
||||
if (e != null) e.printStackTrace();
|
||||
System.exit(exit_code);
|
||||
}
|
||||
|
||||
public void showMessage(String title, String message) {
|
||||
if (title == null) title = _("Message");
|
||||
|
||||
System.out.println(title + ": " + message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Non-fatal error message with optional stack trace side dish.
|
||||
*/
|
||||
public void showWarning(String title, String message, Exception e) {
|
||||
if (title == null) title = _("Warning");
|
||||
|
||||
System.out.println(title + ": " + message);
|
||||
|
||||
if (e != null) e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
283
arduino-core/src/processing/app/helpers/CommandlineParser.java
Normal file
283
arduino-core/src/processing/app/helpers/CommandlineParser.java
Normal file
@ -0,0 +1,283 @@
|
||||
package processing.app.helpers;
|
||||
|
||||
import static processing.app.I18n._;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import processing.app.BaseNoGui;
|
||||
import processing.app.I18n;
|
||||
import processing.app.PreferencesData;
|
||||
import processing.app.debug.TargetBoard;
|
||||
import processing.app.debug.TargetPackage;
|
||||
import processing.app.debug.TargetPlatform;
|
||||
import processing.app.legacy.PApplet;
|
||||
|
||||
public class CommandlineParser {
|
||||
|
||||
protected static enum ACTION { GUI, NOOP, VERIFY, UPLOAD, GET_PREF };
|
||||
|
||||
private ACTION action = ACTION.GUI;
|
||||
private boolean doVerboseBuild = false;
|
||||
private boolean doVerboseUpload = false;
|
||||
private boolean doUseProgrammer = false;
|
||||
private boolean noUploadPort = false;
|
||||
private boolean forceSavePrefs = false;
|
||||
private String getPref = null;
|
||||
private List<String> filenames = new LinkedList<String>();
|
||||
|
||||
public static CommandlineParser newCommandlineParser(String[] args) {
|
||||
return new CommandlineParser(args);
|
||||
}
|
||||
|
||||
private CommandlineParser(String[] args) {
|
||||
parseArguments(args);
|
||||
checkAction();
|
||||
}
|
||||
|
||||
private void parseArguments(String[] args) {
|
||||
// Map of possible actions and corresponding options
|
||||
final Map<String, ACTION> actions = new HashMap<String, ACTION>();
|
||||
actions.put("--verify", ACTION.VERIFY);
|
||||
actions.put("--upload", ACTION.UPLOAD);
|
||||
actions.put("--get-pref", ACTION.GET_PREF);
|
||||
|
||||
// Check if any files were passed in on the command line
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
ACTION a = actions.get(args[i]);
|
||||
if (a != null) {
|
||||
if (action != ACTION.GUI && action != ACTION.NOOP) {
|
||||
String[] valid = actions.keySet().toArray(new String[0]);
|
||||
String mess = I18n.format(_("Can only pass one of: {0}"), PApplet.join(valid, ", "));
|
||||
BaseNoGui.showError(null, mess, 3);
|
||||
}
|
||||
if (a == ACTION.GET_PREF) {
|
||||
i++;
|
||||
if (i >= args.length)
|
||||
BaseNoGui.showError(null, _("Argument required for --get-pref"), 3);
|
||||
getPref = args[i];
|
||||
}
|
||||
action = a;
|
||||
continue;
|
||||
}
|
||||
if (args[i].equals("--verbose") || args[i].equals("-v")) {
|
||||
doVerboseBuild = true;
|
||||
doVerboseUpload = true;
|
||||
if (action == ACTION.GUI)
|
||||
action = ACTION.NOOP;
|
||||
continue;
|
||||
}
|
||||
if (args[i].equals("--verbose-build")) {
|
||||
doVerboseBuild = true;
|
||||
if (action == ACTION.GUI)
|
||||
action = ACTION.NOOP;
|
||||
continue;
|
||||
}
|
||||
if (args[i].equals("--verbose-upload")) {
|
||||
doVerboseUpload = true;
|
||||
if (action == ACTION.GUI)
|
||||
action = ACTION.NOOP;
|
||||
continue;
|
||||
}
|
||||
if (args[i].equals("--useprogrammer")) {
|
||||
doUseProgrammer = true;
|
||||
if (action == ACTION.GUI)
|
||||
action = ACTION.NOOP;
|
||||
continue;
|
||||
}
|
||||
if (args[i].equals("--nouploadport")) {
|
||||
noUploadPort = true;
|
||||
if (action == ACTION.GUI)
|
||||
action = ACTION.NOOP;
|
||||
continue;
|
||||
}
|
||||
if (args[i].equals("--board")) {
|
||||
i++;
|
||||
if (i >= args.length)
|
||||
BaseNoGui.showError(null, _("Argument required for --board"), 3);
|
||||
processBoardArgument(args[i]);
|
||||
if (action == ACTION.GUI)
|
||||
action = ACTION.NOOP;
|
||||
continue;
|
||||
}
|
||||
if (args[i].equals("--port")) {
|
||||
i++;
|
||||
if (i >= args.length)
|
||||
BaseNoGui.showError(null, _("Argument required for --port"), 3);
|
||||
BaseNoGui.selectSerialPort(args[i]);
|
||||
if (action == ACTION.GUI)
|
||||
action = ACTION.NOOP;
|
||||
continue;
|
||||
}
|
||||
if (args[i].equals("--curdir")) {
|
||||
i++;
|
||||
if (i >= args.length)
|
||||
BaseNoGui.showError(null, _("Argument required for --curdir"), 3);
|
||||
// Argument should be already processed by Base.main(...)
|
||||
continue;
|
||||
}
|
||||
if (args[i].equals("--buildpath")) {
|
||||
i++;
|
||||
if (i >= args.length) {
|
||||
BaseNoGui.showError(null, "Argument required for --buildpath", 3);
|
||||
}
|
||||
File buildFolder = new File(args[i]);
|
||||
if (!buildFolder.exists()) {
|
||||
BaseNoGui.showError(null, "The build path doesn't exist", 3);
|
||||
}
|
||||
if (!buildFolder.isDirectory()) {
|
||||
BaseNoGui.showError(null, "The build path is not a folder", 3);
|
||||
}
|
||||
BaseNoGui.setBuildFolder(buildFolder);
|
||||
continue;
|
||||
}
|
||||
if (args[i].equals("--pref")) {
|
||||
i++;
|
||||
if (i >= args.length)
|
||||
BaseNoGui.showError(null, _("Argument required for --pref"), 3);
|
||||
processPrefArgument(args[i]);
|
||||
if (action == ACTION.GUI)
|
||||
action = ACTION.NOOP;
|
||||
continue;
|
||||
}
|
||||
if (args[i].equals("--save-prefs")) {
|
||||
forceSavePrefs = true;
|
||||
continue;
|
||||
}
|
||||
if (args[i].equals("--preferences-file")) {
|
||||
i++;
|
||||
if (i >= args.length)
|
||||
BaseNoGui.showError(null, _("Argument required for --preferences-file"), 3);
|
||||
// Argument should be already processed by Base.main(...)
|
||||
continue;
|
||||
}
|
||||
if (args[i].startsWith("--"))
|
||||
BaseNoGui.showError(null, I18n.format(_("unknown option: {0}"), args[i]), 3);
|
||||
|
||||
filenames.add(args[i]);
|
||||
}
|
||||
}
|
||||
|
||||
private void checkAction() {
|
||||
if ((action == ACTION.UPLOAD || action == ACTION.VERIFY) && filenames.size() != 1)
|
||||
BaseNoGui.showError(null, _("Must specify exactly one sketch file"), 3);
|
||||
|
||||
if ((action == ACTION.NOOP || action == ACTION.GET_PREF) && filenames.size() != 0)
|
||||
BaseNoGui.showError(null, _("Cannot specify any sketch files"), 3);
|
||||
|
||||
if ((action != ACTION.UPLOAD && action != ACTION.VERIFY) && (doVerboseBuild || doVerboseUpload))
|
||||
BaseNoGui.showError(null, _("--verbose, --verbose-upload and --verbose-build can only be used together with --verify or --upload"), 3);
|
||||
}
|
||||
|
||||
private void processBoardArgument(String selectBoard) {
|
||||
// No board selected? Nothing to do
|
||||
if (selectBoard == null)
|
||||
return;
|
||||
|
||||
String[] split = selectBoard.split(":", 4);
|
||||
|
||||
if (split.length < 3) {
|
||||
BaseNoGui.showError(null, I18n.format(_("{0}: Invalid board name, it should be of the form \"package:arch:board\" or \"package:arch:board:options\""), selectBoard), 3);
|
||||
}
|
||||
|
||||
TargetPackage targetPackage = BaseNoGui.getTargetPackage(split[0]);
|
||||
if (targetPackage == null) {
|
||||
BaseNoGui.showError(null, I18n.format(_("{0}: Unknown package"), split[0]), 3);
|
||||
}
|
||||
|
||||
TargetPlatform targetPlatform = targetPackage.get(split[1]);
|
||||
if (targetPlatform == null) {
|
||||
BaseNoGui.showError(null, I18n.format(_("{0}: Unknown architecture"), split[1]), 3);
|
||||
}
|
||||
|
||||
TargetBoard targetBoard = targetPlatform.getBoard(split[2]);
|
||||
if (targetBoard == null) {
|
||||
BaseNoGui.showError(null, I18n.format(_("{0}: Unknown board"), split[2]), 3);
|
||||
}
|
||||
|
||||
BaseNoGui.selectBoard(targetBoard);
|
||||
|
||||
if (split.length > 3) {
|
||||
String[] options = split[3].split(",");
|
||||
for (String option : options) {
|
||||
String[] keyValue = option.split("=", 2);
|
||||
|
||||
if (keyValue.length != 2)
|
||||
BaseNoGui.showError(null, I18n.format(_("{0}: Invalid option, should be of the form \"name=value\""), option, targetBoard.getId()), 3);
|
||||
String key = keyValue[0].trim();
|
||||
String value = keyValue[1].trim();
|
||||
|
||||
if (!targetBoard.hasMenu(key))
|
||||
BaseNoGui.showError(null, I18n.format(_("{0}: Invalid option for board \"{1}\""), key, targetBoard.getId()), 3);
|
||||
if (targetBoard.getMenuLabel(key, value) == null)
|
||||
BaseNoGui.showError(null, I18n.format(_("{0}: Invalid option for \"{1}\" option for board \"{2}\""), value, key, targetBoard.getId()), 3);
|
||||
|
||||
PreferencesData.set("custom_" + key, targetBoard.getId() + "_" + value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void processPrefArgument(String arg) {
|
||||
String[] split = arg.split("=", 2);
|
||||
if (split.length != 2 || split[0].isEmpty())
|
||||
BaseNoGui.showError(null, I18n.format(_("{0}: Invalid argument to --pref, should be of the form \"pref=value\""), arg), 3);
|
||||
|
||||
PreferencesData.set(split[0], split[1]);
|
||||
}
|
||||
|
||||
public boolean isDoVerboseBuild() {
|
||||
return doVerboseBuild;
|
||||
}
|
||||
|
||||
public boolean isDoVerboseUpload() {
|
||||
return doVerboseUpload;
|
||||
}
|
||||
|
||||
public boolean isForceSavePrefs() {
|
||||
return forceSavePrefs;
|
||||
}
|
||||
|
||||
public String getGetPref() {
|
||||
return getPref;
|
||||
}
|
||||
|
||||
public List<String> getFilenames() {
|
||||
return filenames;
|
||||
}
|
||||
|
||||
public boolean isGetPrefMode() {
|
||||
return action == ACTION.GET_PREF;
|
||||
}
|
||||
|
||||
public boolean isGuiMode() {
|
||||
return action == ACTION.GUI;
|
||||
}
|
||||
|
||||
public boolean isNoOpMode() {
|
||||
return action == ACTION.NOOP;
|
||||
}
|
||||
|
||||
public boolean isUploadMode() {
|
||||
return action == ACTION.UPLOAD;
|
||||
}
|
||||
|
||||
public boolean isVerifyMode() {
|
||||
return action == ACTION.VERIFY;
|
||||
}
|
||||
|
||||
public boolean isVerifyOrUploadMode() {
|
||||
return isVerifyMode() || isUploadMode();
|
||||
}
|
||||
|
||||
public boolean isDoUseProgrammer() {
|
||||
return doUseProgrammer;
|
||||
}
|
||||
|
||||
public boolean isNoUploadPort() {
|
||||
return noUploadPort;
|
||||
}
|
||||
|
||||
}
|
259
arduino-core/src/processing/app/helpers/FileUtils.java
Normal file
259
arduino-core/src/processing/app/helpers/FileUtils.java
Normal file
@ -0,0 +1,259 @@
|
||||
package processing.app.helpers;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class FileUtils {
|
||||
|
||||
private static final List<String> SOURCE_CONTROL_FOLDERS = Arrays.asList("CVS", "RCS", ".git", ".svn", ".hg", ".bzr");
|
||||
private static final Pattern BACKSLASH = Pattern.compile("\\\\");
|
||||
|
||||
/**
|
||||
* Checks, whether the child directory is a subdirectory of the base directory.
|
||||
*
|
||||
* @param base the base directory.
|
||||
* @param child the suspected child directory.
|
||||
* @return true, if the child is a subdirectory of the base directory.
|
||||
*/
|
||||
public static boolean isSubDirectory(File base, File child) {
|
||||
try {
|
||||
base = base.getCanonicalFile();
|
||||
child = child.getCanonicalFile();
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
File parentFile = child;
|
||||
while (parentFile != null) {
|
||||
if (base.equals(parentFile)) {
|
||||
return true;
|
||||
}
|
||||
parentFile = parentFile.getParentFile();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void copyFile(File source, File dest) throws IOException {
|
||||
FileInputStream fis = null;
|
||||
FileOutputStream fos = null;
|
||||
try {
|
||||
fis = new FileInputStream(source);
|
||||
fos = new FileOutputStream(dest);
|
||||
byte[] buf = new byte[4096];
|
||||
int readBytes = -1;
|
||||
while ((readBytes = fis.read(buf, 0, buf.length)) != -1) {
|
||||
fos.write(buf, 0, readBytes);
|
||||
}
|
||||
} finally {
|
||||
if (fis != null) {
|
||||
fis.close();
|
||||
}
|
||||
if (fos != null) {
|
||||
fos.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void copy(File sourceFolder, File destFolder) throws IOException {
|
||||
for (File file : sourceFolder.listFiles()) {
|
||||
File destFile = new File(destFolder, file.getName());
|
||||
if (file.isDirectory()) {
|
||||
if (!destFile.mkdir()) {
|
||||
throw new IOException("Unable to create folder: " + destFile);
|
||||
}
|
||||
copy(file, destFile);
|
||||
} else {
|
||||
copyFile(file, destFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void recursiveDelete(File file) {
|
||||
if (file == null) {
|
||||
return;
|
||||
}
|
||||
if (file.isDirectory()) {
|
||||
for (File current : file.listFiles()) {
|
||||
if (current.isDirectory()) {
|
||||
recursiveDelete(current);
|
||||
} else {
|
||||
current.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
file.delete();
|
||||
}
|
||||
|
||||
public static File createTempFolder() throws IOException {
|
||||
File tmpFolder = new File(System.getProperty("java.io.tmpdir"), "arduino_" + new Random().nextInt(1000000));
|
||||
if (!tmpFolder.mkdir()) {
|
||||
throw new IOException("Unable to create temp folder " + tmpFolder);
|
||||
}
|
||||
return tmpFolder;
|
||||
}
|
||||
|
||||
//
|
||||
// Compute relative path to "target" from a directory "origin".
|
||||
//
|
||||
// If "origin" is not absolute, it is relative from the current directory.
|
||||
// If "target" is not absolute, it is relative from "origin".
|
||||
//
|
||||
// by Shigeru KANEMOTO at SWITCHSCIENCE.
|
||||
//
|
||||
public static String relativePath(String origin, String target) {
|
||||
try {
|
||||
origin = (new File(origin)).getCanonicalPath();
|
||||
File targetFile = new File(target);
|
||||
if (targetFile.isAbsolute())
|
||||
target = targetFile.getCanonicalPath();
|
||||
else
|
||||
target = (new File(origin, target)).getCanonicalPath();
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (origin.equals(target)) {
|
||||
// origin and target is identical.
|
||||
return ".";
|
||||
}
|
||||
|
||||
if (origin.equals(File.separator)) {
|
||||
// origin is root.
|
||||
return "." + target;
|
||||
}
|
||||
|
||||
String prefix = "";
|
||||
String root = File.separator;
|
||||
|
||||
if (System.getProperty("os.name").indexOf("Windows") != -1) {
|
||||
if (origin.startsWith("\\\\") || target.startsWith("\\\\")) {
|
||||
// Windows UNC path not supported.
|
||||
return null;
|
||||
}
|
||||
|
||||
char originLetter = origin.charAt(0);
|
||||
char targetLetter = target.charAt(0);
|
||||
if (Character.isLetter(originLetter) && Character.isLetter(targetLetter)) {
|
||||
// Windows only
|
||||
if (originLetter != targetLetter) {
|
||||
// Drive letters differ
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
prefix = "" + originLetter + ':';
|
||||
root = prefix + File.separator;
|
||||
}
|
||||
|
||||
String relative = "";
|
||||
while (!target.startsWith(origin + File.separator)) {
|
||||
origin = (new File(origin)).getParent();
|
||||
if (origin.equals(root))
|
||||
origin = prefix;
|
||||
relative += "..";
|
||||
relative += File.separator;
|
||||
}
|
||||
|
||||
return relative + target.substring(origin.length() + 1);
|
||||
}
|
||||
|
||||
public static String getLinuxPathFrom(File file) {
|
||||
return BACKSLASH.matcher(file.getAbsolutePath()).replaceAll("/");
|
||||
}
|
||||
|
||||
public static boolean isSCCSOrHiddenFile(File file) {
|
||||
return file.isHidden() || file.getName().charAt(0) == '.' || (file.isDirectory() && SOURCE_CONTROL_FOLDERS.contains(file.getName()));
|
||||
}
|
||||
|
||||
public static String readFileToString(File file) throws IOException {
|
||||
BufferedReader reader = null;
|
||||
try {
|
||||
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
sb.append(line).append("\n");
|
||||
}
|
||||
return sb.toString();
|
||||
} finally {
|
||||
if (reader != null) {
|
||||
try {
|
||||
reader.close();
|
||||
} catch (IOException e) {
|
||||
// noop
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the given file has any of the given extensions.
|
||||
* @param file
|
||||
* File whose name to look at
|
||||
* @param extensions
|
||||
* Extensions to consider (just the extension, without the
|
||||
* dot). Should all be lowercase, case insensitive matching
|
||||
* is used.
|
||||
*/
|
||||
public static boolean hasExtension(File file, String... extensions) {
|
||||
return hasExtension(file, Arrays.asList(extensions));
|
||||
}
|
||||
|
||||
public static boolean hasExtension(File file, List<String> extensions) {
|
||||
String pieces[] = file.getName().split("\\.");
|
||||
if (pieces.length < 2)
|
||||
return false;
|
||||
|
||||
String extension = pieces[pieces.length - 1];
|
||||
|
||||
return extensions.contains(extension.toLowerCase());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively find all files in a folder with the specified
|
||||
* extension. Excludes hidden files and folders and
|
||||
* source control folders.
|
||||
*
|
||||
* @param folder
|
||||
* Folder to look into
|
||||
* @param recursive
|
||||
* <b>true</b> will recursively find all files in sub-folders
|
||||
* @param extensions
|
||||
* A list of file extensions to search (just the extension,
|
||||
* without the dot). Should all be lowercase, case
|
||||
* insensitive matching is used. If no extensions are
|
||||
* passed, all files are returned.
|
||||
* @return
|
||||
*/
|
||||
public static List<File> listFiles(File folder, boolean recursive,
|
||||
String... extensions) {
|
||||
return listFiles(folder, recursive, Arrays.asList(extensions));
|
||||
}
|
||||
|
||||
public static List<File> listFiles(File folder, boolean recursive,
|
||||
List<String> extensions) {
|
||||
List<File> result = new ArrayList<File>();
|
||||
|
||||
for (File file : folder.listFiles()) {
|
||||
if (isSCCSOrHiddenFile(file))
|
||||
continue;
|
||||
|
||||
if (file.isDirectory()) {
|
||||
if (recursive)
|
||||
result.addAll(listFiles(file, true, extensions));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (extensions.isEmpty() || hasExtension(file, extensions))
|
||||
result.add(file);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
}
|
28
arduino-core/src/processing/app/helpers/NetUtils.java
Normal file
28
arduino-core/src/processing/app/helpers/NetUtils.java
Normal file
@ -0,0 +1,28 @@
|
||||
package processing.app.helpers;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
|
||||
public abstract class NetUtils {
|
||||
|
||||
public static boolean isReachable(InetAddress address, int port) {
|
||||
Socket socket = null;
|
||||
try {
|
||||
socket = new Socket();
|
||||
socket.connect(new InetSocketAddress(address, port), 100);
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
} finally {
|
||||
if (socket != null) {
|
||||
try {
|
||||
socket.close();
|
||||
} catch (IOException e) {
|
||||
// noop
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
29
arduino-core/src/processing/app/helpers/OSUtils.java
Normal file
29
arduino-core/src/processing/app/helpers/OSUtils.java
Normal file
@ -0,0 +1,29 @@
|
||||
package processing.app.helpers;
|
||||
|
||||
public class OSUtils {
|
||||
|
||||
/**
|
||||
* returns true if running on windows.
|
||||
*/
|
||||
static public boolean isWindows() {
|
||||
//return PApplet.platform == PConstants.WINDOWS;
|
||||
return System.getProperty("os.name").indexOf("Windows") != -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* true if running on linux.
|
||||
*/
|
||||
static public boolean isLinux() {
|
||||
//return PApplet.platform == PConstants.LINUX;
|
||||
return System.getProperty("os.name").indexOf("Linux") != -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns true if Processing is running on a Mac OS X machine.
|
||||
*/
|
||||
static public boolean isMacOS() {
|
||||
//return PApplet.platform == PConstants.MACOSX;
|
||||
return System.getProperty("os.name").indexOf("Mac") != -1;
|
||||
}
|
||||
|
||||
}
|
103
arduino-core/src/processing/app/helpers/PreferencesHelper.java
Normal file
103
arduino-core/src/processing/app/helpers/PreferencesHelper.java
Normal file
@ -0,0 +1,103 @@
|
||||
package processing.app.helpers;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Font;
|
||||
|
||||
public abstract class PreferencesHelper {
|
||||
|
||||
// /**
|
||||
// * Create a Color with the value of the specified key. The format of the color
|
||||
// * should be an hexadecimal number of 6 digit, eventually prefixed with a '#'.
|
||||
// *
|
||||
// * @param name
|
||||
// * @return A Color object or <b>null</b> if the key is not found or the format
|
||||
// * is wrong
|
||||
// */
|
||||
// static public Color getColor(PreferencesMap prefs, String name) {
|
||||
// Color parsed = parseColor(prefs.get(name));
|
||||
// if (parsed != null)
|
||||
// return parsed;
|
||||
// return Color.GRAY; // set a default
|
||||
// }
|
||||
//
|
||||
//
|
||||
// static public void setColor(PreferencesMap prefs, String attr, Color what) {
|
||||
// putColor(prefs, attr, what);
|
||||
// }
|
||||
//
|
||||
//
|
||||
// static public Font getFontWithDefault(PreferencesMap prefs, PreferencesMap defaults, String attr) {
|
||||
// Font font = getFont(prefs, attr);
|
||||
// if (font == null) {
|
||||
// String value = defaults.get(attr);
|
||||
// prefs.put(attr, value);
|
||||
// font = getFont(prefs, attr);
|
||||
// }
|
||||
// return font;
|
||||
// }
|
||||
//
|
||||
// static public SyntaxStyle getStyle(PreferencesMap prefs, String what) {
|
||||
// String str = prefs.get("editor." + what + ".style");
|
||||
//
|
||||
// StringTokenizer st = new StringTokenizer(str, ",");
|
||||
//
|
||||
// String s = st.nextToken();
|
||||
// if (s.indexOf("#") == 0) s = s.substring(1);
|
||||
// Color color = Color.DARK_GRAY;
|
||||
// try {
|
||||
// color = new Color(Integer.parseInt(s, 16));
|
||||
// } catch (Exception e) { }
|
||||
//
|
||||
// s = st.nextToken();
|
||||
// boolean bold = (s.indexOf("bold") != -1);
|
||||
// boolean italic = (s.indexOf("italic") != -1);
|
||||
// boolean underlined = (s.indexOf("underlined") != -1);
|
||||
//
|
||||
// return new SyntaxStyle(color, italic, bold, underlined);
|
||||
// }
|
||||
|
||||
/**
|
||||
* Set the value of the specified key based on the Color passed as parameter.
|
||||
*
|
||||
* @param attr
|
||||
* @param color
|
||||
*/
|
||||
public static void putColor(PreferencesMap prefs, String attr, Color color) {
|
||||
prefs.put(attr, "#" + String.format("%06x", color.getRGB() & 0xffffff));
|
||||
}
|
||||
|
||||
public static Color parseColor(String v) {
|
||||
try {
|
||||
if (v.indexOf("#") == 0)
|
||||
v = v.substring(1);
|
||||
return new Color(Integer.parseInt(v, 16));
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Font getFont(PreferencesMap prefs, String key) {
|
||||
String value = prefs.get(key);
|
||||
if (value == null)
|
||||
return null;
|
||||
String[] split = value.split(",");
|
||||
if (split.length != 3)
|
||||
return null;
|
||||
|
||||
String name = split[0];
|
||||
int style = Font.PLAIN;
|
||||
if (split[1].contains("bold"))
|
||||
style |= Font.BOLD;
|
||||
if (split[1].contains("italic"))
|
||||
style |= Font.ITALIC;
|
||||
int size;
|
||||
try {
|
||||
// ParseDouble handle numbers with decimals too
|
||||
size = (int) Double.parseDouble(split[2]);
|
||||
} catch (NumberFormatException e) {
|
||||
// for wrong formatted size pick the default
|
||||
size = 12;
|
||||
}
|
||||
return new Font(name, style, size);
|
||||
}
|
||||
}
|
322
arduino-core/src/processing/app/helpers/PreferencesMap.java
Normal file
322
arduino-core/src/processing/app/helpers/PreferencesMap.java
Normal file
@ -0,0 +1,322 @@
|
||||
/*
|
||||
PreferencesMap - A Map<String, String> with some useful features
|
||||
to handle preferences.
|
||||
Part of the Arduino project - http://www.arduino.cc/
|
||||
|
||||
Copyright (c) 2014 Cristian Maglie
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software Foundation,
|
||||
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
package processing.app.helpers;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import processing.app.legacy.PApplet;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class PreferencesMap extends LinkedHashMap<String, String> {
|
||||
|
||||
public PreferencesMap(Map<String, String> table) {
|
||||
super(table);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a PreferencesMap and load the content of the file passed as
|
||||
* argument.
|
||||
*
|
||||
* Is equivalent to:
|
||||
*
|
||||
* <pre>
|
||||
* PreferencesMap map = new PreferencesMap();
|
||||
* map.load(file);
|
||||
* </pre>
|
||||
*
|
||||
* @param file
|
||||
* @throws IOException
|
||||
*/
|
||||
public PreferencesMap(File file) throws IOException {
|
||||
super();
|
||||
load(file);
|
||||
}
|
||||
|
||||
public PreferencesMap() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a property list file and put kev/value pairs into the Map
|
||||
*
|
||||
* @param file
|
||||
* @throws FileNotFoundException
|
||||
* @throws IOException
|
||||
*/
|
||||
public void load(File file) throws IOException {
|
||||
load(new FileInputStream(file));
|
||||
}
|
||||
|
||||
protected String processPlatformSuffix(String key, String suffix, boolean isCurrentPlatform) {
|
||||
if (key == null)
|
||||
return null;
|
||||
// Key does not end with the given suffix? Process as normal
|
||||
if (!key.endsWith(suffix))
|
||||
return key;
|
||||
// Not the current platform? Ignore this key
|
||||
if (!isCurrentPlatform)
|
||||
return null;
|
||||
// Strip the suffix from the key
|
||||
return key.substring(0, key.length() - suffix.length());
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a property list stream and put key/value pairs into the Map
|
||||
*
|
||||
* @param input
|
||||
* @throws IOException
|
||||
*/
|
||||
public void load(InputStream input) throws IOException {
|
||||
String[] lines = PApplet.loadStrings(input);
|
||||
for (String line : lines) {
|
||||
if (line.length() == 0 || line.charAt(0) == '#')
|
||||
continue;
|
||||
|
||||
int equals = line.indexOf('=');
|
||||
if (equals != -1) {
|
||||
String key = line.substring(0, equals).trim();
|
||||
String value = line.substring(equals + 1).trim();
|
||||
|
||||
key = processPlatformSuffix(key, ".linux", OSUtils.isLinux());
|
||||
key = processPlatformSuffix(key, ".windows", OSUtils.isWindows());
|
||||
key = processPlatformSuffix(key, ".macosx", OSUtils.isMacOS());
|
||||
|
||||
if (key != null)
|
||||
put(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new PreferenceMap that contains all the top level pairs of the
|
||||
* current mapping. E.g. the folowing mapping:<br />
|
||||
*
|
||||
* <pre>
|
||||
* Map (
|
||||
* alpha = Alpha
|
||||
* alpha.some.keys = v1
|
||||
* alpha.other.keys = v2
|
||||
* beta = Beta
|
||||
* beta.some.keys = v3
|
||||
* )
|
||||
* </pre>
|
||||
*
|
||||
* will generate the following result:
|
||||
*
|
||||
* <pre>
|
||||
* Map (
|
||||
* alpha = Alpha
|
||||
* beta = Beta
|
||||
* )
|
||||
* </pre>
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public PreferencesMap topLevelMap() {
|
||||
PreferencesMap res = new PreferencesMap();
|
||||
for (String key : keySet()) {
|
||||
if (key.contains("."))
|
||||
continue;
|
||||
res.put(key, get(key));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new Map<String, PreferenceMap> where keys are the first level of
|
||||
* the current mapping. Top level pairs are discarded. E.g. the folowing
|
||||
* mapping:<br />
|
||||
*
|
||||
* <pre>
|
||||
* Map (
|
||||
* alpha = Alpha
|
||||
* alpha.some.keys = v1
|
||||
* alpha.other.keys = v2
|
||||
* beta = Beta
|
||||
* beta.some.keys = v3
|
||||
* )
|
||||
* </pre>
|
||||
*
|
||||
* will generate the following result:
|
||||
*
|
||||
* <pre>
|
||||
* alpha = Map(
|
||||
* some.keys = v1
|
||||
* other.keys = v2
|
||||
* )
|
||||
* beta = Map(
|
||||
* some.keys = v3
|
||||
* )
|
||||
* </pre>
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Map<String, PreferencesMap> firstLevelMap() {
|
||||
Map<String, PreferencesMap> res = new LinkedHashMap<String, PreferencesMap>();
|
||||
for (String key : keySet()) {
|
||||
int dot = key.indexOf('.');
|
||||
if (dot == -1)
|
||||
continue;
|
||||
|
||||
String parent = key.substring(0, dot);
|
||||
String child = key.substring(dot + 1);
|
||||
|
||||
if (!res.containsKey(parent))
|
||||
res.put(parent, new PreferencesMap());
|
||||
res.get(parent).put(child, get(key));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new PreferenceMap using a subtree of the current mapping. Top
|
||||
* level pairs are ignored. E.g. with the following mapping:<br />
|
||||
*
|
||||
* <pre>
|
||||
* Map (
|
||||
* alpha = Alpha
|
||||
* alpha.some.keys = v1
|
||||
* alpha.other.keys = v2
|
||||
* beta = Beta
|
||||
* beta.some.keys = v3
|
||||
* )
|
||||
* </pre>
|
||||
*
|
||||
* a call to createSubTree("alpha") will generate the following result:
|
||||
*
|
||||
* <pre>
|
||||
* Map(
|
||||
* some.keys = v1
|
||||
* other.keys = v2
|
||||
* )
|
||||
* </pre>
|
||||
*
|
||||
* @param parent
|
||||
* @return
|
||||
*/
|
||||
public PreferencesMap subTree(String parent) {
|
||||
PreferencesMap res = new PreferencesMap();
|
||||
parent += ".";
|
||||
int parentLen = parent.length();
|
||||
for (String key : keySet()) {
|
||||
if (key.startsWith(parent))
|
||||
res.put(key.substring(parentLen), get(key));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public String toString(String indent) {
|
||||
String res = indent + "{\n";
|
||||
SortedSet<String> treeSet = new TreeSet<String>(keySet());
|
||||
for (String k : treeSet)
|
||||
res += indent + k + " = " + get(k) + "\n";
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value to which the specified key is mapped, or throws a
|
||||
* PreferencesMapException if not found
|
||||
*
|
||||
* @param k
|
||||
* the key whose associated value is to be returned
|
||||
* @return the value to which the specified key is mapped
|
||||
* @throws PreferencesMapException
|
||||
*/
|
||||
public String getOrExcept(String k) throws PreferencesMapException {
|
||||
String r = get(k);
|
||||
if (r == null)
|
||||
throw new PreferencesMapException(k);
|
||||
return r;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return toString("");
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new File instance by converting the value of the key into an
|
||||
* abstract pathname. If the the given key doesn't exists or his value is the
|
||||
* empty string, the result is <b>null</b>.
|
||||
*
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public File getFile(String key) {
|
||||
if (!containsKey(key))
|
||||
return null;
|
||||
String path = get(key).trim();
|
||||
if (path.length() == 0)
|
||||
return null;
|
||||
return new File(path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new File instance by converting the value of the key into an
|
||||
* abstract pathname with the specified sub folder. If the the given key
|
||||
* doesn't exists or his value is the empty string, the result is <b>null</b>.
|
||||
*
|
||||
* @param key
|
||||
* @param subFolder
|
||||
* @return
|
||||
*/
|
||||
public File getFile(String key, String subFolder) {
|
||||
File file = getFile(key);
|
||||
if (file == null)
|
||||
return null;
|
||||
return new File(file, subFolder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the value of the specified key as boolean.
|
||||
*
|
||||
* @param key
|
||||
* @return <b>true</b> if the value of the key is the string "true" (case
|
||||
* insensitive compared), <b>false</b> in any other case
|
||||
*/
|
||||
public boolean getBoolean(String key) {
|
||||
return new Boolean(get(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of the specified key to the string <b>"true"</b> or
|
||||
* <b>"false"</b> based on value of the boolean parameter
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
* @return <b>true</b> if the previous value of the key was the string "true"
|
||||
* (case insensitive compared), <b>false</b> in any other case
|
||||
*/
|
||||
public boolean putBoolean(String key, boolean value) {
|
||||
String prev = put(key, value ? "true" : "false");
|
||||
return new Boolean(prev);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package processing.app.helpers;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class PreferencesMapException extends Exception {
|
||||
|
||||
public PreferencesMapException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
28
arduino-core/src/processing/app/helpers/ProcessUtils.java
Normal file
28
arduino-core/src/processing/app/helpers/ProcessUtils.java
Normal file
@ -0,0 +1,28 @@
|
||||
package processing.app.helpers;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
public class ProcessUtils {
|
||||
|
||||
public static Process exec(String[] command) throws IOException {
|
||||
// No problems on linux and mac
|
||||
if (!OSUtils.isWindows()) {
|
||||
return Runtime.getRuntime().exec(command);
|
||||
}
|
||||
|
||||
// Brutal hack to workaround windows command line parsing.
|
||||
// http://stackoverflow.com/questions/5969724/java-runtime-exec-fails-to-escape-characters-properly
|
||||
// http://msdn.microsoft.com/en-us/library/a1y7w461.aspx
|
||||
// http://bugs.sun.com/view_bug.do?bug_id=6468220
|
||||
// http://bugs.sun.com/view_bug.do?bug_id=6518827
|
||||
String[] cmdLine = new String[command.length];
|
||||
for (int i = 0; i < command.length; i++)
|
||||
cmdLine[i] = command[i].replace("\"", "\\\"");
|
||||
|
||||
ProcessBuilder pb = new ProcessBuilder(cmdLine);
|
||||
Map<String, String> env = pb.environment();
|
||||
env.put("CYGWIN", "nodosfilewarning");
|
||||
return pb.start();
|
||||
}
|
||||
}
|
102
arduino-core/src/processing/app/helpers/StringReplacer.java
Normal file
102
arduino-core/src/processing/app/helpers/StringReplacer.java
Normal file
@ -0,0 +1,102 @@
|
||||
/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
|
||||
/*
|
||||
StringReplacer - Utility class for expression formatting
|
||||
Part of the Arduino project - http://www.arduino.cc/
|
||||
|
||||
Copyright (c) 2011 Cristian Maglie
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software Foundation,
|
||||
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
package processing.app.helpers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class StringReplacer {
|
||||
|
||||
public static String[] formatAndSplit(String src, Map<String, String> dict,
|
||||
boolean recursive) throws Exception {
|
||||
String res;
|
||||
|
||||
// Recursive replace with a max depth of 10 levels.
|
||||
for (int i = 0; i < 10; i++) {
|
||||
// Do a replace with dictionary
|
||||
res = StringReplacer.replaceFromMapping(src, dict);
|
||||
if (!recursive)
|
||||
break;
|
||||
if (res.equals(src))
|
||||
break;
|
||||
src = res;
|
||||
}
|
||||
|
||||
// Split the resulting string in arguments
|
||||
return quotedSplit(src, "\"'", false);
|
||||
}
|
||||
|
||||
public static String[] quotedSplit(String src, String quoteChars,
|
||||
boolean acceptEmptyArguments)
|
||||
throws Exception {
|
||||
List<String> res = new ArrayList<String>();
|
||||
String escapedArg = null;
|
||||
String escapingChar = null;
|
||||
for (String i : src.split(" ")) {
|
||||
if (escapingChar == null) {
|
||||
// If the first char is not an escape char..
|
||||
String first = null;
|
||||
if (i.length() > 0)
|
||||
first = i.substring(0, 1);
|
||||
if (first == null || !quoteChars.contains(first)) {
|
||||
if (i.trim().length() != 0 || acceptEmptyArguments)
|
||||
res.add(i);
|
||||
continue;
|
||||
}
|
||||
|
||||
escapingChar = first;
|
||||
i = i.substring(1);
|
||||
escapedArg = "";
|
||||
}
|
||||
|
||||
if (!i.endsWith(escapingChar)) {
|
||||
escapedArg += i + " ";
|
||||
continue;
|
||||
}
|
||||
|
||||
escapedArg += i.substring(0, i.length() - 1);
|
||||
if (escapedArg.trim().length() != 0 || acceptEmptyArguments)
|
||||
res.add(escapedArg);
|
||||
escapingChar = null;
|
||||
}
|
||||
if (escapingChar != null)
|
||||
throw new Exception("Invalid quoting: no closing [" + escapingChar +
|
||||
"] char found.");
|
||||
return res.toArray(new String[0]);
|
||||
}
|
||||
|
||||
public static String replaceFromMapping(String src, Map<String, String> map) {
|
||||
return replaceFromMapping(src, map, "{", "}");
|
||||
}
|
||||
|
||||
public static String replaceFromMapping(String src, Map<String, String> map,
|
||||
String leftDelimiter,
|
||||
String rightDelimiter) {
|
||||
for (String k : map.keySet()) {
|
||||
String keyword = leftDelimiter + k + rightDelimiter;
|
||||
src = src.replace(keyword, map.get(k));
|
||||
}
|
||||
return src;
|
||||
}
|
||||
|
||||
}
|
29
arduino-core/src/processing/app/helpers/StringUtils.java
Normal file
29
arduino-core/src/processing/app/helpers/StringUtils.java
Normal file
@ -0,0 +1,29 @@
|
||||
package processing.app.helpers;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class StringUtils {
|
||||
|
||||
public static boolean stringContainsOneOf(String input, List<String> listOfStrings) {
|
||||
for (String string : listOfStrings) {
|
||||
if (input.contains(string)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to match <b>input</b> with <b>pattern</b>. The pattern can use the
|
||||
* "*" and "?" globs to match any-char-sequence and any-char respectively.
|
||||
*
|
||||
* @param input The string to be checked
|
||||
* @param pattern The pattern to match
|
||||
* @return <b>true</b> if the <b>input</b> matches the <b>pattern</b>,
|
||||
* <b>false</b> otherwise.
|
||||
*/
|
||||
public static boolean wildcardMatch(String input, String pattern) {
|
||||
String regex = pattern.replace("?", ".?").replace("*", ".*?");
|
||||
return input.matches(regex);
|
||||
}
|
||||
}
|
19
arduino-core/src/processing/app/helpers/UserNotifier.java
Normal file
19
arduino-core/src/processing/app/helpers/UserNotifier.java
Normal file
@ -0,0 +1,19 @@
|
||||
package processing.app.helpers;
|
||||
|
||||
public abstract class UserNotifier {
|
||||
|
||||
public void showError(String title, String message, int exit_code) {
|
||||
showError(title, message, null, exit_code);
|
||||
}
|
||||
|
||||
public void showError(String title, String message, Throwable e) {
|
||||
showError(title, message, e, 1);
|
||||
}
|
||||
|
||||
public abstract void showError(String title, String message, Throwable e, int exit_code);
|
||||
|
||||
public abstract void showMessage(String title, String message);
|
||||
|
||||
public abstract void showWarning(String title, String message, Exception e);
|
||||
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
/*
|
||||
OnlyDirs - FilenameFilter that accepts only directories (CVS, .svn,
|
||||
.DS_Store files are excluded as well)
|
||||
Part of the Arduino project - http://www.arduino.cc/
|
||||
|
||||
Copyright (c) 2011 Cristian Maglie
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software Foundation,
|
||||
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
package processing.app.helpers.filefilters;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FilenameFilter;
|
||||
|
||||
/**
|
||||
* This filter accepts only directories (excluding .DS_Store files, .svn
|
||||
* folders, etc)
|
||||
*
|
||||
* @author Cristian Maglie
|
||||
*/
|
||||
public class OnlyDirs implements FilenameFilter {
|
||||
|
||||
public boolean accept(File dir, String name) {
|
||||
if (name.charAt(0) == '.')
|
||||
return false;
|
||||
if (name.equals("CVS"))
|
||||
return false;
|
||||
return new File(dir, name).isDirectory();
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
/*
|
||||
OnlyFilesWithExtension - FilenameFilter that accepts only files with a
|
||||
specific extension.
|
||||
Part of the Arduino project - http://www.arduino.cc/
|
||||
|
||||
Copyright (c) 2011 Cristian Maglie
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software Foundation,
|
||||
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
package processing.app.helpers.filefilters;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FilenameFilter;
|
||||
|
||||
public class OnlyFilesWithExtension implements FilenameFilter {
|
||||
|
||||
String extensions[];
|
||||
|
||||
public OnlyFilesWithExtension(String... ext) {
|
||||
extensions = ext;
|
||||
}
|
||||
|
||||
public boolean accept(File dir, String name) {
|
||||
for (String ext : extensions)
|
||||
if (name.endsWith(ext))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user