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]. |
46 * | 47 * |
47 * If end-of-file is reached after some data has already been read, that data | 48 * If [retainNewlines] is `false`, the returned String will not contain the |
48 * is returned. | 49 * final newline. If `true`, the returned String will contain the line |
| 50 * terminator. Default is `false`. |
| 51 * |
| 52 * If end-of-file is reached after any bytes have been read from stdin, |
| 53 * that data is returned. |
| 54 * Returns `null` if no bytes preceeded the end of input. |
49 */ | 55 */ |
50 String readLineSync({Encoding encoding: SYSTEM_ENCODING, | 56 String readLineSync({Encoding encoding: SYSTEM_ENCODING, |
51 bool retainNewlines: false}) { | 57 bool retainNewlines: false}) { |
52 const CR = 13; | 58 const CR = 13; |
53 const LF = 10; | 59 const LF = 10; |
54 var line = new StringBuffer(); | 60 final List line = []; |
55 bool end = false; | 61 // On Windows, if lineMode is disabled, only CR is received. |
56 bool lastCharWasCR = false; | 62 bool crIsNewline = Platform.isWindows && |
57 var error; | 63 (stdioType(stdin) == StdioType.TERMINAL) && |
| 64 !lineMode; |
| 65 if (retainNewlines) { |
| 66 int byte; |
| 67 do { |
| 68 byte = readByteSync(); |
| 69 if (byte < 0) { |
| 70 break; |
| 71 } |
| 72 line.add(byte); |
| 73 } while (byte != LF && !(byte == CR && crIsNewline)); |
| 74 if (line.isEmpty) { |
| 75 return null; |
| 76 } |
| 77 } else if (crIsNewline) { |
| 78 // CR and LF are both line terminators, neither is retained. |
| 79 while (true) { |
| 80 int byte = readByteSync(); |
| 81 if (byte < 0) { |
| 82 if (line.isEmpty) return null; |
| 83 break; |
| 84 } |
| 85 if (byte == LF || byte == CR) break; |
| 86 line.add(byte); |
| 87 } |
| 88 } else { |
| 89 // Case having to hande CR LF as a single unretained line terminator. |
| 90 outer: while (true) { |
| 91 int byte = readByteSync(); |
| 92 if (byte == LF) break; |
| 93 if (byte == CR) { |
| 94 do { |
| 95 byte = readByteSync(); |
| 96 if (byte == LF) break outer; |
58 | 97 |
59 StreamController<List<int>> controller = | 98 line.add(CR); |
60 new StreamController<List<int>>(sync: true); | 99 } while (byte == CR); |
61 Stream stream = controller.stream.transform(encoding.decoder); | 100 // 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 } | 101 } |
112 } | 102 if (byte < 0) { |
113 if (error != null) { | 103 if (line.isEmpty) return null; |
114 // Error during decoding. | 104 break; |
115 throw error; | 105 } |
| 106 line.add(byte); |
116 } | 107 } |
117 } | 108 } |
118 | 109 return encoding.decode(line); |
119 if (empty) return null; | |
120 return line.toString(); | |
121 } | 110 } |
122 | 111 |
123 /** | 112 /** |
124 * Check if echo mode is enabled on [stdin]. | 113 * Check if echo mode is enabled on [stdin]. |
125 */ | 114 */ |
126 external bool get echoMode; | 115 external bool get echoMode; |
127 | 116 |
128 /** | 117 /** |
129 * Enable or disable echo mode on [stdin]. | 118 * Enable or disable echo mode on [stdin]. |
130 * | 119 * |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 return StdioType.OTHER; | 321 return StdioType.OTHER; |
333 } | 322 } |
334 | 323 |
335 | 324 |
336 class _StdIOUtils { | 325 class _StdIOUtils { |
337 external static _getStdioOutputStream(int fd); | 326 external static _getStdioOutputStream(int fd); |
338 external static Stdin _getStdioInputStream(); | 327 external static Stdin _getStdioInputStream(); |
339 external static int _socketType(nativeSocket); | 328 external static int _socketType(nativeSocket); |
340 external static _getStdioHandleType(int fd); | 329 external static _getStdioHandleType(int fd); |
341 } | 330 } |
OLD | NEW |