File: memorystream.hpp

package info (click to toggle)
openmw 0.49.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,992 kB
  • sloc: cpp: 372,479; xml: 2,149; sh: 1,403; python: 797; makefile: 26
file content (51 lines) | stat: -rw-r--r-- 1,424 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#ifndef OPENMW_COMPONENTS_FILES_MEMORYSTREAM_H
#define OPENMW_COMPONENTS_FILES_MEMORYSTREAM_H

#include <istream>

namespace Files
{

    struct MemBuf : std::streambuf
    {
        MemBuf(char const* buffer, size_t size)
            // a streambuf isn't specific to istreams, so we need a non-const pointer :/
            : bufferStart(const_cast<char*>(buffer))
            , bufferEnd(bufferStart + size)
        {
            this->setg(bufferStart, bufferStart, bufferEnd);
        }

        pos_type seekoff(off_type off, std::ios_base::seekdir dir, std::ios_base::openmode which) override
        {
            if (dir == std::ios_base::cur)
                gbump(off);
            else
                setg(bufferStart, (dir == std::ios_base::beg ? bufferStart : bufferEnd) + off, bufferEnd);

            return gptr() - bufferStart;
        }

        pos_type seekpos(pos_type pos, std::ios_base::openmode which) override
        {
            return seekoff(pos, std::ios_base::beg, which);
        }

    protected:
        char* bufferStart;
        char* bufferEnd;
    };

    /// @brief A variant of std::istream that reads from a constant in-memory buffer.
    struct IMemStream : virtual MemBuf, std::istream
    {
        IMemStream(char const* buffer, size_t size)
            : MemBuf(buffer, size)
            , std::istream(static_cast<std::streambuf*>(this))
        {
        }
    };

}

#endif