Skip to content

Commit b0d8a50

Browse files
bitroncmaglie
authored andcommitted
Moved isSanitaryName() and sanitizeName() from Sketch to BaseNoGui.
1 parent 4b69baa commit b0d8a50

File tree

4 files changed

+58
-58
lines changed

4 files changed

+58
-58
lines changed

app/src/processing/app/Base.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,7 +1652,7 @@ public void actionPerformed(ActionEvent e) {
16521652
// if a .pde file of the same prefix as the folder exists..
16531653
if (entry.exists()) {
16541654

1655-
if (!Sketch.isSanitaryName(name)) {
1655+
if (!BaseNoGui.isSanitaryName(name)) {
16561656
if (!builtOnce) {
16571657
String complaining = I18n
16581658
.format(
@@ -2725,7 +2725,7 @@ public void handleAddLibrary() {
27252725
// is there a valid library?
27262726
File libFolder = sourceFile;
27272727
String libName = libFolder.getName();
2728-
if (!Sketch.isSanitaryName(libName)) {
2728+
if (!BaseNoGui.isSanitaryName(libName)) {
27292729
String mess = I18n.format(_("The library \"{0}\" cannot be used.\n"
27302730
+ "Library names must contain only basic letters and numbers.\n"
27312731
+ "(ASCII only and no spaces, and it cannot start with a number)"),

app/src/processing/app/BaseNoGui.java

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,13 @@ static public void initVersion() {
315315
PreferencesData.set("last.ide." + VERSION_NAME + ".daterun", "" + (new Date()).getTime() / 1000);
316316
}
317317

318+
/**
319+
* Return true if the name is valid for a Processing sketch.
320+
*/
321+
static public boolean isSanitaryName(String name) {
322+
return sanitizeName(name).equals(name);
323+
}
324+
318325
static protected void loadHardware(File folder) {
319326
if (!folder.isDirectory()) return;
320327

@@ -405,7 +412,7 @@ static public void removeDescendants(File dir) {
405412
if (files[i].equals(".") || files[i].equals("..")) continue;
406413
File dead = new File(dir, files[i]);
407414
if (!dead.isDirectory()) {
408-
if (!Preferences.getBoolean("compiler.save_build_files")) {
415+
if (!PreferencesData.getBoolean("compiler.save_build_files")) {
409416
if (!dead.delete()) {
410417
// temporarily disabled
411418
System.err.println(I18n.format(_("Could not delete {0}"), dead));
@@ -430,6 +437,50 @@ static public void removeDir(File dir) {
430437
}
431438
}
432439

440+
/**
441+
* Produce a sanitized name that fits our standards for likely to work.
442+
* <p/>
443+
* Java classes have a wider range of names that are technically allowed
444+
* (supposedly any Unicode name) than what we support. The reason for
445+
* going more narrow is to avoid situations with text encodings and
446+
* converting during the process of moving files between operating
447+
* systems, i.e. uploading from a Windows machine to a Linux server,
448+
* or reading a FAT32 partition in OS X and using a thumb drive.
449+
* <p/>
450+
* This helper function replaces everything but A-Z, a-z, and 0-9 with
451+
* underscores. Also disallows starting the sketch name with a digit.
452+
*/
453+
static public String sanitizeName(String origName) {
454+
char c[] = origName.toCharArray();
455+
StringBuffer buffer = new StringBuffer();
456+
457+
// can't lead with a digit, so start with an underscore
458+
if ((c[0] >= '0') && (c[0] <= '9')) {
459+
buffer.append('_');
460+
}
461+
for (int i = 0; i < c.length; i++) {
462+
if (((c[i] >= '0') && (c[i] <= '9')) ||
463+
((c[i] >= 'a') && (c[i] <= 'z')) ||
464+
((c[i] >= 'A') && (c[i] <= 'Z')) ||
465+
((i > 0) && (c[i] == '-')) ||
466+
((i > 0) && (c[i] == '.'))) {
467+
buffer.append(c[i]);
468+
} else {
469+
buffer.append('_');
470+
}
471+
}
472+
// let's not be ridiculous about the length of filenames.
473+
// in fact, Mac OS 9 can handle 255 chars, though it can't really
474+
// deal with filenames longer than 31 chars in the Finder.
475+
// but limiting to that for sketches would mean setting the
476+
// upper-bound on the character limit here to 25 characters
477+
// (to handle the base name + ".class")
478+
if (buffer.length() > 63) {
479+
buffer.setLength(63);
480+
}
481+
return buffer.toString();
482+
}
483+
433484
/**
434485
* Spew the contents of a String object out to a file.
435486
*/
@@ -475,7 +526,7 @@ static public LibraryList scanLibraries(File folder) throws IOException {
475526

476527
for (String libName : list) {
477528
File subfolder = new File(folder, libName);
478-
if (!Sketch.isSanitaryName(libName)) {
529+
if (!isSanitaryName(libName)) {
479530
String mess = I18n.format(_("The library \"{0}\" cannot be used.\n"
480531
+ "Library names must contain only basic letters and numbers.\n"
481532
+ "(ASCII only and no spaces, and it cannot start with a number)"),

app/src/processing/app/Sketch.java

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ protected void nameCode(String newName) {
239239
// make sure the user didn't name things poo.time.pde
240240
// or something like that (nothing against poo time)
241241
String shortName = newName.substring(0, dot);
242-
String sanitaryName = Sketch.sanitizeName(shortName);
242+
String sanitaryName = BaseNoGui.sanitizeName(shortName);
243243
if (!shortName.equals(sanitaryName)) {
244244
newName = sanitaryName + "." + newExtension;
245245
}
@@ -1465,7 +1465,7 @@ public String getAppletClassName2() {
14651465
* if changes were made.
14661466
*/
14671467
static public String checkName(String origName) {
1468-
String newName = sanitizeName(origName);
1468+
String newName = BaseNoGui.sanitizeName(origName);
14691469

14701470
if (!newName.equals(origName)) {
14711471
String msg =
@@ -1478,55 +1478,4 @@ static public String checkName(String origName) {
14781478
}
14791479

14801480

1481-
/**
1482-
* Return true if the name is valid for a Processing sketch.
1483-
*/
1484-
static public boolean isSanitaryName(String name) {
1485-
return sanitizeName(name).equals(name);
1486-
}
1487-
1488-
1489-
/**
1490-
* Produce a sanitized name that fits our standards for likely to work.
1491-
* <p/>
1492-
* Java classes have a wider range of names that are technically allowed
1493-
* (supposedly any Unicode name) than what we support. The reason for
1494-
* going more narrow is to avoid situations with text encodings and
1495-
* converting during the process of moving files between operating
1496-
* systems, i.e. uploading from a Windows machine to a Linux server,
1497-
* or reading a FAT32 partition in OS X and using a thumb drive.
1498-
* <p/>
1499-
* This helper function replaces everything but A-Z, a-z, and 0-9 with
1500-
* underscores. Also disallows starting the sketch name with a digit.
1501-
*/
1502-
static public String sanitizeName(String origName) {
1503-
char c[] = origName.toCharArray();
1504-
StringBuffer buffer = new StringBuffer();
1505-
1506-
// can't lead with a digit, so start with an underscore
1507-
if ((c[0] >= '0') && (c[0] <= '9')) {
1508-
buffer.append('_');
1509-
}
1510-
for (int i = 0; i < c.length; i++) {
1511-
if (((c[i] >= '0') && (c[i] <= '9')) ||
1512-
((c[i] >= 'a') && (c[i] <= 'z')) ||
1513-
((c[i] >= 'A') && (c[i] <= 'Z')) ||
1514-
((i > 0) && (c[i] == '-')) ||
1515-
((i > 0) && (c[i] == '.'))) {
1516-
buffer.append(c[i]);
1517-
} else {
1518-
buffer.append('_');
1519-
}
1520-
}
1521-
// let's not be ridiculous about the length of filenames.
1522-
// in fact, Mac OS 9 can handle 255 chars, though it can't really
1523-
// deal with filenames longer than 31 chars in the Finder.
1524-
// but limiting to that for sketches would mean setting the
1525-
// upper-bound on the character limit here to 25 characters
1526-
// (to handle the base name + ".class")
1527-
if (buffer.length() > 63) {
1528-
buffer.setLength(63);
1529-
}
1530-
return buffer.toString();
1531-
}
15321481
}

app/src/processing/app/SketchData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ protected void load() throws IOException {
122122

123123
// Don't allow people to use files with invalid names, since on load,
124124
// it would be otherwise possible to sneak in nasty filenames. [0116]
125-
if (Sketch.isSanitaryName(base)) {
125+
if (BaseNoGui.isSanitaryName(base)) {
126126
addCode(new SketchCodeDocument(new File(folder, filename)));
127127
} else {
128128
System.err.println(I18n.format("File name {0} is invalid: ignored", filename));

0 commit comments

Comments
 (0)