public void run() { try { mainLoop(); } catch (Throwable t) { StringUtil.printException(t); } }
/** GoGuiThumbnailer main function. */ public static void main(String[] args) { try { String options[] = { // "check-expire:", // experimental; needs more testing "config:", // "expire:", // experimental; needs more testing "help", "scale", "size:", "verbose", "version" }; Options opt = Options.parse(args, options); if (opt.contains("help")) { printUsage(System.out); return; } if (opt.contains("version")) { System.out.println("GoGuiThumbnailer " + Version.get()); return; } /* if (opt.contains("expire")) { int seconds = opt.getInteger("expire", 0, 0); ThumbnailUtil.expire(seconds, false); return; } if (opt.contains("check-expire")) { int seconds = opt.getInteger("expire", 0, 0); ThumbnailUtil.expire(seconds, true); return; } */ boolean verbose = opt.contains("verbose"); boolean scale = opt.contains("scale"); ArrayList<String> arguments = opt.getArguments(); if (arguments.isEmpty() || arguments.size() > 2) { printUsage(System.err); System.exit(1); } File input = new File(arguments.get(0)); File output = null; if (arguments.size() == 2) output = new File(arguments.get(1)); int size = opt.getInteger("size", 128, 1); ThumbnailCreator thumbnailCreator = new ThumbnailCreator(verbose); try { thumbnailCreator.create(input, output, size, scale); } catch (ThumbnailCreator.Error e) { System.err.println(e.getMessage()); System.exit(1); } } catch (Throwable t) { StringUtil.printException(t); System.exit(1); } }
/** * Constructor. * * @param program Command line for program. Will be split into words with respect to " as in * StringUtil.tokenize. If the command line contains the string "%SRAND", it will be replaced * by a random seed. This is useful if the random seed can be set by a command line option to * produce deterministic randomness (the command returned by getProgramCommand() will contain * the actual random seed used). * @param workingDirectory The working directory to run the program in or null for the current * directory * @param log Log input, output and error stream to standard error. * @param callback Callback for external display of the streams. */ public GtpClient(String program, File workingDirectory, boolean log, IOCallback callback) throws GtpClient.ExecFailed { if (workingDirectory != null && !workingDirectory.isDirectory()) throw new ExecFailed(program, "Invalid working directory \"" + workingDirectory + "\""); m_log = log; m_callback = callback; m_wasKilled = false; if (program.indexOf("%SRAND") >= 0) { // RAND_MAX in stdlib.h ist at least 32767 int randMax = 32767; int rand = (int) (Math.random() * (randMax + 1)); program = program.replaceAll("%SRAND", Integer.toString(rand)); } m_program = program; if (StringUtil.isEmpty(program)) throw new ExecFailed(program, "Command for invoking Go program must be" + " not empty."); Runtime runtime = Runtime.getRuntime(); try { // Create command array with StringUtil::splitArguments // because Runtime.exec(String) uses a default StringTokenizer // which does not respect ". String[] cmdArray = StringUtil.splitArguments(program); // Make file name absolute, if working directory is not current // directory. With Java 1.5, it seems that Runtime.exec succeeds // if the relative path is valid from the current, but not from // the given working directory, but the process is not usable // (reading from its input stream immediately returns // end-of-stream) if (cmdArray.length > 0) { File file = new File(cmdArray[0]); // Only replace if executable is a path to a file, not // an executable in the exec-path if (file.exists()) cmdArray[0] = file.getAbsolutePath(); } m_process = runtime.exec(cmdArray, null, workingDirectory); } catch (IOException e) { throw new ExecFailed(program, e); } init(m_process.getInputStream(), m_process.getOutputStream(), m_process.getErrorStream()); }
public void run() { try { char[] buffer = new char[4096]; while (true) { int n; try { n = m_in.read(buffer); } catch (IOException e) { return; } if (n <= 0) return; String text = new String(buffer, 0, n); if (m_callback != null) m_callback.receivedStdErr(text); if (m_log) logError(text); } } catch (Throwable t) { StringUtil.printException(t); } }
/** Return a string containing information and statistics of the subtree of a node. */ public static String treeInfo(ConstNode node) { int numberNodes = 0; int numberTerminal = 0; int moreThanOneChild = 0; int maxDepth = 0; int maxChildren = 0; double averageDepth = 0; double averageChildren = 0; double averageChildrenInner = 0; int rootDepth = getDepth(node); while (node != null) { ++numberNodes; int numberChildren = node.getNumberChildren(); int depth = getDepth(node) - rootDepth; assert depth >= 0; if (depth > maxDepth) maxDepth = depth; if (numberChildren > maxChildren) maxChildren = numberChildren; if (numberChildren == 0) ++numberTerminal; else averageChildrenInner += numberChildren; if (numberChildren > 1) ++moreThanOneChild; averageDepth += depth; averageChildren += numberChildren; node = nextNode(node, rootDepth); } int numberInner = numberNodes - numberTerminal; averageDepth /= numberNodes; averageChildren /= numberNodes; averageChildrenInner /= Math.max(numberInner, 1); NumberFormat format = StringUtil.getNumberFormat(3); format.setMinimumFractionDigits(3); StringBuilder buffer = new StringBuilder(); appendInfo(buffer, "Nodes", numberNodes); appendInfo(buffer, "Terminal", numberTerminal); appendInfo(buffer, "Inner", numberInner); appendInfo(buffer, "AvgDepth", format.format(averageDepth)); appendInfo(buffer, "MaxDepth", maxDepth); appendInfo(buffer, "AvgChildren", format.format(averageChildren)); appendInfo(buffer, "AvgChildrenInner", format.format(averageChildrenInner)); appendInfo(buffer, "MaxChildren", maxChildren); appendInfo(buffer, "MoreThanOneChild", moreThanOneChild); return buffer.toString(); }
/** * Get first node of a given variation. Searches the node that can be reached from the root node * by taking the children defined by the integers in the variation string for nodes with more than * one child. * * @param root The root node of the tree. * @param variation The variation string (e.g. "1.1.3.1.5"). * @return The first node of the given variation, or the root node, if the variation string is * empty, or <code>null</code>, if the variation string is invalid or does not specify a node * in the given tree. */ public static ConstNode findByVariation(ConstNode root, String variation) { if (variation.trim().equals("")) return root; String[] tokens = StringUtil.split(variation, '.'); int[] n = new int[tokens.length]; for (int i = 0; i < tokens.length; ++i) { try { n[i] = Integer.parseInt(tokens[i]) - 1; if (n[i] < 0) return null; } catch (NumberFormatException e) { return null; } } ConstNode node = root; for (int i = 0; i < n.length; ++i) { while (node.getNumberChildren() <= 1) { node = node.getChildConst(); if (node == null) return null; } if (n[i] >= node.getNumberChildren()) return null; node = node.getChildConst(n[i]); } return node; }