Skip to content

Commit

Permalink
Reading from console now works without bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
tarvlad committed Mar 2, 2023
1 parent 7acf325 commit 305e740
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/rars/util/SystemIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public class SystemIO {
private static final int STDOUT = 1;
private static final int STDERR = 2;

private static String inputBuffer = "";

/**
* Implements syscall to read an integer value.
* Client is responsible for catching NumberFormatException.
Expand All @@ -94,7 +96,13 @@ private static String readStringInternal(String init, String prompt, int maxleng
String input = init;
if (Globals.getGui() == null) {
try {
input = getInputReader().readLine();
if (!inputBuffer.isEmpty()) {
input = String.valueOf(inputBuffer);
inputBuffer = "";
} else {
input = getInputReader().readLine();
}

if (input == null)
input = "";
} catch (IOException e) {
Expand Down Expand Up @@ -182,12 +190,18 @@ public static String readString(int serviceNumber, int maxLength) {
public static int readChar(int serviceNumber) {
int returnValue = 0;

String input = readStringInternal("0", "Enter a character value (syscall " + serviceNumber + ")", 1);
String input = readStringInternal("0", "Enter a character value (syscall " + serviceNumber + ")", -1);
// The whole try-catch is not really necessary in this case since I'm
// just propagating the runtime exception (the default behavior), but
// I want to make it explicit. The client needs to catch it.

try {
returnValue = (int) (input.charAt(0)); // first character input
if (input.length() > 1) {
inputBuffer = input.substring(1);
} else {
inputBuffer = "";
}
} catch (IndexOutOfBoundsException e) // no chars present
{
throw e; // was: returnValue = 0;
Expand Down

0 comments on commit 305e740

Please sign in to comment.