2
0

Path.hx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. package php.io;
  26. class Path {
  27. public var ext : String;
  28. public var dir : String;
  29. public var file : String;
  30. public var backslash : Bool;
  31. public function new( path : String ) {
  32. var c1 = path.lastIndexOf("/");
  33. var c2 = path.lastIndexOf("\\");
  34. if( c1 < c2 ) {
  35. dir = path.substr(0,c2);
  36. path = path.substr(c2+1);
  37. backslash = true;
  38. } else if( c2 < c1 ) {
  39. dir = path.substr(0,c1);
  40. path = path.substr(c1+1);
  41. } else
  42. dir = null;
  43. var cp = path.lastIndexOf(".");
  44. if( cp != -1 ) {
  45. ext = path.substr(cp+1);
  46. file = path.substr(0,cp);
  47. } else {
  48. ext = null;
  49. file = path;
  50. }
  51. }
  52. public function toString() {
  53. return (if( dir == null ) "" else dir + if( backslash ) "\\" else "/") + file + (if( ext == null ) "" else "." + ext);
  54. }
  55. public static function withoutExtension( path : String ) {
  56. var s = new Path(path);
  57. s.ext = null;
  58. return s.toString();
  59. }
  60. public static inline function withoutDirectory( path : String) : String {
  61. return untyped __call__("basename", path);
  62. }
  63. public static inline function directory( path : String) : String {
  64. return untyped __call__("dirname", path);
  65. }
  66. public static function extension( path ) {
  67. var s = new Path(path);
  68. if( s.ext == null )
  69. return "";
  70. return s.ext;
  71. }
  72. public static function withExtension( path, ext ) {
  73. var s = new Path(path);
  74. s.ext = ext;
  75. return s.toString();
  76. }
  77. }