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

Unified Diff: sdk/lib/io/file_system_entity.dart

Issue 26001002: dart:io | Add FileSystemEntity.parent, make File.directory call it. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix type error on previous CL. Created 7 years, 2 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sdk/lib/io/file_impl.dart ('k') | sdk/lib/io/http_impl.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/io/file_system_entity.dart
diff --git a/sdk/lib/io/file_system_entity.dart b/sdk/lib/io/file_system_entity.dart
index fdeb923c6de41dc31715e1dc13192afcd6546ee2..b2b315f97e337cf38873747f45e2dccc3ae58a10 100644
--- a/sdk/lib/io/file_system_entity.dart
+++ b/sdk/lib/io/file_system_entity.dart
@@ -530,6 +530,42 @@ abstract class FileSystemEntity {
external static _identical(String path1, String path2);
external static _resolveSymbolicLinks(String path);
+ /**
+ * The directory containing [this]. If [this] is a root
+ * directory, returns [this].
+ */
+ Directory get parent {
Søren Gjesse 2013/10/04 12:20:16 How about return new Directory(new Uri.file(path)
Bill Hesse 2013/10/04 12:41:06 This will not give the desired result, since it wi
+ if (Platform.isWindows) {
+ int rootEnd = -1;
+ if (path.startsWith(_absoluteWindowsPathPattern)) {
+ // Root ends at first / or \ after the first two characters.
+ rootEnd = path.indexOf(new RegExp(r'[/\\]'), 2);
+ if (rootEnd == -1) return new Directory(path);
+ }
+ // Ignore trailing slashes - find a slash followed by a non-slash.
+ int pos = path.lastIndexOf(new RegExp(r'[/\\][^/\\]'));
+ if (pos <= rootEnd) {
+ if (rootEnd == -1) {
+ return new Directory('.');
+ } else {
+ return new Directory(path.substring(0, rootEnd + 1));
+ }
+ }
+ while (pos > rootEnd + 1 &&
+ (path[pos - 1] == '\\' || path[pos - 1] == '/')) {
+ pos--;
+ }
+ return new Directory(path.substring(0, pos));
+ } else {
+ // Ignore trailing slashes - find a slash followed by a non-slash.
+ int pos = path.lastIndexOf(new RegExp(r'/[^/]'));
+ if (pos < 0) return new Directory('.');
+ while (pos > 0 && path[pos - 1] == '/') --pos;
+ if (pos == 0) return new Directory('/');
+ return new Directory(path.substring(0, pos));
+ }
+ }
+
static int _getTypeSync(String path, bool followLinks) {
var result = _getType(path, followLinks);
_throwIfError(result, 'Error getting type of FileSystemEntity');
@@ -537,12 +573,13 @@ abstract class FileSystemEntity {
}
static Future<int> _getTypeAsync(String path, bool followLinks) {
- return _IOService.dispatch(_FILE_TYPE, [path, followLinks]).then((response) {
- if (_isErrorResponse(response)) {
- throw _exceptionFromResponse(response, "Error getting type", path);
- }
- return response;
- });
+ return _IOService.dispatch(_FILE_TYPE, [path, followLinks])
+ .then((response) {
+ if (_isErrorResponse(response)) {
+ throw _exceptionFromResponse(response, "Error getting type", path);
+ }
+ return response;
+ });
}
static _throwIfError(Object result, String msg, [String path]) {
« no previous file with comments | « sdk/lib/io/file_impl.dart ('k') | sdk/lib/io/http_impl.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698