mirror of
https://github.com/arduino/Arduino.git
synced 2024-11-29 10:24:12 +01:00
see #1211: formatted code, moved RelativePath code into FileUtils
This commit is contained in:
parent
093483602f
commit
73c8f1c4ab
@ -441,8 +441,7 @@ public class Base {
|
||||
File absolute = new File(portableFolder, path);
|
||||
try {
|
||||
path = absolute.getCanonicalPath();
|
||||
}
|
||||
catch (IOException e) {
|
||||
} catch (IOException e) {
|
||||
// path unchanged.
|
||||
}
|
||||
}
|
||||
@ -485,7 +484,7 @@ public class Base {
|
||||
continue;
|
||||
}
|
||||
if (portableFolder != null) {
|
||||
path = RelativePath.relativePath(portableFolder.toString(), path);
|
||||
path = FileUtils.relativePath(portableFolder.toString(), path);
|
||||
if (path == null)
|
||||
continue;
|
||||
}
|
||||
@ -509,7 +508,7 @@ public class Base {
|
||||
path = "";
|
||||
} else
|
||||
if (portableFolder != null) {
|
||||
path = RelativePath.relativePath(portableFolder.toString(), path);
|
||||
path = FileUtils.relativePath(portableFolder.toString(), path);
|
||||
if (path == null)
|
||||
path = "";
|
||||
}
|
||||
|
@ -23,6 +23,7 @@
|
||||
|
||||
package processing.app;
|
||||
|
||||
import processing.app.helpers.FileUtils;
|
||||
import processing.app.syntax.SyntaxStyle;
|
||||
import processing.core.PApplet;
|
||||
import processing.core.PConstants;
|
||||
@ -328,7 +329,7 @@ public class Preferences {
|
||||
if (file != null) {
|
||||
String path = file.getAbsolutePath();
|
||||
if (Base.getPortableFolder() != null) {
|
||||
path = RelativePath.relativePath(Base.getPortableFolder().toString(), path);
|
||||
path = FileUtils.relativePath(Base.getPortableFolder().toString(), path);
|
||||
if (path == null) {
|
||||
path = Base.getPortableSketchbookFolder();
|
||||
}
|
||||
|
@ -1,74 +0,0 @@
|
||||
/*
|
||||
* by Shigeru KANEMOTO at SWITCHSCIENCE.
|
||||
*/
|
||||
|
||||
package processing.app;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
class RelativePath {
|
||||
//
|
||||
// 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".
|
||||
//
|
||||
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);
|
||||
}
|
||||
}
|
@ -90,4 +90,68 @@ public class FileUtils {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user