[filesystem][lexical_cast] Lexical cast of filenames containing spaces

Hi, An issue has come up in program options. With filesystem v3, this throws: lexical_cast<boost::filesystem::path>("file name") The problem is that filesystem v3's stream extractor treats spaces as separators (which is the right thing to do IMO), so it only extracts "file", which causes lexical cast to throw an exception - because the whole string hasn't been extracted. Would it be possible to create a special case in lexical_cast to deal with this? How does it deal with this problem for strings? Daniel

An issue has come up in program options. With filesystem v3, this throws:
lexical_cast<boost::filesystem::path>("file name")
The problem is that filesystem v3's stream extractor treats spaces as separators (which is the right thing to do IMO), so it only extracts "file", which causes lexical cast to throw an exception - because the whole string hasn't been extracted.
Would it be possible to create a special case in lexical_cast to deal with this? How does it deal with this problem for strings?
This bug was fixed two month ago. Have a look https://svn.boost.org/trac/boost/ticket/3863 Regards, Dmitry

On 14 August 2010 15:27, Dmitry Goncharov <dgoncharov@unison.com> wrote:
This bug was fixed two month ago. Have a look https://svn.boost.org/trac/boost/ticket/3863
No, that's what caused this problem. What happens is that you when run a program that uses program_options, the shell unquotes the arguments, so an argument with a space in is not quoted. Then program options calls lexical_cast to convert the argument from a string to a filesystem path. Lexical cast uses filesystem's stream extractor to perform this conversion. But since there's a space, it only extracts part of the file name. Lexical cast notices that only some of the string has been extracted, realises that something has gone wrong and throws an exception. There's also a double unencoding issue if the file's name contains quotes. Daniel

On 14 August 2010 15:27, Dmitry Goncharov <dgoncharov@unison.com> wrote:
This bug was fixed two month ago. Have a look https://svn.boost.org/trac/boost/ticket/3863
No, that's what caused this problem. What happens is that you when run a program that uses program_options, the shell unquotes the arguments, so an argument with a space in is not quoted. Then program options calls lexical_cast to convert the argument from a string to a filesystem path. Lexical cast uses filesystem's stream extractor to perform this conversion. But since there's a space, it only extracts part of the file name.
You are describing the behavior w/o the patch applied. With the patch applied filesystem's extractor extracts the whole name until a new line. I see that the patch is already in 1.44. What version do you see the problem in ?
Lexical cast notices that only some of the string has been extracted, realises that something has gone wrong and throws an exception.
There's also a double unencoding issue if the file's name contains quotes.
Daniel
Regards, Dmitry

On 16 August 2010 12:12, Dmitry Goncharov <dgoncharov@unison.com> wrote:
You are describing the behavior w/o the patch applied. With the patch applied filesystem's extractor extracts the whole name until a new line. I see that the patch is already in 1.44. What version do you see the problem in ?
Yes, you're right, but there was a further change in changeset 63136 which restored the behaviour for unquoted strings: https://svn.boost.org/trac/boost/changeset/63136/ It was discussed here: http://thread.gmane.org/gmane.comp.lib.boost.devel/205843 Daniel

On Sat, Aug 14, 2010 at 6:20 AM, Daniel James <dnljms@gmail.com> wrote:
The problem is that filesystem v3's stream extractor treats spaces as separators (which is the right thing to do IMO), so it only extracts "file"...
#define BOOST_FILESYSTEM_VERSION 3 #include <boost/filesystem.hpp> #include <iostream> int main() { boost::filesystem::path p("file name"); std::cout << p; return 0; } The output is "file name". Could you be forgetting #define BOOST_FILESYSTEM_VERSION 3? For 1.44.0, the default is still version 2, and for 1.44.0 that produces file name without the quotes. --Beman

On 16 August 2010 14:59, Beman Dawes <bdawes@acm.org> wrote:
On Sat, Aug 14, 2010 at 6:20 AM, Daniel James <dnljms@gmail.com> wrote:
The problem is that filesystem v3's stream extractor treats spaces as separators (which is the right thing to do IMO), so it only extracts "file"...
boost::filesystem::path p("file name"); std::cout << p;
The output is "file name".
That's not what's happening. I wasn't making a bug report for filesystem, it's a problem with the way lexical_cast uses filesystem in program_options. It's more to do with lexical_cast (which is why I asked how it deals with strings, they must have the same problem). The issue came up here: http://lists.boost.org/boost-users/2010/08/61629.php What's happening is something like: std::stringstream ss; boost::filesystem::path p ss << "file name"; ss >> p; Which just reads 'file'. But lexical_cast doesn't do that for strings, so I assume there must be a specialization somewhere. So I wanted to know if something could be added to filesystem or lexical_cast to deal with this case. Or if that's just the way it works, and program_options will have to contain a special case, which would be a pity. Daniel
participants (3)
-
Beman Dawes
-
Daniel James
-
Dmitry Goncharov