From 156676cf5607412e79a7da74723854784927ed41 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Mon, 6 Apr 2020 20:46:11 +0200 Subject: [PATCH] Code cleanup Fixes some lines which were too long Expands some variable names Fixes some code repetition --- .../net/knarcraft/ffmpegconverter/Main.java | 6 ++- .../net/knarcraft/ffmpegconverter/Parser.java | 40 +++++++++++++------ 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/main/java/net/knarcraft/ffmpegconverter/Main.java b/src/main/java/net/knarcraft/ffmpegconverter/Main.java index eee4c49..295738a 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/Main.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/Main.java @@ -27,7 +27,8 @@ public class Main { //parser(tokenizer("AnimeConverter \"C:\\Users\\Kristian\\Downloads\\Anime\\[Kametsu] ERASED (BD 1080p Hi10 FLAC)\"")); //System.exit(1); - int choice = getChoice("Which converter do you want do use?\n1. Anime to web mp4\n2. Audio converter\n3. VideoStream converter", 1, 3); + int choice = getChoice("Which converter do you want do use?\n1. Anime to web mp4\n2. Audio converter\n" + + "3. VideoStream converter", 1, 3); printl("Input for this converter:"); switch (choice) { @@ -144,7 +145,8 @@ public class Main { * @throws IOException

If reading or writing fails.

*/ private static void animeConverter() throws IOException { - printl("[Audio languages jpn,eng,ger,fre] [Subtitle languages eng,ger,fre] [Convert to stereo if necessary true/false] [Prevent signs&songs subtitles true/false]\nYour input: "); + printl("[Audio languages jpn,eng,ger,fre] [Subtitle languages eng,ger,fre] [Convert to stereo if " + + "necessary true/false] [Prevent signs&songs subtitles true/false]\nYour input: "); List input = readInput(4); String[] audioLang = new String[]{"jpn", "*"}; String[] subtitleLang = new String[]{"eng", "*"}; diff --git a/src/main/java/net/knarcraft/ffmpegconverter/Parser.java b/src/main/java/net/knarcraft/ffmpegconverter/Parser.java index 2e6f5bc..0061af3 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/Parser.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/Parser.java @@ -18,10 +18,12 @@ public class Parser { }; Main.converterArgument[] audioArgs = { - new Main.converterArgument("-outext", true, Main.converterArgumentValueType.SINGLE_VALUE) + new Main.converterArgument("-outext", true, + Main.converterArgumentValueType.SINGLE_VALUE) }; Main.converterArgument[] videoArgs = { - new Main.converterArgument("-outext", true, Main.converterArgumentValueType.SINGLE_VALUE) + new Main.converterArgument("-outext", true, + Main.converterArgumentValueType.SINGLE_VALUE) }; String type = tokens.get(0).toLowerCase(); if (!Main.listContains(types, s -> s.equals(type))) { @@ -46,39 +48,51 @@ public class Parser { boolean startedQuote = false; StringBuilder currentToken = new StringBuilder(); for (int i = 0; i < input.length(); i++) { - char c = input.charAt(i); - switch (c) { + char character = input.charAt(i); + switch (character) { case ' ': if (!startedQuote) { //If not inside "", a space marks the end of a parameter - if (!currentToken.toString().trim().equals("")) { + if (isNotEmpty(currentToken)) { tokens.add(currentToken.toString()); - currentToken = new StringBuilder(); - } else { - currentToken = new StringBuilder(); } + currentToken = new StringBuilder(); } else { - currentToken.append(c); + currentToken.append(character); } break; case '"': if (startedQuote) { - if (!currentToken.toString().trim().equals("")) { + //This quote signifies the end of the argument + if (isNotEmpty(currentToken)) { tokens.add(currentToken.toString()); currentToken = new StringBuilder(); } startedQuote = false; } else { + //This quote signifies the start of the argument startedQuote = true; currentToken = new StringBuilder(); - } break; + } + break; default: - currentToken.append(c); + //Adds a normal character to the token. Adds the current token to tokens if at the end of the input. + currentToken.append(character); if (i == input.length() - 1) { tokens.add(currentToken.toString()); - } break; + } + break; } } return tokens; } + + /** + * Checks whether a string builder is empty + * @param builder

The string builder to check.

+ * @return

True if the string builder is non empty.

+ */ + private static boolean isNotEmpty(StringBuilder builder) { + return !builder.toString().trim().equals(""); + } }