Solved

boost library to normalize the path

Posted on 2014-01-31
9
951 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

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!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

688 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