Java tutorial
/* * Copyright (c) 2018 Pawe Cholewa * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated * documentation files (the "Software"), to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and * to permit persons to whom the Software is furnished to do so, subject to the following conditions: * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the * software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ package pl.mcpg.brainfuckjava; import javax.swing.UIManager; import java.awt.EventQueue; import java.awt.Font; import java.awt.FontFormatException; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Locale; import java.util.Properties; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.DefaultParser; import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; import pl.mcpg.brainfuckjava.gui.ProgramFrame; import pl.mcpg.brainfuckjava.interpreter.DefaultBrainfuckInterpreter; public class Start { public static boolean numberDisplayMode = false; public static Font spacemonoFont; public static Locale userLocale = Locale.getDefault(); private static Properties language = new Properties(); public static void main(String[] args) { System.out.println("Detected language is " + userLocale.toString() + "."); switch (userLocale.toString()) { case "pl_PL": loadLanguage("pl"); break; default: loadLanguage("en"); break; } Config.load(); try { spacemonoFont = Font.createFont(Font.TRUETYPE_FONT, Start.class.getResourceAsStream("/fonts/spacemono.ttf")); } catch (FontFormatException | IOException e) { e.printStackTrace(); System.exit(1); } if (args.length == 0) // if there are no args, just run GUI. { runGUI(); return; } Options options = new Options(); Option forceLangOption = new Option("l", "forcelang", true, getText("forcelangCli")); forceLangOption.setArgName("language"); Option interactiveOption = new Option("i", "intercml", false, getText("interactiveCli")); Option runOption = new Option("r", "run", true, getText("runCli")); runOption.setArgs(1); Option displayNumbersOption = new Option("n", "displayint", false, getText("dnmCli")); Option helpOption = new Option("h", "help", false, getText("helpCli")); options.addOption(forceLangOption); options.addOption(interactiveOption); options.addOption(runOption); options.addOption(displayNumbersOption); options.addOption(helpOption); CommandLineParser parser = new DefaultParser(); CommandLine commandLine; try { commandLine = parser.parse(options, args); if (commandLine.hasOption('l')) { boolean found = true; switch (commandLine.getOptionValue('l')) { case "pl": loadLanguage("pl"); break; case "en": loadLanguage("en"); break; default: System.err.println(getText("forcedLangNotFound", commandLine.getOptionValue('l'))); found = false; break; } if (found) { System.out.println(getText("forcedLang", commandLine.getOptionValue('l'))); } } if (commandLine.hasOption('h')) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("java -jar brainfuck.java.jar [-h] [-n] [-i] [-r {file}]", "Brainfuck.java command line options", options, "Issues? Report them on https://github.com/MCPlayG/Brainfuck.java/issues!"); System.exit(0); } if (commandLine.hasOption('n')) { numberDisplayMode = true; } if (commandLine.hasOption('i')) { new InteractiveCommandLine().run(); System.exit(0); } if (commandLine.hasOption('r')) { run(new File(commandLine.getOptionValue('r'))); System.exit(0); } } catch (ParseException e) { System.err.println(getText("parseCliError")); e.printStackTrace(); runGUI(); } runGUI(); } public static String getText(String name, String... args) { if (language.getProperty(name) == null) { return "not found"; } return String.format(language.getProperty(name), args); } public static void run(File fileToRun) { ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[512]; try { FileInputStream fileStream = new FileInputStream(fileToRun); while (fileStream.read(buffer) != -1) { arrayOutputStream.write(buffer); } fileStream.close(); new DefaultBrainfuckInterpreter().readBrainfuck(new String(arrayOutputStream.toByteArray())).run(); arrayOutputStream.close(); } catch (FileNotFoundException e) { System.err.println(getText("fileNotFound", fileToRun.getName())); } catch (IOException e) { System.err.println(getText("readError")); e.printStackTrace(); } } private static void runGUI() { EventQueue.invokeLater(() -> { try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel"); } catch (Exception e) { e.printStackTrace(); } new ProgramFrame().setVisible(true); }); } private static void loadLanguage(String lang) { try { language.load(Start.class.getResourceAsStream("/lang/lang_" + lang + ".properties")); } catch (IOException e) { System.err.println("Error occurred while loading language " + lang + "."); e.printStackTrace(); System.exit(1); } } }