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

Side by Side Diff: sdk/lib/io/stdio.dart

Issue 23003023: Remove usage of dart:utf from dart:io. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Keep forgiving semantics. Created 7 years, 4 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;
(...skipping 12 matching lines...) Expand all
23 return _stream.listen( 23 return _stream.listen(
24 onData, 24 onData,
25 onError: onError, 25 onError: onError,
26 onDone: onDone, 26 onDone: onDone,
27 cancelOnError: cancelOnError); 27 cancelOnError: cancelOnError);
28 } 28 }
29 } 29 }
30 30
31 31
32 class _StdinEventSink implements EventSink<String> { 32 class _StdinEventSink implements EventSink<String> {
33 Function add; 33 Function _add;
34 Function addError; 34 Function _addError;
35 Function close; 35 Function _close;
36 _StdinEventSink(this.add, this.addError, this.close); 36 _StdinEventSink(this._add, this._addError, this._close);
37
38 void add(String string) => _add(string);
39 void addError(errorEvent) => _addError(errorEvent);
40 void close() => _close();
37 } 41 }
38 42
39 /** 43 /**
40 * [Stdin] allows both synchronous and asynchronous reads from the standard 44 * [Stdin] allows both synchronous and asynchronous reads from the standard
41 * input stream. 45 * input stream.
42 * 46 *
43 * Mixing synchronous and asynchronous reads is undefined. 47 * Mixing synchronous and asynchronous reads is undefined.
44 */ 48 */
45 class Stdin extends _StdStream implements Stream<List<int>> { 49 class Stdin extends _StdStream implements Stream<List<int>> {
46 Stdin._(Stream<List<int>> stream) : super(stream); 50 Stdin._(Stream<List<int>> stream) : super(stream);
47 51
48 /** 52 /**
49 * Synchronously read a line from stdin. This call will block until a full 53 * 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). 54 * line is available. The line will contain the newline character(s).
51 * 55 *
52 * If end-of-file is reached, `null` is returned. 56 * If end-of-file is reached, `null` is returned.
53 * 57 *
54 * If end-of-file is reached after some data has already been read, that data 58 * If end-of-file is reached after some data has already been read, that data
55 * is returned. 59 * is returned.
56 */ 60 */
57 String readLineSync({Encoding encoding: Encoding.SYSTEM, 61 String readLineSync({Encoding encoding: Encoding.SYSTEM,
58 bool retainNewlines: false}) { 62 bool retainNewlines: false}) {
59 var decoder = new StringDecoder(encoding)._decoder; 63 const CARRIAGE_RETURN = 13;
Anders Johnsen 2013/08/23 06:07:12 We normally use CR and LF for these constants in I
floitsch 2013/08/23 08:51:59 Done.
64 const LINE_FEED = 10;
60 var line = new StringBuffer(); 65 var line = new StringBuffer();
61 bool end = false; 66 bool end = false;
62 bool lastCharWasCR = false; 67 bool lastCharWasCR = false;
63 var sink = new _StdinEventSink( 68 var error;
64 (char) { 69
65 if (!retainNewlines && char == '\r') { 70 StreamController<List<int>> controller =
66 if (lastCharWasCR) line.write('\r'); 71 new StreamController<List<int>>(sync: true);
67 lastCharWasCR = true; 72 Stream stream = controller.stream.transform(new StringDecoder(encoding));
68 return; 73 stream.listen((String str) {
69 } else if (char == '\n') { 74 line.write(str);
70 end = true; 75 }, onError: (e) {
71 if (!retainNewlines) return; 76 error = e;
72 } else if (lastCharWasCR) { 77 }, onDone: () {
73 lastCharWasCR = false; 78 end = true;
74 line.write('\r'); 79 });
75 }
76 line.write(char);
77 },
78 (error) {
79 throw error;
80 }, () {
81 if (lastCharWasCR) line.write('\r');
82 });
83 80
84 bool empty = true; 81 bool empty = true;
85 while (!end) { 82 while (!end) {
86 int b = readByteSync(); 83 int b = readByteSync();
87 if (b >= 0) { 84
85 if (b < 0) {
86 // We didn't write the carriage return in case a line feed would be
87 // the next character. Add it now.
88 if (lastCharWasCR && !retainNewlines) controller.add([CARRIAGE_RETURN]);
89 controller.close();
90 } else {
88 empty = false; 91 empty = false;
89 decoder.handleData([b], sink); 92 // We consider \r\n and \n as new lines.
90 } else { 93 // A \r on its own is treated like a normal character.
91 decoder.handleDone(sink); 94
92 break; 95 if (b == CARRIAGE_RETURN) {
96 if (lastCharWasCR && !retainNewlines) {
97 // We didn't write the carriage return in case a line feed would be
98 // the next character.
99 // Add it now (since we treat it like a normal character now).
100 controller.add([CARRIAGE_RETURN]);
101 }
102 // We add the carriage return only if we keep new lines.
103 // Otherwise we need to wait for the next character (in case it is
104 // a line feed).
105 if (retainNewlines) controller.add([b]);
106 lastCharWasCR = true;
107 } else if (b == LINE_FEED) {
108 end = true;
109 // We don't care if there was a carriage return before. If we keep
110 // the line separators it has already been added to the controller.
111 // Otherwise we don't want it anyway.
112 if (retainNewlines) controller.add([b]);
113 controller.close();
114 } else {
115 // Since the current character is not a line feed we flush the
116 // carriage return we didn't write last iteration.
117 if (lastCharWasCR) {
118 controller.add([CARRIAGE_RETURN]);
119 lastCharWasCR = false;
120 }
121 controller.add([b]);
122 }
123 }
124 if (error != null) {
125 // Error during decoding.
126 throw error;
93 } 127 }
94 } 128 }
95 129
96 if (empty) return null; 130 if (empty) return null;
97 return line.toString(); 131 return line.toString();
98 } 132 }
99 133
100 /** 134 /**
101 * Enable or disable echo mode on the [Stdin]. 135 * Enable or disable echo mode on the [Stdin].
102 * 136 *
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 } 247 }
214 return StdioType.OTHER; 248 return StdioType.OTHER;
215 } 249 }
216 250
217 251
218 class _StdIOUtils { 252 class _StdIOUtils {
219 external static IOSink _getStdioOutputStream(int fd); 253 external static IOSink _getStdioOutputStream(int fd);
220 external static Stdin _getStdioInputStream(); 254 external static Stdin _getStdioInputStream();
221 external static int _socketType(nativeSocket); 255 external static int _socketType(nativeSocket);
222 } 256 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698