| 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 /** | |
| 6 * TODO(johnniwinther): Path manipulation copied from frog/file_system.dart. | |
| 7 * Should be converted to use Path from dart:io when this is completed. | |
| 8 */ | |
| 9 #library('file_util'); | |
| 10 | |
| 11 /** | |
| 12 * Replaces all back slashes (\) with forward slashes (/) in [path] and | |
| 13 * return the result. | |
| 14 */ | |
| 15 String canonicalizePath(String path) { | |
| 16 return path.replaceAll('\\', '/'); | |
| 17 } | |
| 18 | |
| 19 /** Join [path1] to [path2]. */ | |
| 20 String joinPaths(String path1, String path2) { | |
| 21 path1 = canonicalizePath(path1); | |
| 22 path2 = canonicalizePath(path2); | |
| 23 | |
| 24 var pieces = path1.split('/'); | |
| 25 for (var piece in path2.split('/')) { | |
| 26 if (piece == '..' && pieces.length > 0 && pieces.last() != '.' | |
| 27 && pieces.last() != '..') { | |
| 28 pieces.removeLast(); | |
| 29 } else if (piece != '') { | |
| 30 if (pieces.length > 0 && pieces.last() == '.') { | |
| 31 pieces.removeLast(); | |
| 32 } | |
| 33 pieces.add(piece); | |
| 34 } | |
| 35 } | |
| 36 return Strings.join(pieces, '/'); | |
| 37 } | |
| 38 | |
| 39 /** Returns the directory name for the [path]. */ | |
| 40 String dirname(String path) { | |
| 41 path = canonicalizePath(path); | |
| 42 | |
| 43 int lastSlash = path.lastIndexOf('/', path.length); | |
| 44 if (lastSlash == -1) { | |
| 45 return '.'; | |
| 46 } else { | |
| 47 return path.substring(0, lastSlash); | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 /** Returns the file name without directory for the [path]. */ | |
| 52 String basename(String path) { | |
| 53 path = canonicalizePath(path); | |
| 54 | |
| 55 int lastSlash = path.lastIndexOf('/', path.length); | |
| 56 if (lastSlash == -1) { | |
| 57 return path; | |
| 58 } else { | |
| 59 return path.substring(lastSlash + 1); | |
| 60 } | |
| 61 } | |
| OLD | NEW |