Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for <boost/std>::filesystem::path #25

Closed
Flamefire opened this issue Dec 9, 2018 · 4 comments

Comments

@Flamefire
Copy link

commented Dec 9, 2018

Found during use of boost.program_options: A lexical_cast of an unquoted string to bfs::path (and probably to std::path) stops on the first whitespace making the result invalid.

Reproducer:

std::cout << boost::lexical_castboost::filesystem::path("/home/my user");

Outputs: /home/my
Expected: /home/my user

Reason is the usage of the stream operators which stop on first whitespace

Possible solution: path supports construction from string. So if a ctor from string exists, use that.

More info: http://boost.2283326.n4.nabble.com/program-options-Problem-with-paths-that-have-spaces-td2576490.html

@apolukhin

This comment has been minimized.

Copy link
Member

commented Dec 9, 2018

lexical_cast should either convert all the data or throw an exception. This looks like a bug, but I'm not sure, what's the right behavior for that case.

@Flamefire

This comment has been minimized.

Copy link
Author

commented Dec 10, 2018

What about "if a ctor from string exists, use that." suggested above?

@apolukhin

This comment has been minimized.

Copy link
Member

commented Jun 23, 2019

I've just tried the following code

#include <boost/lexical_cast.hpp>
#include <boost/filesystem/path.hpp>
#include <iostream>

int main() {
    std::cout << 
      boost::lexical_cast<boost::filesystem::path>("/home/my user");
  }

And it throws an exception:

terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_lexical_cast> >'
  what():  bad lexical cast: source type value could not be interpreted as target

Same story with using std::filesystem::path.
That's a valid behavior.

Adding quotes around the path makes the conversion succeed:

#include <boost/lexical_cast.hpp>
#include <filesystem>
#include <iostream>

int main() {
    std::cout << 
      boost::lexical_cast<std::filesystem::path>("\"/home/my user\"");
  }

Note that outputting a path adds quotes:

#include <boost/lexical_cast.hpp>
#include <filesystem>
#include <iostream>

int main() {
    std::cout << 
      std::filesystem::path("/home/my user");
  }

Outputs: "/home/my user"

The existing behavior of the boost::lexical_cast is perfectly right. I'll add tests to make sure that the behavior doesn't change in the future. After that the ticket will be closed.

apolukhin added a commit that referenced this issue Jun 23, 2019
apolukhin added a commit that referenced this issue Jun 23, 2019
@apolukhin

This comment has been minimized.

Copy link
Member

commented Jun 23, 2019

What about "if a ctor from string exists, use that." suggested above?

This will break existing code.
Tests added.

@apolukhin apolukhin closed this Jun 23, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.