Professional Web Applications Themes

File, relative path handling. - Ruby

Before I attempt to re-invent this wheel: Has anyone come up with a method for converting an absolute filesystem path into a relative path, given the path to relate to? Pruning off the front is relatively easy, but handling '../' correctly is likely to be error prone. Given that I need to cope with Ruby 1.6 and 1.8 at the moment, and I will need to do something graceful if the paths are on different Windows drives, well, I suspect someone has run into this before me. I need relative paths so I can move between data sets for testing ...

  1. #1

    Default File, relative path handling.

    Before I attempt to re-invent this wheel:
    Has anyone come up with a method for converting an absolute
    filesystem path into a relative path, given the path to relate to?
    Pruning off the front is relatively easy, but handling '../'
    correctly is likely to be error prone. Given that I need to cope
    with Ruby 1.6 and 1.8 at the moment, and I will need to do something
    graceful if the paths are on different Windows drives, well, I
    suspect someone has run into this before me.

    I need relative paths so I can move between data sets for testing
    and between machines as well.

    Thank you,
    Hugh

    Hugh Sasse Staff Elec Eng Guest

  2. #2

    Default Re: File, relative path handling.

    Hugh,
    > Has anyone come up with a method for converting
    > an absolute filesystem path into a relative
    > path, given the path to relate to?
    Well, this doesn't handle Windows drive letters, but it handles the
    rest:

    # Convert the given absolute path into a path
    # relative to the second given absolute path.
    def relativepath(abspath,relativeto)
    path = abspath.split(File::SEPARATOR)
    rel = relativeto.split(File::SEPARATOR)
    while (path.length > 0) && (path.first == rel.first)
    path.shift
    rel.shift
    end
    ('..' + File::SEPARATOR) * (rel.length - 1) + path.join(File::SEPARATOR)
    end

    I hope this helps.

    - Warren Brown


    Warren Brown Guest

  3. #3

    Default Re: File, relative path handling.

    Hugh Sasse Staff Elec Eng wrote:
    > Before I attempt to re-invent this wheel:
    > Has anyone come up with a method for converting an absolute
    > filesystem path into a relative path, given the path to relate to?
    > Pruning off the front is relatively easy, but handling '../'
    > correctly is likely to be error prone. Given that I need to cope
    > with Ruby 1.6 and 1.8 at the moment, and I will need to do something
    > graceful if the paths are on different Windows drives, well, I
    > suspect someone has run into this before me.
    >
    > I need relative paths so I can move between data sets for testing
    > and between machines as well.
    Strange you should ask. I just wrote one of these and was about to port
    it, and some other string utilities, to a C extension object.

    It's brutal, but it works. Feel free to Rubify, anyone:

    class String
    public
    def relative_path(topath)
    frompath = self.clone
    topath = topath.clone

    fromdir = frompath.slice(/^.*?\//)
    while(fromdir and frompath.slice(/^.*?\//) == topath.slice(/^.*?\//))
    topath.slice!(/^.*?\//)
    fromdir = frompath.slice!(/^.*?\//)
    end

    fromdir = frompath.slice!(/^.*?\//)
    while(fromdir)
    topath = "../" + topath
    fromdir = frompath.slice!(/^.*?\//)
    end

    return topath
    end

    def relative_path!(topath)
    self.replace(relative_path(topath))
    end
    end


    Sean O'Dell

    Sean O'Dell Guest

  4. #4

    Default Re: File, relative path handling.

    Warren Brown wrote:
    > Hugh,
    >
    >
    >>Has anyone come up with a method for converting
    >>an absolute filesystem path into a relative
    >>path, given the path to relate to?
    >
    >
    > Well, this doesn't handle Windows drive letters, but it handles the
    > rest:
    >
    > # Convert the given absolute path into a path
    > # relative to the second given absolute path.
    > def relativepath(abspath,relativeto)
    > path = abspath.split(File::SEPARATOR)
    > rel = relativeto.split(File::SEPARATOR)
    > while (path.length > 0) && (path.first == rel.first)
    > path.shift
    > rel.shift
    > end
    > ('..' + File::SEPARATOR) * (rel.length - 1) + path.join(File::SEPARATOR)
    > end
    Your relative path routine is MUCH more efficient than mine, geez. I
    still don't "think" with everything Ruby has to offer. I'm still too
    much of a "got-no-libs" C/C++ person. =)

    Sean O'Dell

    Sean O'Dell Guest

  5. #5

    Default Re: File, relative path handling.


    I suggest to put that on the Wiki - if it's not already there.

    robert

    "Warren Brown" <wkbairmail.net> schrieb im Newsbeitrag
    news:002001c3762a$36d81260$803888cfwarrenpc...
    > Hugh,
    >
    > > Has anyone come up with a method for converting
    > > an absolute filesystem path into a relative
    > > path, given the path to relate to?
    >
    > Well, this doesn't handle Windows drive letters, but it handles the
    > rest:
    >
    > # Convert the given absolute path into a path
    > # relative to the second given absolute path.
    > def relativepath(abspath,relativeto)
    > path = abspath.split(File::SEPARATOR)
    > rel = relativeto.split(File::SEPARATOR)
    > while (path.length > 0) && (path.first == rel.first)
    > path.shift
    > rel.shift
    > end
    > ('..' + File::SEPARATOR) * (rel.length - 1) +
    path.join(File::SEPARATOR)
    > end
    >
    > I hope this helps.
    >
    > - Warren Brown
    >
    >
    Robert Klemme Guest

  6. #6

    Default Re: File, relative path handling.

    On Tue, 9 Sep 2003, Robert Klemme wrote:
    >
    > I suggest to put that on the Wiki - if it's not already there.
    >
    > robert
    Oh! I seem to have missed this mssage [RubyTalk:81378], but these
    often arrive out of sequence. This is a nice solution, and since at
    the moment I'm keeping all the stuff on the same drive I can live
    without the drive letter for now. This is much more elegant a
    solution that I would have created. Thank you.
    >
    > "Warren Brown" <wkbairmail.net> schrieb im Newsbeitrag
    > news:002001c3762a$36d81260$803888cfwarrenpc...
    [elided for brevity]

    Thank you,
    Hugh

    Hugh Sasse Staff Elec Eng Guest

Similar Threads

  1. #40666 [NEW]: handling of relative paths in include()
    By mfr at bmx-chemnitz dot de in forum PHP Bugs
    Replies: 0
    Last Post: February 28th, 12:39 PM
  2. Contribute 3.1 absolute/relative path bug?
    By Ron Polak in forum Macromedia Contribute General Discussion
    Replies: 0
    Last Post: April 1st, 01:15 AM
  3. Relative Path for Shared Movies
    By j_cutter in forum Macromedia Flash Actionscript
    Replies: 0
    Last Post: March 3rd, 01:18 AM
  4. relative path
    By Jan Eden in forum Macromedia Director 3D
    Replies: 9
    Last Post: January 20th, 07:18 PM
  5. Change relative path to absolute in an HTML file
    By Chinadian in forum PERL Miscellaneous
    Replies: 9
    Last Post: August 30th, 02:50 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139