• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1038
  • Last Modified:

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
0
perlperl
Asked:
perlperl
  • 5
  • 4
1 Solution
 
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
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now