Results 1 to 5 of 5

Thread: Standard function to sanitize absolute file path?

  1. #1
    Join Date
    Aug 2012
    Beans
    185

    Standard function to sanitize absolute file path?

    To check (update: on a modern Unix-like OS) if a file path is (will be) the closest parent of another path I need to first sanitize both paths: remove possible extra '/' and '.' characters (I'm thinking about ".." in the future).
    From
    "/.///home//中央文//.//fox/新华社北京3月2///"
    I should get
    "/home/中央文/fox/新华社北京3月2/"
    The Chinese characters in the path are there to make sure it works with Unicode chars.

    realpath(3) fails with "no such file or directory" so it doesn't suit my needs.
    Perhaps there's some (standard Unix/POSIX) library function that already does this?

    My function:
    Code:
    void
    sanitizePath(Glib::ustring &unb)
    {
        if(unb.size() < 1) {
            return;
        }
        
        Glib::ustring::value_type l, c;
        l = unb.at(0);
        
        for(int i=1; i<unb.size(); i++) {
            c = unb.at(i);
            
            if(c == '/') {
                if(l == '/') {
                    unb.erase(i, 1);
                    i--;
                    continue;
                }
            } else if(c == '.') {
                if((l == '/') && (i < unb.size()-1) && (unb.at(i+1) == '/')) {
                    unb.erase(i, 1);
                    i--;
                    continue;
                }
            }
            
            l = c;
        }
    }
    Usage:
    Code:
    Glib::init();
    Glib::ustring s1 = "/.///home//中央文//.//fox/新华社北京3月2///";
    sanitizePath(s1);
    std::cout << "sanitized: \"" << s1.data() << "\"" << std::endl;
    Yields:
    sanitized: "/home/中央文/fox/新华社北京3月2/"
    Last edited by bird1500; March 23rd, 2013 at 03:54 PM.

  2. #2
    Join Date
    May 2008
    Location
    UK
    Beans
    1,448
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Standard function to sanitize absolute file path?

    And of course your function is none-portable - you may not intend to use it on Windows - but someone else might

    I would at least add a warning on the comments that it is Linux specific.
    Tony - Happy to try to help.
    Unless otherwise stated - all code posted by me is untested. Remember to Mark the Thread as Solved.
    Ubuntu user number # 24044 Projects : TimeWarp - on the fly Backups

  3. #3
    Join Date
    Aug 2012
    Beans
    185

    Re: Standard function to sanitize absolute file path?

    Yes, my function is for my app which is only for Linux, sorry I forgot to mention I only care about (modern) Unix-like systems, I'll update the 1st post.
    Also, I said "standard" mainly because these tend to already be present in the OS and I'd just have to call it.

  4. #4
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,664
    Distro
    Ubuntu 13.10 Saucy Salamander

    Re: Standard function to sanitize absolute file path?

    GIO has g_file_get_path and g_file_resolve_relative_path functions that should do what you need and are probably more robust than most of us would write ourselves.

    If you are already using GLib with C++, configuring the project for GIO should be similar. I think, if you happen to be using gtkmm, that you get GIO anyway -- but you'd have to confirm that.
    Please create new threads for new questions.
    Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].

  5. #5
    Join Date
    Aug 2012
    Beans
    185

    Re: Standard function to sanitize absolute file path?

    That seems to be it. Thanks, it also handles ".." properly (goes up one level).
    Though the Gtkmm (C++) implementation requires to create a Gio::File before using that function which one shouldn't need to create if the path is absolute, in Gtkmm it should be a static function instead.

    EDIT: After tinkering with it it turns out Gio::File sanitizes the path when created at Gio::File::create_for_path(path), after that calling file->get_path() returns a sanitized path. Out of curiosity I wanted to see how it's implemented but I got lost in a labyrinth of #defines and cross-references:
    Glib::RefPtr<Gio::File> is a wrap of GFile which is in libglib2.0-dev which from "gfile.c" calls "g_file_new_for_path()" which calls "g_vfs_get_file_for_path()" from "gvfs.c" which calls:
    class = G_VFS_GET_CLASS (vfs);
    return (* class->get_file_for_path) (vfs, path);
    And that's where I'm lost, where's the class->get_file_for_path() implementation?
    Last edited by bird1500; March 23rd, 2013 at 04:59 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
  •