import java.util.*; import java.io.*; import java.util.regex.*; import java.net.*; public class main { static String[] standardFunctions = { "#2000323/saveTextFile", "#2000322/loadTextFile", "#2000324/loadMainJava", "#2000325/saveMainJava", "#2000326/toLines", "#2000329/fromLines", "#2000330/loadSnippet", "#2000330/parseSnippetID", "#2000331/makeJavaStringLiteral", "#2000367/popupError", "#2000368/exitOnFrameClose", "#2000378/commonPrefix", "#2000379/commonSuffix", "#2000442/quote", "#2000471/unquote", "#2000444/cncToLines", "#2000460/bytesToHex", "#2000466/loadBinaryPage", "#2000464/saveBinaryFile" }; public static void main(String[] args) throws IOException { String s = loadMainJava(); List defd = findFunctions(s); for (int i = 0; i < 2; i++) for (String x : standardFunctions) { String[] f = x.split("/"); if (hasInvocation(s, f[1]) && !defd.contains(f[1])) { System.out.println("Adding function: " + f[1] + " (" + f[0] + ")"); s = addFunction(s, f[0]); defd = findFunctions(s); //defd.add(f[1]); } } saveMainJava(s); } static boolean hasInvocation(String src, String f) { return src.indexOf(f) >= 0; // generous matching :) } static List findFunctions(String src) { int[] main = findMain(src); src = src.substring(main[0], main[1]); /*int idx = src.indexOf("main {"); // yes it's a hack... if (idx >= 0) src = src.substring(idx);*/ //System.out.println("Scanning for functions"); List functions = new ArrayList(); for (String line : toLines(src)) { Matcher matcher = Pattern.compile("static.*\\s+(\\w+)\\(").matcher(line); if (matcher.find()) { String f = matcher.group(1); functions.add(f); //System.out.println("Function found: " + f); } } return functions; } public static String addFunction(String s, String fID) throws IOException { int[] main = findMain(s); int i = s.lastIndexOf('}', main[1]); String function = loadSnippet(fID); return s.substring(0, i) + "\n" + function +"\n" + s.substring(i); } static findMain(String s) { List tok = JavaTok.split(s); for (int i = 1; i+2 < tok.size(); i += 2) if (tok.get(i).equals("class") && tok.get(i+2).equals("main")) { int j = i; while (j < tok.size() && !tok.get(j).equals("{")) j += 2; int k = findEndOfBlock(tok, j); return new int[] {cncLength(tok, i), cncLength(tok, k)}; } //throw new RuntimeException("main class not found"); return new int[] {0, s.length()}; } static int cncLength(List cnc, int idx) { int l = 0; for (int i = 0; i < idx; i++) l += cnc.get(i).length(); return l; } static int findEndOfBlock(List cnc, int i) { int j = i+2, level = 1; while (j < cnc.size()) { if (cnc.get(j).equals("{")) ++level; else if (cnc.get(j).equals("}")) --level; if (level == 0) return j+1; ++j; } return cnc.size(); } public static List toLines(String s) { List lines = new ArrayList(); int start = 0; while (true) { int i = toLines_nextLineBreak(s, start); if (i < 0) { if (s.length() > start) lines.add(s.substring(start)); break; } lines.add(s.substring(start, i)); if (s.charAt(i) == '\r' && i+1 < s.length() && s.charAt(i+1) == '\n') i += 2; else ++i; start = i; } return lines; } private static int toLines_nextLineBreak(String s, int start) { for (int i = start; i < s.length(); i++) { char c = s.charAt(i); if (c == '\r' || c == '\n') return i; } return -1; } static String loadMainJava() throws IOException { return loadTextFile("input/main.java", ""); } static void saveMainJava(String s) throws IOException { saveTextFile("output/main.java", s); } static String charsetForTextFiles = "UTF-8"; public static String loadTextFile(String fileName, String defaultContents) throws IOException { if (!new File(fileName).exists()) return defaultContents; FileInputStream fileInputStream = new FileInputStream(fileName); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, charsetForTextFiles); return loadTextFile(inputStreamReader); } public static String loadTextFile(Reader reader) throws IOException { StringBuilder builder = new StringBuilder(); try { BufferedReader bufferedReader = new BufferedReader(reader); String line; while ((line = bufferedReader.readLine()) != null) builder.append(line).append('\n'); } finally { reader.close(); } return builder.length() == 0 ? "" : builder.substring(0, builder.length()-1); } /** writes safely (to temp file, then rename) */ public static void saveTextFile(String fileName, String contents) throws IOException { File file = new File(fileName); File parentFile = file.getParentFile(); if (parentFile != null) parentFile.mkdirs(); String tempFileName = fileName + "_temp"; FileOutputStream fileOutputStream = new FileOutputStream(tempFileName); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, charsetForTextFiles); PrintWriter printWriter = new PrintWriter(outputStreamWriter); printWriter.print(contents); printWriter.close(); if (file.exists() && !file.delete()) throw new IOException("Can't delete " + fileName); if (!new File(tempFileName).renameTo(file)) throw new IOException("Can't rename " + tempFileName + " to " + fileName); } public static String loadSnippet(String snippetID) throws IOException { return loadSnippet(snippetID, preferCached); } public static String loadSnippet(String snippetID, boolean preferCached) throws IOException { return loadSnippet(parseSnippetID(snippetID), preferCached); } public static long parseSnippetID(String snippetID) { return Long.parseLong(shortenSnippetID(snippetID)); } private static String shortenSnippetID(String snippetID) { if (snippetID.startsWith("#")) snippetID = snippetID.substring(1); String httpBlaBla = "http://tinybrain.de/"; if (snippetID.startsWith(httpBlaBla)) snippetID = snippetID.substring(httpBlaBla.length()); return snippetID; } public static boolean isSnippetID(String snippetID) { snippetID = shortenSnippetID(snippetID); return isInteger(snippetID) && Long.parseLong(snippetID) != 0; } public static boolean isInteger(String s) { return Pattern.matches("\\-?\\d+", s); } static boolean preferCached = false; public static String loadSnippet(long snippetID) throws IOException { return loadSnippet(snippetID, preferCached); } public static String loadSnippet(long snippetID, boolean preferCached) throws IOException { if (preferCached) { initSnippetCache(); String text = DiskSnippetCache_get(snippetID); if (text != null) return text; } String text; try { URL url = new URL("http://tinybrain.de:8080/getraw.php?id=" + snippetID); text = loadPage(url); } catch (FileNotFoundException e) { throw new IOException("Snippet #" + snippetID + " not found or not public"); } try { initSnippetCache(); DiskSnippetCache_put(snippetID, text); } catch (IOException e) { System.err.println("Minor warning: Couldn't save snippet to cache (" + DiskSnippetCache_getDir() + ")"); } return text; } private static String loadPage(URL url) throws IOException { System.out.println("Loading: " + url.toExternalForm()); URLConnection con = url.openConnection(); return loadPage(con, url); } public static String loadPage(URLConnection con, URL url) throws IOException { String contentType = con.getContentType(); if (contentType == null) throw new IOException("Page could not be read: " + url); //Log.info("Content-Type: " + contentType); String charset = guessCharset(contentType); Reader r = new InputStreamReader(con.getInputStream(), charset); StringBuilder buf = new StringBuilder(); while (true) { int ch = r.read(); if (ch < 0) break; //Log.info("Chars read: " + buf.length()); buf.append((char) ch); } return buf.toString(); } public static String guessCharset(String contentType) { Pattern p = Pattern.compile("text/html;\\s+charset=([^\\s]+)\\s*"); Matcher m = p.matcher(contentType); /* If Content-Type doesn't match this pre-conception, choose default and hope for the best. */ return m.matches() ? m.group(1) : "ISO-8859-1"; } static File DiskSnippetCache_dir; public static void initDiskSnippetCache(File dir) { DiskSnippetCache_dir = dir; dir.mkdirs(); } public static synchronized String DiskSnippetCache_get(long snippetID) throws IOException { return loadTextFile(DiskSnippetCache_getFile(snippetID).getPath(), null); } private static File DiskSnippetCache_getFile(long snippetID) { return new File(DiskSnippetCache_dir, "" + snippetID); } public static synchronized void DiskSnippetCache_put(long snippetID, String snippet) throws IOException { saveTextFile(DiskSnippetCache_getFile(snippetID).getPath(), snippet); } public static File DiskSnippetCache_getDir() { return DiskSnippetCache_dir; } public static void initSnippetCache() { if (DiskSnippetCache_dir == null) initDiskSnippetCache(new File(System.getProperty("user.home"), ".tinybrain/snippet-cache")); } }