istream vs memory mapping a file?

前端 未结 3 1812
生来不讨喜
生来不讨喜 2021-02-02 17:31

I am trying to map a file to memory and then parse line by line- is istream what I should be using?

Is istream the same as mapping a file to memory on Windows? I have h

3条回答
  •  天命终不由人
    2021-02-02 18:29

    std::istream is an abstract type – you cannot use it directly. You should be deriving from it with a custom array-backed streambuf:

    #include 
    #include 
    #include 
    #include 
    
    template>
    struct basic_membuf : std::basic_streambuf {
        basic_membuf(CharT const* const buf, std::size_t const size) {
            CharT* const p = const_cast(buf);
            this->setg(p, p, p + size);
        }
    
        //...
    };
    
    template>
    struct basic_imemstream
    : virtual basic_membuf, std::basic_istream {
        basic_imemstream(CharT const* const buf, std::size_t const size)
        : basic_membuf(buf, size),
          std::basic_istream(static_cast*>(this))
        { }
    
        //...
    };
    
    using imemstream = basic_imemstream;
    
    char const* const mmaped_data = /*...*/;
    std::size_t const mmap_size = /*...*/;
    imemstream s(mmaped_data, mmap_size);
    // s now uses the memory mapped data as its underlying buffer.
    

    As for the memory-mapping itself, I recommend using Boost.Interprocess for this purpose:

    #include 
    #include 
    #include 
    #include 
    
    namespace bip = boost::interprocess;
    
    //...
    
    std::string filename = /*...*/;
    bip::file_mapping mapping(filename.c_str(), bip::read_only);
    bip::mapped_region mapped_rgn(mapping, bip::read_only);
    char const* const mmaped_data = static_cast(mapped_rgn.get_address());
    std::size_t const mmap_size = mapped_rgn.get_size();
    

    Code for imemstream taken from this answer by Dietmar Kühl.

提交回复
热议问题