Skip to content

Commit bad1f29

Browse files
authored
Implement writeAsString (#223)
1 parent 9c3c7ee commit bad1f29

File tree

5 files changed

+378
-2
lines changed

5 files changed

+378
-2
lines changed

pkgs/io_file/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ See
3131
| rename | ||||| | |
3232
| set permissions | | | | | | | |
3333
| write file (bytes) | ||||| | |
34-
| write file (string) | | | | | | | |
34+
| write file (string) | | | | | | | |
3535

3636
### File
3737

pkgs/io_file/lib/src/file_system.dart

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'dart:convert';
56
import 'dart:typed_data';
67

78
// TODO(brianquinlan): When we switch to using exception types outside of
@@ -141,7 +142,6 @@ base class FileSystem {
141142
/// Write the given bytes to a file.
142143
///
143144
/// If `path` is a broken symlink and `mode` is [WriteMode.failExisting]:
144-
///
145145
/// - On Windows, the target of the symlink is created, using `data` as its
146146
/// contents.
147147
/// - On POSIX, [writeAsBytes] throws `PathExistsException`.
@@ -152,4 +152,25 @@ base class FileSystem {
152152
]) {
153153
throw UnsupportedError('writeAsBytes');
154154
}
155+
156+
/// Write the string to a file.
157+
///
158+
/// If `path` is a broken symlink and `mode` is [WriteMode.failExisting]:
159+
/// - On Windows, the target of the symlink is created, using `data` as its
160+
/// contents.
161+
/// - On POSIX, [writeAsBytes] throws `PathExistsException`.
162+
///
163+
/// `lineTerminator` is used to replace `'\n'` characters in `content`.
164+
/// If `lineTerminator` is provided, then it must be one of `'\n'` or
165+
/// `'\r\n'`. If `lineTerminator` is not provided then the platform line
166+
/// ending is used, i.e. `'\r\n'` on Windows and `'\n'` everwhere else.
167+
void writeAsString(
168+
String path,
169+
String contents, [
170+
WriteMode mode = WriteMode.failExisting,
171+
Encoding encoding = utf8,
172+
String? lineTerminator,
173+
]) {
174+
throw UnsupportedError('writeAsString');
175+
}
155176
}

pkgs/io_file/lib/src/vm_posix_file_system.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'dart:convert';
56
import 'dart:ffi';
67
import 'dart:io' as io;
78
import 'dart:math';
@@ -251,4 +252,28 @@ base class PosixFileSystem extends FileSystem {
251252
stdlibc.close(fd);
252253
}
253254
}
255+
256+
@override
257+
void writeAsString(
258+
String path,
259+
String contents, [
260+
WriteMode mode = WriteMode.failExisting,
261+
Encoding encoding = utf8,
262+
String? lineTerminator,
263+
]) {
264+
lineTerminator ??= '\n';
265+
final List<int> data;
266+
if (lineTerminator == '\n') {
267+
data = encoding.encode(contents);
268+
} else if (lineTerminator == '\r\n') {
269+
data = encoding.encode(contents.replaceAll('\n', '\r\n'));
270+
} else {
271+
throw ArgumentError.value(lineTerminator, 'lineTerminator');
272+
}
273+
writeAsBytes(
274+
path,
275+
data is Uint8List ? data : Uint8List.fromList(data),
276+
mode,
277+
);
278+
}
254279
}

pkgs/io_file/lib/src/vm_windows_file_system.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'dart:convert';
56
import 'dart:ffi';
67
import 'dart:io' as io;
78
import 'dart:math';
@@ -325,4 +326,28 @@ base class WindowsFileSystem extends FileSystem {
325326
win32.CloseHandle(f);
326327
}
327328
});
329+
330+
@override
331+
void writeAsString(
332+
String path,
333+
String contents, [
334+
WriteMode mode = WriteMode.failExisting,
335+
Encoding encoding = utf8,
336+
String? lineTerminator,
337+
]) {
338+
lineTerminator ??= '\r\n';
339+
final List<int> data;
340+
if (lineTerminator == '\n') {
341+
data = encoding.encode(contents);
342+
} else if (lineTerminator == '\r\n') {
343+
data = encoding.encode(contents.replaceAll('\n', '\r\n'));
344+
} else {
345+
throw ArgumentError.value(lineTerminator, 'lineTerminator');
346+
}
347+
writeAsBytes(
348+
path,
349+
data is Uint8List ? data : Uint8List.fromList(data),
350+
mode,
351+
);
352+
}
328353
}

0 commit comments

Comments
 (0)