[Filesystem] There must be a better way to do path decomposition
This code at least works: string directory_path(argv[1]); path p(directory_path); ostringstream component; string lastComponent; for (path::iterator it = p.begin(); it != p.end(); it++) { component.str(""); // Hack, to be replaced by better way component << *it; // to grab `*it' in a string lastComponent = component.str(); } lastComponent = lastComponent.substr(1, lastComponent.size()-2); According to the documentation examples, the item pointed to by `*it' is of the following type: path::iterator::value_type is path::string_type, and iteration treats path as a container of filenames. I tried to capture `*it' in a string and in a string_type but the compiler does not like the '=' operator. TIA, -RFH
On 03.08.2011 18:16, Ramon F Herrera wrote:
I tried to capture `*it' in a string and in a string_type but the compiler does not like the '=' operator.
I just tested that in Filesystem v3 and there decomposed path parts are still of type filesystem::path. So my code looks something like this in v3. fs::path tmp("/foo/bar/path"); for(fs::path::const_iterator it = tmp.begin(); it != tmp.end(); ++it) { std::string tmpS = (*it).string(); std::wstring tmpWs = (*it).wstring(); } So maybe switching to v3 (if your code is still v2) might be a solution (since it's anyway the default). regards, Norbert
[Please do not mail me a copy of your followup] boost-users@lists.boost.org spake the secret code <4E397DB2.1020203@gmail.com> thusly:
On 03.08.2011 18:16, Ramon F Herrera wrote:
I tried to capture `*it' in a string and in a string_type but the compiler does not like the '=' operator.
I just tested that in Filesystem v3 and there decomposed path parts are still of type filesystem::path.
So my code looks something like this in v3. fs::path tmp("/foo/bar/path"); for(fs::path::const_iterator it = tmp.begin(); it != tmp.end(); ++it) { std::string tmpS = (*it).string(); std::wstring tmpWs = (*it).wstring(); }
So maybe switching to v3 (if your code is still v2) might be a solution (since it's anyway the default).
The problem with v3 filesystem is that its a PITA to write code that conditionally compiles to ANSI and wide character strings because they changed it string() and wstring() accessors from the *same* class. With std::string and std::wstring I can make my own typedef: #if defined(_UNICODE) typedef std::wstring tstring; #else typedef std::string tstring; #endif and then use tstring for all my string objects. With filesystem v3 this is impossible because you can't create a typedef for "narrow filesystem paths" or "wide filesystem paths" because it tries to be both at the same time. I haven't been able to find any documentation explaining *why* this class now tries to be both a wide string and a narrow string, but unfortunately it makes v3 filesystem very unusable IMO. -- "The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download http://legalizeadulthood.wordpress.com/the-direct3d-graphics-pipeline/ Legalize Adulthood! http://legalizeadulthood.wordpress.com
Thanks so much to Norbert and Richard. I solved my problem with this statement: lastComponent = (*it).string(); As long as we are on the topic: How do I expand directory notations with dots? For instance, if my app is called like this: myapp . The dot should expand to `/home/ramon/src/development' or whatever. I discovered that such construct is not expanded by the U*ix shell. Have tried dirname() plus basename() but they don't work. TIA, -Ramon
On 8/4/2011 9:22 AM, Ramon F Herrera wrote:
Thanks so much to Norbert and Richard. I solved my problem with this statement:
lastComponent = (*it).string();
As long as we are on the topic: How do I expand directory notations with dots? For instance, if my app is called like this:
myapp .
The dot should expand to `/home/ramon/src/development' or whatever.
It seems that such directory path expansion would be perfectly matched to the list of Boost::Filesystem features... -RFH
On Thu, Aug 4, 2011 at 07:22, Ramon F Herrera
As long as we are on the topic: How do I expand directory notations with dots? For instance, if my app is called like this:
myapp .
The dot should expand to `/home/ramon/src/development' or whatever.
"." is a relative path to the current directory. If you want to get an absolute path, then use http://www.boost.org/libs/filesystem/v3/doc/reference.html#system_complete to apply the relative path to the system cwd.
participants (4)
-
legalize+jeeves@mail.xmission.com
-
Norbert Wenzel
-
Ramon F Herrera
-
Scott McMurray