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

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: Handle \r\r\n. 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
« no previous file with comments | « sdk/lib/_internal/lib/io_patch.dart ('k') | tests/standalone/io/stdin_sync_script.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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] allows both synchronous and asynchronous reads from the standard
41 * input stream.
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 * Synchronously read a line from stdin. This call will block until a full
50 * line is available. The line will contain the newline character(s).
51 *
52 * If end-of-file is reached, `null` is returned.
53 *
54 * If end-of-file is reached after some data has already been read, that data
55 * is returned.
56 */
57 String readLineSync({Encoding encoding: Encoding.SYSTEM,
58 bool retainNewlines: false}) {
59 var decoder = new StringDecoder(encoding)._decoder;
60 var line = new StringBuffer();
61 bool end = false;
62 bool lastCharWasCR = false;
63 var sink = new _StdinEventSink(
64 (char) {
65 if (!retainNewlines && char == '\r') {
66 if (lastCharWasCR) line.write('\r');
67 lastCharWasCR = true;
68 return;
69 } else if (char == '\n') {
70 end = true;
71 if (!retainNewlines) return;
72 } else if (lastCharWasCR) {
73 lastCharWasCR = false;
74 line.write('\r');
75 }
76 line.write(char);
77 },
78 (error) {
79 throw error;
80 }, () {
81 if (lastCharWasCR) line.write('\r');
82 });
83
84 bool empty = true;
85 while (!end) {
86 int b = readByteSync();
87 if (b >= 0) {
88 empty = false;
89 decoder.handleData([b], sink);
90 } else {
91 decoder.handleDone(sink);
92 break;
93 }
94 }
95
96 if (empty) return null;
97 return line.toString();
98 }
99
100 /**
101 * Synchronously read a byte from stdin. This call will block until a byte is
102 * available.
103 *
104 * If at end of file, -1 is returned.
105 */
106 int readByteSync();
107 }
108
109
30 class _StdSink implements IOSink { 110 class _StdSink implements IOSink {
31 final IOSink _sink; 111 final IOSink _sink;
32 112
33 _StdSink(IOSink this._sink); 113 _StdSink(IOSink this._sink);
34 114
35 Encoding get encoding => _sink.encoding; 115 Encoding get encoding => _sink.encoding;
36 void set encoding(Encoding encoding) { 116 void set encoding(Encoding encoding) {
37 _sink.encoding = encoding; 117 _sink.encoding = encoding;
38 } 118 }
39 void write(object) => _sink.write(object); 119 void write(object) => _sink.write(object);
(...skipping 11 matching lines...) Expand all
51 static const StdioType TERMINAL = const StdioType._("terminal"); 131 static const StdioType TERMINAL = const StdioType._("terminal");
52 static const StdioType PIPE = const StdioType._("pipe"); 132 static const StdioType PIPE = const StdioType._("pipe");
53 static const StdioType FILE = const StdioType._("file"); 133 static const StdioType FILE = const StdioType._("file");
54 static const StdioType OTHER = const StdioType._("other"); 134 static const StdioType OTHER = const StdioType._("other");
55 final String name; 135 final String name;
56 const StdioType._(String this.name); 136 const StdioType._(String this.name);
57 String toString() => "StdioType: $name"; 137 String toString() => "StdioType: $name";
58 } 138 }
59 139
60 140
61 Stream<List<int>> _stdin; 141 Stdin _stdin;
62 IOSink _stdout; 142 IOSink _stdout;
63 IOSink _stderr; 143 IOSink _stderr;
64 144
65 145
66 Stream<List<int>> get stdin { 146 Stream<List<int>> get stdin {
67 if (_stdin == null) { 147 if (_stdin == null) {
68 _stdin = _StdIOUtils._getStdioInputStream(); 148 _stdin = _StdIOUtils._getStdioInputStream();
69 } 149 }
70 return _stdin; 150 return _stdin;
71 } 151 }
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 } catch (e) { 191 } catch (e) {
112 // Only the interface implemented, _sink not available. 192 // Only the interface implemented, _sink not available.
113 } 193 }
114 } 194 }
115 return StdioType.OTHER; 195 return StdioType.OTHER;
116 } 196 }
117 197
118 198
119 class _StdIOUtils { 199 class _StdIOUtils {
120 external static IOSink _getStdioOutputStream(int fd); 200 external static IOSink _getStdioOutputStream(int fd);
121 external static Stream<List<int>> _getStdioInputStream(); 201 external static Stdio _getStdioInputStream();
122 external static int _socketType(nativeSocket); 202 external static int _socketType(nativeSocket);
123 } 203 }
OLDNEW
« no previous file with comments | « sdk/lib/_internal/lib/io_patch.dart ('k') | tests/standalone/io/stdin_sync_script.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698