From afbef461f0daa2578b1821159bad068819113a90 Mon Sep 17 00:00:00 2001 From: Mick Jordan <mick.jordan@oracle.com> Date: Fri, 12 Feb 2016 10:11:19 -0800 Subject: [PATCH] implement warn option on readLines --- .../builtin/base/ConnectionFunctions.java | 12 +++++++++-- .../truffle/r/nodes/builtin/base/Parse.java | 2 +- .../truffle/r/nodes/builtin/base/ReadDCF.java | 2 +- .../truffle/r/nodes/builtin/base/Scan.java | 4 ++-- .../builtin/base/foreign/ReadTableHead.java | 2 +- .../r/runtime/conn/ConnectionSupport.java | 19 ++++++++++------- .../r/runtime/conn/FileConnections.java | 12 +++++------ .../r/runtime/conn/GZIPConnections.java | 4 ++-- .../truffle/r/runtime/conn/RConnection.java | 21 ++++++++++--------- .../r/runtime/conn/SocketConnections.java | 4 ++-- .../r/runtime/conn/StdConnections.java | 2 +- .../r/runtime/conn/TextConnections.java | 2 +- .../r/runtime/conn/URLConnections.java | 4 ++-- 13 files changed, 52 insertions(+), 38 deletions(-) diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/ConnectionFunctions.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/ConnectionFunctions.java index 3d388a9b64..0d76a622a5 100644 --- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/ConnectionFunctions.java +++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/ConnectionFunctions.java @@ -390,13 +390,21 @@ public abstract class ConnectionFunctions { @RBuiltin(name = "readLines", kind = INTERNAL, parameterNames = {"con", "n", "ok", "warn", "encoding", "skipNul"}) public abstract static class ReadLines extends InternalCloseHelper { + + @Override + protected void createCasts(CastBuilder casts) { + casts.toLogical(2); + casts.toLogical(3); + casts.toLogical(5); + } + @Specialization @TruffleBoundary - protected Object readLines(RConnection con, int n, byte ok, @SuppressWarnings("unused") byte warn, @SuppressWarnings("unused") String encoding, @SuppressWarnings("unused") byte skipNul) { + protected Object readLines(RConnection con, int n, byte ok, byte warn, @SuppressWarnings("unused") String encoding, byte skipNul) { // TODO implement all the arguments controlVisibility(); try (RConnection openConn = con.forceOpen("rt")) { - String[] lines = openConn.readLines(n); + String[] lines = openConn.readLines(n, RRuntime.fromLogical(warn), RRuntime.fromLogical(skipNul)); if (n > 0 && lines.length < n && ok == RRuntime.LOGICAL_FALSE) { throw RError.error(this, RError.Message.TOO_FEW_LINES_READ_LINES); } diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Parse.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Parse.java index f465111a0e..69932ef904 100644 --- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Parse.java +++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Parse.java @@ -121,7 +121,7 @@ public abstract class Parse extends RBuiltinNode { throw RError.nyi(this, "parse from stdin not implemented"); } try (RConnection openConn = conn.forceOpen("r")) { - lines = openConn.readLines(0); + lines = openConn.readLines(0, false, false); } catch (IOException ex) { throw RError.error(this, RError.Message.PARSE_ERROR); } diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/ReadDCF.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/ReadDCF.java index e4cefc681c..6f59d3e35a 100644 --- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/ReadDCF.java +++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/ReadDCF.java @@ -52,7 +52,7 @@ public abstract class ReadDCF extends RBuiltinNode { keepWhiteSet.add(keepWhite.getDataAt(i)); } } - dcf = DCF.read(openConn.readLines(0), keepWhiteSet); + dcf = DCF.read(openConn.readLines(0, true, false), keepWhiteSet); } catch (IOException ex) { throw RError.error(this, RError.Message.ERROR_READING_CONNECTION, ex.getMessage()); } diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Scan.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Scan.java index ee0095a55d..1e208c89b9 100644 --- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Scan.java +++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Scan.java @@ -225,7 +225,7 @@ public abstract class Scan extends RBuiltinNode { try (RConnection openConn = data.con.forceOpen("r")) { if (nskip > 0) { - openConn.readLines(nskip); + openConn.readLines(nskip, true, RRuntime.fromLogical(skipNull)); } if (what instanceof RList) { return scanFrame((RList) what, nmax, nlines, flush == RRuntime.LOGICAL_TRUE, fill == RRuntime.LOGICAL_TRUE, strip == RRuntime.LOGICAL_TRUE, blSkip == RRuntime.LOGICAL_TRUE, @@ -335,7 +335,7 @@ public abstract class Scan extends RBuiltinNode { private static String[] getItems(LocalData data, boolean blSkip) throws IOException { while (true) { - String[] str = data.con.readLines(1); + String[] str = data.con.readLines(1, true, false); if (str == null || str.length == 0) { return null; } else { diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/foreign/ReadTableHead.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/foreign/ReadTableHead.java index 03f7edb975..562d03ee71 100644 --- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/foreign/ReadTableHead.java +++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/foreign/ReadTableHead.java @@ -27,7 +27,7 @@ public final class ReadTableHead extends RExternalBuiltinNode { RConnection conn = (RConnection) argValues[0]; int nlines = castInt(castVector(argValues[1])); try (RConnection openConn = conn.forceOpen("r")) { - return RDataFactory.createStringVector(openConn.readLines(nlines), RDataFactory.COMPLETE_VECTOR); + return RDataFactory.createStringVector(openConn.readLines(nlines, true, false), RDataFactory.COMPLETE_VECTOR); } catch (IOException ex) { errorProfile.enter(); throw RError.error(this, RError.Message.ERROR_READING_CONNECTION, ex.getMessage()); diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/ConnectionSupport.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/ConnectionSupport.java index b7729ed856..83a92ea99a 100644 --- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/ConnectionSupport.java +++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/ConnectionSupport.java @@ -342,12 +342,12 @@ public class ConnectionSupport { private static final int INVALID_DESCRIPTOR = -1; @Override - public String[] readLinesInternal(int n) throws IOException { + public String[] readLinesInternal(int n, boolean warn, boolean skipNul) throws IOException { throw RInternalError.shouldNotReachHere("INVALID CONNECTION"); } @Override - public String[] readLines(int n) throws IOException { + public String[] readLines(int n, boolean warn, boolean skipNul) throws IOException { throw RInternalError.shouldNotReachHere("INVALID CONNECTION"); } @@ -633,9 +633,9 @@ public class ConnectionSupport { } @Override - public String[] readLinesInternal(int n) throws IOException { + public String[] readLinesInternal(int n, boolean warn, boolean skipNul) throws IOException { checkOpen(); - return theConnection.readLinesInternal(n); + return theConnection.readLinesInternal(n, warn, skipNul); } @Override @@ -789,8 +789,11 @@ public class ConnectionSupport { * {@code readLines} from an {@link InputStream}. It would be convenient to use a * {@link BufferedReader} but mixing binary and text operations, which is a requirement, * would then be difficult. + * + * @param warn TODO + * @param skipNul TODO */ - default String[] readLinesHelper(InputStream in, int n) throws IOException { + default String[] readLinesHelper(InputStream in, int n, boolean warn, boolean skipNul) throws IOException { ArrayList<String> lines = new ArrayList<>(); int totalRead = 0; byte[] buffer = new byte[64]; @@ -813,7 +816,9 @@ public class ConnectionSupport { * "name" for the warning. */ lines.add(new String(buffer, 0, totalRead)); - RError.warning(RError.SHOW_CALLER2, RError.Message.INCOMPLETE_FINAL_LINE, "TODO: connection path"); + if (warn) { + RError.warning(RError.SHOW_CALLER2, RError.Message.INCOMPLETE_FINAL_LINE, "TODO: connection path"); + } } break; } @@ -1031,7 +1036,7 @@ public class ConnectionSupport { } @Override - public String[] readLinesInternal(int n) throws IOException { + public String[] readLinesInternal(int n, boolean warn, boolean skipNul) throws IOException { throw new IOException(RError.Message.CANNOT_READ_CONNECTION.message); } diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/FileConnections.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/FileConnections.java index 732a23f795..37f44c12ed 100644 --- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/FileConnections.java +++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/FileConnections.java @@ -120,8 +120,8 @@ public class FileConnections { @TruffleBoundary @Override - public String[] readLinesInternal(int n) throws IOException { - return readLinesHelper(inputStream, n); + public String[] readLinesInternal(int n, boolean warn, boolean skipNul) throws IOException { + return readLinesHelper(inputStream, n, warn, skipNul); } @Override @@ -223,8 +223,8 @@ public class FileConnections { @TruffleBoundary @Override - public String[] readLinesInternal(int n) throws IOException { - return readLinesHelper(inputStream, n); + public String[] readLinesInternal(int n, boolean warn, boolean skipNul) throws IOException { + return readLinesHelper(inputStream, n, warn, skipNul); } @Override @@ -382,9 +382,9 @@ public class FileConnections { } @Override - public String[] readLinesInternal(int n) throws IOException { + public String[] readLinesInternal(int n, boolean warn, boolean skipNul) throws IOException { raf.seek(readOffset); - return readLinesHelper(inputStream, n); + return readLinesHelper(inputStream, n, warn, skipNul); } @Override diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/GZIPConnections.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/GZIPConnections.java index 01592b2d84..d5e4582c3c 100644 --- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/GZIPConnections.java +++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/GZIPConnections.java @@ -128,8 +128,8 @@ public class GZIPConnections { } @Override - public String[] readLinesInternal(int n) throws IOException { - return readLinesHelper(inputStream, n); + public String[] readLinesInternal(int n, boolean warn, boolean skipNul) throws IOException { + return readLinesHelper(inputStream, n, warn, skipNul); } @Override diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/RConnection.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/RConnection.java index bf6656c846..67af8beb9f 100644 --- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/RConnection.java +++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/RConnection.java @@ -45,13 +45,13 @@ public abstract class RConnection extends RAttributeStorage implements RTypedVal private LinkedList<String> pushBack; - public abstract String[] readLinesInternal(int n) throws IOException; + public abstract String[] readLinesInternal(int n, boolean warn, boolean skipNul) throws IOException; public RType getRType() { return RType.Integer; } - private String readOneLineWithPushBack(String[] res, int ind) { + private String readOneLineWithPushBack(String[] res, int ind, @SuppressWarnings("unused") boolean warn, @SuppressWarnings("unused") boolean skipNul) { String s = pushBack.pollLast(); if (s == null) { return null; @@ -93,14 +93,14 @@ public abstract class RConnection extends RAttributeStorage implements RTypedVal } @TruffleBoundary - private String[] readLinesWithPushBack(int n) throws IOException { + private String[] readLinesWithPushBack(int n, boolean warn, boolean skipNul) throws IOException { String[] res = new String[n]; for (int i = 0; i < n; i++) { - String s = readOneLineWithPushBack(res, i); + String s = readOneLineWithPushBack(res, i, warn, skipNul); if (s == null) { if (res[i] == null) { // no more push back value - System.arraycopy(readLinesInternal(n - i), 0, res, i, n - i); + System.arraycopy(readLinesInternal(n - i, warn, skipNul), 0, res, i, n - i); pushBack = null; break; } @@ -108,7 +108,7 @@ public abstract class RConnection extends RAttributeStorage implements RTypedVal } else { // reached the last push back value without reaching and of line assert pushBack.size() == 0; - System.arraycopy(readLinesInternal(n - i), 0, res, i, n - i); + System.arraycopy(readLinesInternal(n - i, warn, skipNul), 0, res, i, n - i); res[i] = s + res[i]; pushBack = null; break; @@ -119,16 +119,17 @@ public abstract class RConnection extends RAttributeStorage implements RTypedVal /** * Read (n > 0 up to n else unlimited) lines on the connection. + * */ @TruffleBoundary - public String[] readLines(int n) throws IOException { + public String[] readLines(int n, boolean warn, boolean skipNul) throws IOException { if (pushBack == null) { - return readLinesInternal(n); + return readLinesInternal(n, warn, skipNul); } else if (pushBack.size() == 0) { pushBack = null; - return readLinesInternal(n); + return readLinesInternal(n, warn, skipNul); } else { - return readLinesWithPushBack(n); + return readLinesWithPushBack(n, warn, skipNul); } } diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/SocketConnections.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/SocketConnections.java index 4c3dad02c9..bd347d3e42 100644 --- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/SocketConnections.java +++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/SocketConnections.java @@ -97,8 +97,8 @@ public class SocketConnections { } @Override - public String[] readLinesInternal(int n) throws IOException { - return readLinesHelper(inputStream, n); + public String[] readLinesInternal(int n, boolean warn, boolean skipNul) throws IOException { + return readLinesHelper(inputStream, n, warn, skipNul); } @Override diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/StdConnections.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/StdConnections.java index e4d8ab0574..a1c74e8f6b 100644 --- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/StdConnections.java +++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/StdConnections.java @@ -193,7 +193,7 @@ public class StdConnections { @Override @TruffleBoundary - public String[] readLinesInternal(int n) throws IOException { + public String[] readLinesInternal(int n, boolean warn, boolean skipNul) throws IOException { ConsoleHandler console = RContext.getInstance().getConsoleHandler(); ArrayList<String> lines = new ArrayList<>(); String line; diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/TextConnections.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/TextConnections.java index 8e045d5e41..e1943b9a83 100644 --- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/TextConnections.java +++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/TextConnections.java @@ -93,7 +93,7 @@ public class TextConnections { } @Override - public String[] readLinesInternal(int n) throws IOException { + public String[] readLinesInternal(int n, boolean warn, boolean skipNul) throws IOException { int nleft = lines.length - index; int nlines = nleft; if (n > 0) { diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/URLConnections.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/URLConnections.java index b5d14c9f6b..1af709c104 100644 --- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/URLConnections.java +++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/URLConnections.java @@ -79,8 +79,8 @@ public class URLConnections { } @Override - public String[] readLinesInternal(int n) throws IOException { - return readLinesHelper(inputStream, n); + public String[] readLinesInternal(int n, boolean warn, boolean skipNul) throws IOException { + return readLinesHelper(inputStream, n, warn, skipNul); } @Override -- GitLab