4#ifndef INCLUDED_SDSL_RAM_FSTREAMBUF
5#define INCLUDED_SDSL_RAM_FSTREAMBUF
20 void pbump64(std::ptrdiff_t x)
22 while (x > std::numeric_limits<int>::max())
24 pbump(std::numeric_limits<int>::max());
25 x -= std::numeric_limits<int>::max();
36 : m_ram_file(&ram_file)
38 char * begin = m_ram_file->data();
39 char * end = begin + m_ram_file->size();
40 setg(begin, begin, end);
43 std::streambuf *
open(
const std::string name, std::ios_base::openmode mode)
46 if ((mode & std::ios_base::in) and !(mode & std::ios_base::trunc))
63 if ((mode & std::ios_base::out) and !(mode & std::ios_base::app)) { m_ram_file->clear(); }
66 if (m_ram_file and (mode & std::ios_base::trunc)) { m_ram_file->clear(); }
69 if (mode & std::ios_base::ate)
75 setg(m_ram_file->data(), m_ram_file->data(), m_ram_file->data() + m_ram_file->size());
76 setp(m_ram_file->data(), m_ram_file->data() + m_ram_file->size());
79 return m_ram_file ? this :
nullptr;
82 bool is_open() {
return m_ram_file !=
nullptr; }
86 if (!this->
is_open())
return nullptr;
88 setg(
nullptr,
nullptr,
nullptr);
89 setp(
nullptr,
nullptr);
93 pos_type
seekpos(pos_type sp, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out)
override
95 if (sp >= (pos_type)0 and sp <= (pos_type)m_ram_file->size())
97 setg(m_ram_file->data(), m_ram_file->data() + sp, m_ram_file->data() + m_ram_file->size());
98 setp(m_ram_file->data(), m_ram_file->data() + m_ram_file->size());
103 if (mode & std::ios_base::out)
106 m_ram_file->reserve(sp);
107 m_ram_file->resize(sp, 0);
108 setg(m_ram_file->data(), m_ram_file->data() + sp, m_ram_file->data() + m_ram_file->size());
109 setp(m_ram_file->data(), m_ram_file->data() + m_ram_file->size());
114 return pos_type(off_type(-1));
121 std::ios_base::seekdir way,
122 std::ios_base::openmode which = std::ios_base::in | std::ios_base::out)
124 if (std::ios_base::beg == way)
126 if (
seekpos(off, which) == pos_type(-1)) {
return pos_type(-1); }
128 else if (std::ios_base::cur == way)
130 if (
seekpos(gptr() - eback() + off, which) == pos_type(-1)) {
return pos_type(-1); }
132 else if (std::ios_base::end == way)
134 if (
seekpos(egptr() - eback() + off, which) == pos_type(-1)) {
return pos_type(-1); }
136 return gptr() - eback();
139 pos_type
pubseekpos(pos_type sp, std::ios_base::openmode which = std::ios_base::in | std::ios_base::out)
141 if (
seekpos(sp, which) == pos_type(-1)) {
return pos_type(-1); }
144 return gptr() - eback();
148 std::streamsize
xsputn(
const char_type * s, std::streamsize n)
override
152 if (!m_ram_file) {
return 0; }
154 if (n < epptr() - pptr())
156 std::copy(s, s + n, pptr());
162 if (epptr() - pbase() == (std::ptrdiff_t)m_ram_file->size() and epptr() == pptr())
164 m_ram_file->insert(m_ram_file->end(), s, s + n);
165 setp(m_ram_file->data(), m_ram_file->data() + m_ram_file->size());
166 std::ptrdiff_t add = epptr() - pbase();
168 setg(m_ram_file->data(), gptr(), m_ram_file->data() + m_ram_file->size());
173 for (std::streamsize i = 0; i < n; ++i)
175 if (traits_type::eq_int_type(sputc(s[i]), traits_type::eof())) {
return i; }
187 int_type
overflow(int_type c = traits_type::eof())
override
191 m_ram_file->push_back(c);
192 setp(m_ram_file->data(), m_ram_file->data() + m_ram_file->size());
193 std::ptrdiff_t add = epptr() - pbase();
195 setg(m_ram_file->data(), gptr(), m_ram_file->data() + m_ram_file->size());
197 return traits_type::to_int_type(c);
std::streambuf * open(const std::string name, std::ios_base::openmode mode)
int_type overflow(int_type c=traits_type::eof()) override
ram_filebuf(ram_fs::content_type &ram_file)
pos_type seekpos(pos_type sp, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out) override
std::streamsize xsputn(const char_type *s, std::streamsize n) override
pos_type pubseekpos(pos_type sp, std::ios_base::openmode which=std::ios_base::in|std::ios_base::out)
pos_type pubseekoff(off_type off, std::ios_base::seekdir way, std::ios_base::openmode which=std::ios_base::in|std::ios_base::out)
bool exists(const std::string &name)
Check if the file exists.
void store(const std::string &name, content_type data)
std::vector< char, track_allocator< char > > content_type
content_type & content(const std::string &name)
Get the content.
Namespace for the succinct data structure library.