mirror of
https://github.com/arduino/Arduino.git
synced 2024-11-29 10:24:12 +01:00
Moved isSanitaryName() and sanitizeName() from Sketch to BaseNoGui.
This commit is contained in:
parent
4b69baadac
commit
b0d8a504dd
@ -1652,7 +1652,7 @@ public class Base {
|
||||
// if a .pde file of the same prefix as the folder exists..
|
||||
if (entry.exists()) {
|
||||
|
||||
if (!Sketch.isSanitaryName(name)) {
|
||||
if (!BaseNoGui.isSanitaryName(name)) {
|
||||
if (!builtOnce) {
|
||||
String complaining = I18n
|
||||
.format(
|
||||
@ -2725,7 +2725,7 @@ public class Base {
|
||||
// is there a valid library?
|
||||
File libFolder = sourceFile;
|
||||
String libName = libFolder.getName();
|
||||
if (!Sketch.isSanitaryName(libName)) {
|
||||
if (!BaseNoGui.isSanitaryName(libName)) {
|
||||
String mess = I18n.format(_("The library \"{0}\" cannot be used.\n"
|
||||
+ "Library names must contain only basic letters and numbers.\n"
|
||||
+ "(ASCII only and no spaces, and it cannot start with a number)"),
|
||||
|
@ -315,6 +315,13 @@ public class BaseNoGui {
|
||||
PreferencesData.set("last.ide." + VERSION_NAME + ".daterun", "" + (new Date()).getTime() / 1000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the name is valid for a Processing sketch.
|
||||
*/
|
||||
static public boolean isSanitaryName(String name) {
|
||||
return sanitizeName(name).equals(name);
|
||||
}
|
||||
|
||||
static protected void loadHardware(File folder) {
|
||||
if (!folder.isDirectory()) return;
|
||||
|
||||
@ -405,7 +412,7 @@ public class BaseNoGui {
|
||||
if (files[i].equals(".") || files[i].equals("..")) continue;
|
||||
File dead = new File(dir, files[i]);
|
||||
if (!dead.isDirectory()) {
|
||||
if (!Preferences.getBoolean("compiler.save_build_files")) {
|
||||
if (!PreferencesData.getBoolean("compiler.save_build_files")) {
|
||||
if (!dead.delete()) {
|
||||
// temporarily disabled
|
||||
System.err.println(I18n.format(_("Could not delete {0}"), dead));
|
||||
@ -430,6 +437,50 @@ public class BaseNoGui {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Produce a sanitized name that fits our standards for likely to work.
|
||||
* <p/>
|
||||
* Java classes have a wider range of names that are technically allowed
|
||||
* (supposedly any Unicode name) than what we support. The reason for
|
||||
* going more narrow is to avoid situations with text encodings and
|
||||
* converting during the process of moving files between operating
|
||||
* systems, i.e. uploading from a Windows machine to a Linux server,
|
||||
* or reading a FAT32 partition in OS X and using a thumb drive.
|
||||
* <p/>
|
||||
* This helper function replaces everything but A-Z, a-z, and 0-9 with
|
||||
* underscores. Also disallows starting the sketch name with a digit.
|
||||
*/
|
||||
static public String sanitizeName(String origName) {
|
||||
char c[] = origName.toCharArray();
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
|
||||
// can't lead with a digit, so start with an underscore
|
||||
if ((c[0] >= '0') && (c[0] <= '9')) {
|
||||
buffer.append('_');
|
||||
}
|
||||
for (int i = 0; i < c.length; i++) {
|
||||
if (((c[i] >= '0') && (c[i] <= '9')) ||
|
||||
((c[i] >= 'a') && (c[i] <= 'z')) ||
|
||||
((c[i] >= 'A') && (c[i] <= 'Z')) ||
|
||||
((i > 0) && (c[i] == '-')) ||
|
||||
((i > 0) && (c[i] == '.'))) {
|
||||
buffer.append(c[i]);
|
||||
} else {
|
||||
buffer.append('_');
|
||||
}
|
||||
}
|
||||
// let's not be ridiculous about the length of filenames.
|
||||
// in fact, Mac OS 9 can handle 255 chars, though it can't really
|
||||
// deal with filenames longer than 31 chars in the Finder.
|
||||
// but limiting to that for sketches would mean setting the
|
||||
// upper-bound on the character limit here to 25 characters
|
||||
// (to handle the base name + ".class")
|
||||
if (buffer.length() > 63) {
|
||||
buffer.setLength(63);
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Spew the contents of a String object out to a file.
|
||||
*/
|
||||
@ -475,7 +526,7 @@ public class BaseNoGui {
|
||||
|
||||
for (String libName : list) {
|
||||
File subfolder = new File(folder, libName);
|
||||
if (!Sketch.isSanitaryName(libName)) {
|
||||
if (!isSanitaryName(libName)) {
|
||||
String mess = I18n.format(_("The library \"{0}\" cannot be used.\n"
|
||||
+ "Library names must contain only basic letters and numbers.\n"
|
||||
+ "(ASCII only and no spaces, and it cannot start with a number)"),
|
||||
|
@ -239,7 +239,7 @@ public class Sketch {
|
||||
// make sure the user didn't name things poo.time.pde
|
||||
// or something like that (nothing against poo time)
|
||||
String shortName = newName.substring(0, dot);
|
||||
String sanitaryName = Sketch.sanitizeName(shortName);
|
||||
String sanitaryName = BaseNoGui.sanitizeName(shortName);
|
||||
if (!shortName.equals(sanitaryName)) {
|
||||
newName = sanitaryName + "." + newExtension;
|
||||
}
|
||||
@ -1465,7 +1465,7 @@ public class Sketch {
|
||||
* if changes were made.
|
||||
*/
|
||||
static public String checkName(String origName) {
|
||||
String newName = sanitizeName(origName);
|
||||
String newName = BaseNoGui.sanitizeName(origName);
|
||||
|
||||
if (!newName.equals(origName)) {
|
||||
String msg =
|
||||
@ -1478,55 +1478,4 @@ public class Sketch {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return true if the name is valid for a Processing sketch.
|
||||
*/
|
||||
static public boolean isSanitaryName(String name) {
|
||||
return sanitizeName(name).equals(name);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Produce a sanitized name that fits our standards for likely to work.
|
||||
* <p/>
|
||||
* Java classes have a wider range of names that are technically allowed
|
||||
* (supposedly any Unicode name) than what we support. The reason for
|
||||
* going more narrow is to avoid situations with text encodings and
|
||||
* converting during the process of moving files between operating
|
||||
* systems, i.e. uploading from a Windows machine to a Linux server,
|
||||
* or reading a FAT32 partition in OS X and using a thumb drive.
|
||||
* <p/>
|
||||
* This helper function replaces everything but A-Z, a-z, and 0-9 with
|
||||
* underscores. Also disallows starting the sketch name with a digit.
|
||||
*/
|
||||
static public String sanitizeName(String origName) {
|
||||
char c[] = origName.toCharArray();
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
|
||||
// can't lead with a digit, so start with an underscore
|
||||
if ((c[0] >= '0') && (c[0] <= '9')) {
|
||||
buffer.append('_');
|
||||
}
|
||||
for (int i = 0; i < c.length; i++) {
|
||||
if (((c[i] >= '0') && (c[i] <= '9')) ||
|
||||
((c[i] >= 'a') && (c[i] <= 'z')) ||
|
||||
((c[i] >= 'A') && (c[i] <= 'Z')) ||
|
||||
((i > 0) && (c[i] == '-')) ||
|
||||
((i > 0) && (c[i] == '.'))) {
|
||||
buffer.append(c[i]);
|
||||
} else {
|
||||
buffer.append('_');
|
||||
}
|
||||
}
|
||||
// let's not be ridiculous about the length of filenames.
|
||||
// in fact, Mac OS 9 can handle 255 chars, though it can't really
|
||||
// deal with filenames longer than 31 chars in the Finder.
|
||||
// but limiting to that for sketches would mean setting the
|
||||
// upper-bound on the character limit here to 25 characters
|
||||
// (to handle the base name + ".class")
|
||||
if (buffer.length() > 63) {
|
||||
buffer.setLength(63);
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
}
|
||||
|
@ -122,7 +122,7 @@ public class SketchData {
|
||||
|
||||
// Don't allow people to use files with invalid names, since on load,
|
||||
// it would be otherwise possible to sneak in nasty filenames. [0116]
|
||||
if (Sketch.isSanitaryName(base)) {
|
||||
if (BaseNoGui.isSanitaryName(base)) {
|
||||
addCode(new SketchCodeDocument(new File(folder, filename)));
|
||||
} else {
|
||||
System.err.println(I18n.format("File name {0} is invalid: ignored", filename));
|
||||
|
Loading…
Reference in New Issue
Block a user