Add ability for Lua to query movie length, rerecord count and headers
[jpcrr.git] / ExceptionDefProcessor.java
blob5b6c47475da684ef73812c564ed969ab32a9f7ad
1 import java.util.*;
2 import java.io.*;
3 import java.nio.*;
4 import java.nio.charset.*;
6 class ExceptionDefProcessor
8 private static Map<Class<?>, String> classes;
10 static class UTFStream
12 FileOutputStream stream;
14 UTFStream(String name) throws IOException
16 stream = new FileOutputStream(name);
19 void println(String str)
21 try {
22 ByteBuffer buf;
23 buf = Charset.forName("UTF-8").newEncoder().encode(CharBuffer.wrap(str));
24 byte[] buf2 = new byte[buf.remaining() + 1];
25 buf.get(buf2, 0, buf.remaining());
26 buf2[buf2.length - 1] = 10;
27 stream.write(buf2);
28 } catch(Exception e) {
29 e.printStackTrace();
33 void close()
35 try {
36 stream.close();
37 } catch(Exception e) {
38 e.printStackTrace();
43 private static char identity(char x)
45 return x;
48 private static String getRevision() throws IOException
50 String x = "$Format:%h by %cn on %ci$";
51 if(identity(x.charAt(0)) != '$') {
52 System.err.println("Detected revision: " + x + ".");
53 return x;
55 ProcessBuilder gitproc = new ProcessBuilder();
56 gitproc.command("git", "log", "--pretty=format:%h by %cn on %ci", "-1");
57 Process git = gitproc.start();
58 InputStream output = git.getInputStream();
59 while(true) {
60 try {
61 if(git.waitFor() != 0)
62 throw new IOException("Git subprocess failed");
63 break;
64 } catch(InterruptedException e) {
67 BufferedReader r = new BufferedReader(new InputStreamReader(output));
68 x = r.readLine();
69 r.close();
71 System.err.println("Detected revision: " + x + ".");
72 return x;
75 private static String escapeString(String s)
77 StringBuffer r = new StringBuffer();;
78 for(int i = 0; i < s.length(); i++) {
79 char x = s.charAt(i);
80 if(x == '\"')
81 r.append("\"");
82 else if(x == '\\')
83 r.append("\\");
84 else
85 r.append(x);
87 return r.toString();
90 private static void doClass(String line)
92 int split = line.indexOf(32);
93 String clazz = line.substring(0, split);
94 String desc = line.substring(split + 1);
95 Class<?> classObject;
97 try {
98 classObject = Class.forName(clazz);
99 } catch(Exception e) {
100 System.err.println("Warning: Can't find class \"" + clazz + "\", dropping.");
101 return;
103 classes.put(classObject, desc);
106 public static void main(String[] args)
108 classes = new HashMap<Class<?>, String>();
110 if(args == null || args.length < 1) {
111 System.err.println("Syntax: java ExceptionDefProcessor <inputfile>");
112 return;
115 String autoexec = args[0];
116 try {
117 BufferedReader kbd2 = new BufferedReader(new InputStreamReader(
118 new FileInputStream(autoexec), "UTF-8"));
119 while(true) {
120 String cmd = kbd2.readLine();
121 if(cmd == null)
122 break;
123 if(!cmd.equals(""))
124 doClass(cmd);
126 } catch (Exception e) {
127 System.err.println("Failed to load exception defintions: " + e.getMessage());
130 Class<?> failingClass = null;
131 do {
132 if(failingClass != null)
133 classes.put(failingClass, failingClass.getName());
134 failingClass = null;
135 for(Map.Entry<Class<?>, String> x : classes.entrySet()) {
136 Class<?> superclass = x.getKey().getSuperclass();
137 if(x.getKey().getName().equals("java.lang.Error") ||
138 x.getKey().getName().equals("java.lang.RuntimeException"))
139 continue;
140 if(!classes.containsKey(superclass)) {
141 System.err.println("Warning: Missing superclass \"" + superclass.getName() + "\" for \"" +
142 x.getKey().getName() + "\".");
143 failingClass = superclass;
144 break;
147 } while(failingClass != null);
149 UTFStream stream = null;
150 try {
151 stream = new UTFStream("org/jpc/Exceptions.java");
152 } catch(Exception e) {
153 System.err.println("Can't open org/jpc/Exceptions.java: " + e.getMessage());
154 return;
157 stream.println("package org.jpc;");
158 stream.println("import java.util.*;");
159 stream.println("class Exceptions {");
160 stream.println("public static Map<String,String> classes;");
161 stream.println("static {");
162 stream.println("classes = new HashMap<String,String>();");
163 Class<?> out = null;
164 String desc = null;
165 do {
166 if(out != null) {
167 classes.remove(out);
168 stream.println("classes.put(\"" + out.getName() + "\", \"" + desc + "\");");
170 out = null;
171 for(Map.Entry<Class<?>, String> x : classes.entrySet()) {
172 Class<?> superclass = x.getKey().getSuperclass();
173 if(!classes.containsKey(superclass)) {
174 out = x.getKey();
175 desc = x.getValue();
176 break;
179 } while(out != null);
180 stream.println("}}");
181 stream.close();
183 try {
184 stream = new UTFStream("org/jpc/Revision.java");
185 } catch(Exception e) {
186 System.err.println("Can't open org/jpc/Revision.java: " + e.getMessage());
187 return;
189 stream.println("package org.jpc;");
190 stream.println("public class Revision {");
191 stream.println("public static String getRevision() {");
192 try {
193 stream.println("return \"" + escapeString(getRevision()) + "\";");
194 } catch(Exception e) {
195 System.err.println("Can't get revision: " + e.getMessage());
196 return;
198 stream.println("}}");
199 stream.close();