1

I'm trying to create a string I can tokenise, from a file. What works is

boost::iostreams::stream_buffer<boost::iostreams::mapped_file_source> file("file.txt");
boost::char_separator<char> sep(",");
std::string buf(file->begin(),file->end())
boost::tokenizer<boost::char_separator<char>> tokeniser(buf,sep);

But I don't to unnecessarily copy file's buffer to the string I thought I could do:

boost::iostreams::stream_buffer<boost::iostreams::mapped_file_source> file("file.txt");
boost::iterator_range<const char*> it = make_iterator_range(file->begin(),file->end());
boost::tokenizer<boost::char_separator<char>> tokeniser(it,sep);

or

std::istringstream buf;
buf.rdbuf()->pubsetbuf(const_cast<char*>(file->data()),file->size());
boost::char_separator<char> sep(",");
boost::tokenizer<boost::char_separator<char>> tokeniser(buf,sep);

I also tried

boost::tokenizer<boost::char_separator<char>> tokeniser(boost::as_literal(file->data()),sep);

But each time the tokenizer doesn't accept the iterator

Is there an efficient portable way of doing this?

Edit Possible answer I think would solve this

What I have thought of since, is to use an istream iterator to achieve the non-copy as I'm fairly (though not 100%) confident that iterators iterate over and do not copy their underlying data

boost::iostreams::stream_buffer<boost::iostreams::mapped_file_source> file("names.txt");
std::istream in(&file);
std::istream_iterator<std::string> iter(in);
boost::char_separator<char> sep(",");
boost::tokenizer<boost::char_separator<char>> tokeniser(*iter,sep);

Thoughts? Or is there something better?

Delta_Fore
  • 3,079
  • 4
  • 26
  • 46

0 Answers0