mirror of
https://github.com/peterosterlund2/droidfish.git
synced 2024-11-27 06:10:28 +01:00
Make it possible to read PGN games from a String
This commit is contained in:
parent
152f9600b5
commit
26d03e7d91
|
@ -18,6 +18,7 @@
|
||||||
|
|
||||||
package org.petero.droidfish.activities;
|
package org.petero.droidfish.activities;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.Closeable;
|
import java.io.Closeable;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
|
@ -25,6 +26,7 @@ import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.RandomAccessFile;
|
import java.io.RandomAccessFile;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import org.petero.droidfish.DroidFishApp;
|
import org.petero.droidfish.DroidFishApp;
|
||||||
|
@ -188,21 +190,41 @@ public class PGNFile {
|
||||||
public Pair<GameInfoResult,ArrayList<GameInfo>> getGameInfo(Activity activity,
|
public Pair<GameInfoResult,ArrayList<GameInfo>> getGameInfo(Activity activity,
|
||||||
ProgressDialog progress) {
|
ProgressDialog progress) {
|
||||||
if (activity == null || progress == null)
|
if (activity == null || progress == null)
|
||||||
return getGameInfo(null, -1);
|
return getGameInfoFromFile(null, -1);
|
||||||
ProgressHandler handler = new ProgressHandler(fileName, activity, progress);
|
ProgressHandler handler = new ProgressHandler(fileName, activity, progress);
|
||||||
return getGameInfo(handler, -1);
|
return getGameInfoFromFile(handler, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Return info about up to "maxGames" PGN games in a file. */
|
/** Return info about up to "maxGames" PGN games in a file. */
|
||||||
public Pair<GameInfoResult,ArrayList<GameInfo>> getGameInfo(int maxGames) {
|
public Pair<GameInfoResult,ArrayList<GameInfo>> getGameInfo(int maxGames) {
|
||||||
return getGameInfo(null, maxGames);
|
return getGameInfoFromFile(null, maxGames);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Pair<GameInfoResult,ArrayList<GameInfo>> getGameInfo(String pgnData,
|
||||||
|
int maxGames) {
|
||||||
|
try (InputStream is = new ByteArrayInputStream(pgnData.getBytes("UTF-8"))) {
|
||||||
|
return getGameInfo(is, null, maxGames);
|
||||||
|
} catch (IOException ex) {
|
||||||
|
return new Pair<>(GameInfoResult.NOT_PGN, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Pair<GameInfoResult,ArrayList<GameInfo>> getGameInfoFromFile(ProgressHandler progress,
|
||||||
|
int maxGames) {
|
||||||
|
try (InputStream is = new FileInputStream(fileName)) {
|
||||||
|
return getGameInfo(is, progress, maxGames);
|
||||||
|
} catch (IOException ex) {
|
||||||
|
return new Pair<>(GameInfoResult.NOT_PGN, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/** Return info about PGN games in a file. */
|
/** Return info about PGN games in a file. */
|
||||||
private Pair<GameInfoResult,ArrayList<GameInfo>> getGameInfo(ProgressHandler progress,
|
private static Pair<GameInfoResult,ArrayList<GameInfo>> getGameInfo(InputStream is,
|
||||||
int maxGames) {
|
ProgressHandler progress,
|
||||||
|
int maxGames) {
|
||||||
ArrayList<GameInfo> gamesInFile = new ArrayList<>();
|
ArrayList<GameInfo> gamesInFile = new ArrayList<>();
|
||||||
try (BufferedInput f = new BufferedInput(new FileInputStream(fileName))) {
|
try (BufferedInput f = new BufferedInput(is)) {
|
||||||
GameInfo gi = null;
|
GameInfo gi = null;
|
||||||
HeaderInfo hi = null;
|
HeaderInfo hi = null;
|
||||||
boolean inHeader = false;
|
boolean inHeader = false;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user