SRD

setg

SRD
Jun 17th, 2017
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. // I memory map a file using mapped_file_source from boost which has these two members ...
  2.  
  3. class mapped_file_source {
  4.     // ...
  5.  
  6.     size_type size() const;
  7.     const char* data() const;    // ...
  8. };
  9.  
  10. // I'm trying to put data into an istream so that I can parse the data line by line.
  11.  
  12.     struct membuf : streambuf
  13.     {                                  
  14.         membuf(char* begin, char* end) {
  15.             this->setg(begin, begin, end);
  16.         }
  17.     };
  18.  
  19.     // ....
  20.  
  21.     membuf sbuf(const_cast<char *>(map.data()), map.size());
  22.     istream in(&sbuf);
  23.  
  24.     while (getline(in, line)) {
  25.         // do stuff w/ line
  26.     }
  27.  
  28. /*
  29. test.cpp:49:54: error: invalid conversion from ‘boost::iostreams::mapped_file_source::size_type {aka long unsigned int}’ to ‘char*’ [-fpermissive]
  30.   membuf sbuf(const_cast<char *>(map.data()), map.size());
  31.                                                       ^
  32.  
  33. The problem is that setg takes char *'s and map.size() returns size_type (a.k.a. long unsigned int).
  34.  
  35. Other thoughts on how to get data into an istream from a 'const char*' that is as efficient as possible?
  36. */
Advertisement
Add Comment
Please, Sign In to add comment