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 27 matching lines...) Expand all Loading... | |
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. The line will contain the newline character(s). |
44 * | 44 * |
45 * If end-of-file is reached, `null` is returned. | 45 * If end-of-file is reached, `null` is returned. |
46 * | 46 * |
47 * If end-of-file is reached after some data has already been read, that data | 47 * If end-of-file is reached after some data has already been read, that data |
48 * is returned. | 48 * is returned. |
Søren Gjesse
2014/04/22 08:34:26
Please document the optional arguments.
Anders Johnsen
2014/04/22 10:45:42
Done.
| |
49 */ | 49 */ |
50 String readLineSync({Encoding encoding: SYSTEM_ENCODING, | 50 String readLineSync({Encoding encoding: SYSTEM_ENCODING, |
51 bool retainNewlines: false}) { | 51 bool retainNewlines: false}) { |
Lasse Reichstein Nielsen
2014/04/22 08:07:01
The retainNewlines parameter isn't documented.
It
Anders Johnsen
2014/04/22 10:45:42
Done.
| |
52 const CR = 13; | 52 const CR = 13; |
53 const LF = 10; | 53 const LF = 10; |
54 var line = new StringBuffer(); | 54 final List line = []; |
55 bool end = false; | 55 // On Windows, if lineMode is disabled, only CR is received. |
56 bool crIsNewline = Platform.isWindows && !lineMode; | |
56 bool lastCharWasCR = false; | 57 bool lastCharWasCR = false; |
57 var error; | |
58 | |
59 StreamController<List<int>> controller = | |
60 new StreamController<List<int>>(sync: true); | |
61 Stream stream = controller.stream.transform(encoding.decoder); | |
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; | 58 bool empty = true; |
71 while (!end) { | 59 while (true) { |
72 int b = readByteSync(); | 60 int b = readByteSync(); |
Lasse Reichstein Nielsen
2014/04/22 08:07:01
b -> byte
Anders Johnsen
2014/04/22 10:45:42
Done.
| |
73 | |
74 if (b < 0) { | 61 if (b < 0) { |
75 // We didn't write the carriage return in case a line feed would be | 62 // We didn't write the carriage return in case a line feed would be |
76 // the next character. Add it now. | 63 // the next character. Add it now. |
77 if (lastCharWasCR && !retainNewlines) controller.add([CR]); | 64 if (lastCharWasCR && !retainNewlines) line.add(CR); |
78 controller.close(); | 65 break; |
79 } else { | 66 } else { |
80 empty = false; | 67 empty = false; |
81 // We consider \r\n and \n as new lines. | 68 // We consider \r\n and \n as new lines. |
Lasse Reichstein Nielsen
2014/04/22 08:07:01
Both \r\n and \n are considered line terminators.
Anders Johnsen
2014/04/22 10:45:42
Done.
| |
82 // A \r on its own is treated like a normal character. | 69 // A \r on its own is treated like a normal character. |
Lasse Reichstein Nielsen
2014/04/22 08:07:01
treated like -> treated as
Anders Johnsen
2014/04/22 10:45:42
Done.
| |
83 | 70 |
84 if (b == CR) { | 71 if (b == CR) { |
72 if (crIsNewline) { | |
73 if (retainNewlines) line.add(b); | |
74 break; | |
75 } | |
85 if (lastCharWasCR && !retainNewlines) { | 76 if (lastCharWasCR && !retainNewlines) { |
Lasse Reichstein Nielsen
2014/04/22 08:07:01
You are testing retainNewlines twice.
How about:
Anders Johnsen
2014/04/22 10:45:42
Done.
| |
86 // We didn't write the carriage return in case a line feed would be | 77 // We didn't write the carriage return in case a line feed would be |
87 // the next character. | 78 // the next character. |
88 // Add it now (since we treat it like a normal character now). | 79 // Add it now (since we treat it like a normal character now). |
89 controller.add([CR]); | 80 line.add(CR); |
90 } | 81 } |
91 // We add the carriage return only if we keep new lines. | 82 // 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 | 83 // Otherwise we need to wait for the next character (in case it is |
93 // a line feed). | 84 // a line feed). |
94 if (retainNewlines) controller.add([b]); | 85 if (retainNewlines) line.add(b); |
95 lastCharWasCR = true; | 86 lastCharWasCR = true; |
96 } else if (b == LF) { | 87 } else if (b == LF) { |
97 end = true; | |
98 // We don't care if there was a carriage return before. If we keep | 88 // 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. | 89 // the line separators it has already been added to the controller. |
100 // Otherwise we don't want it anyway. | 90 // Otherwise we don't want it anyway. |
101 if (retainNewlines) controller.add([b]); | 91 if (retainNewlines) line.add(b); |
102 controller.close(); | 92 break; |
103 } else { | 93 } else { |
104 // Since the current character is not a line feed we flush the | 94 // Since the current character is not a line feed we flush the |
105 // carriage return we didn't write last iteration. | 95 // carriage return we didn't write last iteration. |
106 if (lastCharWasCR) { | 96 if (lastCharWasCR) { |
Lasse Reichstein Nielsen
2014/04/22 08:07:01
&& !retainNewLines
?
Anders Johnsen
2014/04/22 10:45:42
Done.
| |
107 controller.add([CR]); | 97 line.add(CR); |
108 lastCharWasCR = false; | 98 lastCharWasCR = false; |
109 } | 99 } |
110 controller.add([b]); | 100 line.add(b); |
111 } | 101 } |
112 } | 102 } |
113 if (error != null) { | |
114 // Error during decoding. | |
115 throw error; | |
116 } | |
117 } | 103 } |
Lasse Reichstein Nielsen
2014/04/22 08:07:01
This seems complicated. You are handling two separ
Anders Johnsen
2014/04/22 10:45:42
Done.
| |
118 | |
119 if (empty) return null; | 104 if (empty) return null; |
120 return line.toString(); | 105 return encoding.decode(line); |
121 } | 106 } |
122 | 107 |
123 /** | 108 /** |
124 * Check if echo mode is enabled on [stdin]. | 109 * Check if echo mode is enabled on [stdin]. |
125 */ | 110 */ |
126 external bool get echoMode; | 111 external bool get echoMode; |
127 | 112 |
128 /** | 113 /** |
129 * Enable or disable echo mode on [stdin]. | 114 * Enable or disable echo mode on [stdin]. |
130 * | 115 * |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
332 return StdioType.OTHER; | 317 return StdioType.OTHER; |
333 } | 318 } |
334 | 319 |
335 | 320 |
336 class _StdIOUtils { | 321 class _StdIOUtils { |
337 external static _getStdioOutputStream(int fd); | 322 external static _getStdioOutputStream(int fd); |
338 external static Stdin _getStdioInputStream(); | 323 external static Stdin _getStdioInputStream(); |
339 external static int _socketType(nativeSocket); | 324 external static int _socketType(nativeSocket); |
340 external static _getStdioHandleType(int fd); | 325 external static _getStdioHandleType(int fd); |
341 } | 326 } |
OLD | NEW |