Skip to content

Commit 316b871

Browse files
committed
Refactored PdePreprocessor class
1 parent 8a8bb44 commit 316b871

File tree

2 files changed

+72
-106
lines changed

2 files changed

+72
-106
lines changed

app/src/processing/app/Sketch.java

+9-75
Original file line numberDiff line numberDiff line change
@@ -1339,7 +1339,6 @@ public String preprocess(String buildPath, PdePreprocessor preprocessor) throws
13391339
// make sure the user didn't hide the sketch folder
13401340
ensureExistence();
13411341

1342-
String[] codeFolderPackages = null;
13431342
classPath = buildPath;
13441343

13451344
// // figure out the contents of the code folder to see if there
@@ -1381,12 +1380,8 @@ public String preprocess(String buildPath, PdePreprocessor preprocessor) throws
13811380
// Note that the headerOffset isn't applied until compile and run, because
13821381
// it only applies to the code after it's been written to the .java file.
13831382
int headerOffset = 0;
1384-
//PdePreprocessor preprocessor = new PdePreprocessor();
13851383
try {
1386-
headerOffset = preprocessor.writePrefix(bigCode.toString(),
1387-
buildPath,
1388-
name,
1389-
codeFolderPackages);
1384+
headerOffset = preprocessor.writePrefix(bigCode.toString());
13901385
} catch (FileNotFoundException fnfe) {
13911386
fnfe.printStackTrace();
13921387
String msg = _("Build folder disappeared or could not be written");
@@ -1399,24 +1394,14 @@ public String preprocess(String buildPath, PdePreprocessor preprocessor) throws
13991394
String primaryClassName = null;
14001395

14011396
try {
1402-
// if (i != 0) preproc will fail if a pde file is not
1403-
// java mode, since that's required
1404-
String className = preprocessor.write();
1405-
1406-
if (className == null) {
1407-
throw new RunnerException(_("Could not find main class"));
1408-
// this situation might be perfectly fine,
1409-
// (i.e. if the file is empty)
1410-
//System.out.println("No class found in " + code[i].name);
1411-
//System.out.println("(any code in that file will be ignored)");
1412-
//System.out.println();
1413-
1414-
// } else {
1415-
// code[0].setPreprocName(className + ".java");
1416-
}
1417-
1397+
// Output file
1398+
File streamFile = new File(buildPath, name + ".cpp");
1399+
FileOutputStream outputStream = new FileOutputStream(streamFile);
1400+
preprocessor.write(outputStream);
1401+
outputStream.close();
1402+
14181403
// store this for the compiler and the runtime
1419-
primaryClassName = className + ".cpp";
1404+
primaryClassName = name + ".cpp";
14201405

14211406
} catch (FileNotFoundException fnfe) {
14221407
fnfe.printStackTrace();
@@ -1668,58 +1653,7 @@ protected String upload(String buildPath, String suggestedClassName, boolean usi
16681653
return success ? suggestedClassName : null;
16691654
}
16701655

1671-
/**
1672-
* Replace all commented portions of a given String as spaces.
1673-
* Utility function used here and in the preprocessor.
1674-
*/
1675-
static public String scrubComments(String what) {
1676-
char p[] = what.toCharArray();
1677-
1678-
int index = 0;
1679-
while (index < p.length) {
1680-
// for any double slash comments, ignore until the end of the line
1681-
if ((p[index] == '/') &&
1682-
(index < p.length - 1) &&
1683-
(p[index+1] == '/')) {
1684-
p[index++] = ' ';
1685-
p[index++] = ' ';
1686-
while ((index < p.length) &&
1687-
(p[index] != '\n')) {
1688-
p[index++] = ' ';
1689-
}
1690-
1691-
// check to see if this is the start of a new multiline comment.
1692-
// if it is, then make sure it's actually terminated somewhere.
1693-
} else if ((p[index] == '/') &&
1694-
(index < p.length - 1) &&
1695-
(p[index+1] == '*')) {
1696-
p[index++] = ' ';
1697-
p[index++] = ' ';
1698-
boolean endOfRainbow = false;
1699-
while (index < p.length - 1) {
1700-
if ((p[index] == '*') && (p[index+1] == '/')) {
1701-
p[index++] = ' ';
1702-
p[index++] = ' ';
1703-
endOfRainbow = true;
1704-
break;
1705-
1706-
} else {
1707-
// continue blanking this area
1708-
p[index++] = ' ';
1709-
}
1710-
}
1711-
if (!endOfRainbow) {
1712-
throw new RuntimeException(_("Missing the */ from the end of a " +
1713-
"/* comment */"));
1714-
}
1715-
} else { // any old character, move along
1716-
index++;
1717-
}
1718-
}
1719-
return new String(p);
1720-
}
1721-
1722-
1656+
17231657
public boolean exportApplicationPrompt() throws IOException, RunnerException {
17241658
return false;
17251659
}

app/src/processing/app/preproc/PdePreprocessor.java

+63-31
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Processing version Copyright (c) 2004-05 Ben Fry and Casey Reas
2929

3030
package processing.app.preproc;
3131

32+
import static processing.app.I18n._;
3233
import processing.app.*;
3334
import processing.core.*;
3435

@@ -61,38 +62,22 @@ public class PdePreprocessor {
6162
// than the others, since the imports are auto-generated.
6263
List<String> codeFolderImports;
6364

64-
String indent;
65-
66-
PrintStream stream;
6765
String program;
68-
String buildPath;
69-
// starts as sketch name, ends as main class name
70-
String name;
7166

7267

7368
/**
7469
* Setup a new preprocessor.
7570
*/
7671
public PdePreprocessor() {
77-
int tabSize = Preferences.getInteger("editor.tabs.size");
78-
char[] indentChars = new char[tabSize];
79-
Arrays.fill(indentChars, ' ');
80-
indent = new String(indentChars);
8172
}
8273

8374
/**
8475
* Writes out the head of the c++ code generated for a sketch.
8576
* Called from processing.app.Sketch.
8677
* @param program the concatenated code from all tabs containing pde-files
87-
* @param buildPath the path into which the processed pde-code is to be written
88-
* @param name the name of the sketch
89-
* @param codeFolderPackages unused param (leftover from processing)
9078
*/
91-
public int writePrefix(String program, String buildPath,
92-
String sketchName, String codeFolderPackages[]) throws FileNotFoundException {
93-
this.buildPath = buildPath;
94-
this.name = sketchName;
95-
79+
public int writePrefix(String program)
80+
throws FileNotFoundException {
9681
// if the program ends with no CR or LF an OutOfMemoryError will happen.
9782
// not gonna track down the bug now, so here's a hack for it:
9883
// http://dev.processing.org/bugs/show_bug.cgi?id=5
@@ -102,7 +87,7 @@ public int writePrefix(String program, String buildPath,
10287
// an OutOfMemoryError or NullPointerException will happen.
10388
// again, not gonna bother tracking this down, but here's a hack.
10489
// http://dev.processing.org/bugs/show_bug.cgi?id=16
105-
Sketch.scrubComments(program);
90+
scrubComments(program);
10691
// If there are errors, an exception is thrown and this fxn exits.
10792

10893
if (Preferences.getBoolean("preproc.substitute_unicode")) {
@@ -134,10 +119,6 @@ public int writePrefix(String program, String buildPath,
134119
// do this after the program gets re-combobulated
135120
this.program = program;
136121

137-
// output the code
138-
File streamFile = new File(buildPath, name + ".cpp");
139-
stream = new PrintStream(new FileOutputStream(streamFile));
140-
141122
return headerCount + prototypeCount;
142123
}
143124

@@ -181,17 +162,16 @@ static String substituteUnicode(String program) {
181162
}
182163

183164
/**
184-
* preprocesses a pde file and writes out a java file
185-
* @return the classname of the exported Java
165+
* preprocesses a pde file and writes out a cpp file into the specified
166+
* OutputStream
167+
*
168+
* @param output
169+
* @throws Exception
186170
*/
187-
//public String write(String program, String buildPath, String name,
188-
// String extraImports[]) throws java.lang.Exception {
189-
public String write() throws java.lang.Exception {
171+
public void write(OutputStream output) throws Exception {
172+
PrintStream stream = new PrintStream(output);
190173
writeProgram(stream, program, prototypes);
191174
writeFooter(stream);
192-
stream.close();
193-
194-
return name;
195175
}
196176

197177
// Write the pde program to the cpp file
@@ -344,4 +324,56 @@ public ArrayList<String> prototypes(String in) {
344324

345325
return functionMatches;
346326
}
327+
328+
329+
/**
330+
* Replace all commented portions of a given String as spaces.
331+
* Utility function used here and in the preprocessor.
332+
*/
333+
static public String scrubComments(String what) {
334+
char p[] = what.toCharArray();
335+
336+
int index = 0;
337+
while (index < p.length) {
338+
// for any double slash comments, ignore until the end of the line
339+
if ((p[index] == '/') &&
340+
(index < p.length - 1) &&
341+
(p[index+1] == '/')) {
342+
p[index++] = ' ';
343+
p[index++] = ' ';
344+
while ((index < p.length) &&
345+
(p[index] != '\n')) {
346+
p[index++] = ' ';
347+
}
348+
349+
// check to see if this is the start of a new multiline comment.
350+
// if it is, then make sure it's actually terminated somewhere.
351+
} else if ((p[index] == '/') &&
352+
(index < p.length - 1) &&
353+
(p[index+1] == '*')) {
354+
p[index++] = ' ';
355+
p[index++] = ' ';
356+
boolean endOfRainbow = false;
357+
while (index < p.length - 1) {
358+
if ((p[index] == '*') && (p[index+1] == '/')) {
359+
p[index++] = ' ';
360+
p[index++] = ' ';
361+
endOfRainbow = true;
362+
break;
363+
364+
} else {
365+
// continue blanking this area
366+
p[index++] = ' ';
367+
}
368+
}
369+
if (!endOfRainbow) {
370+
throw new RuntimeException(_("Missing the */ from the end of a " +
371+
"/* comment */"));
372+
}
373+
} else { // any old character, move along
374+
index++;
375+
}
376+
}
377+
return new String(p);
378+
}
347379
}

0 commit comments

Comments
 (0)