remove NUL characters from files just loaded

This commit is contained in:
Ben Fry
2013-07-21 11:57:18 -04:00
parent 3b08db7fdb
commit 0e599a673f
3 changed files with 114 additions and 100 deletions
+71 -62
View File
@@ -31,7 +31,7 @@ import javax.swing.undo.*;
/**
* Represents a single tab of a sketch.
* Represents a single tab of a sketch.
*/
public class SketchCode {
/** Pretty name (no extension), not the full file name */
@@ -40,12 +40,12 @@ public class SketchCode {
/** File object for where this code is located */
private File file;
/** Extension for this file (no dots, and in lowercase). */
/** Extension for this file (no dots, and in lowercase). */
private String extension;
/** Text of the program text for this tab */
private String program;
/** Last version of the program on disk. */
private String savedProgram;
@@ -54,15 +54,15 @@ public class SketchCode {
/** Last time this tab was visited */
long visited;
/**
* Undo Manager for this tab, each tab keeps track of their own
* Editor.undo will be set to this object when this code is the tab
* that's currently the front.
*/
private UndoManager undo = new UndoManager();
/** What was on top of the undo stack when last saved. */
/** What was on top of the undo stack when last saved. */
// private UndoableEdit lastEdit;
// saved positions from last time this tab was used
@@ -73,9 +73,9 @@ public class SketchCode {
private boolean modified;
/** name of .java file after preproc */
// private String preprocName;
// private String preprocName;
/** where this code starts relative to the concat'd code */
private int preprocOffset;
private int preprocOffset;
public SketchCode(File file, String extension) {
@@ -102,23 +102,23 @@ public class SketchCode {
public File getFile() {
return file;
}
protected boolean fileExists() {
return file.exists();
}
protected boolean fileReadOnly() {
return !file.canWrite();
}
protected boolean deleteFile() {
return file.delete();
}
protected boolean renameTo(File what, String ext) {
// System.out.println("renaming " + file);
// System.out.println(" to " + what);
@@ -130,32 +130,32 @@ public class SketchCode {
}
return success;
}
public void copyTo(File dest) throws IOException {
Base.saveFile(program, dest);
}
public String getFileName() {
return file.getName();
}
public String getPrettyName() {
return prettyName;
}
public String getExtension() {
return extension;
}
public boolean isExtension(String what) {
return extension.equals(what);
}
/** get the current text for this tab */
public String getProgram() {
@@ -174,12 +174,12 @@ public class SketchCode {
return savedProgram;
}
public int getLineCount() {
return Base.countLines(program);
}
public void setModified(boolean modified) {
this.modified = modified;
}
@@ -208,8 +208,8 @@ public class SketchCode {
public int getPreprocOffset() {
return preprocOffset;
}
public void addPreprocOffset(int extra) {
preprocOffset += extra;
}
@@ -218,72 +218,81 @@ public class SketchCode {
public Document getDocument() {
return document;
}
public void setDocument(Document d) {
document = d;
}
public UndoManager getUndo() {
return undo;
}
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
// TODO these could probably be handled better, since it's a general state
// issue that's read/write from only one location in Editor (on tab switch.)
public int getSelectionStart() {
return selectionStart;
}
public int getSelectionStop() {
return selectionStop;
}
public int getScrollPosition() {
return scrollPosition;
}
protected void setState(String p, int start, int stop, int pos) {
program = p;
selectionStart = start;
selectionStop = stop;
scrollPosition = pos;
}
public long lastVisited() {
return visited;
}
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
/**
* Load this piece of code from a file.
*/
public void load() throws IOException {
program = Base.loadFile(file);
// Remove NUL characters because they'll cause problems,
// and their presence is very difficult to debug.
// https://github.com/processing/processing/issues/1973
if (program.indexOf('\0') != -1) {
program = program.replaceAll("\0", "");
}
savedProgram = program;
// This used to be the "Fix Encoding and Reload" warning, but since that
// tool has been removed, it just rambles about text editors and encodings.
if (program.indexOf('\uFFFD') != -1) {
System.err.println(file.getName() + " contains unrecognized characters.");
System.err.println("If this code was created with an older version of Processing,");
System.err.println("you may need to use Tools -> Fix Encoding & Reload to update");
System.err.println("the sketch to use UTF-8 encoding. If not, you may need to");
System.err.println(file.getName() + " contains unrecognized characters.");
System.err.println("You should re-open " + file.getName() +
" with a text editor,");
System.err.println("and re-save it in UTF-8 format. Otherwise, you can");
System.err.println("delete the bad characters to get rid of this warning.");
System.err.println();
}
setModified(false);
}
@@ -312,11 +321,11 @@ public class SketchCode {
makePrettyName();
setModified(false);
}
/**
* Called when the sketch folder name/location has changed. Called when
* renaming tab 0, the main code.
* Called when the sketch folder name/location has changed. Called when
* renaming tab 0, the main code.
*/
public void setFolder(File sketchFolder) {
file = new File(sketchFolder, file.getName());
@@ -78,7 +78,7 @@ public class JEditTextArea extends JComponent
private InputMethodSupport inputMethodSupport = null;
private Brackets bracketHelper = new Brackets();
/**
* Creates a new JEditTextArea with the specified settings.
@@ -156,8 +156,8 @@ public class JEditTextArea extends JComponent
// System.out.println(" mods extext = " + mods + " " + mods.length() + " " + PApplet.hex(mods.charAt(0)));
// }
// System.out.println(" " + e);
// inertia scrolling on OS X will fire several shift-wheel events
// inertia scrolling on OS X will fire several shift-wheel events
// that are negative values.. this makes the scrolling area jump.
boolean skip = Base.isMacOS() && e.isShiftDown();
//if (ex == 0) {
@@ -187,7 +187,7 @@ public class JEditTextArea extends JComponent
return null;
}
/**
* Get current position of the vertical scroll bar. [fry]
*/
@@ -211,7 +211,7 @@ public class JEditTextArea extends JComponent
return painter;
}
/**
* Returns the input handler.
*/
@@ -219,7 +219,7 @@ public class JEditTextArea extends JComponent
return inputHandler;
}
/**
* Sets the input handler.
* @param inputHandler The new input handler
@@ -228,7 +228,7 @@ public class JEditTextArea extends JComponent
this.inputHandler = inputHandler;
}
/**
* Returns true if the caret is blinking, false otherwise.
*/
@@ -236,7 +236,7 @@ public class JEditTextArea extends JComponent
return caretBlinks;
}
/**
* Toggles caret blinking.
* @param caretBlinks True if the caret should blink, false otherwise
@@ -249,7 +249,7 @@ public class JEditTextArea extends JComponent
painter.invalidateSelectedLines();
}
/**
* Returns true if the caret is visible, false otherwise.
*/
@@ -257,7 +257,7 @@ public class JEditTextArea extends JComponent
return (!caretBlinks || blink) && caretVisible;
}
/**
* Sets if the caret should be visible.
* @param caretVisible True if the caret should be visible, false
@@ -270,7 +270,7 @@ public class JEditTextArea extends JComponent
painter.invalidateSelectedLines();
}
/**
* Blinks the caret.
*/
@@ -283,7 +283,7 @@ public class JEditTextArea extends JComponent
}
}
/**
* Returns the number of lines from the top and button of the
* text area that are always visible.
@@ -292,7 +292,7 @@ public class JEditTextArea extends JComponent
return electricScroll;
}
/**
* Sets the number of lines from the top and bottom of the text
* area that are always visible
@@ -349,7 +349,7 @@ public class JEditTextArea extends JComponent
}
}
/**
* Returns the line displayed at the text area's origin.
*/
@@ -357,7 +357,7 @@ public class JEditTextArea extends JComponent
return firstLine;
}
/**
* Sets the line displayed at the text area's origin without
* updating the scroll bars.
@@ -371,15 +371,15 @@ public class JEditTextArea extends JComponent
}
painter.repaint();
}
/**
* Convenience for checking what's on-screen. [fry]
/**
* Convenience for checking what's on-screen. [fry]
*/
public final int getLastLine() {
return getFirstLine() + getVisibleLines();
}
/**
* Returns the number of lines visible in this text area.
@@ -388,7 +388,7 @@ public class JEditTextArea extends JComponent
return visibleLines;
}
/**
* Recalculates the number of visible lines. This should not
* be called directly.
@@ -402,7 +402,7 @@ public class JEditTextArea extends JComponent
updateScrollBars();
}
/**
* Returns the horizontal offset of drawn lines.
*/
@@ -410,7 +410,7 @@ public class JEditTextArea extends JComponent
return horizontalOffset;
}
/**
* Sets the horizontal offset of drawn lines. This can be used to
* implement horizontal scrolling.
@@ -427,7 +427,7 @@ public class JEditTextArea extends JComponent
painter.repaint();
}
/**
* A fast way of changing both the first line and horizontal
* offset.
@@ -442,12 +442,12 @@ public class JEditTextArea extends JComponent
this.horizontalOffset = horizontalOffset;
changed = true;
}
if (firstLine != this.firstLine) {
this.firstLine = firstLine;
changed = true;
}
if (changed) {
updateScrollBars();
painter.repaint();
@@ -455,7 +455,7 @@ public class JEditTextArea extends JComponent
return changed;
}
/**
* Ensures that the caret is visible by scrolling the text area if
* necessary.
@@ -471,7 +471,7 @@ public class JEditTextArea extends JComponent
return scrollTo(line,offset);
}
/**
* Ensures that the specified line and offset is visible by scrolling
* the text area if necessary.
@@ -516,7 +516,7 @@ public class JEditTextArea extends JComponent
return setOrigin(newFirstLine,newHorizontalOffset);
}
/**
* Converts a line index to a y co-ordinate.
* @param line The line
@@ -527,7 +527,7 @@ public class JEditTextArea extends JComponent
- (fm.getLeading() + fm.getMaxDescent());
}
/**
* Converts a y co-ordinate to a line index.
* @param y The y co-ordinate
@@ -539,7 +539,7 @@ public class JEditTextArea extends JComponent
y / height + firstLine));
}
/**
* Converts an offset in a line into an x co-ordinate. This is a
* slow version that can be used any time.
@@ -552,7 +552,7 @@ public class JEditTextArea extends JComponent
return _offsetToX(line,offset);
}
/**
* Converts an offset in a line into an x coordinate. This is a
* fast version that should only be used if no changes were made
@@ -575,9 +575,9 @@ public class JEditTextArea extends JComponent
if (tokenMarker == null) {
lineSegment.count = offset;
return x + Utilities.getTabbedTextWidth(lineSegment, fm, x, painter, 0);
} else {
// If syntax coloring is enabled, we have to do this
// If syntax coloring is enabled, we have to do this
// because tokens can vary in width
Token tokens;
if (painter.currentLineIndex == line && painter.currentLineTokens != null) {
@@ -1884,11 +1884,15 @@ public class JEditTextArea extends JComponent
// Seen often on Mac OS X when pasting from Safari. [fry 030929]
selection = selection.replace('\u00A0', ' ');
// Remove ASCII NUL characters. Reported when pasting from
// Acrobat Reader and PDF documents. [fry 130719]
// Remove ASCII NUL characters. Reported when pasting from
// Acrobat Reader and PDF documents. [fry 130719]
// https://github.com/processing/processing/issues/1973
if (selection.indexOf('\0') != -1) {
//System.out.println("found NUL charaacters");
//int before = selection.length();
selection = selection.replaceAll("\0", "");
//int after = selection.length();
//System.out.println(before + " " + after);
}
int repeatCount = inputHandler.getRepeatCount();
@@ -2329,8 +2333,8 @@ public class JEditTextArea extends JComponent
}
}
class DragHandler implements MouseMotionListener
class DragHandler implements MouseMotionListener
{
public void mouseDragged(MouseEvent evt) {
if (popup != null && popup.isVisible()) return;
+1
View File
@@ -12,6 +12,7 @@ o b86 supposed to have some support (not available yet)
o http://jdk8.java.net/download.html
X code with a NUL character causes an error
X https://github.com/processing/processing/issues/1973
X also remove NUL characters when loading a file
X Add "Processing Foundation" to the Help menu
X https://github.com/processing/processing/issues/1908
X Update JNA from 3.2.4 to 3.5.2