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
perlperlAsked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
Well, then it migh twork. BTW, just spotted a typo, that should be

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

pathName.normalize();

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

Open in new window

0
 
jkrCommented:
'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

0
 
jkrCommented:
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
perlperlAuthor Commented:
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
0
 
perlperlAuthor Commented:
Great!!  This works like a charm.

Do I have use try catch?
0
 
perlperlAuthor Commented:
i know some boost need to catch as it can throw exception
0
 
jkrCommented:
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
0
 
perlperlAuthor Commented:
I think so its

filesystem_error and not exception

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

I even linked the correct page and spelled it wrong *grr*
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.