Skip to content

Commit 12c52b5

Browse files
committed
sketch load/save all utf-8. add item to tools menu for reload using local
encoding
1 parent b63aa3e commit 12c52b5

4 files changed

Lines changed: 83 additions & 38 deletions

File tree

app/src/processing/app/Base.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,7 +1432,8 @@ static public String loadFile(File file) throws IOException {
14321432
// empty code file.. no worries, might be getting filled up later
14331433
if (file.length() == 0) return "";
14341434

1435-
InputStreamReader isr = new InputStreamReader(new FileInputStream(file));
1435+
FileInputStream fis = new FileInputStream(file);
1436+
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
14361437
BufferedReader reader = new BufferedReader(isr);
14371438

14381439
StringBuffer buffer = new StringBuffer();
@@ -1449,15 +1450,14 @@ static public String loadFile(File file) throws IOException {
14491450
/**
14501451
* Spew the contents of a String object out to a file.
14511452
*/
1452-
static public void saveFile(String str,
1453-
File file) throws IOException {
1454-
1453+
static public void saveFile(String str, File file) throws IOException {
14551454
ByteArrayInputStream bis = new ByteArrayInputStream(str.getBytes());
14561455
InputStreamReader isr = new InputStreamReader(bis);
14571456
BufferedReader reader = new BufferedReader(isr);
14581457

1459-
FileWriter fw = new FileWriter(file);
1460-
PrintWriter writer = new PrintWriter(new BufferedWriter(fw));
1458+
FileOutputStream fos = new FileOutputStream(file);
1459+
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
1460+
PrintWriter writer = new PrintWriter(osw);
14611461

14621462
String line = null;
14631463
while ((line = reader.readLine()) != null) {

app/src/processing/app/Editor.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,54 @@ public void actionPerformed(ActionEvent e) {
683683
}
684684
});
685685
menu.add(item);
686+
687+
item = new JMenuItem("Fix encoding and reload file");
688+
item.addActionListener(new ActionListener() {
689+
public void actionPerformed(ActionEvent e) {
690+
SwingUtilities.invokeLater(new Runnable() {
691+
public void run() {
692+
SketchCode code = sketch.current;
693+
if (code.modified) {
694+
int result =
695+
JOptionPane.showConfirmDialog(Editor.this,
696+
"Discard changes and reload?",
697+
"Reload",
698+
JOptionPane.YES_NO_OPTION,
699+
JOptionPane.QUESTION_MESSAGE);
700+
701+
if (result == JOptionPane.NO_OPTION) {
702+
return;
703+
}
704+
}
705+
File file = code.file;
706+
707+
// empty code file.. no worries, might be getting filled up later
708+
if (file.length() != 0) {
709+
try {
710+
FileReader fr = new FileReader(file);
711+
BufferedReader reader = new BufferedReader(fr);
712+
713+
StringBuffer buffer = new StringBuffer();
714+
String line = null;
715+
while ((line = reader.readLine()) != null) {
716+
buffer.append(line);
717+
buffer.append('\n');
718+
}
719+
reader.close();
720+
beginCompoundEdit();
721+
textarea.setText(buffer.toString());
722+
endCompoundEdit();
686723

724+
} catch (IOException e) {
725+
e.printStackTrace();
726+
}
727+
}
728+
}
729+
});
730+
}
731+
});
732+
menu.add(item);
733+
687734
/*
688735
item = new JMenuItem("Export Folder...");
689736
item.addActionListener(new ActionListener() {

app/src/processing/app/SketchCode.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ public class SketchCode {
6161
public int scrollPosition;
6262

6363
public boolean modified;
64-
//SketchHistory history; // TODO add history information
6564

6665
public String preprocName; // name of .java file after preproc
6766
public int preprocOffset; // where this code starts relative to the concat'd code
@@ -75,7 +74,7 @@ public SketchCode(String name, File file, int flavor) {
7574
try {
7675
load();
7776
} catch (IOException e) {
78-
System.err.println("error while loading code " + name);
77+
System.err.println("Error while loading code " + name);
7978
}
8079
}
8180

todo.txt

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
11
0141 pde
22

3-
3+
charset changes
44
_ make sure that export is using utf8 for writing the .pde files etc
55
_ should be primarily loadFile() and saveFile() inside Base
6-
_ http://dev.processing.org/bugs/show_bug.cgi?id=743
7-
_ change pde files to use utf8
8-
_ 1) if file contains binary data and
9-
_ 2) its mod date is earlier than when p5 0125 was installed
10-
_ point the user to Tools -> Reload sketch with local encoding
11-
_ then re-save the file to update the mod date
12-
_ ...or, when first running p5 0125, offer to update sketches
13-
_ this is a bad idea--since it's probably
14-
_ need to set a default charset for use in files (utf8)
15-
_ add option to change charset or specify as part of loading
16-
_ need to specify the default encoding
6+
X http://dev.processing.org/bugs/show_bug.cgi?id=743
7+
X change pde files to use utf8
8+
o 1) if file contains binary data and
9+
o 2) its mod date is earlier than when p5 0125 was installed
10+
o point the user to Tools -> Reload sketch with local encoding
11+
o then re-save the file to update the mod date
12+
o ...or, when first running p5 0125, offer to update sketches
13+
o this is a bad idea--since it's probably
14+
X need to set a default charset for use in files (utf8)
15+
X add option to change charset or specify as part of loading
16+
X need to specify the default encoding
1717
_ xml element needs to be readable from other charsets
1818
_ same with the other methods like loadStrings()
1919
_ could also be a way to handle gzip too?
2020
_ tho charset + gzip would be a problem
2121

22-
_ inside Sketch.java, don't hardwire the file extension types
23-
_ arduino uses .c, .cpp, .h instead of .java
24-
_ http://dev.processing.org/bugs/show_bug.cgi?id=807
22+
23+
. . .
24+
25+
26+
[ needs verification ]
2527

2628
_ check to see whether this bug is fixed once 0140 is released
2729
_ properly handle non-ascii chars in p5 folder name
@@ -31,9 +33,13 @@ o perhaps the get around this by building into sketch folder
3133
o when non-ascii chars in use, just launch everything externally
3234
_ also fails with export-to-application
3335
_ http://dev.processing.org/bugs/show_bug.cgi?id=252
34-
35-
36-
. . .
36+
_ too many NPEs on loadimage may freeze the app (visualizar example?)
37+
_ hopefully this should be fixed with 0136 changes
38+
_ lots of runtime exceptions still being lost on osx
39+
_ particularly with multi-threaded applications
40+
_ macosx dropping exceptions all the time.. grr
41+
_ solution is to export, and then see how it runs
42+
_ this is particularly bad with threaded applications
3743

3844

3945
[ known bugs ]
@@ -218,11 +224,7 @@ X move p5 site bug reporting to bugzilla
218224
_ do the same for suggestions
219225
_ and detail the suggestions policy on the dev site: diy
220226
_ post a page explaining the bug reporting system
221-
222-
223-
DOC / Frequent
224-
225-
_ arrays
227+
_ arrays (frequent questions)
226228
_ using arraylist (avoid vector)
227229
_ cannot use generics
228230
_ using mod to avoid shifting an array
@@ -232,6 +234,7 @@ _ using expand and arrays to move quickly
232234

233235

234236
DOC / Write Me
237+
235238
_ quicktime likes to crash (not just on windows)
236239
_ http://dev.processing.org/bugs/show_bug.cgi?id=791
237240
_ createInput, createInputRaw, createOutput
@@ -813,13 +816,6 @@ _ will return 1.5.0 (or maybe 1.6 for others?)
813816

814817
PDE / Runner
815818

816-
_ too many NPEs on loadimage may freeze the app (visualizar example?)
817-
_ hopefully this should be fixed with 0136 changes
818-
_ lots of runtime exceptions still being lost on osx
819-
_ particularly with multi-threaded applications
820-
_ macosx dropping exceptions all the time.. grr
821-
_ solution is to export, and then see how it runs
822-
_ this is particularly bad with threaded applications
823819
_ use debugger to get better exceptions
824820
_ and getting the error to show up in the window inside p5
825821
_ also highlighting the correct line
@@ -851,6 +847,9 @@ _ is this only a problem on macosx?
851847

852848
PDE / Sketch & Sketchbook
853849

850+
_ inside Sketch.java, don't hardwire the file extension types
851+
_ arduino uses .c, .cpp, .h instead of .java
852+
_ http://dev.processing.org/bugs/show_bug.cgi?id=807
854853
_ don't reload sketch on "save as"
855854
_ this can result in loss of data (undo is lost)
856855
_ http://dev.processing.org/bugs/show_bug.cgi?id=433

0 commit comments

Comments
 (0)