@@ -29,6 +29,7 @@ Processing version Copyright (c) 2004-05 Ben Fry and Casey Reas
29
29
30
30
package processing .app .preproc ;
31
31
32
+ import static processing .app .I18n ._ ;
32
33
import processing .app .*;
33
34
import processing .core .*;
34
35
@@ -61,38 +62,22 @@ public class PdePreprocessor {
61
62
// than the others, since the imports are auto-generated.
62
63
List <String > codeFolderImports ;
63
64
64
- String indent ;
65
-
66
- PrintStream stream ;
67
65
String program ;
68
- String buildPath ;
69
- // starts as sketch name, ends as main class name
70
- String name ;
71
66
72
67
73
68
/**
74
69
* Setup a new preprocessor.
75
70
*/
76
71
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 );
81
72
}
82
73
83
74
/**
84
75
* Writes out the head of the c++ code generated for a sketch.
85
76
* Called from processing.app.Sketch.
86
77
* @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)
90
78
*/
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 {
96
81
// if the program ends with no CR or LF an OutOfMemoryError will happen.
97
82
// not gonna track down the bug now, so here's a hack for it:
98
83
// http://dev.processing.org/bugs/show_bug.cgi?id=5
@@ -102,7 +87,7 @@ public int writePrefix(String program, String buildPath,
102
87
// an OutOfMemoryError or NullPointerException will happen.
103
88
// again, not gonna bother tracking this down, but here's a hack.
104
89
// http://dev.processing.org/bugs/show_bug.cgi?id=16
105
- Sketch . scrubComments (program );
90
+ scrubComments (program );
106
91
// If there are errors, an exception is thrown and this fxn exits.
107
92
108
93
if (Preferences .getBoolean ("preproc.substitute_unicode" )) {
@@ -134,10 +119,6 @@ public int writePrefix(String program, String buildPath,
134
119
// do this after the program gets re-combobulated
135
120
this .program = program ;
136
121
137
- // output the code
138
- File streamFile = new File (buildPath , name + ".cpp" );
139
- stream = new PrintStream (new FileOutputStream (streamFile ));
140
-
141
122
return headerCount + prototypeCount ;
142
123
}
143
124
@@ -181,17 +162,16 @@ static String substituteUnicode(String program) {
181
162
}
182
163
183
164
/**
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
186
170
*/
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 );
190
173
writeProgram (stream , program , prototypes );
191
174
writeFooter (stream );
192
- stream .close ();
193
-
194
- return name ;
195
175
}
196
176
197
177
// Write the pde program to the cpp file
@@ -344,4 +324,56 @@ public ArrayList<String> prototypes(String in) {
344
324
345
325
return functionMatches ;
346
326
}
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
+ }
347
379
}
0 commit comments