Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++/Boost: Writing a more powerful sscanf replacement

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
like image 1000
deuberger Avatar asked Feb 17 '11 22:02

deuberger


People also ask

What to use instead of scanf_ s?

The most common ways of reading input are: using fgets with a fixed size, which is what is usually suggested, and. using fgetc , which may be useful if you're only reading a single char .

What is the equivalent of scanf in C++?

The C++ version of scanf is std::scanf and can be found in the <cstdio> header. Yes, it's the same function - because C functions can also be used in C++.

Can I use scanf in C++?

The scanf() function in C++ is used to read the data from the standard input ( stdin ). The read data is stored in the respective variables. It is defined in the cstdio header file.


2 Answers

What's about that?

void sscanf(std::string str,
            const std::string& format,
            std::vector<boost::any>& result)
{
  std::string::const_iterator i = format.begin();
  while (i != format.end())
  {
    if (*i == '%')
    {
      ++i; // now *i is the conversion specifier
      char specifier = *i;

      ++i; // now *i is the next seperator
      std::string extract = str.substr(0, str.find(*i));

      switch (specifier) 
      {
        // matching an integer
        case 'i':
          result.push_back(boost::lexical_cast<int>(extract));
          break;
        // matching a floating point number
        case 'a': case 'e': case 'f': case 'g':
          result.push_back(boost::lexical_cast<float>(extract));
          break;
        // matching a single character
        case 'c':
          result.push_back(boost::lexical_cast<char>(extract));
          break;
        // matching a string
        case 's':
          result.push_back(extract);
          break;
        // Invalid conversion specifier, throwing an exception
        default:
          throw std::runtime_error("invalid conversion specifier");
          break;
      }
    }
    else
    {
      // if it's not a %, eat!
      str.erase(0, str.find(*i)+1);
      ++i;
    }
  }
}

Some conversions specifiers are missing – but principally it works.

like image 157
Karl von Moor Avatar answered Oct 11 '22 17:10

Karl von Moor


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.

like image 21
Macke Avatar answered Oct 11 '22 16:10

Macke