28 _Success_(
return)
bool read_id(_In_ std::istream& stream, _Out_ T_ID &
id, _In_opt_ std::streamoff end = (std::streamoff)-1)
30 if (end == (std::streamoff)-1 || stream.tellg() < end) {
31 stream.read((
char*)&
id,
sizeof(
id));
49 _Success_(
return)
bool read_id(_In_
stdex::stream::basic_file& stream, _Out_ T_ID &
id, _In_opt_ stdex::stream::fpos_t end = stdex::stream::fpos_max)
51 if (end == stdex::stream::fpos_max || stream.tell() < end) {
67 template <
class T_SIZE, T_SIZE ALIGN>
68 bool ignore(_In_ std::istream& stream)
72 stream.read((
char*)&size,
sizeof(size));
73 if (!stream.good()) _Unlikely_
return false;
76 size += (T_SIZE)(ALIGN - size) % ALIGN;
78 if (!stream.good()) _Unlikely_
return false;
92 template <
class T_SIZE, T_SIZE ALIGN>
98 if (!stream.ok()) _Unlikely_
return false;
101 size += (T_SIZE)(ALIGN - size) % ALIGN;
103 if (!stream.ok()) _Unlikely_
return false;
119 template <
class T_ID,
class T_SIZE, T_SIZE ALIGN>
120 bool find(_In_ std::istream& stream, _In_ T_ID
id, _In_opt_ std::streamoff end = (std::streamoff)-1)
123 while (end == (std::streamoff)-1 || stream.tellg() < end) {
124 stream.read((
char*)&_id,
sizeof(_id));
125 if (!stream.good()) _Unlikely_
return false;
130 ignore<T_SIZE, ALIGN>(stream);
146 template <
class T_ID,
class T_SIZE, T_SIZE ALIGN>
147 bool find(_In_
stdex::stream::basic_file& stream, _In_ T_ID
id, _In_opt_ stdex::stream::fpos_t end = stdex::stream::fpos_max)
150 while (end == stdex::stream::fpos_max || stream.tell() < end) {
152 if (!stream.ok()) _Unlikely_
return false;
157 ignore<T_SIZE, ALIGN>(stream);
170 template <
class T_ID,
class T_SIZE>
171 std::streamoff open(_In_ std::ostream& stream, _In_ T_ID
id)
173 std::streamoff start = stream.tellp();
176 if (stream.fail()) _Unlikely_
return (std::streamoff)-1;
177 stream.write((
const char*)&
id,
sizeof(
id));
180 if (stream.fail()) _Unlikely_
return (std::streamoff)-1;
182 stream.write((
const char*)&size,
sizeof(size));
195 template <
class T_ID,
class T_SIZE>
198 auto start = stream.tell();
217 template <
class T_ID,
class T_SIZE, T_SIZE ALIGN>
218 std::streamoff close(_In_ std::ostream& stream, _In_ std::streamoff start)
220 std::streamoff end = stream.tellp();
222 size =
static_cast<T_SIZE
>(end - start -
sizeof(T_ID) -
sizeof(T_SIZE)),
223 remainder =
static_cast<T_SIZE
>((ALIGN - size) % ALIGN);
227 static const char padding[ALIGN] = {};
228 stream.write(padding, remainder);
233 if (stream.fail()) _Unlikely_
return (std::streamoff)-1;
234 stream.seekp(start +
sizeof(T_ID));
235 stream.write((
const char*)&size,
sizeof(size));
249 template <
class T_ID,
class T_SIZE, T_SIZE ALIGN>
252 auto end = stream.tell();
254 size =
static_cast<T_SIZE
>(end - start -
sizeof(T_ID) -
sizeof(T_SIZE)),
255 remainder =
static_cast<T_SIZE
>((ALIGN - size) % ALIGN);
259 static const char padding[ALIGN] = {};
260 stream.write_array(padding,
sizeof(
char), remainder);
265 if (!stream.ok()) _Unlikely_
return stdex::stream::fpos_max;
266 stream.seek(start +
sizeof(T_ID));
276 template <
class T,
class T_ID, const T_ID ID,
class T_SIZE, T_SIZE ALIGN>
297 static const T_ID
id()
322 static std::streamoff
open(_In_ std::ostream& stream)
324 return stdex::idrec::open<T_ID, T_SIZE>(stream, ID);
336 return stdex::idrec::open<T_ID, T_SIZE>(stream, ID);
347 static std::streamoff
close(_In_ std::ostream& stream, _In_ std::streamoff start)
349 return stdex::idrec::close<T_ID, T_SIZE, ALIGN>(stream, start);
362 return stdex::idrec::close<T_ID, T_SIZE, ALIGN>(stream, start);
375 static bool find(_In_ std::istream& stream, _In_opt_ std::streamoff end = (std::streamoff)-1)
377 return stdex::idrec::find<T_ID, T_SIZE, ALIGN>(stream, ID, end);
392 return stdex::idrec::find<T_ID, T_SIZE, ALIGN>(stream, ID, end);
409 auto start = r.open(stream);
410 if (stream.fail()) _Unlikely_
return stream;
412 r.close(stream, start);
429 auto start = r.open(stream);
430 if (!stream.ok()) _Unlikely_
return stream;
432 r.close(stream, start);
450 auto start = r.open(temp);
451 if (!temp.
ok()) _Unlikely_
return stream;
453 r.close(temp, start);
455 stream.write_stream(temp);
474 stream.read((
char*)&size,
sizeof(size));
475 if (!stream.good()) _Unlikely_
return stream;
478 std::streamoff start = stream.tellg();
480 if (!stream.good()) _Unlikely_
return stream;
482 size +=
static_cast<T_SIZE
>((ALIGN - size) % ALIGN);
483 stream.seekg(start + size);
503 if (!stream.ok()) _Unlikely_
return stream;
506 auto start = stream.tell();
510 if (!limiter.
ok()) _Unlikely_
return stream;
513 size +=
static_cast<T_SIZE
>((ALIGN - size) % ALIGN);
514 stream.seek(start + size);
534 if (!stream.ok()) _Unlikely_
return stream;
539 if (!limiter.
ok()) _Unlikely_
return stream;
542 stream.skip(
static_cast<T_SIZE
>((ALIGN - size) % ALIGN));
Helper class for read/write of records to/from memory.
Definition idrec.hpp:278
static stdex::stream::foff_t open(stdex::stream::basic_file &stream)
Writes record header.
Definition idrec.hpp:334
friend std::istream & operator>>(std::istream &stream, record< T, T_ID, ID, T_SIZE, ALIGN > r)
Reads record from a stream.
Definition idrec.hpp:468
static stdex::stream::foff_t close(stdex::stream::basic_file &stream, stdex::stream::foff_t start)
Updates record header.
Definition idrec.hpp:360
static bool find(std::istream &stream, std::streamoff end=(std::streamoff) -1)
Finds record data.
Definition idrec.hpp:375
T & data
Record data reference.
Definition idrec.hpp:395
static const T_ID id()
Returns record id.
Definition idrec.hpp:297
static std::streamoff open(std::ostream &stream)
Writes record header.
Definition idrec.hpp:322
const record< T, T_ID, ID, T_SIZE, ALIGN > & operator=(const record< T, T_ID, ID, T_SIZE, ALIGN > &r)
Assignment operator.
Definition idrec.hpp:309
record(T &d)
Constructs the class.
Definition idrec.hpp:285
static bool find(stdex::stream::basic_file &stream, stdex::stream::foff_t end=stdex::stream::foff_max)
Finds record data.
Definition idrec.hpp:390
friend std::ostream & operator<<(std::ostream &stream, const record< T, T_ID, ID, T_SIZE, ALIGN > r)
Writes record to a stream.
Definition idrec.hpp:405
record(const T &d)
Constructs the class.
Definition idrec.hpp:292
static std::streamoff close(std::ostream &stream, std::streamoff start)
Updates record header.
Definition idrec.hpp:347
Basic seekable stream operations.
Definition stream.hpp:823
UTF-8 byte-order-mark
Definition stream.hpp:78
bool ok() const
Returns true if the stream state is clean i.e. previous operation was succesful.
Definition stream.hpp:174
virtual void skip(fsize_t amount)
Skips given amount of bytes of data on the stream.
Definition stream.hpp:141
Limits reading from/writing to stream to a predefined number of bytes.
Definition stream.hpp:1550
fsize_t read_limit
Number of bytes left that may be read from the stream.
Definition stream.hpp:1602
In-memory file.
Definition stream.hpp:3171
virtual fpos_t seek(foff_t offset, seek_t how=seek_t::beg)
Seeks to specified relative file position.
Definition stream.hpp:3732
const void * data() const
Returns pointer to data.
Definition stream.hpp:3381