Fixes some lines which were too long Expands some variable names Fixes some code repetition
99 lines
3.8 KiB
Java
99 lines
3.8 KiB
Java
package net.knarcraft.ffmpegconverter;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class Parser {
|
|
|
|
/**
|
|
* This function parses command inputs into understandable converter instructions
|
|
* @param tokens <p>A list of tokens containing all arguments</p>
|
|
*/
|
|
private static void parse(List<String> tokens) {
|
|
String[] types = {"animeconverter", "audioconverter", "videoconverter"};
|
|
Main.converterArgument[] commonArgs = {
|
|
new Main.converterArgument("-recursions", true, Main.converterArgumentValueType.INT)
|
|
};
|
|
Main.converterArgument[] animeArgs = {
|
|
|
|
};
|
|
Main.converterArgument[] audioArgs = {
|
|
new Main.converterArgument("-outext", true,
|
|
Main.converterArgumentValueType.SINGLE_VALUE)
|
|
};
|
|
Main.converterArgument[] videoArgs = {
|
|
new Main.converterArgument("-outext", true,
|
|
Main.converterArgumentValueType.SINGLE_VALUE)
|
|
};
|
|
String type = tokens.get(0).toLowerCase();
|
|
if (!Main.listContains(types, s -> s.equals(type))) {
|
|
throw new IllegalArgumentException("Unknown converter type chosen.");
|
|
}
|
|
if (tokens.size() < 2) {
|
|
throw new IllegalArgumentException("No file/folder path in argument.");
|
|
}
|
|
for (int i = 1; i < tokens.size() - 1; i++) {
|
|
//TODO: Find the type of argument and check the value
|
|
//TODO: Find an executable way to represent the chain of commands parsed
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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 character = input.charAt(i);
|
|
switch (character) {
|
|
case ' ':
|
|
if (!startedQuote) {
|
|
//If not inside "", a space marks the end of a parameter
|
|
if (isNotEmpty(currentToken)) {
|
|
tokens.add(currentToken.toString());
|
|
}
|
|
currentToken = new StringBuilder();
|
|
} else {
|
|
currentToken.append(character);
|
|
}
|
|
break;
|
|
case '"':
|
|
if (startedQuote) {
|
|
//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;
|
|
default:
|
|
//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;
|
|
}
|
|
}
|
|
return tokens;
|
|
}
|
|
|
|
/**
|
|
* Checks whether a string builder is empty
|
|
* @param builder <p>The string builder to check.</p>
|
|
* @return <p>True if the string builder is non empty.</p>
|
|
*/
|
|
private static boolean isNotEmpty(StringBuilder builder) {
|
|
return !builder.toString().trim().equals("");
|
|
}
|
|
}
|