Starts moving parsing to its own class
This commit is contained in:
parent
1887d7c1fe
commit
93f8a28872
@ -11,6 +11,8 @@ import java.util.List;
|
||||
import java.util.Scanner;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import static ffmpegconverter.Parser.tokenize;
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @throws IOException <p>If reading or writing fails.</p>
|
||||
@ -261,7 +217,7 @@ public class Main {
|
||||
* @return <p>A list of tokens.</p>
|
||||
*/
|
||||
private static List<String> readInput(int max) {
|
||||
List<String> tokens = tokenizer(READER.nextLine());
|
||||
List<String> tokens = tokenize(READER.nextLine());
|
||||
if (max < tokens.size()) {
|
||||
throw new IllegalArgumentException("Input contains " + tokens.size() +
|
||||
" arguments, but the input only supports " + max + " arguments.");
|
||||
|
53
src/main/java/ffmpegconverter/Parser.java
Normal file
53
src/main/java/ffmpegconverter/Parser.java
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user