Link to home
Start Free TrialLog in
Avatar of perlperl
perlperl

asked on

boost library to normalize the path

Is there any boost library that will normalize the unix file path.

For example)

1)  input  ////root///dir1///////dir2/dir3   should be normalized to

/root/dir1/dir2/dir3


2)  input   root///////dir4       should be normalized to

root/dir4


I was just wondering if there is boost library then i don't want to write my own
Avatar of jkr
jkr
Flag of Germany image

'boost::filesystem' has 'normalize()'. But an input like '////root///dir1///////dir2/dir3' simply is erroneous, thus I am not sure if you will get the desired result. Anyway, try it out:

filesystem::path pathName( “////root///dir1///////dir2/dir3” );

pathName.normalize();

cout << path'Name.string() << endl();

Open in new window

Avatar of perlperl
perlperl

ASKER

Sure I will try and see.
Its not errornous, one can do ls on such a path on unix and it does work.
so i need to handle such case in my code
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great!!  This works like a charm.

Do I have use try catch?
i know some boost need to catch as it can throw exception
I could not see that anything there would cause an exception (except a completely buggy path, but just in case:

filesystem::path pathName( “////root///dir1///////dir2/dir3” );

try {

    pathName.normalize();

} catch(filesystem::filesystem_exception& e) {

    // handle here
}

cout << pathName.string() << endl();                      
                                            

Open in new window


See http://www.boost.org/doc/libs/1_31_0/libs/filesystem/doc/exception.htm
I think so its

filesystem_error and not exception

catch(boost::filesystem::filesystem_error& e
Yes, sorry - typo :-/

I even linked the correct page and spelled it wrong *grr*