Fixed UCI option parsing

This commit is contained in:
Kai S. K. Engelbart 2019-07-22 07:29:58 +02:00
parent 3581904abc
commit d34244b1c3
Signed by: kske
GPG Key ID: 8BEB13EC5DF7EF13
3 changed files with 26 additions and 23 deletions

View File

@ -83,8 +83,7 @@ class UCIPlayerListener implements UCIListener {
@Override @Override
public void onOption(String name, GUIType type, String defaultVal, String minVal, String maxVal, List<String> var) { public void onOption(String name, GUIType type, String defaultVal, String minVal, String maxVal, List<String> var) {
// TODO Auto-generated method stub System.out.printf("Option %s of type %s, min %s, max %s, default %s%n", name, type, minVal, maxVal, defaultVal);
} }
public String getName() { return name; }; public String getName() { return name; };

View File

@ -22,7 +22,7 @@ public class UCIHandle {
} }
public void start() { public void start() {
new Thread(receiver).start(); new Thread(receiver, "UCI Receiver").start();
uci(); uci();
} }

View File

@ -9,6 +9,7 @@ import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.StringJoiner;
import dev.kske.chess.uci.UCIListener.GUIType; import dev.kske.chess.uci.UCIListener.GUIType;
@ -30,20 +31,21 @@ public class UCIReceiver implements Runnable {
@Override @Override
public void run() { public void run() {
try { String line;
String line; while (!Thread.currentThread().isInterrupted())
while (!Thread.currentThread().isInterrupted()) try {
if ((line = in.readLine()) != null) parse(line); if ((line = in.readLine()) != null && !line.isBlank()) parse(line);
} catch (IndexOutOfBoundsException ex) { } catch (IndexOutOfBoundsException ex) {
System.err.println("Too few arguments were provided!"); System.err.println("Too few arguments were provided!");
ex.printStackTrace(); ex.printStackTrace();
} catch (IOException ex) { } catch (IOException ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
} }
private void parse(String line) { private void parse(String line) {
String command = line.substring(0, line.indexOf(' ')); int spaceIndex = line.indexOf(' ');
String command = spaceIndex == -1 ? line : line.substring(0, spaceIndex);
switch (command) { switch (command) {
case "id": case "id":
parseId(line.substring(command.length() + 1)); parseId(line.substring(command.length() + 1));
@ -154,7 +156,7 @@ public class UCIReceiver implements Runnable {
private void parseOption(String line) { private void parseOption(String line) {
String[] tokens = line.split(" "); String[] tokens = line.split(" ");
String name = ""; StringJoiner name = new StringJoiner(" ");
GUIType type = null; GUIType type = null;
String defaultVal = null; String defaultVal = null;
String minVal = null; String minVal = null;
@ -164,18 +166,20 @@ public class UCIReceiver implements Runnable {
for (int i = 0; i < tokens.length; i++) for (int i = 0; i < tokens.length; i++)
switch (tokens[i]) { switch (tokens[i]) {
case "name": case "name":
while (!Arrays.asList("type", "defaultVal", "minVal", "maxVal", "var").contains(tokens[++i])) while (!Arrays.asList("type", "default", "min", "max", "var").contains(tokens[i + 1]))
name += tokens[i]; name.add(tokens[++i]);
break; break;
case "type": case "type":
type = GUIType.valueOf(tokens[i].toUpperCase()); type = GUIType.valueOf(tokens[++i].toUpperCase());
case "defaultVal":
defaultVal = tokens[++i];
break; break;
case "minVal": case "default":
// Default string may be empty
defaultVal = i == tokens.length - 1 ? "" : tokens[++i];
break;
case "min":
minVal = tokens[++i]; minVal = tokens[++i];
break; break;
case "maxVal": case "max":
maxVal = tokens[++i]; maxVal = tokens[++i];
break; break;
case "var": case "var":
@ -184,7 +188,7 @@ public class UCIReceiver implements Runnable {
default: default:
System.err.printf("Unknown parameter '%s' for command 'option' found!%n", tokens[i]); System.err.printf("Unknown parameter '%s' for command 'option' found!%n", tokens[i]);
} }
listener.onOption(name, type, defaultVal, minVal, maxVal, var); listener.onOption(name.toString(), type, defaultVal, minVal, maxVal, var);
} }
public void setListener(UCIListener listener) { this.listener = listener; } public void setListener(UCIListener listener) { this.listener = listener; }