| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
| 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. | |
| 4 | |
| 5 /** Abstraction for file systems and utility functions to manipulate paths. */ | |
| 6 #library('file_system'); | |
| 7 | |
| 8 /** | |
| 9 * Abstraction around file system access to work in a variety of different | |
| 10 * environments. | |
| 11 */ | |
| 12 interface FileSystem { | |
| 13 String readAll(String filename); | |
| 14 | |
| 15 void writeString(String outfile, String text); | |
| 16 | |
| 17 bool fileExists(String filename); | |
| 18 | |
| 19 void createDirectory(String path, [bool recursive]); | |
| 20 void removeDirectory(String path, [bool recursive]); | |
| 21 } | |
| 22 | |
| 23 /** | |
| 24 * Replaces all back slashes (\) with forward slashes (/) in [path] and | |
| 25 * return the result. | |
| 26 */ | |
| 27 String canonicalizePath(String path) { | |
| 28 return path.replaceAll('\\', '/'); | |
| 29 } | |
| 30 | |
| 31 /** Join [path1] to [path2]. */ | |
| 32 String joinPaths(String path1, String path2) { | |
| 33 path1 = canonicalizePath(path1); | |
| 34 path2 = canonicalizePath(path2); | |
| 35 | |
| 36 var pieces = path1.split('/'); | |
| 37 for (var piece in path2.split('/')) { | |
| 38 if (piece == '..' && pieces.length > 0 && pieces.last() != '.' | |
| 39 && pieces.last() != '..') { | |
| 40 pieces.removeLast(); | |
| 41 } else if (piece != '') { | |
| 42 if (pieces.length > 0 && pieces.last() == '.') { | |
| 43 pieces.removeLast(); | |
| 44 } | |
| 45 pieces.add(piece); | |
| 46 } | |
| 47 } | |
| 48 return Strings.join(pieces, '/'); | |
| 49 } | |
| 50 | |
| 51 /** Returns the directory name for the [path]. */ | |
| 52 String dirname(String path) { | |
| 53 path = canonicalizePath(path); | |
| 54 | |
| 55 int lastSlash = path.lastIndexOf('/', path.length); | |
| 56 if (lastSlash == -1) { | |
| 57 return '.'; | |
| 58 } else { | |
| 59 return path.substring(0, lastSlash); | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 /** Returns the file name without directory for the [path]. */ | |
| 64 String basename(String path) { | |
| 65 path = canonicalizePath(path); | |
| 66 | |
| 67 int lastSlash = path.lastIndexOf('/', path.length); | |
| 68 if (lastSlash == -1) { | |
| 69 return path; | |
| 70 } else { | |
| 71 return path.substring(lastSlash + 1); | |
| 72 } | |
| 73 } | |
| OLD | NEW |