I want to write a function in C++ to replace C's sscanf that assigns the matches to iterator.
Basically, I want something like:
string s = "0.5 6 hello";
std::vector<boost::any> any_vector;
sscanv(s, "%f %i %s", any_vector);
cout << "float: " << any_cast<float>(any_vector[0]);
cout << "integer: " << any_cast<integer(any_vector[1]);
cout << "string: " << any_cast<string>(any_vector[2]);
The exact details may vary, but you get the idea. Any ideas for implementation?
Options so far along with problems so far:
- std::istringstream: there's no manipulator for matching constant expressions
- Boost.Regex: not sure if this will work and it seems much more complicated than necessary for this
- Boost.Spirit: don't think this will work for dynamically generated format strings and it also seems more complicated then necessary
- sscanf: it would work, but is non-standard, etc, and using it would require a lot of overhead since the number of arguments is determined at compile time
If your format string is determined at compile time, there are some variadic-template printf replacements written. Inverting those should work reasonably well.
You could then use istream's >> operator for reading, or the c-stdlib functions.
What's about that?
Some conversions specifiers are missing – but principally it works.