Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I format the boost path object without quotes?

Tags:

c++

path

boost

Here is my code:

fs::path datadir = ...;
std::string dataDirOption((boost::format("--datadir=%1%") % datadir).str());

For datadir=="c:/db" I get dataDirOption=="--datadir=\"c:/db\"", instead of "--datadir=c:/db"

Is it possible to tell boost::filesystem::path to skip the quotes when being formatted?

Now, I know I can substitute datadir.string() for datadir and get rid of the quotes in this way, but I am wondering whether I can do so without the extra string.

Thanks.

like image 982
mark Avatar asked Jan 18 '23 18:01

mark


2 Answers

No it is not, is is a bug files on the boost framework version 1.47.0 which has not yet been given a milestone on when to be fixed.

The workaround however is:

std::cout << path("/foo/bar.txt").filename().string()
like image 159
fredrik Avatar answered Jan 20 '23 06:01

fredrik


The % operator for format uses the << stream-insertion operator for user-defined types, and the documentation tells us it's effectively the following for path:

os << boost::io::quoted(p.string<std::basic_string<Char>>(), static_cast<Char>('&'));

To leave out the quotes, you need to pass something different to the format object, such as the output of the string method as you've already discovered.

like image 36
Rob Kennedy Avatar answered Jan 20 '23 08:01

Rob Kennedy