Solved

boost library to normalize the path

Posted on 2014-01-31
9
926 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
Technology Partners: 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!

 
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
 

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

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

735 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