Boost logo

Boost Users :

From: Rush Manbert (rush_at_[hidden])
Date: 2005-12-23 16:31:12


me22 wrote:
> On 23/12/05, David Xiao <davihigh_at_[hidden]> wrote:
>
>>For example I have path "C:\Temp\abcd\hello\", I wish to iterate to get C: ,
>>C:\Temp , C:\Temp\abcd , C:\Temp\abcd\hello .
>>
>
>
> I think you have the choice of either using branch_path repeatedly or
> using the exposed iterators.
>
> using the iterators, I would guess something similar to the following:
> path my_path( "C:\Temp\abcd\hello\", native );
> assert( my_path.has_root_path() );
> path now_path = my_path.root_path();
> my_path = my_path.relative_path();
> for ( path::iterator b = my_path.begin(); b != my_path.end(); ++b ) {
> now_path /= *b;
> std::cout << now_path.native_directory_string() << std::endl;
> }
>
> That of course assumes they're all directories -- you might want to
> choose native_directory_string or native_file_string based on
> is_directory.
>
> You might also look at how
> http://boost.org/libs/filesystem/doc/convenience.htm#create_directories
> is implemented, since it presumably has to do something similar
> internally.
>
> - Scott

If you have the path string and it's valid (you can construct a
boost::filesystem::path object without throwing an exception) then I
think you can just use path.normalize() followed by path.string() to get
the string representation in canonical form. Then just use the regex
library or the string algorithms library to tokenize the string using
"/" as the separator. After that it's pretty easy to put the pieces back
together to make each of the path strings that you want.

If you really want to expand paths like c:/Temp/*/junk then it's more
complicated but a couple of people have addressed it here and their code
is available for download.

My 2 cents...

- Rush


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net