Solved

boost library to normalize the path

Posted on 2014-01-31
9
859 Views
Last Modified: 2014-02-03
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
Comment
Question by:perlperl
  • 5
  • 4
9 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 39825369
'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
 
LVL 86

Expert Comment

by:jkr
ID: 39825371
0
 

Author Comment

by:perlperl
ID: 39825433
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
 
LVL 86

Accepted Solution

by:
jkr earned 300 total points
ID: 39825439
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:perlperl
ID: 39825458
Great!!  This works like a charm.

Do I have use try catch?
0
 

Author Comment

by:perlperl
ID: 39825461
i know some boost need to catch as it can throw exception
0
 
LVL 86

Expert Comment

by:jkr
ID: 39825475
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
 

Author Comment

by:perlperl
ID: 39829552
I think so its

filesystem_error and not exception

catch(boost::filesystem::filesystem_error& e
0
 
LVL 86

Expert Comment

by:jkr
ID: 39829725
Yes, sorry - typo :-/

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now