Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(74)

Unified Diff: sdk/lib/io/stdio.dart

Issue 18516002: Add Stdio.readByteSync and Stdio.readLineSync. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: sdk/lib/io/stdio.dart
diff --git a/sdk/lib/io/stdio.dart b/sdk/lib/io/stdio.dart
index afa7ea013bb0fe8a9a123bb1393c75f39034a545..353505780068a22c45796491d4840bb6c13ab174 100644
--- a/sdk/lib/io/stdio.dart
+++ b/sdk/lib/io/stdio.dart
@@ -10,6 +10,7 @@ const int _STDIO_HANDLE_TYPE_FILE = 2;
const int _STDIO_HANDLE_TYPE_SOCKET = 3;
const int _STDIO_HANDLE_TYPE_OTHER = 4;
+
class _StdStream extends Stream<List<int>> {
final Stream<List<int>> _stream;
@@ -27,6 +28,67 @@ class _StdStream extends Stream<List<int>> {
}
}
+
+class _StdinEventSink {
+ Function add;
+ Function addError;
+ Function close;
+ _StdinEventSink(this.add, this.addError, this.close);
+}
+
+/**
+ * [Stdin] class that enable both synchronous and asynchronous reads from the
+ * stdin pipe.
Bill Hesse 2013/07/02 11:31:54 [Stdin] allows both synchronous and asynchronous r
Anders Johnsen 2013/07/02 12:21:27 Done.
+ *
+ * Mixing synchronous and asynchronous reads is undefined.
+ */
+class Stdin extends _StdStream {
+ Stdin._(Stream<List<int>> stream) : super(stream);
+
+ /**
+ * Read a line from stdin. This call will block until a full line is
+ * available. The line will contain the newline character(s).
Bill Hesse 2013/07/02 11:31:54 Synchronously read a line from stdin.
Anders Johnsen 2013/07/02 12:21:27 Done.
+ *
+ * If at end of file, `null` is returned.
Bill Hesse 2013/07/02 11:31:54 If end-of-file is reached,
Anders Johnsen 2013/07/02 12:21:27 Done.
+ *
+ * If at end of file, while some data is already read, that data is returned.
Bill Hesse 2013/07/02 11:31:54 If end-of-file is reached after some data has alre
Anders Johnsen 2013/07/02 12:21:27 Done.
+ */
+ String readLineSync({Encoding encoding: Encoding.SYSTEM}) {
+ var decoder = new StringDecoder(encoding)._decoder;
+ var line = new StringBuffer();
+ bool end = false;
+ var sink = new _StdinEventSink(
+ (chunk) {
+ if (chunk == '\n') end = true;
+ line.write(chunk);
+ },
+ (error) {
+ throw error;
+ }, () {});
+
+ while (!end) {
+ int b = readByteSync();
+ if (b >= 0) {
+ decoder.handleData([b], sink);
+ } else {
+ decoder.handleDone(sink);
+ break;
+ }
+ }
+
+ if (line.isEmpty) return null;
+ return line.toString();
+ }
+
+ /**
+ * Read a byte from stdin. This call will block until a byte is available.
Bill Hesse 2013/07/02 11:31:54 Synchronously
Anders Johnsen 2013/07/02 12:21:27 Done.
+ *
+ * If at end of file, -1 is returned.
+ */
+ int readByteSync();
+}
+
+
class _StdSink implements IOSink {
final IOSink _sink;

Powered by Google App Engine
This is Rietveld 408576698