10#include "interval.hpp"
29#include <condition_variable>
37#if !defined(SET_FILE_OP_TIMES) && defined(RDAT_BELEZI_CAS_DOSTOPA_VER)
38#define SET_FILE_OP_TIMES 1
39#pragma message("RDAT_BELEZI_CAS_DOSTOPA_VER is deprecated. Use SET_FILE_OP_TIMES instead.")
40#elif !defined(SET_FILE_OP_TIMES)
41#define SET_FILE_OP_TIMES 0
43#if !defined(CHECK_STREAM_STATE) && defined(RDAT_NE_PREVERJAJ_STANJA_VER)
44#define CHECK_STREAM_STATE 0
45#pragma message("RDAT_NE_PREVERJAJ_EOF_VER is deprecated. Use CHECK_STREAM_STATE=0 instead.")
47#define CHECK_STREAM_STATE 1
66 using fsize_t = uint64_t;
67 constexpr fsize_t fsize_max = UINT64_MAX;
69 constexpr size_t iterate_count = 0x10;
70 constexpr size_t default_block_size = 0x10000;
71 constexpr char16_t utf16_bom = u
'\ufeff';
72 constexpr char32_t utf32_bom = U
'\ufeff';
73 constexpr const char utf8_bom[3] = {
'\xef',
'\xbb',
'\xbf' };
83 virtual ~basic()
noexcept(
false) {}
96 virtual _Success_(
return != 0 || length == 0) size_t
read(
97 _Out_writes_bytes_to_opt_(length, return)
void* data, _In_
size_t length)
100 _Unreferenced_(length);
101 m_state = state_t::fail;
114 virtual _Success_(
return != 0) size_t
write(
115 _In_reads_bytes_opt_(length) const
void* data, _In_
size_t length)
117 _Unreferenced_(data);
118 _Unreferenced_(length);
119 m_state = state_t::fail;
128 m_state = state_t::ok;
136 m_state = state_t::ok;
142 virtual void skip(_In_ fsize_t amount)
146 else if (amount < iterate_count) {
147 for (
size_t i = 0; i < static_cast<size_t>(amount); i++) {
149 if (!
ok()) _Unlikely_
154 size_t block =
static_cast<size_t>(std::min<fsize_t>(amount, default_block_size));
156 std::unique_ptr<uint8_t[]> dummy(
new uint8_t[block]);
158 amount -=
read_array(dummy.get(),
sizeof(uint8_t),
static_cast<size_t>(std::min<fsize_t>(amount, block)));
159 if (!
ok()) _Unlikely_
163 catch (
const std::bad_alloc&) { m_state = state_t::fail; }
170 inline state_t
state()
const {
return m_state; };
175 inline bool ok()
const {
return m_state == state_t::ok; };
186 std::vector<uint8_t> result;
187 size_t offset, length;
189 length = default_block_size;
190 while (offset < max_length) {
191 length = std::min(length, max_length);
192 try { result.resize(length); }
193 catch (
const std::bad_alloc&) {
194 m_state = state_t::fail;
197 auto num_read =
read_array(result.data() + offset,
sizeof(uint8_t), length - offset);
199 if (!
ok()) _Unlikely_
201 length += default_block_size;
203 result.resize(offset);
215 throw std::system_error(sys_error(), std::system_category(),
"failed to read");
224 write(&
byte,
sizeof(uint8_t));
225 else if (amount < iterate_count) {
226 for (
size_t i = 0; i < static_cast<size_t>(amount); i++) {
227 write(&
byte,
sizeof(uint8_t));
228 if (!
ok()) _Unlikely_
233 size_t block =
static_cast<size_t>(std::min<fsize_t>(amount, default_block_size));
235 std::unique_ptr<uint8_t[]> dummy(
new uint8_t[block]);
236 memset(dummy.get(),
byte, block);
238 amount -=
write_array(dummy.get(),
sizeof(uint8_t),
static_cast<size_t>(std::min<fsize_t>(amount, block)));
239 if (!
ok()) _Unlikely_
243 catch (
const std::bad_alloc&) { m_state = state_t::fail; }
261 if (!
ok()) _Unlikely_ {
270 m_state = state_t::eof;
289 if (!
ok()) _Unlikely_
291#if BYTE_ORDER == BIG_ENDIAN
292 T data_le = HE2LE(data);
293 write(&data_le,
sizeof(T));
295 write(&data,
sizeof(T));
305 template<
class _Traits = std::
char_traits<
char>,
class _Ax = std::allocator<
char>>
306 inline size_t readln(_Inout_ std::basic_string<char, _Traits, _Ax>& str)
317 template<
class _Traits = std::
char_traits<
wchar_t>,
class _Ax = std::allocator<
wchar_t>>
318 inline size_t readln(_Inout_ std::basic_string<wchar_t, _Traits, _Ax>& wstr)
329 template<
class T_from,
class T_to,
class _Traits = std::
char_traits<T_to>,
class _Ax = std::allocator<T_to>>
332 if (encoder.from_encoding() == encoder.to_encoding())
336 encoder.strcpy(wstr, str);
345 template<
class _Elem,
class _Traits = std::
char_traits<_Elem>,
class _Ax = std::allocator<_Elem>>
349 _Elem chr, previous = (_Elem)0;
352 if (!initial && !(previous ==
static_cast<_Elem
>(
'\r') && chr ==
static_cast<_Elem
>(
'\n')))
357 }
while (
ok() && chr !=
static_cast<_Elem
>(
'\n'));
366 template<
class T_from,
class T_to,
class _Traits = std::
char_traits<T_to>,
class _Ax = std::allocator<T_to>>
369 if (encoder.from_encoding() == encoder.to_encoding())
373 encoder.strcat(wstr, str);
382 size_t read_array(_Out_writes_bytes_(size* count)
void* array, _In_
size_t size, _In_
size_t count)
384 for (
size_t to_read = mul(size, count);;) {
385 size_t num_read =
read(array, to_read);
389 if (!
ok()) _Unlikely_
390 return count - to_read / size;
391 reinterpret_cast<uint8_t*&
>(array) += num_read;
400 inline size_t write_array(_In_reads_bytes_opt_(size* count)
const void* array, _In_
size_t size, _In_
size_t count)
402 return write(array, mul(size, count)) / size;
413 template <
class T_from,
class T_to>
416 if (!
ok()) _Unlikely_
418 size_t num_chars = stdex::strlen(wstr);
419 if (encoder.from_encoding() == encoder.to_encoding())
420 return write_array(wstr,
sizeof(T_from), num_chars);
421 std::basic_string<T_to> str(encoder.convert(wstr, num_chars));
422 return write_array(str.data(),
sizeof(T_to), str.size());
434 template <
class T_from,
class T_to>
437 if (!
ok()) _Unlikely_
439 num_chars = stdex::strnlen(wstr, num_chars);
440 if (encoder.from_encoding() == encoder.to_encoding())
441 return write_array(wstr,
sizeof(T_from), num_chars);
442 std::basic_string<T_to> str(encoder.convert(wstr, num_chars));
443 return write_array(str.data(),
sizeof(T_to), str.size());
454 template<
class T_from,
class T_to,
class _Traits = std::
char_traits<T_from>,
class _Ax = std::allocator<T_from>>
457 if (!
ok()) _Unlikely_
459 if (encoder.from_encoding() == encoder.to_encoding())
460 return write_array(wstr.data(),
sizeof(T_from), wstr.size());
461 std::basic_string<T_to> str(encoder.convert(wstr));
462 return write_array(str.data(),
sizeof(T_to), str.size());
476 template<
class _Elem,
class _Traits = std::
char_traits<_Elem>,
class _Ax = std::allocator<_Elem>>
480 if (!
ok()) _Unlikely_
484 if (!
ok()) _Unlikely_
486 data.reserve(num_chars);
489 uint32_t num_read =
static_cast<uint32_t
>(
read_array(buf,
sizeof(_Elem), std::min<uint32_t>(num_chars, _countof(buf))));
490 data.append(buf, buf + num_read);
491 num_chars -= num_read;
492 if (!num_chars || !
ok())
512 size_t num_chars = stdex::strlen(data);
513 if (num_chars > UINT32_MAX)
514 throw std::invalid_argument(
"string too long");
516 if (!
ok()) _Unlikely_
533 template<
class _Elem,
class _Traits = std::
char_traits<_Elem>,
class _Ax = std::allocator<_Elem>>
537 size_t num_chars = data.size();
538 if (num_chars > UINT32_MAX)
539 throw std::invalid_argument(
"string too long");
541 if (!
ok()) _Unlikely_
543 write_array(data.data(),
sizeof(_Elem), num_chars);
553 size_t write_sa(_In_ LPSAFEARRAY sa)
555 safearray_accessor<void> a(sa);
557 if (FAILED(SafeArrayGetUBound(sa, 1, &ubound)) ||
558 FAILED(SafeArrayGetLBound(sa, 1, &lbound)))
559 throw std::invalid_argument(
"SafeArrayGet[UL]Bound failed");
560 return write(a.data(),
static_cast<size_t>(ubound) - lbound + 1);
571 std::unique_ptr<uint8_t[]> data(
new uint8_t[
static_cast<size_t>(std::min<fsize_t>(amount, default_block_size))]);
572 fsize_t num_copied = 0, to_write = amount;
573 m_state = state_t::ok;
575 size_t num_read = stream.read(data.get(),
static_cast<size_t>(std::min<fsize_t>(default_block_size, to_write)));
576 size_t num_written =
write(data.get(), num_read);
577 num_copied += num_written;
578 to_write -= num_written;
579 if (stream.m_state == state_t::eof) {
581 m_state = state_t::ok;
584 m_state = stream.m_state;
596 if (charset == charset_id::utf32)
598 else if (charset == charset_id::utf16)
600 else if (charset == charset_id::utf8)
609 size_t write_sprintf(_In_z_ _Printf_format_string_params_(2)
const char* format, _In_opt_ locale_t locale, ...)
612 va_start(params, locale);
623 size_t write_sprintf(_In_z_ _Printf_format_string_params_(2)
const wchar_t* format, _In_opt_ locale_t locale, ...)
626 va_start(params, locale);
637 size_t write_vsprintf(_In_z_ _Printf_format_string_params_(2)
const char* format, _In_opt_ locale_t locale, _In_ va_list params)
640 str.reserve(default_block_size);
641 vappendf(str, format, locale, params);
642 return write_array(str.data(),
sizeof(
char), str.size());
650 size_t write_vsprintf(_In_z_ _Printf_format_string_params_(2)
const wchar_t* format, _In_opt_ locale_t locale, _In_ va_list params)
653 str.reserve(default_block_size);
654 vappendf(str, format, locale, params);
655 return write_array(str.data(),
sizeof(
wchar_t), str.size());
658 inline basic& operator >>(_Out_ int8_t& data) {
return read_data(data); }
659 inline basic& operator <<(_In_
const int8_t data) {
return write_data(data); }
660 inline basic& operator >>(_Out_ int16_t& data) {
return read_data(data); }
661 inline basic& operator <<(_In_
const int16_t data) {
return write_data(data); }
662 inline basic& operator >>(_Out_ int32_t& data) {
return read_data(data); }
663 inline basic& operator <<(_In_
const int32_t data) {
return write_data(data); }
664 inline basic& operator >>(_Out_ int64_t& data) {
return read_data(data); }
665 inline basic& operator <<(_In_
const int64_t data) {
return write_data(data); }
666 inline basic& operator >>(_Out_ uint8_t& data) {
return read_data(data); }
667 inline basic& operator <<(_In_
const uint8_t data) {
return write_data(data); }
668 inline basic& operator >>(_Out_ uint16_t& data) {
return read_data(data); }
669 inline basic& operator <<(_In_
const uint16_t data) {
return write_data(data); }
670 inline basic& operator >>(_Out_ uint32_t& data) {
return read_data(data); }
671 inline basic& operator <<(_In_
const uint32_t data) {
return write_data(data); }
672 inline basic& operator >>(_Out_ uint64_t& data) {
return read_data(data); }
673 inline basic& operator <<(_In_
const uint64_t data) {
return write_data(data); }
674 inline basic& operator >>(_Out_
float& data) {
return read_data(data); }
675 inline basic& operator <<(_In_
const float data) {
return write_data(data); }
676 inline basic& operator >>(_Out_
double& data) {
return read_data(data); }
677 inline basic& operator <<(_In_
const double data) {
return write_data(data); }
678 inline basic& operator >>(_Out_
char& data) {
return read_data(data); }
679 inline basic& operator <<(_In_
const char data) {
return write_data(data); }
680#ifdef _NATIVE_WCHAR_T_DEFINED
681 inline basic& operator >>(_Out_
wchar_t& data) {
return read_data(data); }
682 inline basic& operator <<(_In_
const wchar_t data) {
return write_data(data); }
684 template<
class _Elem,
class _Traits = std::
char_traits<_Elem>,
class _Ax = std::allocator<_Elem>>
685 inline basic& operator >>(_Out_ std::basic_string<_Elem, _Traits, _Ax>& data) {
return read_str(data); }
687 inline basic& operator <<(_In_
const T* data) {
return write_str(data); }
688 template<
class _Elem,
class _Traits = std::
char_traits<_Elem>,
class _Ax = std::allocator<_Elem>>
689 inline basic& operator <<(_In_
const std::basic_string<_Elem, _Traits, _Ax>& data) {
return write_str(data); }
691 template <
class _Ty,
class _Alloc = std::allocator<_Ty>>
692 basic& operator <<(_In_
const std::vector<_Ty, _Alloc>& data)
694 size_t num = data.size();
695 if (num > UINT32_MAX) _Unlikely_
696 throw std::invalid_argument(
"collection too big");
697 *this << static_cast<uint32_t>(num);
698 for (
auto& el : data)
703 template <
class _Ty,
class _Alloc = std::allocator<_Ty>>
704 basic& operator >>(_Out_ std::vector<_Ty, _Alloc>& data)
709 if (!
ok()) _Unlikely_
712 for (uint32_t i = 0; i < num; ++i) {
715 if (!
ok()) _Unlikely_
717 data.push_back(std::move(el));
721 template <
class _Kty,
class _Pr = std::less<_Kty>,
class _Alloc = std::allocator<_Kty>>
722 basic& operator <<(_In_
const std::set<_Kty, _Pr, _Alloc>& data)
724 size_t num = data.size();
725 if (num > UINT32_MAX) _Unlikely_
726 throw std::invalid_argument(
"collection too big");
727 *this << static_cast<uint32_t>(num);
728 for (
auto& el : data)
733 template <
class _Kty,
class _Pr = std::less<_Kty>,
class _Alloc = std::allocator<_Kty>>
734 basic& operator >>(_Out_ std::set<_Kty, _Pr, _Alloc>& data)
739 if (!
ok()) _Unlikely_
741 for (uint32_t i = 0; i < num; ++i) {
744 if (!
ok()) _Unlikely_
746 data.insert(std::move(el));
750 template <
class _Kty,
class _Pr = std::less<_Kty>,
class _Alloc = std::allocator<_Kty>>
751 basic& operator <<(_In_
const std::multiset<_Kty, _Pr, _Alloc>& data)
753 size_t num = data.size();
754 if (num > UINT32_MAX) _Unlikely_
755 throw std::invalid_argument(
"collection too big");
756 *this << static_cast<uint32_t>(num);
757 for (
auto& el : data)
762 template <
class _Kty,
class _Pr = std::less<_Kty>,
class _Alloc = std::allocator<_Kty>>
763 basic& operator >>(_Out_ std::multiset<_Kty, _Pr, _Alloc>& data)
768 if (!
ok()) _Unlikely_
770 for (uint32_t i = 0; i < num; ++i) {
773 if (!
ok()) _Unlikely_
775 data.insert(std::move(el));
787 using fpos_t = uint64_t;
788 constexpr fpos_t fpos_max = UINT64_MAX;
789 constexpr fpos_t fpos_min = 0;
794 using foff_t = int64_t;
795 constexpr foff_t foff_max = INT64_MAX;
796 constexpr foff_t foff_min = INT64_MIN;
814 using clock = std::chrono::file_clock;
816 using clock = std::chrono::system_clock;
818 using time_point = std::chrono::time_point<clock>;
828 size_t length = std::min<size_t>(max_length,
static_cast<size_t>(
size() -
tell()));
829 std::vector<uint8_t> result;
830 try { result.resize(length); }
831 catch (
const std::bad_alloc&) {
832 m_state = state_t::fail;
835 result.resize(
read_array(result.data(),
sizeof(uint8_t), length));
844 virtual fpos_t
seek(_In_ foff_t offset, _In_ seek_t how = seek_t::beg) = 0;
851 inline fpos_t
seekbeg(_In_ fpos_t offset) {
return seek(offset, seek_t::beg); }
858 inline fpos_t
seekcur(_In_ foff_t offset) {
return seek(offset, seek_t::cur); }
865 inline fpos_t
seekend(_In_ foff_t offset) {
return seek(offset, seek_t::end); }
867 virtual void skip(_In_ fsize_t amount)
869 seek(amount, seek_t::cur);
878 virtual fpos_t
tell()
const = 0;
883 virtual void lock(_In_ fpos_t offset, _In_ fsize_t length)
885 _Unreferenced_(offset);
886 _Unreferenced_(length);
887 throw std::domain_error(
"not implemented");
893 virtual void unlock(_In_ fpos_t offset, _In_ fsize_t length)
895 _Unreferenced_(offset);
896 _Unreferenced_(length);
897 throw std::domain_error(
"not implemented");
904 virtual fsize_t
size()
const = 0;
916 return time_point::min();
924 return time_point::min();
932 return time_point::min();
940 _Unreferenced_(date);
941 throw std::domain_error(
"not implemented");
949 _Unreferenced_(date);
950 throw std::domain_error(
"not implemented");
958 _Unreferenced_(date);
959 throw std::domain_error(
"not implemented");
966 LPSAFEARRAY read_sa()
968 _Assume_(
size() <= SIZE_MAX);
969 size_t length =
static_cast<size_t>(
size());
970 std::unique_ptr<SAFEARRAY, SafeArrayDestroy_delete> sa(SafeArrayCreateVector(VT_UI1, 0, (ULONG)length));
972 throw std::runtime_error(
"SafeArrayCreateVector failed");
973 safearray_accessor<void> a(sa.get());
974 if (
seek(0) != 0) _Unlikely_
975 throw std::system_error(sys_error(), std::system_category(),
"failed to seek");
976 if (
read_array(a.data(), 1, length) != length)
977 throw std::system_error(sys_error(), std::system_category(),
"failed to read");
987 charset_id
read_charset(_In_ charset_id default_charset = charset_id::system)
989 if (
seek(0) != 0) _Unlikely_
990 throw std::system_error(sys_error(), std::system_category(),
"failed to seek");
993 if (
ok() && id_utf32 == utf32_bom)
994 return charset_id::utf32;
996 if (
seek(0) != 0) _Unlikely_
997 throw std::system_error(sys_error(), std::system_category(),
"failed to seek");
1000 if (
ok() && id_utf16 == utf16_bom)
1001 return charset_id::utf16;
1003 if (
seek(0) != 0) _Unlikely_
1004 throw std::system_error(sys_error(), std::system_category(),
"failed to seek");
1005 char id_utf8[3] = { 0 };
1007 if (
ok() && strncmp(id_utf8, _countof(id_utf8), utf8_bom, _countof(utf8_bom)) == 0)
1008 return charset_id::utf8;
1010 if (
seek(0) != 0) _Unlikely_
1011 throw std::system_error(sys_error(), std::system_category(),
"failed to seek");
1012 return default_charset;
1025#pragma warning(suppress: 26495)
1028 void init(_Inout_
basic& source)
1036 m_state = m_source->
state();
1047 basic(source.state()),
1051 virtual _Success_(
return != 0 || length == 0) size_t
read(
1052 _Out_writes_bytes_to_opt_(length, return)
void* data, _In_
size_t length)
1054 size_t num_read = m_source->
read(data, length);
1055 m_state = m_source->
state();
1059 virtual _Success_(
return != 0) size_t
write(
1060 _In_reads_bytes_opt_(length) const
void* data, _In_
size_t length)
1062 size_t num_written = m_source->
write(data, length);
1063 m_state = m_source->
state();
1070 m_state = m_source->
state();
1076 m_state = m_source->
state();
1091 for (
auto w = m_workers.begin(), w_end = m_workers.end(); w != w_end; ++w) {
1094 const std::lock_guard<std::mutex> lk(_w->mutex);
1095 _w->op = worker::op_t::quit;
1097 _w->cv.notify_one();
1099 for (
auto w = m_workers.begin(), w_end = m_workers.end(); w != w_end; ++w)
1108 m_workers.push_back(std::unique_ptr<worker>(
new worker(source)));
1116 for (
auto w = m_workers.begin(), w_end = m_workers.end(); w != w_end; ++w) {
1118 if (_w->source == source) {
1120 const std::lock_guard<std::mutex> lk(_w->mutex);
1121 _w->op = worker::op_t::quit;
1123 _w->cv.notify_one();
1131 virtual _Success_(
return != 0) size_t
write(
1132 _In_reads_bytes_opt_(length) const
void* data, _In_
size_t length)
1134 for (
auto w = m_workers.begin(), w_end = m_workers.end(); w != w_end; ++w) {
1137 const std::lock_guard<std::mutex> lk(_w->mutex);
1138 _w->op = worker::op_t::write;
1140 _w->length = length;
1142 _w->cv.notify_one();
1144 size_t num_written = length;
1145 m_state = state_t::ok;
1146 for (
auto w = m_workers.begin(), w_end = m_workers.end(); w != w_end; ++w) {
1148 std::unique_lock<std::mutex> lk(_w->mutex);
1149 _w->cv.wait(lk, [&] {
return _w->op == worker::op_t::noop; });
1150 if (_w->num_written < num_written)
1151 num_written = _w->num_written;
1152 if (
ok() && !_w->source->ok())
1153 m_state = _w->source->state();
1160 foreach_worker(worker::op_t::close);
1165 foreach_worker(worker::op_t::flush);
1179 *
static_cast<std::thread*
>(
this) = std::thread([](_Inout_
worker& w) { w.process_op(); }, std::ref(*
this));
1186 std::unique_lock<std::mutex> lk(mutex);
1187 cv.wait(lk, [&] {
return op != op_t::noop; });
1221 std::condition_variable cv;
1224 void foreach_worker(_In_ worker::op_t op)
1226 for (
auto w = m_workers.begin(), w_end = m_workers.end(); w != w_end; ++w) {
1229 const std::lock_guard<std::mutex> lk(_w->mutex);
1232 _w->cv.notify_one();
1234 m_state = state_t::ok;
1235 for (
auto w = m_workers.begin(), w_end = m_workers.end(); w != w_end; ++w) {
1237 std::unique_lock<std::mutex> lk(_w->mutex);
1238 _w->cv.wait(lk, [&] {
return _w->op == worker::op_t::noop; });
1240 m_state = _w->source->state();
1244 std::list<std::unique_ptr<worker>> m_workers;
1247 constexpr size_t default_async_limit = 0x100000;
1254 template <
size_t CAPACITY = default_async_limit>
1260 m_worker([](_Inout_
async_reader& w) { w.process(); }, std::ref(*
this))
1269#pragma warning(suppress: 6101)
1270 virtual _Success_(
return != 0 || length == 0) size_t
read(
1271 _Out_writes_bytes_to_opt_(length, return)
void* data, _In_
size_t length)
1273 _Assume_(data || !length);
1274 for (
size_t to_read = length;;) {
1275 uint8_t* ptr;
size_t num_read;
1276 std::tie(ptr, num_read) = m_ring.front();
1277 if (!ptr) _Unlikely_ {
1278 m_state = to_read < length || !length ? state_t::ok : m_source->
state();
1279 return length - to_read;
1281 if (to_read < num_read)
1283 memcpy(data, ptr, num_read);
1284 m_ring.pop(num_read);
1285 to_read -= num_read;
1287 m_state = state_t::ok;
1290 reinterpret_cast<uint8_t*&
>(data) += num_read;
1298 uint8_t* ptr;
size_t num_write;
1299 std::tie(ptr, num_write) = m_ring.back();
1300 if (!ptr) _Unlikely_
1302 num_write = m_source->
read(ptr, num_write);
1303 m_ring.push(num_write);
1304 if (!m_source->
ok()) {
1312 ring<uint8_t, CAPACITY> m_ring;
1313 std::thread m_worker;
1321 template <
size_t CAPACITY = default_async_limit>
1327 m_worker([](_Inout_
async_writer& w) { w.process(); }, std::ref(*
this))
1336 virtual _Success_(
return != 0) size_t
write(
1337 _In_reads_bytes_opt_(length) const
void* data, _In_
size_t length)
1339 _Assume_(data || !length);
1340 for (
size_t to_write = length;;) {
1341 uint8_t* ptr;
size_t num_write;
1342 std::tie(ptr, num_write) = m_ring.back();
1343 if (!ptr) _Unlikely_ {
1344 m_state = state_t::fail;
1345 return length - to_write;
1347 if (to_write < num_write)
1348 num_write = to_write;
1349 memcpy(ptr, data, num_write);
1350 m_ring.push(num_write);
1351 to_write -= num_write;
1353 m_state = state_t::ok;
1356 reinterpret_cast<const uint8_t*&
>(data) += num_write;
1370 uint8_t* ptr;
size_t num_read;
1371 std::tie(ptr, num_read) = m_ring.front();
1374 num_read = m_source->
write(ptr, num_read);
1375 m_ring.pop(num_read);
1376 if (!m_source->
ok()) {
1384 ring<uint8_t, CAPACITY> m_ring;
1385 std::thread m_worker;
1388 constexpr size_t default_buffer_size = 0x400;
1397 explicit buffer(_In_
size_t read_buffer_size = default_buffer_size, _In_
size_t write_buffer_size = default_buffer_size) :
1399 m_read_buffer(read_buffer_size),
1400 m_write_buffer(write_buffer_size)
1412 buffer(_Inout_
basic& source, _In_
size_t read_buffer_size = default_buffer_size, _In_
size_t write_buffer_size = default_buffer_size) :
1414 m_read_buffer(read_buffer_size),
1415 m_write_buffer(write_buffer_size)
1424 virtual _Success_(
return != 0 || length == 0) size_t
read(
1425 _Out_writes_bytes_to_opt_(length, return)
void* data, _In_
size_t length)
1427 _Assume_(data || !length);
1428 for (
size_t to_read = length;;) {
1429 size_t buffer_size = m_read_buffer.tail - m_read_buffer.head;
1430 if (to_read <= buffer_size) {
1431 memcpy(data, m_read_buffer.data + m_read_buffer.head, to_read);
1432 m_read_buffer.head += to_read;
1433 m_state = state_t::ok;
1437 memcpy(data, m_read_buffer.data + m_read_buffer.head, buffer_size);
1438 reinterpret_cast<uint8_t*&
>(data) += buffer_size;
1439 to_read -= buffer_size;
1441 m_read_buffer.head = 0;
1442 if (to_read > m_read_buffer.capacity) {
1444 m_read_buffer.tail = 0;
1445 to_read -= m_source->
read(data, to_read);
1446 m_state = to_read < length ? state_t::ok : m_source->
state();
1447 return length - to_read;
1449 m_read_buffer.tail = m_source->
read(m_read_buffer.data, m_read_buffer.capacity);
1450 if (m_read_buffer.tail < m_read_buffer.capacity && m_read_buffer.tail < to_read) _Unlikely_ {
1451 memcpy(data, m_read_buffer.data, m_read_buffer.tail);
1452 m_read_buffer.head = m_read_buffer.tail;
1453 to_read -= m_read_buffer.tail;
1454 m_state = to_read < length ? state_t::ok : m_source->
state();
1455 return length - to_read;
1460 virtual _Success_(
return != 0) size_t
write(
1461 _In_reads_bytes_opt_(length) const
void* data, _In_
size_t length)
1463 _Assume_(data || !length);
1464 if (!length) _Unlikely_ {
1467 if (!
ok()) _Unlikely_
1473 for (
size_t to_write = length;;) {
1474 size_t available_buffer = m_write_buffer.capacity - m_write_buffer.tail;
1475 if (to_write <= available_buffer) {
1476 memcpy(m_write_buffer.data + m_write_buffer.tail, data, to_write);
1477 m_write_buffer.tail += to_write;
1478 m_state = state_t::ok;
1481 if (available_buffer) {
1482 memcpy(m_write_buffer.data + m_write_buffer.tail, data, available_buffer);
1483 reinterpret_cast<const uint8_t*&
>(data) += available_buffer;
1484 to_write -= available_buffer;
1485 m_write_buffer.tail += available_buffer;
1487 size_t buffer_size = m_write_buffer.tail - m_write_buffer.head;
1489 m_write_buffer.head +=
converter::write(m_write_buffer.data + m_write_buffer.head, buffer_size);
1490 if (m_write_buffer.head == m_write_buffer.tail)
1491 m_write_buffer.head = m_write_buffer.tail = 0;
1493 return length - to_write;
1495 if (to_write > m_write_buffer.capacity) {
1498 return length - to_write;
1513 size_t buffer_size = m_write_buffer.tail - m_write_buffer.head;
1515 m_write_buffer.head += m_source->
write(m_write_buffer.data + m_write_buffer.head, buffer_size);
1516 if (m_write_buffer.head == m_write_buffer.tail) {
1517 m_write_buffer.head = 0;
1518 m_write_buffer.tail = 0;
1521 m_state = m_source->
state();
1525 m_state = state_t::ok;
1530 size_t head, tail, capacity;
1532 buffer_t(_In_
size_t buffer_size) :
1535 capacity(buffer_size),
1536 data(buffer_size ?
new uint8_t[buffer_size] :
nullptr)
1544 } m_read_buffer, m_write_buffer;
1553 limiter(_Inout_
basic& source, _In_ fsize_t _read_limit = 0, _In_ fsize_t _write_limit = 0) :
1559 virtual _Success_(
return != 0 || length == 0) size_t
read(
1560 _Out_writes_bytes_to_opt_(length, return)
void* data, _In_
size_t length)
1571 m_state = state_t::eof;
1580 virtual _Success_(
return != 0) size_t
write(
1581 _In_reads_bytes_opt_(length) const
void* data, _In_
size_t length)
1592 m_state = state_t::fail;
1613 window(_Inout_
basic& source, _In_ fpos_t _read_offset = 0, _In_ fsize_t
read_limit = fsize_max, _In_ fpos_t _write_offset = 0, _In_ fsize_t
write_limit = fsize_max) :
1619 virtual _Success_(
return != 0 || length == 0) size_t
read(
1620 _Out_writes_bytes_to_opt_(length, return)
void* data, _In_
size_t length)
1624 m_state = m_source->
state();
1625 if (!
ok()) _Unlikely_
1638 m_source->
skip(length);
1639 m_state = state_t::eof;
1648 virtual _Success_(
return != 0) size_t
write(
1649 _In_reads_bytes_opt_(length) const
void* data, _In_
size_t length)
1651 size_t num_skipped, num_written;
1654 m_state = state_t::ok;
1658 reinterpret_cast<const uint8_t*&
>(data) +=
static_cast<size_t>(
write_offset);
1672 num_skipped += length;
1674 m_state = state_t::ok;
1677 num_skipped += length -
static_cast<size_t>(
write_limit);
1681 return num_skipped + num_written;
1697 basic(source.state()),
1699 m_offset(source.tell()),
1700 m_region(offset, offset + length)
1703 virtual _Success_(
return != 0 || length == 0) size_t
read(
1704 _Out_writes_bytes_to_opt_(length, return)
void* data, _In_
size_t length)
1706 _Assume_(data || !length);
1708 size_t num_read = m_source.
read(data,
static_cast<size_t>(std::min<fpos_t>(length, m_region.
end - m_offset)));
1709 m_state = m_source.
state();
1710 m_offset += num_read;
1713 m_state = length ? state_t::eof : state_t::ok;
1717 virtual _Success_(
return != 0) size_t
write(
1718 _In_reads_bytes_opt_(length) const
void* data, _In_
size_t length)
1720 _Assume_(data || !length);
1722 size_t num_written = m_source.
write(data,
static_cast<size_t>(std::min<fpos_t>(length, m_region.
end - m_offset)));
1723 m_state = m_source.
state();
1724 m_offset += num_written;
1727 m_state = state_t::fail;
1734 m_state = m_source.
state();
1740 m_state = m_source.
state();
1743 virtual fpos_t
seek(_In_ foff_t offset, _In_ seek_t how = seek_t::beg)
1745 m_offset = m_source.
seek(offset, how);
1746 m_state = m_source.
state();
1747 return ok() ? m_offset - m_region.
start : fpos_max;
1750 virtual void skip(_In_ fsize_t amount)
1752 m_source.
skip(amount);
1753 m_state = m_source.
state();
1758 fpos_t offset = m_source.
tell();
1759 return m_region.
contains(offset) ? offset - m_region.
start : fpos_max;
1762 virtual void lock(_In_ fpos_t offset, _In_ fsize_t length)
1765 m_source.
lock(m_region.
start + offset, std::min<fsize_t>(length, m_region.
end - offset));
1766 m_state = m_source.
state();
1769 m_state = state_t::fail;
1772 virtual void unlock(_In_ fpos_t offset, _In_ fsize_t length)
1775 m_source.
unlock(m_region.
start + offset, std::min<fsize_t>(length, m_region.
end - offset));
1776 m_state = m_source.
state();
1779 m_state = state_t::fail;
1784 return m_region.
size();
1789 m_state = state_t::fail;
1798 constexpr size_t default_cache_size = 0x1000;
1807#pragma warning(suppress: 26495)
1808 explicit cache(_In_
size_t cache_size = default_cache_size) :
1809 basic(state_t::fail),
1821 m_state = m_source->state();
1822 m_offset = m_source->tell();
1823#if SET_FILE_OP_TIMES
1824 m_atime = m_source->atime();
1825 m_mtime = m_source->mtime();
1833 if (!
ok()) _Unlikely_
1834 throw std::system_error(sys_error(), std::system_category(),
"failed to flush cache");
1835 m_source->seek(m_offset);
1836#if SET_FILE_OP_TIMES
1837 m_source->set_atime(m_atime);
1838 m_source->set_mtime(m_mtime);
1846 cache(_Inout_
basic_file& source, _In_
size_t cache_size = default_cache_size) :
1847 basic(source.state()),
1849 m_cache(cache_size),
1850 m_offset(source.tell())
1851#if SET_FILE_OP_TIMES
1852 , m_atime(source.atime())
1853 , m_mtime(source.mtime())
1857 virtual ~cache()
noexcept(
false)
1861 if (!
ok()) _Unlikely_
1862 throw std::system_error(sys_error(), std::system_category(),
"failed to flush cache");
1863 m_source->seek(m_offset);
1864#if SET_FILE_OP_TIMES
1865 m_source->set_atime(m_atime);
1866 m_source->set_mtime(m_mtime);
1871 virtual _Success_(
return != 0 || length == 0) size_t
read(
1872 _Out_writes_bytes_to_opt_(length, return)
void* data, _In_
size_t length)
1874 _Assume_(data || !length);
1875#if SET_FILE_OP_TIMES
1876 m_atime = time_point::now();
1878 for (
size_t to_read = length;;) {
1879 if (m_cache.status != cache_t::cache_t::status_t::empty) {
1880 if (m_cache.region.contains(m_offset)) {
1881 size_t remaining_cache =
static_cast<size_t>(m_cache.region.end - m_offset);
1882 if (to_read <= remaining_cache) {
1883 memcpy(data, m_cache.data +
static_cast<size_t>(m_offset - m_cache.region.start), to_read);
1884 m_offset += to_read;
1885 m_state = state_t::ok;
1888 memcpy(data, m_cache.data +
static_cast<size_t>(m_offset - m_cache.region.start), remaining_cache);
1889 reinterpret_cast<uint8_t*&
>(data) += remaining_cache;
1890 to_read -= remaining_cache;
1891 m_offset += remaining_cache;
1894 if (!
ok()) _Unlikely_ {
1895 if (to_read < length)
1896 m_state = state_t::ok;
1897 return length - to_read;
1901 fpos_t end_max = m_offset + to_read;
1902 if (m_offset / m_cache.capacity < end_max / m_cache.capacity) {
1904 m_source->seek(m_offset);
1905 if (!m_source->ok()) _Unlikely_ {
1906 m_state = to_read < length ? state_t::ok : state_t::fail;
1907 return length - to_read;
1909 size_t num_read = m_source->read(data, to_read -
static_cast<size_t>(end_max % m_cache.capacity));
1910 m_offset += num_read;
1911 to_read -= num_read;
1913 m_state = state_t::ok;
1916 reinterpret_cast<uint8_t*&
>(data) += num_read;
1917 m_state = m_source->state();
1919 if (to_read < length)
1920 m_state = state_t::ok;
1921 return length - to_read;
1925 load_cache(m_offset);
1926 if (!
ok() || m_cache.region.end <= m_offset) _Unlikely_ {
1927 m_state = to_read < length ? state_t::ok : state_t::fail;
1928 return length - to_read;
1933 virtual _Success_(
return != 0) size_t
write(
1934 _In_reads_bytes_opt_(length) const
void* data, _In_
size_t length)
1936 _Assume_(data || !length);
1937#if SET_FILE_OP_TIMES
1938 m_atime = m_mtime = time_point::now();
1940 for (
size_t to_write = length;;) {
1941 if (m_cache.status != cache_t::cache_t::status_t::empty) {
1942 fpos_t end_max = m_cache.region.start + m_cache.capacity;
1943 if (m_cache.region.start <= m_offset && m_offset < end_max) {
1944 size_t remaining_cache =
static_cast<size_t>(end_max - m_offset);
1945 if (to_write <= remaining_cache) {
1946 memcpy(m_cache.data +
static_cast<size_t>(m_offset - m_cache.region.start), data, to_write);
1947 m_offset += to_write;
1948 m_cache.status = cache_t::cache_t::status_t::dirty;
1949 m_cache.region.end = std::max(m_cache.region.end, m_offset);
1950 m_state = state_t::ok;
1953 memcpy(m_cache.data +
static_cast<size_t>(m_offset - m_cache.region.start), data, remaining_cache);
1954 reinterpret_cast<const uint8_t*&
>(data) += remaining_cache;
1955 to_write -= remaining_cache;
1956 m_offset += remaining_cache;
1957 m_cache.status = cache_t::cache_t::status_t::dirty;
1958 m_cache.region.end = end_max;
1961 if (!
ok()) _Unlikely_
1962 return length - to_write;
1965 fpos_t end_max = m_offset + to_write;
1966 if (m_offset / m_cache.capacity < end_max / m_cache.capacity) {
1968 m_source->seek(m_offset);
1969 if (!
ok()) _Unlikely_
1970 return length - to_write;
1971 size_t num_written = m_source->write(data, to_write -
static_cast<size_t>(end_max % m_cache.capacity));
1972 m_offset += num_written;
1973 m_state = m_source->state();
1974 to_write -= num_written;
1975 if (!to_write || !
ok())
1976 return length - to_write;
1977 reinterpret_cast<const uint8_t*&
>(data) += num_written;
1980 load_cache(m_offset);
1981 if (!
ok()) _Unlikely_
1982 return length - to_write;
1989 if (!
ok()) _Unlikely_
1990 throw std::system_error(sys_error(), std::system_category(),
"failed to flush cache");
1992 m_state = m_source->state();
1997#if SET_FILE_OP_TIMES
1998 m_atime = m_mtime = time_point::min();
2001 if (!
ok()) _Unlikely_
2006 virtual fpos_t
seek(_In_ foff_t offset, _In_ seek_t how = seek_t::beg)
2008 m_state = state_t::ok;
2011 return m_offset = offset;
2013 return m_offset += offset;
2016 if (n == fsize_max) _Unlikely_{
2017 m_state = state_t::fail;
2020 return m_offset = n + offset;
2023 throw std::invalid_argument(
"unknown seek origin");
2032 virtual void lock(_In_ fpos_t offset, _In_ fsize_t length)
2034 m_source->lock(offset, length);
2035 m_state = m_source->state();
2038 virtual void unlock(_In_ fpos_t offset, _In_ fsize_t length)
2040 m_source->unlock(offset, length);
2041 m_state = m_source->state();
2046 return m_cache.status != cache_t::cache_t::status_t::empty ?
2047 std::max(m_source->size(), m_cache.region.end) :
2053#if SET_FILE_OP_TIMES
2054 m_atime = m_mtime = time_point::now();
2056 m_source->seek(m_offset);
2057 if (m_cache.region.end <= m_offset) {
2060 else if (m_cache.region.start <= m_offset) {
2062 m_cache.region.end = m_offset;
2066 m_cache.status = cache_t::cache_t::status_t::empty;
2068 m_source->truncate();
2069 m_state = m_source->state();
2074 return m_source->ctime();
2079#if SET_FILE_OP_TIMES
2080 return std::max(m_atime, m_source->atime());
2082 return m_source->atime();
2088#if SET_FILE_OP_TIMES
2089 return std::max(m_mtime, m_source->mtime());
2091 return m_source->mtime();
2097 m_source->set_ctime(date);
2102#if SET_FILE_OP_TIMES
2105 m_source->set_atime(date);
2110#if SET_FILE_OP_TIMES
2113 m_source->set_mtime(date);
2120 if (m_cache.status != cache_t::cache_t::status_t::dirty)
2121 m_state = state_t::ok;
2122 else if (!m_cache.region.empty()) {
2125 m_cache.status = cache_t::cache_t::status_t::loaded;
2128 m_state = state_t::ok;
2129 m_cache.status = cache_t::cache_t::status_t::loaded;
2133 void invalidate_cache()
2135 if (m_cache.status == cache_t::cache_t::status_t::dirty && !m_cache.region.empty()) {
2137 if (!
ok()) _Unlikely_
2140 m_state = state_t::ok;
2141 m_cache.status = cache_t::cache_t::status_t::empty;
2144 void load_cache(_In_ fpos_t start)
2146 _Assume_(m_cache.status != cache_t::cache_t::status_t::dirty);
2147 start -= start % m_cache.capacity;
2148 m_source->seek(m_cache.region.start = start);
2149 if (m_source->ok()) {
2150 m_cache.region.end = start + m_source->read(m_cache.data, m_cache.capacity);
2151 m_cache.status = cache_t::cache_t::status_t::loaded;
2152 m_state = state_t::ok;
2155 m_state = state_t::fail;
2160 _Assume_(m_cache.status == cache_t::cache_t::status_t::dirty);
2161 m_source->seek(m_cache.region.start);
2162 m_source->write(m_cache.data,
static_cast<size_t>(m_cache.region.size()));
2163 m_state = m_source->state();
2166 basic_file* m_source;
2170 enum class status_t {
2175 interval<fpos_t> region;
2177 cache_t(_In_
size_t _capacity) :
2178 data(new uint8_t[_capacity]),
2179 capacity(_capacity),
2180 status(status_t::empty),
2190#if SET_FILE_OP_TIMES
2204 basic_sys(_In_opt_ sys_handle h = invalid_handle, _In_ state_t
state = state_t::ok) :
2209 virtual _Success_(
return != 0 || length == 0) size_t
read(
2210 _Out_writes_bytes_to_opt_(length, return)
void* data, _In_
size_t length)
2212 _Assume_(data || !length);
2218 block_size = 0x1F80000;
2219#elif defined(_WIN32)
2220 block_size = 0x3f00000;
2222 block_size = SSIZE_MAX;
2224 for (
size_t to_read = length;;) {
2229 __try { succeeded = ReadFile(m_h, data,
static_cast<DWORD
>(std::min<size_t>(to_read, block_size)), &num_read,
nullptr); }
2230 __except (EXCEPTION_EXECUTE_HANDLER) { succeeded = FALSE; SetLastError(ERROR_UNHANDLED_EXCEPTION); num_read = 0; }
2231 if (!succeeded && GetLastError() == ERROR_NO_SYSTEM_RESOURCES && block_size > default_block_size) _Unlikely_ {
2234 block_size = default_block_size;
2237 if (!succeeded) _Unlikely_
2239 ssize_t num_read =
::read(m_h, data,
static_cast<ssize_t
>(std::min<size_t>(to_read, block_size)));
2240 if (num_read < 0) _Unlikely_
2243 m_state = to_read < length ? state_t::ok : state_t::fail;
2244 return length - to_read;
2246 if (!num_read) _Unlikely_ {
2247 m_state = to_read < length || !length ? state_t::ok : state_t::eof;
2248 return length - to_read;
2250 to_read -= num_read;
2252 m_state = state_t::ok;
2255 reinterpret_cast<uint8_t*&
>(data) += num_read;
2259 virtual _Success_(
return != 0) size_t
write(
2260 _In_reads_bytes_opt_(length) const
void* data, _In_
size_t length)
2267 block_size = 0x1F80000;
2268#elif defined(_WIN32)
2269 block_size = 0x3f00000;
2271 block_size = SSIZE_MAX;
2273 for (
size_t to_write = length;;) {
2278 __try { succeeded = WriteFile(m_h, data,
static_cast<DWORD
>(std::min<size_t>(to_write, block_size)), &num_written,
nullptr); }
2279 __except (EXCEPTION_EXECUTE_HANDLER) { succeeded = FALSE; SetLastError(ERROR_UNHANDLED_EXCEPTION); num_written = 0; }
2280 to_write -= num_written;
2282 m_state = state_t::ok;
2285 reinterpret_cast<const uint8_t*&
>(data) += num_written;
2286 if (!succeeded) _Unlikely_ {
2287 m_state = state_t::fail;
2288 return length - to_write;
2291 ssize_t num_written =
::write(m_h, data,
static_cast<ssize_t
>(std::min<size_t>(to_write, block_size)));
2292 if (num_written < 0) _Unlikely_ {
2293 m_state = state_t::fail;
2294 return length - to_write;
2296 to_write -= num_written;
2298 m_state = state_t::ok;
2301 reinterpret_cast<const uint8_t*&
>(data) += num_written;
2310 m_state = state_t::ok;
2313 m_state = state_t::fail;
2320 m_state = FlushFileBuffers(m_h) ? state_t::ok : state_t::fail;
2322 m_state = fsync(m_h) >= 0 ? state_t::ok : state_t::fail;
2333 buffered_sys(_In_opt_ sys_handle h = invalid_handle,
size_t read_buffer_size = default_buffer_size,
size_t write_buffer_size = default_buffer_size) :
2334 buffer(read_buffer_size, write_buffer_size),
2355 socket(_In_opt_ socket_t h = invalid_socket, _In_ state_t
state = state_t::ok) :
2365 socket(_Inout_
socket&& other) noexcept : m_h(other.m_h)
2367 other.m_h = invalid_socket;
2372 if (
this != std::addressof(other)) {
2373 if (m_h != invalid_socket)
2376 other.m_h = invalid_socket;
2388 socket(_In_
int af, _In_
int type, _In_
int protocol)
2390 m_h = ::socket(af, type, protocol);
2391 if (m_h == invalid_socket) _Unlikely_
2392 m_state = state_t::fail;
2397 if (m_h != invalid_socket)
2404 inline operator bool() const noexcept {
return m_h != invalid_socket; }
2409 inline socket_t
get() const noexcept {
return m_h; }
2411 virtual _Success_(
return != 0 || length == 0) size_t
read(
2412 _Out_writes_bytes_to_opt_(length, return)
void* data, _In_
size_t length)
2414 _Assume_(data || !length);
2415 constexpr int block_size = 0x10000000;
2416 for (
size_t to_read = length;;) {
2417 int num_read = recv(m_h,
reinterpret_cast<char*
>(data),
static_cast<int>(std::min<size_t>(to_read, block_size)), 0);
2418 if (num_read == SOCKET_ERROR) _Unlikely_ {
2419 m_state = to_read < length ? state_t::ok : state_t::fail;
2420 return length - to_read;
2423 m_state = to_read < length || !length ? state_t::ok : state_t::eof;
2424 return length - to_read;
2426 to_read -= num_read;
2428 m_state = state_t::ok;
2431 reinterpret_cast<uint8_t*&
>(data) += num_read;
2435 virtual _Success_(
return != 0) size_t
write(
2436 _In_reads_bytes_opt_(length) const
void* data, _In_
size_t length)
2438 _Assume_(data || !length);
2439 constexpr int block_size = 0x10000000;
2440 for (
size_t to_write = length;;) {
2441 int num_written = send(m_h,
reinterpret_cast<const char*
>(data),
static_cast<int>(std::min<size_t>(to_write, block_size)), 0);
2442 if (num_written == SOCKET_ERROR) _Unlikely_ {
2443 m_state = state_t::fail;
2444 return length - to_write;
2446 to_write -= num_written;
2448 m_state = state_t::ok;
2451 reinterpret_cast<const uint8_t*&
>(data) += num_written;
2457 if (m_h != invalid_socket) {
2459 m_h = invalid_socket;
2461 m_state = state_t::ok;
2472 class sequential_stream :
public basic
2475 sequential_stream(_In_ ISequentialStream* source) : m_source(source)
2480 virtual ~sequential_stream()
2482 m_source->Release();
2485 virtual _Success_(
return != 0 || length == 0) size_t read(
2486 _Out_writes_bytes_to_opt_(length, return)
void* data, _In_
size_t length)
2488 _Assume_(data || !length);
2489 for (
size_t to_read = length;;) {
2492 __try { hr = m_source->Read(data, (ULONG)std::min<size_t>(to_read, ULONG_MAX), &num_read); }
2493 __except (EXCEPTION_EXECUTE_HANDLER) { hr = E_FAIL; }
2494 if (FAILED(hr)) _Unlikely_ {
2495 m_state = to_read < length ? state_t::ok : state_t::fail;
2496 return length - to_read;
2498 to_read -= num_read;
2499 if (hr == S_FALSE) _Unlikely_ {
2500 m_state = to_read < length || !length ? state_t::ok : state_t::eof;
2501 return length - to_read;
2504 m_state = state_t::ok;
2507 reinterpret_cast<uint8_t*&
>(data) += num_read;
2511 virtual _Success_(
return != 0) size_t write(
2512 _In_reads_bytes_opt_(length) const
void* data, _In_
size_t length)
2514 _Assume_(data || !length);
2515 for (
size_t to_write = length;;) {
2517 ULONG num_written = 0;
2518 __try { hr = m_source->Write(data,
static_cast<ULONG
>(std::min<size_t>(to_write, ULONG_MAX)), &num_written); }
2519 __except (EXCEPTION_EXECUTE_HANDLER) { hr = E_FAIL; }
2522 if (FAILED(hr)) _Unlikely_ {
2523 m_state = state_t::fail;
2524 return length - to_write;
2526 to_write -= num_written;
2528 m_state = state_t::ok;
2531 reinterpret_cast<const uint8_t*&
>(data) += num_written;
2536 ISequentialStream* m_source;
2542 class asp :
public basic
2545 asp(_In_opt_ IRequest* request, _In_opt_ IResponse* response) :
2547 m_response(response)
2550 m_request->AddRef();
2552 m_response->AddRef();
2558 m_request->Release();
2560 m_response->Release();
2563 virtual _Success_(
return != 0 || length == 0) size_t read(
2564 _Out_writes_bytes_to_opt_(length, return)
void* data, _In_
size_t length)
2566 _Assume_(data || !length);
2567 if (!m_request) _Unlikely_ {
2568 m_state = state_t::fail;
2571 for (
size_t to_read = length;;) {
2572 VARIANT var_amount, var_data;
2573 V_VT(&var_amount) = VT_I4;
2574 V_I4(&var_amount) = (LONG)std::min<size_t>(to_read, LONG_MAX);
2575 V_VT(&var_data) = VT_EMPTY;
2576 HRESULT hr = [&]() {
2577 __try {
return m_request->BinaryRead(&var_amount, &var_data); }
2578 __except (EXCEPTION_EXECUTE_HANDLER) {
return E_FAIL; }
2580 if (FAILED(hr)) _Unlikely_ {
2581 m_state = to_read < length ? state_t::ok : state_t::fail;
2582 return length - to_read;
2584 _Assume_(V_VT(&var_amount) == VT_I4);
2585 _Assume_(V_VT(&var_data) == (VT_ARRAY | VT_UI1));
2586 std::unique_ptr<SAFEARRAY, SafeArrayDestroy_delete> sa(V_ARRAY(&var_data));
2587 if (!V_I4(&var_amount)) _Unlikely_ {
2588 m_state = to_read < length || !length ? state_t::ok : state_t::eof;
2589 return length - to_read;
2591 safearray_accessor<uint8_t> a(sa.get());
2592 memcpy(data, a.data(), V_I4(&var_amount));
2593 to_read -= V_I4(&var_amount);
2595 m_state = state_t::ok;
2598 reinterpret_cast<uint8_t*&
>(data) += V_I4(&var_amount);
2602 virtual _Success_(
return != 0) size_t write(
2603 _In_reads_bytes_opt_(length) const
void* data, _In_
size_t length)
2606 m_state = state_t::fail;
2609 for (
size_t to_write = length;;) {
2610 UINT num_written =
static_cast<UINT
>(std::min<size_t>(to_write, UINT_MAX));
2611 std::unique_ptr<OLECHAR, SysFreeString_delete> bstr_data(SysAllocStringByteLen(
reinterpret_cast<LPCSTR
>(data), num_written));
2613 V_VT(&var_data) = VT_BSTR;
2614 V_BSTR(&var_data) = bstr_data.get();
2615 HRESULT hr = [&]() {
2616 __try {
return m_response->BinaryWrite(var_data); }
2617 __except (EXCEPTION_EXECUTE_HANDLER) {
return E_FAIL; }
2619 if (FAILED(hr)) _Unlikely_ {
2620 m_state = state_t::fail;
2621 return length - to_write;
2623 to_write -= num_written;
2625 m_state = state_t::ok;
2628 reinterpret_cast<const uint8_t*&
>(data) += num_written;
2632 virtual void close()
2635 __try { m_response->End(); }
2636 __except (EXCEPTION_EXECUTE_HANDLER) {}
2638 m_state = state_t::ok;
2641 virtual void flush()
2645 __try { hr = m_response->Flush(); }
2646 __except (EXCEPTION_EXECUTE_HANDLER) { hr = E_FAIL; }
2647 m_state = SUCCEEDED(hr) ? state_t::ok : state_t::fail;
2652 IRequest* m_request;
2653 IResponse* m_response;
2662 mode_for_reading = 1 << 0,
2663 mode_for_writing = 1 << 1,
2664 mode_for_chmod = 1 << 2,
2666 mode_open_existing = 0 << 3,
2667 mode_truncate_existing = 1 << 3,
2668 mode_preserve_existing = 2 << 3,
2669 mode_create_new = 3 << 3,
2670 mode_create = 4 << 3,
2671 mode_disposition_mask = 7 << 3,
2673 mode_append = 1 << 6,
2675 mode_binary = 1 << 7,
2678 share_reading = 1 << 8,
2679 share_writing = 1 << 9,
2680 share_deleting = 1 << 10,
2681 share_all = share_reading | share_writing | share_deleting,
2683 inherit_handle = 1 << 11,
2685 hint_write_thru = 1 << 12,
2686 hint_no_buffering = 1 << 13,
2687 hint_random_access = 1 << 14,
2688 hint_sequential_access = 1 << 15,
2691#pragma warning(push)
2692#pragma warning(disable: 4250)
2707 file(_In_z_
const schar_t* filename, _In_
int mode)
2709 open(filename, mode);
2718 inline file(_In_
const stdex::sstring& filename, _In_
int mode) :
file(filename.c_str(), mode) {}
2726 void open(_In_z_
const schar_t* filename, _In_
int mode)
2728 if (m_h != invalid_handle)
2732 DWORD dwDesiredAccess = 0;
2733 if (mode & mode_for_reading) dwDesiredAccess |= GENERIC_READ;
2734 if (mode & mode_for_writing) dwDesiredAccess |= GENERIC_WRITE;
2735 if (mode & mode_for_chmod) dwDesiredAccess |= FILE_WRITE_ATTRIBUTES;
2737 DWORD dwShareMode = 0;
2738 if (mode & share_reading) dwShareMode |= FILE_SHARE_READ;
2739 if (mode & share_writing) dwShareMode |= FILE_SHARE_WRITE;
2740 if (mode & share_deleting) dwShareMode |= FILE_SHARE_DELETE;
2742 SECURITY_ATTRIBUTES sa = {
sizeof(SECURITY_ATTRIBUTES) };
2743 sa.bInheritHandle = mode & inherit_handle ? true :
false;
2745 DWORD dwCreationDisposition;
2746 switch (mode & mode_disposition_mask) {
2747 case mode_open_existing: dwCreationDisposition = OPEN_EXISTING;
break;
2748 case mode_truncate_existing: dwCreationDisposition = TRUNCATE_EXISTING;
break;
2749 case mode_preserve_existing: dwCreationDisposition = OPEN_ALWAYS;
break;
2750 case mode_create_new: dwCreationDisposition = CREATE_NEW;
break;
2751 case mode_create: dwCreationDisposition = CREATE_ALWAYS;
break;
2752 default:
throw std::invalid_argument(
"invalid mode");
2755 DWORD dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
2756 if (mode & hint_write_thru) dwFlagsAndAttributes |= FILE_FLAG_WRITE_THROUGH;
2757 if (mode & hint_no_buffering) dwFlagsAndAttributes |= FILE_FLAG_NO_BUFFERING;
2758 if (mode & hint_random_access) dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
2759 if (mode & hint_sequential_access) dwFlagsAndAttributes |= FILE_FLAG_SEQUENTIAL_SCAN;
2761 m_h = CreateFile(filename, dwDesiredAccess, dwShareMode, &sa, dwCreationDisposition, dwFlagsAndAttributes, NULL);
2764 switch (mode & (mode_for_reading | mode_for_writing)) {
2765 case mode_for_reading: flags |= O_RDONLY;
break;
2766 case mode_for_writing: flags |= O_WRONLY;
break;
2767 case mode_for_reading | mode_for_writing: flags |= O_RDWR;
break;
2769 switch (mode & mode_disposition_mask) {
2770 case mode_open_existing:
break;
2771 case mode_truncate_existing: flags |= O_TRUNC;
break;
2772 case mode_preserve_existing: flags |= O_CREAT;
break;
2773 case mode_create_new: flags |= O_CREAT | O_EXCL;
break;
2774 case mode_create: flags |= O_CREAT | O_TRUNC;
break;
2775 default:
throw std::invalid_argument(
"invalid mode");
2777 if (mode & hint_write_thru) flags |= O_DSYNC;
2779 if (mode & hint_no_buffering) flags |= O_RSYNC;
2782 m_h =
::open(filename, flags, DEFFILEMODE);
2784 if (m_h != invalid_handle) {
2785 m_state = state_t::ok;
2786 if (mode & mode_append)
2787 seek(0, seek_t::end);
2790 m_state = state_t::fail;
2799 inline void open(_In_
const stdex::sstring& filename, _In_
int mode)
2801 open(filename.c_str(), mode);
2804 virtual fpos_t
seek(_In_ foff_t offset, _In_ seek_t how = seek_t::beg)
2808 li.QuadPart = offset;
2809 li.LowPart = SetFilePointer(m_h, li.LowPart, &li.HighPart,
static_cast<DWORD
>(how));
2810 if (li.LowPart != 0xFFFFFFFF || GetLastError() == NO_ERROR) {
2811 m_state = state_t::ok;
2815 off64_t result = lseek64(m_h, offset,
static_cast<int>(how));
2817 m_state = state_t::ok;
2821 m_state = state_t::fail;
2827 if (m_h != invalid_handle) {
2831 li.LowPart = SetFilePointer(m_h, 0, &li.HighPart, FILE_CURRENT);
2832 if (li.LowPart != 0xFFFFFFFF || GetLastError() == NO_ERROR)
2835 off64_t result = lseek64(m_h, 0, SEEK_CUR);
2843 virtual void lock(_In_ fpos_t offset, _In_ fsize_t length)
2846 LARGE_INTEGER liOffset;
2847 LARGE_INTEGER liSize;
2848 liOffset.QuadPart = offset;
2849 liSize.QuadPart = length;
2850 if (LockFile(m_h, liOffset.LowPart, liOffset.HighPart, liSize.LowPart, liSize.HighPart)) {
2851 m_state = state_t::ok;
2855 off64_t orig = lseek64(m_h, 0, SEEK_CUR);
2857 m_state = lseek64(m_h, offset, SEEK_SET) >= 0 && lockf64(m_h, F_LOCK, length) >= 0 ? state_t::ok : state_t::fail;
2858 lseek64(m_h, orig, SEEK_SET);
2859 m_state = state_t::ok;
2863 m_state = state_t::fail;
2866 virtual void unlock(_In_ fpos_t offset, _In_ fsize_t length)
2869 LARGE_INTEGER liOffset;
2870 LARGE_INTEGER liSize;
2871 liOffset.QuadPart = offset;
2872 liSize.QuadPart = length;
2873 if (UnlockFile(m_h, liOffset.LowPart, liOffset.HighPart, liSize.LowPart, liSize.HighPart)) {
2874 m_state = state_t::ok;
2878 off64_t orig = lseek64(m_h, 0, SEEK_CUR);
2880 if (lseek64(m_h, offset, SEEK_SET) >= 0 && lockf64(m_h, F_ULOCK, length) >= 0) {
2881 lseek64(m_h, orig, SEEK_SET);
2882 m_state = state_t::ok;
2885 lseek64(m_h, orig, SEEK_SET);
2888 m_state = state_t::fail;
2895 li.LowPart = GetFileSize(m_h, (LPDWORD)&li.HighPart);
2896 if (li.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR)
2900 off64_t length = -1, orig = lseek64(m_h, 0, SEEK_CUR);
2902 length = lseek64(m_h, 0, SEEK_END);
2903 lseek64(m_h, orig, SEEK_SET);
2912 if (SetEndOfFile(m_h)) {
2913 m_state = state_t::ok;
2917 off64_t length = lseek64(m_h, 0, SEEK_CUR);
2918 if (length >= 0 && ftruncate64(m_h, length) >= 0) {
2919 m_state = state_t::ok;
2923 m_state = state_t::fail;
2927 static inline time_point ft2tp(_In_
const FILETIME& ft)
2930 uint64_t t = (
static_cast<int64_t
>(ft.dwHighDateTime) << 32) | ft.dwLowDateTime;
2932 uint64_t t = ((
static_cast<int64_t
>(ft.dwHighDateTime) << 32) | ft.dwLowDateTime) - 116444736000000000ll;
2934 return time_point(time_point::duration(t));
2937 static inline void tp2ft(_In_ time_point tp, _Out_ FILETIME& ft)
2940 uint64_t t = tp.time_since_epoch().count();
2942 uint64_t t = tp.time_since_epoch().count() + 116444736000000000ll;
2944 ft.dwHighDateTime =
static_cast<DWORD
>((t >> 32) & 0xffffffff);
2945 ft.dwLowDateTime =
static_cast<DWORD
>(t & 0xffffffff);
2953 if (GetFileTime(m_h, &ft,
nullptr,
nullptr))
2956 return time_point::min();
2963 if (GetFileTime(m_h,
nullptr, &ft,
nullptr))
2967 if (fstat(m_h, &buf) >= 0)
2968 return clock::from_time_t(buf.st_atime);
2970 return time_point::min();
2977 if (GetFileTime(m_h,
nullptr,
nullptr, &ft))
2981 if (fstat(m_h, &buf) >= 0)
2982 return clock::from_time_t(buf.st_mtime);
2984 return time_point::min();
2989 _Assume_(m_h != invalid_handle);
2993 if (SetFileTime(m_h, &ft,
nullptr,
nullptr))
2995 throw std::system_error(GetLastError(), std::system_category(),
"SetFileTime failed");
2997 throw std::runtime_error(
"not supported");
3003 _Assume_(m_h != invalid_handle);
3007 if (SetFileTime(m_h,
nullptr, &ft,
nullptr))
3009 throw std::system_error(GetLastError(), std::system_category(),
"SetFileTime failed");
3011 struct timespec ts[2] = {
3012 { date.time_since_epoch().count(), 0 },
3015 if (futimens(m_h, ts) >= 0)
3017 throw std::system_error(errno, std::system_category(),
"futimens failed");
3026 if (SetFileTime(m_h,
nullptr,
nullptr, &ft))
3028 throw std::system_error(GetLastError(), std::system_category(),
"SetFileTime failed");
3030 struct timespec ts[2] = {
3032 { date.time_since_epoch().count(), 0 },
3034 if (futimens(m_h, ts) >= 0)
3036 throw std::system_error(errno, std::system_category(),
"futimens failed");
3045 static bool exists(_In_z_
const stdex::schar_t* filename)
3048 return GetFileAttributes(filename) != INVALID_FILE_ATTRIBUTES;
3051 return stat(filename, &s) == 0;
3060 static inline bool exists(_In_
const stdex::sstring& filename)
3062 return exists(filename.c_str());
3072 static bool readonly(_In_z_
const stdex::schar_t* filename)
3075 DWORD dwAttr = GetFileAttributes(filename);
3076 return dwAttr != INVALID_FILE_ATTRIBUTES && (dwAttr & FILE_ATTRIBUTE_READONLY) != 0;
3079 return stat(filename, &s) == 0 && (s.st_mode & (S_IWUSR|S_IWGRP|S_IWOTH)) == 0;
3090 static inline bool readonly(_In_
const stdex::sstring& filename)
3103 cached_file(_In_opt_ sys_handle h = invalid_handle, _In_ state_t
state = state_t::ok, _In_
size_t cache_size = default_cache_size) :
3117 cached_file(_In_z_
const schar_t* filename, _In_
int mode, _In_
size_t cache_size = default_cache_size) :
3119 m_source(filename, mode & mode_for_writing ? mode | mode_for_reading : mode)
3131 inline cached_file(_In_
const stdex::sstring& filename, _In_
int mode, _In_
size_t cache_size = default_cache_size) :
cached_file(filename.c_str(), mode, cache_size) {}
3144 void open(_In_z_
const schar_t* filename, _In_
int mode)
3147 if (!
ok()) _Unlikely_{
3148 m_state = state_t::fail;
3151 m_source.
open(filename, mode & mode_for_writing ? mode | mode_for_reading : mode);
3152 if (m_source.
ok()) {
3156 m_state = state_t::fail;
3165 inline void open(_In_
const stdex::sstring& filename, _In_
int mode)
3167 open(filename.c_str(), mode);
3188#if SET_FILE_OP_TIMES
3189 m_ctime = m_atime = m_mtime = time_point::now();
3201 m_data(reinterpret_cast<uint8_t*>(malloc(
size))),
3208 m_state = state_t::fail;
3209 throw std::bad_alloc();
3211#if SET_FILE_OP_TIMES
3212 m_ctime = m_atime = m_mtime = time_point::now();
3234 _Assume_(reserved >=
size);
3235#if SET_FILE_OP_TIMES
3236 m_ctime = m_atime = m_mtime = time_point::now();
3260 load(filename, mode);
3278 m_data(reinterpret_cast<uint8_t*>(malloc(other.
m_size))),
3283#if SET_FILE_OP_TIMES
3284 , m_ctime(other.m_ctime)
3285 , m_atime(other.m_atime)
3286 , m_mtime(other.m_mtime)
3290 m_state = state_t::fail;
3291 throw std::bad_alloc();
3293 memcpy(
m_data, other.m_data, other.m_size);
3303 if (
this != std::addressof(other)) {
3307 m_data =
reinterpret_cast<uint8_t*
>(malloc(other.m_size));
3309 m_state = state_t::fail;
3310 throw std::bad_alloc();
3312 memcpy(
m_data, other.m_data, other.m_size);
3317#if SET_FILE_OP_TIMES
3318 m_ctime = other.m_ctime;
3319 m_atime = other.m_atime;
3320 m_mtime = other.m_mtime;
3338#if SET_FILE_OP_TIMES
3339 , m_ctime(other.m_ctime)
3340 , m_atime(other.m_atime)
3341 , m_mtime(other.m_mtime)
3344 other.m_state = state_t::ok;
3345 other.m_data =
nullptr;
3348 other.m_reserved = 0;
3349 other.m_manage =
true;
3350#if SET_FILE_OP_TIMES
3351 other.m_ctime = other.m_atime = other.m_mtime = time_point::now();
3362 if (
this != std::addressof(other)) {
3363 *
static_cast<basic_file*
>(
this) = std::move(other);
3367 other.m_data =
nullptr;
3373 other.m_reserved = 0;
3375 other.m_manage =
true;
3376#if SET_FILE_OP_TIMES
3377 m_ctime = other.m_ctime;
3378 m_atime = other.m_atime;
3379 m_mtime = other.m_mtime;
3380 other.m_ctime = other.m_atime = other.m_mtime = time_point::now();
3398 void reserve(_In_
size_t required, _In_
bool tight =
false) noexcept
3401 m_state = state_t::ok;
3405 m_state = state_t::fail;
3408 size_t reserved = tight ? required : ((required + required / 4 + (default_block_size - 1)) / default_block_size) * default_block_size;
3409 auto data =
reinterpret_cast<uint8_t*
>(realloc(
m_data, reserved));
3410 if (!
data && reserved) _Unlikely_ {
3411 m_state = state_t::fail;
3418 m_state = state_t::ok;
3427 void load(_In_z_
const schar_t* filename, _In_
int mode)
3429 file f(filename, (mode & ~hint_random_access) | mode_for_reading | hint_sequential_access);
3431 m_state = state_t::fail;
3435 if (
size > SIZE_MAX) {
3436 m_state = state_t::fail;
3440 if (!
ok()) _Unlikely_ {
3447#if SET_FILE_OP_TIMES
3448 m_ctime = f.
ctime();
3449 m_atime = f.
atime();
3450 m_mtime = f.
mtime();
3460 inline void load(_In_
const stdex::sstring& filename, _In_
int mode)
3462 load(filename.c_str(), mode);
3471 void save(_In_z_
const schar_t* filename, _In_
int mode)
3473 file f(filename, (mode & ~hint_random_access) | mode_for_writing | hint_sequential_access);
3475 m_state = state_t::fail;
3480 m_state = state_t::fail;
3484#if SET_FILE_OP_TIMES
3497 inline void save(_In_
const stdex::sstring& filename, _In_
int mode)
3499 save(filename.c_str(), mode);
3507 virtual _Success_(
return != 0 || length == 0) size_t
read(
3508 _Out_writes_bytes_to_opt_(length, return)
void*
data, _In_
size_t length)
3510 _Assume_(
data || !length);
3511#if SET_FILE_OP_TIMES
3512 m_atime = time_point::now();
3515 if (length <= available) {
3518 m_state = state_t::ok;
3521 if (length && !available) {
3522 m_state = state_t::eof;
3527 m_state = state_t::ok;
3548#if SET_FILE_OP_TIMES
3549 m_atime = time_point::now();
3551 if (CHECK_STREAM_STATE && !
ok()) _Unlikely_ {
3555 size_t end_offset =
m_offset +
sizeof(T);
3556 if (end_offset <=
m_size) {
3559#if !CHECK_STREAM_STATE
3560 m_state = state_t::ok;
3566 m_state = state_t::eof;
3585 template<
class _Elem,
class _Traits = std::
char_traits<_Elem>,
class _Ax = std::allocator<_Elem>>
3588#if SET_FILE_OP_TIMES
3589 m_atime = time_point::now();
3591 if (CHECK_STREAM_STATE && !
ok()) _Unlikely_ {
3595 size_t end_offset =
m_offset +
sizeof(uint32_t);
3596 if (end_offset <=
m_size) {
3597 uint32_t num_chars = LE2HE(*
reinterpret_cast<uint32_t*
>(
m_data +
m_offset));
3599 end_offset = stdex::add(
m_offset, stdex::mul(num_chars,
sizeof(_Elem)));
3601 if (end_offset <=
m_size) {
3602 data.assign(start, start + num_chars);
3604#if !CHECK_STREAM_STATE
3605 m_state = state_t::ok;
3609 if (end_offset <=
m_size)
3613 m_state = state_t::eof;
3617 virtual _Success_(
return != 0) size_t
write(
3618 _In_reads_bytes_opt_(length) const
void*
data, _In_
size_t length)
3620 _Assume_(
data || !length);
3621#if SET_FILE_OP_TIMES
3622 m_atime = m_mtime = time_point::now();
3624 size_t end_offset =
m_offset + length;
3627 if (!
ok()) _Unlikely_
3634 m_state = state_t::ok;
3643#if SET_FILE_OP_TIMES
3644 m_atime = m_mtime = time_point::now();
3646 size_t end_offset =
m_offset + amount;
3649 if (!
ok()) _Unlikely_
3656 m_state = state_t::ok;
3676#if SET_FILE_OP_TIMES
3677 m_atime = m_mtime = time_point::now();
3679 if (CHECK_STREAM_STATE && !
ok()) _Unlikely_
3681 size_t end_offset =
m_offset +
sizeof(T);
3684 if (!
ok()) _Unlikely_
3691#if !CHECK_STREAM_STATE
3692 m_state = state_t::ok;
3714#if SET_FILE_OP_TIMES
3715 m_atime = m_mtime = time_point::now();
3717 if (CHECK_STREAM_STATE && !
ok()) _Unlikely_
3719 size_t num_chars = stdex::strlen(
data);
3720 if (num_chars > UINT32_MAX)
3721 throw std::invalid_argument(
"string too long");
3722 size_t size_chars = num_chars *
sizeof(T);
3723 size_t size =
sizeof(uint32_t) + size_chars;
3727 if (!
ok()) _Unlikely_
3731 *
reinterpret_cast<uint32_t*
>(p) = HE2LE((uint32_t)num_chars);
3732 memcpy(p +
sizeof(uint32_t),
data, size_chars);
3736#if !CHECK_STREAM_STATE
3737 m_state = state_t::ok;
3756 template<
class _Elem,
class _Traits = std::
char_traits<_Elem>,
class _Ax = std::allocator<_Elem>>
3759#if SET_FILE_OP_TIMES
3760 m_atime = m_mtime = time_point::now();
3762 if (CHECK_STREAM_STATE && !
ok()) _Unlikely_
3764 size_t num_chars =
data.size();
3765 if (num_chars > UINT32_MAX)
3766 throw std::invalid_argument(
"string too long");
3767 size_t size_chars = num_chars *
sizeof(_Elem);
3768 size_t size =
sizeof(uint32_t) + size_chars;
3772 if (!
ok()) _Unlikely_
3776 *
reinterpret_cast<uint32_t*
>(p) = HE2LE((uint32_t)num_chars);
3777 memcpy(p +
sizeof(uint32_t),
data.data(), size_chars);
3781#if !CHECK_STREAM_STATE
3782 m_state = state_t::ok;
3794#if SET_FILE_OP_TIMES
3795 m_atime = m_mtime = time_point::now();
3798 size_t num_copied = 0, to_write = amount;
3799 m_state = state_t::ok;
3800 if (amount != SIZE_MAX) {
3801 dst_size = stdex::add(dst_size, amount);
3803 if (!
ok()) _Unlikely_
3806 num_read = stream.read(
m_data + dst_offset, to_write);
3807 dst_size = dst_offset += num_read;
3808 num_copied += num_read;
3809 to_write -= num_read;
3811 if (stream.state() != state_t::eof)
3812 m_state = state_t::fail;
3820 block_size = std::min(to_write, default_block_size);
3821 dst_size = stdex::add(dst_size, block_size);
3823 if (!
ok()) _Unlikely_
3825 num_read = stream.read(
m_data + dst_offset, block_size);
3826 dst_size = dst_offset += num_read;
3827 num_copied += num_read;
3828 to_write -= num_read;
3830 if (stream.state() != state_t::eof)
3831 m_state = state_t::fail;
3850#if SET_FILE_OP_TIMES
3851 m_ctime = m_atime = m_mtime = time_point::min();
3853 m_state = state_t::ok;
3856 virtual fpos_t
seek(_In_ foff_t offset, _In_ seek_t how = seek_t::beg)
3860 case seek_t::beg: target = offset;
break;
3861 case seek_t::cur: target =
static_cast<fpos_t
>(
m_offset) + offset;
break;
3862 case seek_t::end: target =
static_cast<fpos_t
>(
m_size) + offset;
break;
3863 default:
throw std::invalid_argument(
"unknown seek origin");
3865 if (target <= SIZE_MAX) {
3866 m_state = state_t::ok;
3867 return m_offset =
static_cast<size_t>(target);
3869 m_state = state_t::fail;
3885#if SET_FILE_OP_TIMES
3886 m_atime = m_mtime = time_point::now();
3892#if SET_FILE_OP_TIMES
3893 virtual time_point
ctime()
const
3898 virtual time_point
atime()
const
3903 virtual time_point
mtime()
const
3933 inline void set(_In_ fpos_t offset, _In_
const T
data)
3935#if SET_FILE_OP_TIMES
3936 m_atime = m_mtime = time_point::now();
3938 _Assume_(offset +
sizeof(T) <
m_size);
3939 (*
reinterpret_cast<T*
>(
m_data + offset)) = HE2LE(
data);
3943 inline void set(_In_ fpos_t offset, _In_
const int8_t
data) { set<int8_t>(offset,
data); }
3944 inline void set(_In_ fpos_t offset, _In_
const int16_t
data) { set<int16_t>(offset,
data); }
3945 inline void set(_In_ fpos_t offset, _In_
const int32_t
data) { set<int32_t>(offset,
data); }
3946 inline void set(_In_ fpos_t offset, _In_
const int64_t
data) { set<int64_t>(offset,
data); }
3947 inline void set(_In_ fpos_t offset, _In_
const uint8_t
data) { set<uint8_t>(offset,
data); }
3948 inline void set(_In_ fpos_t offset, _In_
const uint16_t
data) { set<uint16_t>(offset,
data); }
3949 inline void set(_In_ fpos_t offset, _In_
const uint32_t
data) { set<uint32_t>(offset,
data); }
3950 inline void set(_In_ fpos_t offset, _In_
const uint64_t
data) { set<uint64_t>(offset,
data); }
3951 inline void set(_In_ fpos_t offset, _In_
const float data) { set<float>(offset,
data); }
3952 inline void set(_In_ fpos_t offset, _In_
const double data) { set<double>(offset,
data); }
3953 inline void set(_In_ fpos_t offset, _In_
const char data) { set<char>(offset,
data); }
3954#ifdef _NATIVE_WCHAR_T_DEFINED
3955 inline void set(_In_ fpos_t offset, _In_
const wchar_t data) { set<wchar_t>(offset,
data); }
3967 inline void get(_In_ fpos_t offset, _Out_ T &
data)
3969 _Assume_(offset +
sizeof(T) <
m_size);
3971#if SET_FILE_OP_TIMES
3972 m_atime = time_point::now();
3977 inline void get(_In_ fpos_t offset, _Out_ int8_t &
data) { get<int8_t>(offset,
data); }
3978 inline void get(_In_ fpos_t offset, _Out_ int16_t &
data) { get<int16_t>(offset,
data); }
3979 inline void get(_In_ fpos_t offset, _Out_ int32_t &
data) { get<int32_t>(offset,
data); }
3980 inline void get(_In_ fpos_t offset, _Out_ int64_t &
data) { get<int64_t>(offset,
data); }
3981 inline void get(_In_ fpos_t offset, _Out_ uint8_t &
data) { get<uint8_t>(offset,
data); }
3982 inline void get(_In_ fpos_t offset, _Out_ uint16_t &
data) { get<uint16_t>(offset,
data); }
3983 inline void get(_In_ fpos_t offset, _Out_ uint32_t &
data) { get<uint32_t>(offset,
data); }
3984 inline void get(_In_ fpos_t offset, _Out_ uint64_t &
data) { get<uint64_t>(offset,
data); }
3985 inline void get(_In_ fpos_t offset, _Out_
float&
data) { get<float>(offset,
data); }
3986 inline void get(_In_ fpos_t offset, _Out_
double&
data) { get<double>(offset,
data); }
3987 inline void get(_In_ fpos_t offset, _Out_
char&
data) { get<char>(offset,
data); }
3988#ifdef _NATIVE_WCHAR_T_DEFINED
3989 inline void get(_In_ fpos_t offset, _Out_
wchar_t&
data) { get<wchar_t>(offset,
data); }
3993 inline memory_file& operator >>(_Out_ int8_t &
data) {
return read_data(
data); }
3994 inline memory_file& operator <<(_In_
const int16_t
data) {
return write_data(
data); }
3995 inline memory_file& operator >>(_Out_ int16_t &
data) {
return read_data(
data); }
3996 inline memory_file& operator <<(_In_
const int32_t
data) {
return write_data(
data); }
3997 inline memory_file& operator >>(_Out_ int32_t &
data) {
return read_data(
data); }
3998 inline memory_file& operator <<(_In_
const int64_t
data) {
return write_data(
data); }
3999 inline memory_file& operator >>(_Out_ int64_t &
data) {
return read_data(
data); }
4000 inline memory_file& operator <<(_In_
const uint8_t
data) {
return write_data(
data); }
4001 inline memory_file& operator >>(_Out_ uint8_t &
data) {
return read_data(
data); }
4002 inline memory_file& operator <<(_In_
const uint16_t
data) {
return write_data(
data); }
4003 inline memory_file& operator >>(_Out_ uint16_t &
data) {
return read_data(
data); }
4004 inline memory_file& operator <<(_In_
const uint32_t
data) {
return write_data(
data); }
4005 inline memory_file& operator >>(_Out_ uint32_t &
data) {
return read_data(
data); }
4006 inline memory_file& operator <<(_In_
const uint64_t
data) {
return write_data(
data); }
4007 inline memory_file& operator >>(_Out_ uint64_t &
data) {
return read_data(
data); }
4014#ifdef _NATIVE_WCHAR_T_DEFINED
4015 inline memory_file& operator <<(_In_
const wchar_t data) {
return write_data(
data); }
4016 inline memory_file& operator >>(_Out_
wchar_t&
data) {
return read_data(
data); }
4018 template<
class _Elem,
class _Traits = std::
char_traits<_Elem>,
class _Ax = std::allocator<_Elem>>
4019 inline memory_file& operator >>(_Out_ std::basic_string<_Elem, _Traits, _Ax>&
data) {
return read_str(
data); }
4021 inline memory_file& operator <<(_In_
const T *
data) {
return write_str(
data); }
4022 template<
class _Elem,
class _Traits = std::
char_traits<_Elem>,
class _Ax = std::allocator<_Elem>>
4023 inline memory_file& operator <<(_In_
const std::basic_string<_Elem, _Traits, _Ax>&
data) {
return write_str(
data); }
4031#if SET_FILE_OP_TIMES
4060#pragma warning(suppress: 6101)
4061 virtual _Success_(
return != 0 || length == 0) size_t
read(
4062 _Out_writes_bytes_to_opt_(length, return)
void* data, _In_
size_t length)
4064 _Assume_(data || !length);
4065 for (
size_t to_read = length;;) {
4066 if (!m_head) _Unlikely_ {
4067 m_state = to_read < length || !length ? state_t::ok : state_t::eof;
4068 return length - to_read;
4070 size_t remaining = m_head->size - m_offset;
4071 if (remaining > to_read) {
4072 memcpy(data, m_head->data + m_offset, to_read);
4073 m_offset += to_read;
4075 m_state = state_t::ok;
4078 memcpy(data, m_head->data + m_offset, remaining);
4080 m_size -= remaining;
4081 reinterpret_cast<uint8_t*&
>(data) += remaining;
4082 to_read -= remaining;
4089 virtual _Success_(
return != 0) size_t
write(
4090 _In_reads_bytes_opt_(length) const
void* data, _In_
size_t length)
4092 _Assume_(data || !length);
4094 std::unique_ptr<node_t> n(
reinterpret_cast<node_t*
>(
new uint8_t[
sizeof(
node_t) + length]));
4097 memcpy(n->data, data, length);
4100 m_tail = m_tail->next = n.release();
4102 m_head = m_tail = n.release();
4103 m_state = state_t::ok;
4106 catch (
const std::bad_alloc&) {
4107 m_state = state_t::fail;
4114 m_size = m_offset = 0;
4120 m_state = state_t::ok;
4126 inline size_t size()
const {
return m_size; };
4129 size_t m_offset, m_size;
4133#pragma warning(suppress:4200)
4135 } *m_head, * m_tail;
4144 basic(num_files ? files[0]->
state() : state_t::fail),
4145 m_files(files, files + num_files)
4148 virtual _Success_(
return != 0 || length == 0) size_t
read(
4149 _Out_writes_bytes_to_opt_(length, return)
void* data, _In_
size_t length)
4151 _Assume_(data || !length);
4152 if (m_files.empty()) {
4153 m_state = state_t::fail;
4156 size_t result = m_files[0]->read(data, length);
4157 _Assume_(result <= length);
4158 m_state = m_files[0]->state();
4159 if (length > m_tmp.size())
4160 m_tmp.resize(length);
4161 for (
size_t i = 1, n = m_files.size(); i < n; ++i) {
4162 if (m_files[i]->
read(m_tmp.data(), length) != result ||
4163 memcmp(m_tmp.data(), data, result))
4164 throw std::runtime_error(
"read mismatch");
4165 if (m_files[i]->
state() != m_state)
4166 throw std::runtime_error(
"state mismatch");
4171 virtual _Success_(
return != 0) size_t
write(
4172 _In_reads_bytes_opt_(length) const
void* data, _In_
size_t length)
4174 if (m_files.empty()) {
4175 m_state = state_t::fail;
4178 size_t result = m_files[0]->write(data, length);
4179 m_state = m_files[0]->state();
4180 for (
size_t i = 1, n = m_files.size(); i < n; ++i) {
4181 if (m_files[i]->
write(data, length) != result)
4182 throw std::runtime_error(
"write mismatch");
4183 if (m_files[i]->
state() != m_state)
4184 throw std::runtime_error(
"state mismatch");
4191 if (m_files.empty()) {
4192 m_state = state_t::ok;
4195 m_files[0]->flush();
4196 m_state = m_files[0]->state();
4197 for (
size_t i = 1, n = m_files.size(); i < n; ++i) {
4198 m_files[i]->flush();
4199 if (m_files[i]->
state() != m_state)
4200 throw std::runtime_error(
"state mismatch");
4206 if (m_files.empty()) {
4207 m_state = state_t::ok;
4210 m_files[0]->close();
4211 m_state = m_files[0]->state();
4212 for (
size_t i = 1, n = m_files.size(); i < n; ++i) {
4213 m_files[i]->close();
4214 if (m_files[i]->
state() != m_state)
4215 throw std::runtime_error(
"state mismatch");
4218 m_tmp.shrink_to_fit();
4221 virtual fpos_t
seek(_In_ foff_t offset, _In_ seek_t how = seek_t::beg)
4223 if (m_files.empty()) {
4224 m_state = state_t::fail;
4227 fpos_t result = m_files[0]->seek(offset, how);
4228 m_state = m_files[0]->state();
4229 for (
size_t i = 1, n = m_files.size(); i < n; ++i) {
4230 if (m_files[i]->
seek(offset, how) != result)
4231 throw std::runtime_error(
"seek mismatch");
4232 if (m_files[i]->
state() != m_state)
4233 throw std::runtime_error(
"state mismatch");
4240 if (m_files.empty())
4242 fpos_t result = m_files[0]->tell();
4243 for (
size_t i = 1, n = m_files.size(); i < n; ++i) {
4244 if (m_files[i]->
tell() != result)
4245 throw std::runtime_error(
"tell mismatch");
4250 virtual void lock(_In_ fpos_t offset, _In_ fsize_t length)
4252 if (m_files.empty())
4253 m_state = state_t::fail;
4254 m_files[0]->lock(offset, length);
4255 m_state = m_files[0]->state();
4256 for (
size_t i = 1, n = m_files.size(); i < n; ++i) {
4257 m_files[i]->lock(offset, length);
4258 if (m_files[i]->
state() != m_state)
4259 throw std::runtime_error(
"state mismatch");
4263 virtual void unlock(_In_ fpos_t offset, _In_ fsize_t length)
4265 if (m_files.empty())
4266 m_state = state_t::fail;
4267 m_files[0]->unlock(offset, length);
4268 m_state = m_files[0]->state();
4269 for (
size_t i = 1, n = m_files.size(); i < n; ++i) {
4270 m_files[i]->unlock(offset, length);
4271 if (m_files[i]->
state() != m_state)
4272 throw std::runtime_error(
"state mismatch");
4278 if (m_files.empty())
4280 fsize_t result = m_files[0]->size();
4281 for (
size_t i = 1, n = m_files.size(); i < n; ++i) {
4282 if (m_files[i]->
size() != result)
4283 throw std::runtime_error(
"size mismatch");
4290 if (m_files.empty())
4291 m_state = state_t::fail;
4292 m_files[0]->truncate();
4293 m_state = m_files[0]->state();
4294 for (
size_t i = 1, n = m_files.size(); i < n; ++i) {
4295 m_files[i]->truncate();
4296 if (m_files[i]->
state() != m_state)
4297 throw std::runtime_error(
"state mismatch");
4302 std::vector<basic_file*> m_files;
4303 std::vector<uint8_t> m_tmp;
Encoding converter context.
Definition unicode.hpp:133
Provides read-ahead stream capability.
Definition stream.hpp:1256
virtual size_t read(_Out_writes_bytes_to_opt_(length, return) void *data, size_t length)
Reads block of data from the stream.
Definition stream.hpp:1270
Provides write-back stream capability.
Definition stream.hpp:1323
virtual void flush()
Persists volatile element data.
Definition stream.hpp:1360
virtual size_t write(_In_reads_bytes_opt_(length) const void *data, size_t length)
Writes block of data to the stream.
Definition stream.hpp:1336
Basic seekable stream operations.
Definition stream.hpp:824
virtual void skip(fsize_t amount)
Skips given amount of bytes of data on the stream.
Definition stream.hpp:867
virtual time_point ctime() const
Returns file creation time.
Definition stream.hpp:914
virtual void lock(fpos_t offset, fsize_t length)
Locks file section for exclusive access.
Definition stream.hpp:883
virtual void truncate()=0
Sets file size - truncates the remainder of file content from the current file position to the end of...
virtual fsize_t size() const =0
Returns file size Should the file size cannot be determined, the method returns fsize_max and it does...
charset_id read_charset(charset_id default_charset=charset_id::system)
Attempts to detect textfile charset based on UTF-32, UTF-16 or UTF-8 BOM.
Definition stream.hpp:987
fpos_t seekbeg(fpos_t offset)
Seeks to absolute file position.
Definition stream.hpp:851
virtual std::vector< uint8_t > read_remainder(size_t max_length=SIZE_MAX)
Reads and returns remainder of the stream.
Definition stream.hpp:826
virtual void set_mtime(time_point date)
Sets file modification time.
Definition stream.hpp:956
fpos_t seekcur(foff_t offset)
Seeks to relative from current file position.
Definition stream.hpp:858
virtual time_point atime() const
Returns file access time.
Definition stream.hpp:922
virtual void set_ctime(time_point date)
Sets file create time.
Definition stream.hpp:938
virtual void unlock(fpos_t offset, fsize_t length)
Unlocks file section for exclusive access.
Definition stream.hpp:893
virtual fpos_t tell() const =0
Returns absolute file position in file or fpos_max if fails. This method does not update stream state...
virtual time_point mtime() const
Returns file modification time.
Definition stream.hpp:930
fpos_t seekend(foff_t offset)
Seeks to relative from end file position.
Definition stream.hpp:865
virtual void set_atime(time_point date)
Sets file access time.
Definition stream.hpp:947
virtual fpos_t seek(foff_t offset, seek_t how=seek_t::beg)=0
Seeks to specified relative file position.
OS data stream (file, pipe, socket...)
Definition stream.hpp:2202
virtual size_t write(_In_reads_bytes_opt_(length) const void *data, size_t length)
Writes block of data to the stream.
Definition stream.hpp:2259
virtual void flush()
Persists volatile element data.
Definition stream.hpp:2317
virtual size_t read(_Out_writes_bytes_to_opt_(length, return) void *data, size_t length)
Reads block of data from the stream.
Definition stream.hpp:2209
virtual void close()
Closes the stream.
Definition stream.hpp:2306
UTF-8 byte-order-mark
Definition stream.hpp:79
bool ok() const
Returns true if the stream state is clean i.e. previous operation was succesful.
Definition stream.hpp:175
size_t write_vsprintf(_Printf_format_string_params_(2) const char *format, locale_t locale, va_list params)
Writes formatted string to the stream.
Definition stream.hpp:637
size_t write_array(const std::basic_string< T_from, _Traits, _Ax > &wstr, charset_encoder< T_from, T_to > &encoder)
Writes array of characters to the stream.
Definition stream.hpp:455
state_t state() const
Returns stream state after last operation.
Definition stream.hpp:170
basic & read_str(std::basic_string< _Elem, _Traits, _Ax > &data)
Reads length-prefixed string from the stream.
Definition stream.hpp:477
size_t write_sprintf(_Printf_format_string_params_(2) const wchar_t *format, locale_t locale,...)
Writes formatted string to the stream.
Definition stream.hpp:623
size_t write_vsprintf(_Printf_format_string_params_(2) const wchar_t *format, locale_t locale, va_list params)
Writes formatted string to the stream.
Definition stream.hpp:650
virtual void flush()
Persists volatile element data.
Definition stream.hpp:126
virtual void skip(fsize_t amount)
Skips given amount of bytes of data on the stream.
Definition stream.hpp:142
virtual void close()
Closes the stream.
Definition stream.hpp:134
uint8_t read_byte()
Reads one byte of data.
Definition stream.hpp:210
virtual std::vector< uint8_t > read_remainder(size_t max_length=SIZE_MAX)
Reads and returns remainder of the stream.
Definition stream.hpp:184
size_t write_sprintf(_Printf_format_string_params_(2) const char *format, locale_t locale,...)
Writes formatted string to the stream.
Definition stream.hpp:609
size_t readln(std::basic_string< char, _Traits, _Ax > &str)
Reads stream to the end-of-line or end-of-file.
Definition stream.hpp:306
size_t readln_and_attach(std::basic_string< _Elem, _Traits, _Ax > &str)
Reads stream to the end-of-line or end-of-file and append to str.
Definition stream.hpp:346
size_t read_array(_Out_writes_bytes_(size *count) void *array, size_t size, size_t count)
Reads an array of data from the stream.
Definition stream.hpp:382
basic & write_str(const T *data)
Writes string to the stream length-prefixed.
Definition stream.hpp:509
virtual size_t read(_Out_writes_bytes_to_opt_(length, return) void *data, size_t length)
Reads block of data from the stream.
Definition stream.hpp:96
size_t readln(std::basic_string< wchar_t, _Traits, _Ax > &wstr)
Reads stream to the end-of-line or end-of-file.
Definition stream.hpp:318
void write_charset(charset_id charset)
Writes UTF8, UTF-16 or UTF-32 byte-order-mark.
Definition stream.hpp:594
basic & write_str(const std::basic_string< _Elem, _Traits, _Ax > &data)
Writes string to the stream length-prefixed.
Definition stream.hpp:534
basic & write_data(const T data)
Writes one primitive data type.
Definition stream.hpp:287
size_t write_array(const T_from *wstr, charset_encoder< T_from, T_to > &encoder)
Writes array of characters to the stream.
Definition stream.hpp:414
size_t readln_and_attach(std::basic_string< T_to, _Traits, _Ax > &wstr, charset_encoder< T_from, T_to > &encoder)
Reads stream to the end-of-line or end-of-file and append to str.
Definition stream.hpp:367
fsize_t write_stream(basic &stream, fsize_t amount=fsize_max)
Writes content of another stream.
Definition stream.hpp:569
virtual size_t write(_In_reads_bytes_opt_(length) const void *data, size_t length)
Writes block of data to the stream.
Definition stream.hpp:114
size_t write_array(_In_reads_or_z_opt_(num_chars) const T_from *wstr, size_t num_chars, charset_encoder< T_from, T_to > &encoder)
Writes array of characters to the stream.
Definition stream.hpp:435
size_t readln(std::basic_string< T_to, _Traits, _Ax > &wstr, charset_encoder< T_from, T_to > &encoder)
Reads stream to the end-of-line or end-of-file.
Definition stream.hpp:330
size_t write_array(_In_reads_bytes_opt_(size *count) const void *array, size_t size, size_t count)
Writes an array of data to the stream.
Definition stream.hpp:400
void write_byte(uint8_t byte, fsize_t amount=1)
Writes a byte of data.
Definition stream.hpp:221
basic & read_data(T &data)
Reads one primitive data type.
Definition stream.hpp:259
Buffered read/write stream.
Definition stream.hpp:1394
virtual void flush()
Persists volatile element data.
Definition stream.hpp:1503
virtual size_t read(_Out_writes_bytes_to_opt_(length, return) void *data, size_t length)
Reads block of data from the stream.
Definition stream.hpp:1424
virtual size_t write(_In_reads_bytes_opt_(length) const void *data, size_t length)
Writes block of data to the stream.
Definition stream.hpp:1460
Buffered OS data stream (file, pipe, socket...)
Definition stream.hpp:2331
Cached file.
Definition stream.hpp:1804
virtual time_point ctime() const
Returns file creation time.
Definition stream.hpp:2072
virtual void truncate()
Sets file size - truncates the remainder of file content from the current file position to the end of...
Definition stream.hpp:2051
virtual size_t read(_Out_writes_bytes_to_opt_(length, return) void *data, size_t length)
Reads block of data from the stream.
Definition stream.hpp:1871
virtual time_point atime() const
Returns file access time.
Definition stream.hpp:2077
virtual void unlock(fpos_t offset, fsize_t length)
Unlocks file section for exclusive access.
Definition stream.hpp:2038
virtual time_point mtime() const
Returns file modification time.
Definition stream.hpp:2086
virtual void close()
Closes the stream.
Definition stream.hpp:1986
virtual void set_mtime(time_point date)
Sets file modification time.
Definition stream.hpp:2108
virtual void lock(fpos_t offset, fsize_t length)
Locks file section for exclusive access.
Definition stream.hpp:2032
virtual void flush()
Persists volatile element data.
Definition stream.hpp:1995
virtual size_t write(_In_reads_bytes_opt_(length) const void *data, size_t length)
Writes block of data to the stream.
Definition stream.hpp:1933
virtual fsize_t size() const
Returns file size Should the file size cannot be determined, the method returns fsize_max and it does...
Definition stream.hpp:2044
virtual void set_ctime(time_point date)
Sets file create time.
Definition stream.hpp:2095
virtual fpos_t tell() const
Returns absolute file position in file or fpos_max if fails. This method does not update stream state...
Definition stream.hpp:2027
virtual void set_atime(time_point date)
Sets file access time.
Definition stream.hpp:2100
virtual fpos_t seek(foff_t offset, seek_t how=seek_t::beg)
Seeks to specified relative file position.
Definition stream.hpp:2006
Cached file-system file.
Definition stream.hpp:3101
cached_file(const stdex::sstring &filename, int mode, size_t cache_size=default_cache_size)
Opens file.
Definition stream.hpp:3131
void open(const stdex::sstring &filename, int mode)
Opens file.
Definition stream.hpp:3165
void open(const schar_t *filename, int mode)
Opens file.
Definition stream.hpp:3144
cached_file(const schar_t *filename, int mode, size_t cache_size=default_cache_size)
Opens file.
Definition stream.hpp:3117
Modifies data on the fly when reading from/writing to a source stream. Could also be used to modify r...
Definition stream.hpp:1022
virtual void flush()
Persists volatile element data.
Definition stream.hpp:1073
virtual void close()
Closes the stream.
Definition stream.hpp:1067
virtual size_t read(_Out_writes_bytes_to_opt_(length, return) void *data, size_t length)
Reads block of data from the stream.
Definition stream.hpp:1051
virtual size_t write(_In_reads_bytes_opt_(length) const void *data, size_t length)
Writes block of data to the stream.
Definition stream.hpp:1059
Compares multiple files to perform the same.
Definition stream.hpp:4141
virtual void truncate()
Sets file size - truncates the remainder of file content from the current file position to the end of...
Definition stream.hpp:4288
virtual size_t write(_In_reads_bytes_opt_(length) const void *data, size_t length)
Writes block of data to the stream.
Definition stream.hpp:4171
virtual void close()
Closes the stream.
Definition stream.hpp:4204
virtual fsize_t size() const
Returns file size Should the file size cannot be determined, the method returns fsize_max and it does...
Definition stream.hpp:4276
virtual void lock(fpos_t offset, fsize_t length)
Locks file section for exclusive access.
Definition stream.hpp:4250
virtual void unlock(fpos_t offset, fsize_t length)
Unlocks file section for exclusive access.
Definition stream.hpp:4263
virtual fpos_t seek(foff_t offset, seek_t how=seek_t::beg)
Seeks to specified relative file position.
Definition stream.hpp:4221
virtual fpos_t tell() const
Returns absolute file position in file or fpos_max if fails. This method does not update stream state...
Definition stream.hpp:4238
virtual void flush()
Persists volatile element data.
Definition stream.hpp:4189
virtual size_t read(_Out_writes_bytes_to_opt_(length, return) void *data, size_t length)
Reads block of data from the stream.
Definition stream.hpp:4148
In-memory FIFO queue.
Definition stream.hpp:4042
virtual void close()
Closes the stream.
Definition stream.hpp:4112
virtual size_t write(_In_reads_bytes_opt_(length) const void *data, size_t length)
Writes block of data to the stream.
Definition stream.hpp:4089
size_t size() const
Returns total size of pending data in the queue.
Definition stream.hpp:4126
virtual size_t read(_Out_writes_bytes_to_opt_(length, return) void *data, size_t length)
Reads block of data from the stream.
Definition stream.hpp:4061
Limits file reading/writing to a predefined window.
Definition stream.hpp:1694
virtual void truncate()
Sets file size - truncates the remainder of file content from the current file position to the end of...
Definition stream.hpp:1787
virtual void flush()
Persists volatile element data.
Definition stream.hpp:1737
virtual void skip(fsize_t amount)
Skips given amount of bytes of data on the stream.
Definition stream.hpp:1750
virtual fpos_t seek(foff_t offset, seek_t how=seek_t::beg)
Seeks to specified relative file position.
Definition stream.hpp:1743
virtual size_t write(_In_reads_bytes_opt_(length) const void *data, size_t length)
Writes block of data to the stream.
Definition stream.hpp:1717
virtual void lock(fpos_t offset, fsize_t length)
Locks file section for exclusive access.
Definition stream.hpp:1762
virtual size_t read(_Out_writes_bytes_to_opt_(length, return) void *data, size_t length)
Reads block of data from the stream.
Definition stream.hpp:1703
virtual void unlock(fpos_t offset, fsize_t length)
Unlocks file section for exclusive access.
Definition stream.hpp:1772
virtual fpos_t tell() const
Returns absolute file position in file or fpos_max if fails. This method does not update stream state...
Definition stream.hpp:1756
virtual void close()
Closes the stream.
Definition stream.hpp:1731
virtual fsize_t size() const
Returns file size Should the file size cannot be determined, the method returns fsize_max and it does...
Definition stream.hpp:1782
File-system file.
Definition stream.hpp:2697
virtual fsize_t size() const
Returns file size Should the file size cannot be determined, the method returns fsize_max and it does...
Definition stream.hpp:2891
file(const stdex::sstring &filename, int mode)
Opens file.
Definition stream.hpp:2718
static bool readonly(const stdex::sstring &filename)
Checks if file/folder/symlink is read-only.
Definition stream.hpp:3090
virtual time_point mtime() const
Returns file modification time.
Definition stream.hpp:2973
virtual void unlock(fpos_t offset, fsize_t length)
Unlocks file section for exclusive access.
Definition stream.hpp:2866
file(const schar_t *filename, int mode)
Opens file.
Definition stream.hpp:2707
virtual void set_ctime(time_point date)
Sets file create time.
Definition stream.hpp:2987
static bool readonly(const stdex::schar_t *filename)
Checks if file/folder/symlink is read-only.
Definition stream.hpp:3072
static bool exists(const stdex::sstring &filename)
Checks if file/folder/symlink likely exists.
Definition stream.hpp:3060
virtual time_point atime() const
Returns file access time.
Definition stream.hpp:2959
void open(const schar_t *filename, int mode)
Opens file.
Definition stream.hpp:2726
virtual void set_mtime(time_point date)
Sets file modification time.
Definition stream.hpp:3021
virtual void set_atime(time_point date)
Sets file access time.
Definition stream.hpp:3001
virtual void lock(fpos_t offset, fsize_t length)
Locks file section for exclusive access.
Definition stream.hpp:2843
virtual void truncate()
Sets file size - truncates the remainder of file content from the current file position to the end of...
Definition stream.hpp:2909
virtual time_point ctime() const
Returns file creation time.
Definition stream.hpp:2949
void open(const stdex::sstring &filename, int mode)
Opens file.
Definition stream.hpp:2799
virtual fpos_t seek(foff_t offset, seek_t how=seek_t::beg)
Seeks to specified relative file position.
Definition stream.hpp:2804
virtual fpos_t tell() const
Returns absolute file position in file or fpos_max if fails. This method does not update stream state...
Definition stream.hpp:2825
static bool exists(const stdex::schar_t *filename)
Checks if file/folder/symlink likely exists.
Definition stream.hpp:3045
Limits reading from/writing to stream to a predefined number of bytes.
Definition stream.hpp:1551
fsize_t read_limit
Number of bytes left that may be read from the stream.
Definition stream.hpp:1603
virtual size_t read(_Out_writes_bytes_to_opt_(length, return) void *data, size_t length)
Reads block of data from the stream.
Definition stream.hpp:1559
virtual size_t write(_In_reads_bytes_opt_(length) const void *data, size_t length)
Writes block of data to the stream.
Definition stream.hpp:1580
fsize_t write_limit
Number of bytes left, that can be written to the stream.
Definition stream.hpp:1604
In-memory file.
Definition stream.hpp:3178
memory_file & operator=(memory_file &&other) noexcept
Moves content from another file.
Definition stream.hpp:3360
virtual fsize_t size() const
Returns file size Should the file size cannot be determined, the method returns fsize_max and it does...
Definition stream.hpp:3878
memory_file(const schar_t *filename, int mode)
Loads content from file-system file.
Definition stream.hpp:3258
memory_file & write_str(const std::basic_string< _Elem, _Traits, _Ax > &data)
Writes string to the stream length-prefixed.
Definition stream.hpp:3757
memory_file(const memory_file &other)
Copies content from another file.
Definition stream.hpp:3276
size_t m_size
file size
Definition stream.hpp:4029
void get(fpos_t offset, T &data)
Reads data from specified file location This does not move file pointer. It checks for data size Assu...
Definition stream.hpp:3967
size_t write_stream(basic &stream, size_t amount=SIZE_MAX)
Writes content of another stream.
Definition stream.hpp:3792
uint8_t * m_data
file data
Definition stream.hpp:4026
memory_file & read_data(T &data)
Reads one primitive data type.
Definition stream.hpp:3546
virtual void close()
Closes the stream.
Definition stream.hpp:3842
virtual size_t read(_Out_writes_bytes_to_opt_(length, return) void *data, size_t length)
Reads block of data from the stream.
Definition stream.hpp:3507
virtual fpos_t tell() const
Returns absolute file position in file or fpos_max if fails. This method does not update stream state...
Definition stream.hpp:3873
size_t m_reserved
reserved file size
Definition stream.hpp:4030
memory_file(size_t size, state_t state=state_t::ok)
Creates an empty file of reserved size.
Definition stream.hpp:3199
void reserve(size_t required, bool tight=false) noexcept
Reallocates memory.
Definition stream.hpp:3398
memory_file(const stdex::sstring &filename, int mode)
Loads content from file-system file.
Definition stream.hpp:3269
memory_file & read_str(std::basic_string< _Elem, _Traits, _Ax > &data)
Reads length-prefixed string from the stream.
Definition stream.hpp:3586
memory_file(memory_file &&other) noexcept
Moves content from another file.
Definition stream.hpp:3331
void write_byte(uint8_t byte, size_t amount=1)
Writes a byte of data.
Definition stream.hpp:3641
memory_file & operator=(const memory_file &other)
Copies content from another file.
Definition stream.hpp:3301
void set(fpos_t offset, const T data)
Writes data to specified file location This does not move file pointer nor update file size....
Definition stream.hpp:3933
void load(const stdex::sstring &filename, int mode)
Loads content from a file-system file.
Definition stream.hpp:3460
size_t m_offset
file pointer
Definition stream.hpp:4028
void save(const schar_t *filename, int mode)
Saves content to a file-system file.
Definition stream.hpp:3471
void load(const schar_t *filename, int mode)
Loads content from a file-system file.
Definition stream.hpp:3427
virtual fpos_t seek(foff_t offset, seek_t how=seek_t::beg)
Seeks to specified relative file position.
Definition stream.hpp:3856
virtual void truncate()
Sets file size - truncates the remainder of file content from the current file position to the end of...
Definition stream.hpp:3883
memory_file & write_data(const T data)
Writes one primitive data type.
Definition stream.hpp:3674
memory_file & write_str(const T *data)
Writes string to the stream length-prefixed.
Definition stream.hpp:3712
void save(const stdex::sstring &filename, int mode)
Saves content to a file-system file.
Definition stream.hpp:3497
bool m_manage
may reallocate m_data?
Definition stream.hpp:4027
memory_file(void *data, size_t size, bool manage=false, state_t state=state_t::ok)
Creates a file based on available data.
Definition stream.hpp:3248
virtual size_t write(_In_reads_bytes_opt_(length) const void *data, size_t length)
Writes block of data to the stream.
Definition stream.hpp:3617
memory_file(void *data, size_t size, size_t reserved, bool manage=false, state_t state=state_t::ok)
Creates a file based on available data.
Definition stream.hpp:3225
const void * data() const
Returns pointer to data.
Definition stream.hpp:3505
Definition stream.hpp:1170
enum stdex::stream::replicator::worker::op_t op
Operation to perform.
size_t num_written
Number of bytes written.
Definition stream.hpp:1219
size_t length
Byte limit of data to write.
Definition stream.hpp:1218
const void * data
Data to write.
Definition stream.hpp:1217
Replicates writing of the same data to multiple streams.
Definition stream.hpp:1087
void push_back(basic *source)
Adds stream on the list.
Definition stream.hpp:1106
virtual void flush()
Persists volatile element data.
Definition stream.hpp:1163
void remove(basic *source)
Removes stream from the list.
Definition stream.hpp:1114
virtual size_t write(_In_reads_bytes_opt_(length) const void *data, size_t length)
Writes block of data to the stream.
Definition stream.hpp:1131
virtual void close()
Closes the stream.
Definition stream.hpp:1158
Socket stream.
Definition stream.hpp:2353
socket_t get() const noexcept
Returns socket handle.
Definition stream.hpp:2409
virtual void close()
Closes the stream.
Definition stream.hpp:2455
socket(int af, int type, int protocol)
Creates a socket.
Definition stream.hpp:2388
virtual size_t read(_Out_writes_bytes_to_opt_(length, return) void *data, size_t length)
Reads block of data from the stream.
Definition stream.hpp:2411
virtual size_t write(_In_reads_bytes_opt_(length) const void *data, size_t length)
Writes block of data to the stream.
Definition stream.hpp:2435
Limits reading from/writing to stream to a predefined window.
Definition stream.hpp:1611
fpos_t write_offset
Number of bytes to discard on write.
Definition stream.hpp:1687
virtual size_t write(_In_reads_bytes_opt_(length) const void *data, size_t length)
Writes block of data to the stream.
Definition stream.hpp:1648
virtual size_t read(_Out_writes_bytes_to_opt_(length, return) void *data, size_t length)
Reads block of data from the stream.
Definition stream.hpp:1619
fpos_t read_offset
Number of bytes to skip on read.
Definition stream.hpp:1686
Operating system object (file, pipe, anything with an OS handle etc.)
Definition system.hpp:93
virtual void close()
Closes object.
Definition system.hpp:134
Numerical interval.
Definition interval.hpp:18
bool contains(T x) const
Is value in interval?
Definition interval.hpp:79
T size() const
Returns interval size.
Definition interval.hpp:47
T end
interval end
Definition interval.hpp:20
T start
interval start
Definition interval.hpp:19
Definition stream.hpp:1528
Definition stream.hpp:4130