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

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: 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.
Bill Hesse 2013/07/02 11:31:54 [Stdin] allows both synchronous and asynchronous r
Anders Johnsen 2013/07/02 12:21:27 Done.
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).
Bill Hesse 2013/07/02 11:31:54 Synchronously read a line from stdin.
Anders Johnsen 2013/07/02 12:21:27 Done.
51 *
52 * 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.
53 *
54 * 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.
55 */
56 String readLineSync({Encoding encoding: Encoding.SYSTEM}) {
57 var decoder = new StringDecoder(encoding)._decoder;
58 var line = new StringBuffer();
59 bool end = false;
60 var sink = new _StdinEventSink(
61 (chunk) {
62 if (chunk == '\n') end = true;
63 line.write(chunk);
64 },
65 (error) {
66 throw error;
67 }, () {});
68
69 while (!end) {
70 int b = readByteSync();
71 if (b >= 0) {
72 decoder.handleData([b], sink);
73 } else {
74 decoder.handleDone(sink);
75 break;
76 }
77 }
78
79 if (line.isEmpty) return null;
80 return line.toString();
81 }
82
83 /**
84 * 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.
85 *
86 * If at end of file, -1 is returned.
87 */
88 int readByteSync();
89 }
90
91
30 class _StdSink implements IOSink { 92 class _StdSink implements IOSink {
31 final IOSink _sink; 93 final IOSink _sink;
32 94
33 _StdSink(IOSink this._sink); 95 _StdSink(IOSink this._sink);
34 96
35 Encoding get encoding => _sink.encoding; 97 Encoding get encoding => _sink.encoding;
36 void set encoding(Encoding encoding) { 98 void set encoding(Encoding encoding) {
37 _sink.encoding = encoding; 99 _sink.encoding = encoding;
38 } 100 }
39 void write(object) => _sink.write(object); 101 void write(object) => _sink.write(object);
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 } 172 }
111 } catch (e) { 173 } catch (e) {
112 // Only the interface implemented, _sink not available. 174 // Only the interface implemented, _sink not available.
113 } 175 }
114 } 176 }
115 return StdioType.OTHER; 177 return StdioType.OTHER;
116 } 178 }
117 179
118 180
119 class _StdIOUtils { 181 class _StdIOUtils {
120 external static IOSink _getStdioOutputStream(int fd); 182 external static IOSink _getStdioOutputStream(int fd);
Bill Hesse 2013/07/02 11:31:54 Should these types be changed?
Anders Johnsen 2013/07/02 12:21:27 Done.
121 external static Stream<List<int>> _getStdioInputStream(); 183 external static Stream<List<int>> _getStdioInputStream();
122 external static int _socketType(nativeSocket); 184 external static int _socketType(nativeSocket);
123 } 185 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698