-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCTranslator.java
More file actions
74 lines (53 loc) · 2.13 KB
/
Copy pathCTranslator.java
File metadata and controls
74 lines (53 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.io.*;
public class CTranslator {
public CTranslator() {
}//CTranslator constructor
public void CTranslate(String line, File outfile) {
try {
PrintWriter writer = new PrintWriter(new FileOutputStream(outfile, true));//, "UTF-8"); // allows us to write into a new file
writer.println(line); // prints the line of code into the new file
writer.close(); // closes new file
} catch (Exception IOException) {
System.out.println("Some sort of IO error here");
}//try-catch
}//CTranslate
public void addJavaHeader(String name, File outfile) {
try {
PrintWriter writer = new PrintWriter(new FileOutputStream(outfile, true)); // declares writer as a new PrintWriter, allows us to write into new file
/*
Adds java headers
*/
writer.println("public class " + name + " {");
writer.println(" public static void main(String[] args) {");
writer.println();
writer.close();
} catch (Exception IOException) {
System.out.println("Some sort of IO error here");
}
}//addJavaHeader
public void CstringTrans(String line, File outfile) {
try {
PrintWriter writer = new PrintWriter(new FileOutputStream(outfile, true));
String[] printStatement = line.split("<<");
int lastentry = printStatement.length - 1;
if (printStatement[lastentry].equals(" endl;")) {
writer.print("System.out.println(");
lastentry--;
} else {
writer.print("System.out.print(");
}//if
for (int i = 1; i < lastentry - 1; i++) {
writer.print(printStatement[i] + " + ");
}//for
if (lastentry > 0) { //remove ; from the final entry
String newline = printStatement[lastentry].replaceAll("\\;","");
writer.println(newline + ");");
} else {
writer.println(");");
}//if-else
writer.close();
} catch (Exception IOException) {
System.out.println("Some sort of IO error here");
}
}//CTranslator
}