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

Side by Side 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: Add retainNewlines Created 7 years, 5 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of dart.io; 5 part of dart.io;
6 6
7 const int _STDIO_HANDLE_TYPE_TERMINAL = 0; 7 const int _STDIO_HANDLE_TYPE_TERMINAL = 0;
8 const int _STDIO_HANDLE_TYPE_PIPE = 1; 8 const int _STDIO_HANDLE_TYPE_PIPE = 1;
9 const int _STDIO_HANDLE_TYPE_FILE = 2; 9 const int _STDIO_HANDLE_TYPE_FILE = 2;
10 const int _STDIO_HANDLE_TYPE_SOCKET = 3; 10 const int _STDIO_HANDLE_TYPE_SOCKET = 3;
11 const int _STDIO_HANDLE_TYPE_OTHER = 4; 11 const int _STDIO_HANDLE_TYPE_OTHER = 4;
12 12
13
13 class _StdStream extends Stream<List<int>> { 14 class _StdStream extends Stream<List<int>> {
14 final Stream<List<int>> _stream; 15 final Stream<List<int>> _stream;
15 16
16 _StdStream(Stream<List<int>> this._stream); 17 _StdStream(Stream<List<int>> this._stream);
17 18
18 StreamSubscription<List<int>> listen(void onData(List<int> event), 19 StreamSubscription<List<int>> listen(void onData(List<int> event),
19 {void onError(error), 20 {void onError(error),
20 void onDone(), 21 void onDone(),
21 bool cancelOnError}) { 22 bool cancelOnError}) {
22 return _stream.listen( 23 return _stream.listen(
23 onData, 24 onData,
24 onError: onError, 25 onError: onError,
25 onDone: onDone, 26 onDone: onDone,
26 cancelOnError: cancelOnError); 27 cancelOnError: cancelOnError);
27 } 28 }
28 } 29 }
29 30
31
32 class _StdinEventSink {
33 Function add;
34 Function addError;
35 Function close;
36 _StdinEventSink(this.add, this.addError, this.close);
37 }
38
39 /**
40 * [Stdin] class that enable both synchronous and asynchronous reads from the
41 * stdin pipe.
42 *
43 * Mixing synchronous and asynchronous reads is undefined.
44 */
45 class Stdin extends _StdStream {
46 Stdin._(Stream<List<int>> stream) : super(stream);
47
48 /**
49 * Read a line from stdin. This call will block until a full line is
50 * available. The line will contain the newline character(s).
51 *
52 * If at end of file, `null` is returned.
53 *
54 * If at end of file, while some data is already read, that data is returned.
55 */
56 String readLineSync({Encoding encoding: Encoding.SYSTEM,
57 bool retainNewlines: false}) {
58 var decoder = new StringDecoder(encoding)._decoder;
59 var line = new StringBuffer();
60 bool end = false;
61 var sink = new _StdinEventSink(
62 (chunk) {
Bill Hesse 2013/07/02 11:31:54 Why is chunk not char, or rune? It seems misleadi
Anders Johnsen 2013/07/02 12:21:27 Done.
63 if (chunk == '\n') end = true;
64 line.write(chunk);
65 },
66 (error) {
67 throw error;
68 }, () {});
69
70 while (!end) {
71 int b = readByteSync();
72 if (b >= 0) {
73 decoder.handleData([b], sink);
74 } else {
75 decoder.handleDone(sink);
76 break;
77 }
78 }
79
80 if (line.isEmpty) return null;
81 line = line.toString();
82 int trim = 0;
83 if (!retainNewlines) {
Bill Hesse 2013/07/02 11:31:54 I would have put this into the sink object, with m
Anders Johnsen 2013/07/02 12:21:27 Done.
84 if (line.endsWith('\r\n')) {
85 trim = 2;
86 } else if (line.endsWith('\n')) {
87 trim = 1;
88 }
89 }
90 return line.substring(0, line.length - trim);
91 }
92
93 /**
94 * Read a byte from stdin. This call will block until a byte is available.
95 *
96 * If at end of file, -1 is returned.
97 */
98 int readByteSync();
99 }
100
101
30 class _StdSink implements IOSink { 102 class _StdSink implements IOSink {
31 final IOSink _sink; 103 final IOSink _sink;
32 104
33 _StdSink(IOSink this._sink); 105 _StdSink(IOSink this._sink);
34 106
35 Encoding get encoding => _sink.encoding; 107 Encoding get encoding => _sink.encoding;
36 void set encoding(Encoding encoding) { 108 void set encoding(Encoding encoding) {
37 _sink.encoding = encoding; 109 _sink.encoding = encoding;
38 } 110 }
39 void write(object) => _sink.write(object); 111 void write(object) => _sink.write(object);
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 } 186 }
115 return StdioType.OTHER; 187 return StdioType.OTHER;
116 } 188 }
117 189
118 190
119 class _StdIOUtils { 191 class _StdIOUtils {
120 external static IOSink _getStdioOutputStream(int fd); 192 external static IOSink _getStdioOutputStream(int fd);
121 external static Stream<List<int>> _getStdioInputStream(); 193 external static Stream<List<int>> _getStdioInputStream();
122 external static int _socketType(nativeSocket); 194 external static int _socketType(nativeSocket);
123 } 195 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698