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