Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 22 matching lines...) Expand all Loading... | |
| 33 * [Stdin] allows both synchronous and asynchronous reads from the standard | 33 * [Stdin] allows both synchronous and asynchronous reads from the standard |
| 34 * input stream. | 34 * input stream. |
| 35 * | 35 * |
| 36 * Mixing synchronous and asynchronous reads is undefined. | 36 * Mixing synchronous and asynchronous reads is undefined. |
| 37 */ | 37 */ |
| 38 class Stdin extends _StdStream implements Stream<List<int>> { | 38 class Stdin extends _StdStream implements Stream<List<int>> { |
| 39 Stdin._(Stream<List<int>> stream) : super(stream); | 39 Stdin._(Stream<List<int>> stream) : super(stream); |
| 40 | 40 |
| 41 /** | 41 /** |
| 42 * Synchronously read a line from stdin. This call will block until a full | 42 * Synchronously read a line from stdin. This call will block until a full |
| 43 * line is available. The line will contain the newline character(s). | 43 * line is available. |
| 44 * | 44 * |
| 45 * If end-of-file is reached, `null` is returned. | 45 * The argument [encoding] can be used to changed how the input should be |
| 46 * decoded. Default is [SYSTEM_ENCODING]. | |
| 47 * | |
| 48 * If [retainNewlines] is `false`, the returned String will not contain the | |
| 49 * final newline. Default is `false`. | |
|
Lasse Reichstein Nielsen
2014/04/22 10:56:08
Doesn't say what happens if retainNewlines is true
Anders Johnsen
2014/11/13 16:21:05
Done.
| |
| 46 * | 50 * |
| 47 * If end-of-file is reached after some data has already been read, that data | 51 * If end-of-file is reached after some data has already been read, that data |
|
Lasse Reichstein Nielsen
2014/04/22 10:56:08
How about "after any bytes have been read from std
Anders Johnsen
2014/11/13 16:21:06
Done.
| |
| 48 * is returned. | 52 * is returned. If no data was read, `null` is returned. |
| 49 */ | 53 */ |
| 50 String readLineSync({Encoding encoding: SYSTEM_ENCODING, | 54 String readLineSync({Encoding encoding: SYSTEM_ENCODING, |
| 51 bool retainNewlines: false}) { | 55 bool retainNewlines: false}) { |
| 52 const CR = 13; | 56 const CR = 13; |
| 53 const LF = 10; | 57 const LF = 10; |
| 54 var line = new StringBuffer(); | 58 final List line = []; |
| 55 bool end = false; | 59 // On Windows, if lineMode is disabled, only CR is received. |
| 56 bool lastCharWasCR = false; | 60 bool crIsNewline = Platform.isWindows && |
| 57 var error; | 61 (stdioType(stdin) == StdioType.TERMINAL) && |
| 62 !lineMode; | |
| 63 if (retainNewlines) { | |
| 64 int byte; | |
| 65 do { | |
| 66 byte = readByteSync(); | |
| 67 if (byte < 0) { | |
| 68 break; | |
| 69 } | |
| 70 line.add(byte); | |
| 71 } while (byte != LF && !(byte == CR && crIsNewline)); | |
| 72 if (line.isEmpty) { | |
| 73 return null; | |
| 74 } | |
| 75 } else if (crIsNewline) { | |
| 76 // CR and LF are both line terminators, neither is retained. | |
| 77 while (true) { | |
| 78 int byte = readByteSync(); | |
| 79 if (byte < 0) { | |
| 80 if (line.isEmpty) return null; | |
| 81 break; | |
| 82 } | |
| 83 if (byte == LF || byte == CR) break; | |
| 84 line.add(byte); | |
| 85 } | |
| 86 } else { | |
| 87 // Case having to hande CR LF as a single unretained line terminator. | |
| 88 outer: | |
|
Lasse Reichstein Nielsen
2014/04/22 10:56:08
Indent label, and preferably put it on same line a
Anders Johnsen
2014/11/13 16:21:06
Done.
| |
| 89 while (true) { | |
| 90 int byte = readByteSync(); | |
| 91 if (byte == LF) break; | |
| 92 if (byte == CR) { | |
| 93 do { | |
| 94 byte = readByteSync(); | |
| 95 if (byte == LF) break outer; | |
| 58 | 96 |
| 59 StreamController<List<int>> controller = | 97 line.add(CR); |
| 60 new StreamController<List<int>>(sync: true); | 98 } while (byte == CR); |
| 61 Stream stream = controller.stream.transform(encoding.decoder); | 99 // Fall through and handle non-CR character. |
| 62 stream.listen((String str) { | |
| 63 line.write(str); | |
| 64 }, onError: (e) { | |
| 65 error = e; | |
| 66 }, onDone: () { | |
| 67 end = true; | |
| 68 }); | |
| 69 | |
| 70 bool empty = true; | |
| 71 while (!end) { | |
| 72 int b = readByteSync(); | |
| 73 | |
| 74 if (b < 0) { | |
| 75 // We didn't write the carriage return in case a line feed would be | |
| 76 // the next character. Add it now. | |
| 77 if (lastCharWasCR && !retainNewlines) controller.add([CR]); | |
| 78 controller.close(); | |
| 79 } else { | |
| 80 empty = false; | |
| 81 // We consider \r\n and \n as new lines. | |
| 82 // A \r on its own is treated like a normal character. | |
| 83 | |
| 84 if (b == CR) { | |
| 85 if (lastCharWasCR && !retainNewlines) { | |
| 86 // We didn't write the carriage return in case a line feed would be | |
| 87 // the next character. | |
| 88 // Add it now (since we treat it like a normal character now). | |
| 89 controller.add([CR]); | |
| 90 } | |
| 91 // We add the carriage return only if we keep new lines. | |
| 92 // Otherwise we need to wait for the next character (in case it is | |
| 93 // a line feed). | |
| 94 if (retainNewlines) controller.add([b]); | |
| 95 lastCharWasCR = true; | |
| 96 } else if (b == LF) { | |
| 97 end = true; | |
| 98 // We don't care if there was a carriage return before. If we keep | |
| 99 // the line separators it has already been added to the controller. | |
| 100 // Otherwise we don't want it anyway. | |
| 101 if (retainNewlines) controller.add([b]); | |
| 102 controller.close(); | |
| 103 } else { | |
| 104 // Since the current character is not a line feed we flush the | |
| 105 // carriage return we didn't write last iteration. | |
| 106 if (lastCharWasCR) { | |
| 107 controller.add([CR]); | |
| 108 lastCharWasCR = false; | |
| 109 } | |
| 110 controller.add([b]); | |
| 111 } | 100 } |
| 112 } | 101 if (byte < 0) { |
| 113 if (error != null) { | 102 if (line.isEmpty) return null; |
| 114 // Error during decoding. | 103 break; |
| 115 throw error; | 104 } |
| 105 line.add(byte); | |
| 116 } | 106 } |
| 117 } | 107 } |
| 118 | 108 return encoding.decode(line); |
| 119 if (empty) return null; | |
| 120 return line.toString(); | |
| 121 } | 109 } |
| 122 | 110 |
| 123 /** | 111 /** |
| 124 * Check if echo mode is enabled on [stdin]. | 112 * Check if echo mode is enabled on [stdin]. |
| 125 */ | 113 */ |
| 126 external bool get echoMode; | 114 external bool get echoMode; |
| 127 | 115 |
| 128 /** | 116 /** |
| 129 * Enable or disable echo mode on [stdin]. | 117 * Enable or disable echo mode on [stdin]. |
| 130 * | 118 * |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 332 return StdioType.OTHER; | 320 return StdioType.OTHER; |
| 333 } | 321 } |
| 334 | 322 |
| 335 | 323 |
| 336 class _StdIOUtils { | 324 class _StdIOUtils { |
| 337 external static _getStdioOutputStream(int fd); | 325 external static _getStdioOutputStream(int fd); |
| 338 external static Stdin _getStdioInputStream(); | 326 external static Stdin _getStdioInputStream(); |
| 339 external static int _socketType(nativeSocket); | 327 external static int _socketType(nativeSocket); |
| 340 external static _getStdioHandleType(int fd); | 328 external static _getStdioHandleType(int fd); |
| 341 } | 329 } |
| OLD | NEW |