Starts moving parsing to its own class

This commit is contained in:
Kristian Knarvik 2020-02-11 19:15:51 +01:00
parent 1887d7c1fe
commit 93f8a28872
2 changed files with 56 additions and 47 deletions

View File

@ -11,6 +11,8 @@ import java.util.List;
import java.util.Scanner; import java.util.Scanner;
import java.util.function.Predicate; import java.util.function.Predicate;
import static ffmpegconverter.Parser.tokenize;
/** /**
* Converts a files or files in a folder to a web playable mp4. * Converts a files or files in a folder to a web playable mp4.
*/ */
@ -165,52 +167,6 @@ public class Main {
} }
} }
/**
* Tokenizes a string
* @param input <p>A string.</p>
* @return <p>A list of tokens.</p>
*/
private static List<String> tokenizer(String input) {
List<String> tokens = new ArrayList<>();
boolean startedQuote = false;
StringBuilder currentToken = new StringBuilder();
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
switch (c) {
case ' ':
if (!startedQuote) {
//If not inside "", a space marks the end of a parameter
if (!currentToken.toString().trim().equals("")) {
tokens.add(currentToken.toString());
currentToken = new StringBuilder();
} else {
currentToken = new StringBuilder();
}
} else {
currentToken.append(c);
}
break;
case '"':
if (startedQuote) {
if (!currentToken.toString().trim().equals("")) {
tokens.add(currentToken.toString());
currentToken = new StringBuilder();
}
startedQuote = false;
} else {
startedQuote = true;
currentToken = new StringBuilder();
} break;
default:
currentToken.append(c);
if (i == input.length() - 1) {
tokens.add(currentToken.toString());
} break;
}
}
return tokens;
}
/** /**
* Initializes the anime converter * Initializes the anime converter
* @throws IOException <p>If reading or writing fails.</p> * @throws IOException <p>If reading or writing fails.</p>
@ -261,7 +217,7 @@ public class Main {
* @return <p>A list of tokens.</p> * @return <p>A list of tokens.</p>
*/ */
private static List<String> readInput(int max) { private static List<String> readInput(int max) {
List<String> tokens = tokenizer(READER.nextLine()); List<String> tokens = tokenize(READER.nextLine());
if (max < tokens.size()) { if (max < tokens.size()) {
throw new IllegalArgumentException("Input contains " + tokens.size() + throw new IllegalArgumentException("Input contains " + tokens.size() +
" arguments, but the input only supports " + max + " arguments."); " arguments, but the input only supports " + max + " arguments.");

View File

@ -0,0 +1,53 @@
package ffmpegconverter;
import java.util.ArrayList;
import java.util.List;
public class Parser {
/**
* Tokenizes a string
* @param input <p>A string.</p>
* @return <p>A list of tokens.</p>
*/
public static List<String> tokenize(String input) {
List<String> tokens = new ArrayList<>();
boolean startedQuote = false;
StringBuilder currentToken = new StringBuilder();
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
switch (c) {
case ' ':
if (!startedQuote) {
//If not inside "", a space marks the end of a parameter
if (!currentToken.toString().trim().equals("")) {
tokens.add(currentToken.toString());
currentToken = new StringBuilder();
} else {
currentToken = new StringBuilder();
}
} else {
currentToken.append(c);
}
break;
case '"':
if (startedQuote) {
if (!currentToken.toString().trim().equals("")) {
tokens.add(currentToken.toString());
currentToken = new StringBuilder();
}
startedQuote = false;
} else {
startedQuote = true;
currentToken = new StringBuilder();
} break;
default:
currentToken.append(c);
if (i == input.length() - 1) {
tokens.add(currentToken.toString());
} break;
}
}
return tokens;
}
}