I have a Visual Studio 2008 C++03 project where I would like to read a file of key-value pairs in to a std::map.
To do that I've created an istreambuf_pair_iterator
as below:
typedef std::map< std::string, std::string > Properties;
class istreambuf_pair_iterator :
public boost::iterator_adaptor< istreambuf_pair_iterator,
std::pair< std::string, std::string >*,
boost::use_default,
boost::forward_traversal_tag >
{
public:
istreambuf_pair_iterator() : sb_( 0 ) { };
explicit istreambuf_pair_iterator( std::istream& is ) : sb_( is.rdbuf() ) { };
private:
void increment()
{
std::string line;
std::istream is( sb_ );
std::getline( is, line );
// TODO: parse the key=value to a std::pair
// where do I store the pair???
};
friend class boost::iterator_core_access;
std::streambuf* sb_;
};
Properties ReadProperties( const char* file )
{
std::ifstream f( file );
Properties p;
std::copy( istreambuf_pair_iterator( f ),
istreambuf_pair_iterator(),
std::inserter( p, p.end() ) );
return p;
}
Once I have a std::pair<>
made from the string read from the file, where do I store it such that it can be inserted by the std::inserter
in to the std::map
?