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,
unsigned int 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,
unsigned int 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,
unsigned int 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,
unsigned int 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();
201 if (!stream.ok()) _Unlikely_
return stdex::stream::fpos_max;
205 if (!stream.ok()) _Unlikely_
return stdex::stream::fpos_max;
219 template <
class T_ID,
class T_SIZE,
unsigned int ALIGN>
220 std::streamoff close(_In_ std::ostream& stream, _In_ std::streamoff start)
222 std::streamoff end = stream.tellp();
224 size =
static_cast<T_SIZE
>(end - start -
sizeof(T_ID) -
sizeof(T_SIZE)),
225 remainder =
static_cast<T_SIZE
>((ALIGN - size) % ALIGN);
229 static const char padding[ALIGN] = {};
230 stream.write(padding, remainder);
235 if (stream.fail()) _Unlikely_
return (std::streamoff)-1;
236 stream.seekp(start +
sizeof(T_ID));
237 stream.write((
const char*)&size,
sizeof(size));
251 template <
class T_ID,
class T_SIZE,
unsigned int ALIGN>
254 auto end = stream.tell();
256 size =
static_cast<T_SIZE
>(end - start -
sizeof(T_ID) -
sizeof(T_SIZE)),
257 remainder =
static_cast<T_SIZE
>((ALIGN - size) % ALIGN);
261 static const char padding[ALIGN] = {};
262 stream.write_array(padding,
sizeof(
char), remainder);
267 if (!stream.ok()) _Unlikely_
return stdex::stream::fpos_max;
268 stream.seek(start +
sizeof(T_ID));
278 template <
class T,
class T_ID, const T_ID ID,
class T_SIZE,
unsigned int ALIGN>
299 static const T_ID
id()
324 static std::streamoff
open(_In_ std::ostream& stream)
326 return stdex::idrec::open<T_ID, T_SIZE>(stream, ID);
338 return stdex::idrec::open<T_ID, T_SIZE>(stream, ID);
349 static std::streamoff
close(_In_ std::ostream& stream, _In_ std::streamoff start)
351 return stdex::idrec::close<T_ID, T_SIZE, ALIGN>(stream, start);
364 return stdex::idrec::close<T_ID, T_SIZE, ALIGN>(stream, start);
377 static bool find(_In_ std::istream& stream, _In_opt_ std::streamoff end = (std::streamoff)-1)
379 return stdex::idrec::find<T_ID, T_SIZE, ALIGN>(stream, ID, end);
394 return stdex::idrec::find<T_ID, T_SIZE, ALIGN>(stream, ID, end);
410template <
class T,
class T_ID, T_ID ID,
class T_SIZE,
unsigned int ALIGN>
415 auto start = r.open(stream);
416 if (stream.fail()) _Unlikely_
return stream;
418 r.close(stream, start);
431template <
class T,
class T_ID, T_ID ID,
class T_SIZE,
unsigned int ALIGN>
436 auto start = r.open(stream);
437 if (!stream.ok()) _Unlikely_
return stream;
439 r.close(stream, start);
452template <
class T,
class T_ID, T_ID ID,
class T_SIZE,
unsigned int ALIGN>
459 stream.read((
char*)&size,
sizeof(size));
460 if (!stream.good()) _Unlikely_
return stream;
463 std::streamoff start = stream.tellg();
465 if (!stream.good()) _Unlikely_
return stream;
467 size +=
static_cast<T_SIZE
>((ALIGN - size) % ALIGN);
468 stream.seekg(start + size);
481template <
class T,
class T_ID, T_ID ID,
class T_SIZE,
unsigned int ALIGN>
489 if (!stream.ok()) _Unlikely_
return stream;
492 auto start = stream.tell();
496 if (!limiter.ok()) _Unlikely_
return stream;
499 size +=
static_cast<T_SIZE
>((ALIGN - size) % ALIGN);
500 stream.seek(start + size);
Helper class for read/write of records to/from memory.
Definition idrec.hpp:280
static stdex::stream::foff_t open(stdex::stream::basic_file &stream)
Writes record header.
Definition idrec.hpp:336
static stdex::stream::foff_t close(stdex::stream::basic_file &stream, stdex::stream::foff_t start)
Updates record header.
Definition idrec.hpp:362
static bool find(std::istream &stream, std::streamoff end=(std::streamoff) -1)
Finds record data.
Definition idrec.hpp:377
T & data
Record data reference.
Definition idrec.hpp:397
static const T_ID id()
Returns record id.
Definition idrec.hpp:299
static std::streamoff open(std::ostream &stream)
Writes record header.
Definition idrec.hpp:324
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:311
record(T &d)
Constructs the class.
Definition idrec.hpp:287
static bool find(stdex::stream::basic_file &stream, stdex::stream::foff_t end=stdex::stream::foff_max)
Finds record data.
Definition idrec.hpp:392
record(const T &d)
Constructs the class.
Definition idrec.hpp:294
static std::streamoff close(std::ostream &stream, std::streamoff start)
Updates record header.
Definition idrec.hpp:349
Basic seekable stream operations.
Definition stream.hpp:695
Limits reading from/writing to stream to a predefined number of bytes.
Definition stream.hpp:1413