11#include "interval.hpp"
30#include <condition_variable>
39#pragma GCC diagnostic push
40#pragma GCC diagnostic ignored "-Wunknown-pragmas"
43#if !defined(SET_FILE_OP_TIMES) && defined(RDAT_BELEZI_CAS_DOSTOPA_VER)
44#define SET_FILE_OP_TIMES 1
45#pragma message("RDAT_BELEZI_CAS_DOSTOPA_VER is deprecated. Use SET_FILE_OP_TIMES instead.")
46#elif !defined(SET_FILE_OP_TIMES)
47#define SET_FILE_OP_TIMES 0
49#if !defined(CHECK_STREAM_STATE) && defined(RDAT_NE_PREVERJAJ_STANJA_VER)
50#define CHECK_STREAM_STATE 0
51#pragma message("RDAT_NE_PREVERJAJ_EOF_VER is deprecated. Use CHECK_STREAM_STATE=0 instead.")
53#define CHECK_STREAM_STATE 1
72 using fsize_t = uint64_t;
73 constexpr fsize_t fsize_max = UINT64_MAX;
75 constexpr size_t iterate_count = 0x10;
76 constexpr size_t default_block_size = 0x10000;
77 constexpr utf16_t utf16_bom = u
'\ufeff';
78 constexpr utf32_t utf32_bom = U
'\ufeff';
79 constexpr const char utf8_bom[3] = {
'\xef',
'\xbb',
'\xbf' };
89 virtual ~basic()
noexcept(
false) {}
102 virtual _Success_(
return != 0 || length == 0) size_t
read(
103 _Out_writes_bytes_to_opt_(length, return)
void* data, _In_
size_t length)
105 _Unreferenced_(data);
106 _Unreferenced_(length);
107 m_state = state_t::fail;
120 virtual _Success_(
return != 0) size_t
write(
121 _In_reads_bytes_opt_(length) const
void* data, _In_
size_t length)
123 _Unreferenced_(data);
124 _Unreferenced_(length);
125 m_state = state_t::fail;
134 m_state = state_t::ok;
142 m_state = state_t::ok;
148 virtual void skip(_In_ fsize_t amount)
152 else if (amount < iterate_count) {
153 for (
size_t i = 0; i < static_cast<size_t>(amount); i++) {
155 if (!
ok()) _Unlikely_
160 size_t block =
static_cast<size_t>(std::min<fsize_t>(amount, default_block_size));
162 std::unique_ptr<uint8_t[]> dummy(
new uint8_t[block]);
164 amount -=
read_array(dummy.get(),
sizeof(uint8_t),
static_cast<size_t>(std::min<fsize_t>(amount, block)));
165 if (!
ok()) _Unlikely_
169 catch (
const std::bad_alloc&) { m_state = state_t::fail; }
176 state_t
state()
const {
return m_state; };
181 bool ok()
const {
return m_state == state_t::ok; };
192 std::vector<uint8_t> result;
193 size_t offset, length;
195 length = default_block_size;
196 while (offset < max_length) {
197 length = std::min(length, max_length);
198 try { result.resize(length); }
199 catch (
const std::bad_alloc&) {
200 m_state = state_t::fail;
203 auto num_read =
read_array(result.data() + offset,
sizeof(uint8_t), length - offset);
205 if (!
ok()) _Unlikely_
207 length += default_block_size;
209 result.resize(offset);
221 throw std::system_error(sys_error(), std::system_category(),
"failed to read");
230 write(&
byte,
sizeof(uint8_t));
231 else if (amount < iterate_count) {
232 for (
size_t i = 0; i < static_cast<size_t>(amount); i++) {
233 write(&
byte,
sizeof(uint8_t));
234 if (!
ok()) _Unlikely_
239 size_t block =
static_cast<size_t>(std::min<fsize_t>(amount, default_block_size));
241 std::unique_ptr<uint8_t[]> dummy(
new uint8_t[block]);
242 memset(dummy.get(),
byte, block);
244 amount -=
write_array(dummy.get(),
sizeof(uint8_t),
static_cast<size_t>(std::min<fsize_t>(amount, block)));
245 if (!
ok()) _Unlikely_
249 catch (
const std::bad_alloc&) { m_state = state_t::fail; }
267 if (!
ok()) _Unlikely_ {
276 m_state = state_t::eof;
295 if (!
ok()) _Unlikely_
297#if BYTE_ORDER == BIG_ENDIAN
298 T data_le = HE2LE(data);
299 write(&data_le,
sizeof(T));
301 write(&data,
sizeof(T));
311 template<
class T,
class TR = std::
char_traits<T>,
class AX = std::allocator<T>>
312 size_t readln(_Inout_ std::basic_string<T, TR, AX>& str)
323 template<
class T_from,
class T_to,
class TR = std::
char_traits<T_to>,
class AX = std::allocator<T_to>>
326 if (encoder.from_encoding() == encoder.to_encoding())
328 std::basic_string<T_from> tmp;
330 encoder.strcpy(str, tmp);
339 template<
class T,
class TR = std::
char_traits<T>,
class AX = std::allocator<T>>
343 T chr =
static_cast<T
>(0), previous =
static_cast<T
>(0);
346 if (!initial && !(previous ==
static_cast<T
>(
'\r') && chr ==
static_cast<T
>(
'\n')))
351 }
while (
ok() && chr !=
static_cast<T
>(
'\n'));
360 template<
class T_from,
class T_to,
class TR = std::
char_traits<T_to>,
class AX = std::allocator<T_to>>
363 if (encoder.from_encoding() == encoder.to_encoding())
365 std::basic_string<T_from> tmp;
367 encoder.strcat(str, tmp);
376 size_t read_array(_Out_writes_bytes_(size* count)
void* array, _In_
size_t size, _In_
size_t count)
378 for (
size_t to_read = mul(size, count);;) {
379 size_t num_read =
read(array, to_read);
383 if (!
ok()) _Unlikely_
384 return count - to_read / size;
385 reinterpret_cast<uint8_t*&
>(array) += num_read;
394 size_t write_array(_In_reads_bytes_opt_(size* count)
const void* array, _In_
size_t size, _In_
size_t count)
396 return write(array, mul(size, count)) / size;
407 template <
class T_from,
class T_to>
410 if (!
ok()) _Unlikely_
412 size_t num_chars = stdex::strlen(str);
413 if (encoder.from_encoding() == encoder.to_encoding())
414 return write_array(str,
sizeof(T_from), num_chars);
415 std::basic_string<T_to> tmp(encoder.convert(str, num_chars));
416 return write_array(tmp.data(),
sizeof(T_to), tmp.size());
428 template <
class T_from,
class T_to>
431 if (!
ok()) _Unlikely_
433 num_chars = stdex::strnlen(str, num_chars);
434 if (encoder.from_encoding() == encoder.to_encoding())
435 return write_array(str,
sizeof(T_from), num_chars);
436 std::basic_string<T_to> tmp(encoder.convert(str, num_chars));
437 return write_array(tmp.data(),
sizeof(T_to), tmp.size());
448 template<
class T_from,
class T_to,
class TR = std::
char_traits<T_from>,
class AX = std::allocator<T_from>>
451 if (!
ok()) _Unlikely_
453 if (encoder.from_encoding() == encoder.to_encoding())
454 return write_array(str.data(),
sizeof(T_from), str.size());
455 std::basic_string<T_to> tmp(encoder.convert(str));
456 return write_array(tmp.data(),
sizeof(T_to), tmp.size());
470 template<
class T,
class TR = std::
char_traits<T>,
class AX = std::allocator<T>>
474 if (!
ok()) _Unlikely_
478 if (!
ok()) _Unlikely_
480 data.reserve(num_chars);
482 constexpr uint32_t buf_chars = 0x400;
484 uint32_t num_read =
static_cast<uint32_t
>(
read_array(buf,
sizeof(T), std::min<uint32_t>(num_chars, buf_chars)));
485 data.append(buf, num_read);
486 num_chars -= num_read;
487 if (!num_chars || !
ok())
507 size_t num_chars = stdex::strlen(data);
508 if (num_chars > UINT32_MAX)
509 throw std::invalid_argument(
"string too long");
511 if (!
ok()) _Unlikely_
528 template<
class T,
class TR = std::
char_traits<T>,
class AX = std::allocator<T>>
532 size_t num_chars = data.size();
533 if (num_chars > UINT32_MAX)
534 throw std::invalid_argument(
"string too long");
536 if (!
ok()) _Unlikely_
548 size_t write_sa(_In_ LPSAFEARRAY sa)
550 safearray_accessor_with_size<uint8_t> a(sa);
551 return write(a.data(), a.size());
562 std::unique_ptr<uint8_t[]> data(
new uint8_t[
static_cast<size_t>(std::min<fsize_t>(amount, default_block_size))]);
563 fsize_t num_copied = 0, to_write = amount;
564 m_state = state_t::ok;
566 size_t num_read = stream.read(data.get(),
static_cast<size_t>(std::min<fsize_t>(default_block_size, to_write)));
567 size_t num_written =
write(data.get(), num_read);
568 num_copied += num_written;
569 to_write -= num_written;
570 if (stream.m_state == state_t::eof) {
572 m_state = state_t::ok;
575 m_state = stream.m_state;
587 if (charset == charset_id::utf32)
589 else if (charset == charset_id::utf16)
591 else if (charset == charset_id::utf8)
600 size_t write_sprintf(_In_z_ _Printf_format_string_params_(2)
const char* format, _In_opt_ locale_t
locale, ...)
614 size_t write_sprintf(_In_z_ _Printf_format_string_params_(2)
const wchar_t* format, _In_opt_ locale_t
locale, ...)
628 size_t write_vsprintf(_In_z_ _Printf_format_string_params_(2)
const char* format, _In_opt_ locale_t
locale, _In_ va_list params)
631 tmp.reserve(default_block_size);
632 vappendf(tmp, format,
locale, params);
633 return write_array(tmp.data(),
sizeof(
char), tmp.size());
641 size_t write_vsprintf(_In_z_ _Printf_format_string_params_(2)
const wchar_t* format, _In_opt_ locale_t
locale, _In_ va_list params)
644 tmp.reserve(default_block_size);
645 vappendf(tmp, format,
locale, params);
646 return write_array(tmp.data(),
sizeof(
wchar_t), tmp.size());
649 basic& operator >>(_Out_ int8_t& data) {
return read_data(data); }
650 basic& operator <<(_In_
const int8_t data) {
return write_data(data); }
651 basic& operator >>(_Out_ int16_t& data) {
return read_data(data); }
652 basic& operator <<(_In_
const int16_t data) {
return write_data(data); }
653 basic& operator >>(_Out_ int32_t& data) {
return read_data(data); }
654 basic& operator <<(_In_
const int32_t data) {
return write_data(data); }
655 basic& operator >>(_Out_ int64_t& data) {
return read_data(data); }
656 basic& operator <<(_In_
const int64_t data) {
return write_data(data); }
657 basic& operator >>(_Out_ uint8_t& data) {
return read_data(data); }
658 basic& operator <<(_In_
const uint8_t data) {
return write_data(data); }
659 basic& operator >>(_Out_ uint16_t& data) {
return read_data(data); }
660 basic& operator <<(_In_
const uint16_t data) {
return write_data(data); }
661 basic& operator >>(_Out_ uint32_t& data) {
return read_data(data); }
662 basic& operator <<(_In_
const uint32_t data) {
return write_data(data); }
663 basic& operator >>(_Out_ uint64_t& data) {
return read_data(data); }
664 basic& operator <<(_In_
const uint64_t data) {
return write_data(data); }
665 basic& operator >>(_Out_
float& data) {
return read_data(data); }
666 basic& operator <<(_In_
const float data) {
return write_data(data); }
667 basic& operator >>(_Out_
double& data) {
return read_data(data); }
668 basic& operator <<(_In_
const double data) {
return write_data(data); }
669 basic& operator >>(_Out_
char& data) {
return read_data(data); }
670 basic& operator <<(_In_
const char data) {
return write_data(data); }
671#ifdef _NATIVE_WCHAR_T_DEFINED
672 basic& operator >>(_Out_
wchar_t& data) {
return read_data(data); }
673 basic& operator <<(_In_
const wchar_t data) {
return write_data(data); }
675 template<
class T,
class TR = std::
char_traits<T>,
class AX = std::allocator<T>>
676 basic& operator >>(_Out_ std::basic_string<T, TR, AX>& data) {
return read_str(data); }
678 basic& operator <<(_In_
const T* data) {
return write_str(data); }
679 template<
class T,
class TR = std::
char_traits<T>,
class AX = std::allocator<T>>
680 basic& operator <<(_In_
const std::basic_string<T, TR, AX>& data) {
return write_str(data); }
682 template <
class T,
class AX = std::allocator<T>>
683 basic& operator <<(_In_
const std::vector<T, AX>& data)
685 size_t num = data.size();
686 if (num > UINT32_MAX) _Unlikely_
687 throw std::invalid_argument(
"collection too big");
688 *this << static_cast<uint32_t>(num);
689 for (
auto& el : data)
694 template <
class T,
class AX = std::allocator<T>>
695 basic& operator >>(_Out_ std::vector<T, AX>& data)
700 if (!
ok()) _Unlikely_
703 for (uint32_t i = 0; i < num; ++i) {
706 if (!
ok()) _Unlikely_
708 data.push_back(std::move(el));
712 template <
class KEY,
class PR = std::less<KEY>,
class AX = std::allocator<KEY>>
713 basic& operator <<(_In_
const std::set<KEY, PR, AX>& data)
715 size_t num = data.size();
716 if (num > UINT32_MAX) _Unlikely_
717 throw std::invalid_argument(
"collection too big");
718 *this << static_cast<uint32_t>(num);
719 for (
auto& el : data)
724 template <
class KEY,
class PR = std::less<KEY>,
class AX = std::allocator<KEY>>
725 basic& operator >>(_Out_ std::set<KEY, PR, AX>& data)
730 if (!
ok()) _Unlikely_
732 for (uint32_t i = 0; i < num; ++i) {
735 if (!
ok()) _Unlikely_
737 data.insert(std::move(el));
741 template <
class KEY,
class PR = std::less<KEY>,
class AX = std::allocator<KEY>>
742 basic& operator <<(_In_
const std::multiset<KEY, PR, AX>& data)
744 size_t num = data.size();
745 if (num > UINT32_MAX) _Unlikely_
746 throw std::invalid_argument(
"collection too big");
747 *this << static_cast<uint32_t>(num);
748 for (
auto& el : data)
753 template <
class KEY,
class PR = std::less<KEY>,
class AX = std::allocator<KEY>>
754 basic& operator >>(_Out_ std::multiset<KEY, PR, AX>& data)
759 if (!
ok()) _Unlikely_
761 for (uint32_t i = 0; i < num; ++i) {
764 if (!
ok()) _Unlikely_
766 data.insert(std::move(el));
778 using fpos_t = uint64_t;
779 constexpr fpos_t fpos_max = UINT64_MAX;
780 constexpr fpos_t fpos_min = 0;
785 using foff_t = int64_t;
786 constexpr foff_t foff_max = INT64_MAX;
787 constexpr foff_t foff_min = INT64_MIN;
805 using clock = std::chrono::file_clock;
807 using clock = std::chrono::system_clock;
809 using time_point = std::chrono::time_point<clock>;
819 size_t length = std::min<size_t>(max_length,
static_cast<size_t>(
size() -
tell()));
820 std::vector<uint8_t> result;
821 try { result.resize(length); }
822 catch (
const std::bad_alloc&) {
823 m_state = state_t::fail;
826 result.resize(
read_array(result.data(),
sizeof(uint8_t), length));
835 virtual fpos_t
seek(_In_ foff_t offset, _In_ seek_t how = seek_t::beg) = 0;
844 return seek(
static_cast<foff_t
>(offset), seek_t::beg);
852 fpos_t
seekcur(_In_ foff_t offset) {
return seek(offset, seek_t::cur); }
859 fpos_t
seekend(_In_ foff_t offset) {
return seek(offset, seek_t::end); }
861 virtual void skip(_In_ fsize_t amount)
863 if (amount > foff_max)
864 throw std::invalid_argument(
"file offset too big");
865 seek(
static_cast<foff_t
>(amount), seek_t::cur);
874 virtual fpos_t
tell()
const = 0;
879 virtual void lock(_In_ fpos_t offset, _In_ fsize_t length)
881 _Unreferenced_(offset);
882 _Unreferenced_(length);
883 throw std::domain_error(
"not implemented");
889 virtual void unlock(_In_ fpos_t offset, _In_ fsize_t length)
891 _Unreferenced_(offset);
892 _Unreferenced_(length);
893 throw std::domain_error(
"not implemented");
900 virtual fsize_t
size()
const = 0;
912 return time_point::min();
920 return time_point::min();
928 return time_point::min();
936 _Unreferenced_(date);
937 throw std::domain_error(
"not implemented");
945 _Unreferenced_(date);
946 throw std::domain_error(
"not implemented");
954 _Unreferenced_(date);
955 throw std::domain_error(
"not implemented");
962 LPSAFEARRAY read_sa()
964 stdex_assert(
size() <= SIZE_MAX);
965 if (
size() > ULONG_MAX)
966 throw std::range_error(
"data too big");
967 ULONG length =
static_cast<ULONG
>(
size());
968 std::unique_ptr<SAFEARRAY, SafeArrayDestroy_delete> sa(SafeArrayCreateVector(VT_UI1, 0, length));
970 throw std::runtime_error(
"SafeArrayCreateVector failed");
971 if (
seek(0) != 0) _Unlikely_
972 throw std::system_error(sys_error(), std::system_category(),
"failed to seek");
973 safearray_accessor<void> a(sa.get());
974 if (
read_array(a.data(), 1, length) != length)
975 throw std::system_error(sys_error(), std::system_category(),
"failed to read");
985 charset_id
read_charset(_In_ charset_id default_charset = charset_id::system)
987 if (
seek(0) != 0) _Unlikely_
988 throw std::system_error(sys_error(), std::system_category(),
"failed to seek");
991 if (
ok() && id_utf32 == utf32_bom)
992 return charset_id::utf32;
994 if (
seek(0) != 0) _Unlikely_
995 throw std::system_error(sys_error(), std::system_category(),
"failed to seek");
998 if (
ok() && id_utf16 == utf16_bom)
999 return charset_id::utf16;
1001 if (
seek(0) != 0) _Unlikely_
1002 throw std::system_error(sys_error(), std::system_category(),
"failed to seek");
1003 char id_utf8[3] = { 0 };
1005 if (
ok() && strncmp(id_utf8, _countof(id_utf8), utf8_bom, _countof(utf8_bom)) == 0)
1006 return charset_id::utf8;
1008 if (
seek(0) != 0) _Unlikely_
1009 throw std::system_error(sys_error(), std::system_category(),
"failed to seek");
1010 return default_charset;
1023#pragma warning(suppress: 26495)
1026 void init(_Inout_
basic& source)
1034 m_state = m_source->
state();
1045 basic(source.state()),
1049 virtual _Success_(
return != 0 || length == 0) size_t
read(
1050 _Out_writes_bytes_to_opt_(length, return)
void* data, _In_
size_t length)
1052 size_t num_read = m_source->
read(data, length);
1053 m_state = m_source->
state();
1057 virtual _Success_(
return != 0) size_t
write(
1058 _In_reads_bytes_opt_(length) const
void* data, _In_
size_t length)
1060 size_t num_written = m_source->
write(data, length);
1061 m_state = m_source->
state();
1068 m_state = m_source->
state();
1074 m_state = m_source->
state();
1089 for (
auto w = m_workers.begin(), w_end = m_workers.end(); w != w_end; ++w) {
1092 const std::lock_guard<std::mutex> lk(_w->mutex);
1093 _w->op = worker::op_t::quit;
1095 _w->cv.notify_one();
1097 for (
auto w = m_workers.begin(), w_end = m_workers.end(); w != w_end; ++w)
1106 m_workers.push_back(std::unique_ptr<worker>(
new worker(source)));
1114 for (
auto w = m_workers.begin(), w_end = m_workers.end(); w != w_end; ++w) {
1116 if (_w->source == source) {
1118 const std::lock_guard<std::mutex> lk(_w->mutex);
1119 _w->op = worker::op_t::quit;
1121 _w->cv.notify_one();
1129 virtual _Success_(
return != 0) size_t
write(
1130 _In_reads_bytes_opt_(length) const
void* data, _In_
size_t length)
1132 for (
auto w = m_workers.begin(), w_end = m_workers.end(); w != w_end; ++w) {
1135 const std::lock_guard<std::mutex> lk(_w->mutex);
1136 _w->op = worker::op_t::write;
1138 _w->length = length;
1140 _w->cv.notify_one();
1142 size_t num_written = length;
1143 m_state = state_t::ok;
1144 for (
auto w = m_workers.begin(), w_end = m_workers.end(); w != w_end; ++w) {
1146 std::unique_lock<std::mutex> lk(_w->mutex);
1147 _w->cv.wait(lk, [&] {return _w->op == worker::op_t::noop; });
1148 if (_w->num_written < num_written)
1149 num_written = _w->num_written;
1150 if (
ok() && !_w->source->ok())
1151 m_state = _w->source->state();
1158 foreach_worker(worker::op_t::close);
1163 foreach_worker(worker::op_t::flush);
1177 *
static_cast<std::thread*
>(
this) = std::thread([](_Inout_
worker& w) { w.process_op(); }, std::ref(*
this));
1184 std::unique_lock<std::mutex> lk(mutex);
1185 cv.wait(lk, [&] {
return op != op_t::noop; });
1190 num_written = source->write(data, length);
1219 std::condition_variable cv;
1222 void foreach_worker(_In_ worker::op_t op)
1224 for (
auto w = m_workers.begin(), w_end = m_workers.end(); w != w_end; ++w) {
1227 const std::lock_guard<std::mutex> lk(_w->mutex);
1230 _w->cv.notify_one();
1232 m_state = state_t::ok;
1233 for (
auto w = m_workers.begin(), w_end = m_workers.end(); w != w_end; ++w) {
1235 std::unique_lock<std::mutex> lk(_w->mutex);
1236 _w->cv.wait(lk, [&] {
return _w->op == worker::op_t::noop; });
1238 m_state = _w->source->state();
1242 std::list<std::unique_ptr<worker>> m_workers;
1245 constexpr size_t default_async_limit = 0x100000;
1252 template <
size_t N_cap = default_async_limit>
1258 m_worker([](_Inout_
async_reader& w) { w.process(); }, std::ref(*
this))
1267#pragma warning(suppress: 6101)
1268 virtual _Success_(
return != 0 || length == 0) size_t read(
1269 _Out_writes_bytes_to_opt_(length, return)
void* data, _In_
size_t length)
1271 stdex_assert(data || !length);
1272 for (
size_t to_read = length;;) {
1273 uint8_t* ptr;
size_t num_read;
1274 std::tie(ptr, num_read) = m_ring.front();
1275 if (!ptr) _Unlikely_ {
1276 m_state = to_read < length || !length ? state_t::ok : m_source->state();
1277 return length - to_read;
1279 if (to_read < num_read)
1281 memcpy(data, ptr, num_read);
1282 m_ring.pop(num_read);
1283 to_read -= num_read;
1285 m_state = state_t::ok;
1288 reinterpret_cast<uint8_t*&
>(data) += num_read;
1296 uint8_t* ptr;
size_t num_write;
1297 std::tie(ptr, num_write) = m_ring.back();
1298 if (!ptr) _Unlikely_
1300 num_write = m_source->read(ptr, num_write);
1301 m_ring.push(num_write);
1302 if (!m_source->ok()) {
1310 ring<uint8_t, N_cap> m_ring;
1311 std::thread m_worker;
1319 template <
size_t N_cap = default_async_limit>
1325 m_worker([](_Inout_
async_writer& w) { w.process(); }, std::ref(*
this))
1334 virtual _Success_(
return != 0) size_t write(
1335 _In_reads_bytes_opt_(length) const
void* data, _In_
size_t length)
1337 stdex_assert(data || !length);
1338 for (
size_t to_write = length;;) {
1339 uint8_t* ptr;
size_t num_write;
1340 std::tie(ptr, num_write) = m_ring.back();
1341 if (!ptr) _Unlikely_ {
1342 m_state = state_t::fail;
1343 return length - to_write;
1345 if (to_write < num_write)
1346 num_write = to_write;
1347 memcpy(ptr, data, num_write);
1348 m_ring.push(num_write);
1349 to_write -= num_write;
1351 m_state = state_t::ok;
1354 reinterpret_cast<const uint8_t*&
>(data) += num_write;
1368 uint8_t* ptr;
size_t num_read;
1369 std::tie(ptr, num_read) = m_ring.front();
1372 num_read = m_source->write(ptr, num_read);
1373 m_ring.pop(num_read);
1374 if (!m_source->ok()) {
1382 ring<uint8_t, N_cap> m_ring;
1383 std::thread m_worker;
1386 constexpr size_t default_buffer_size = 0x400;
1395 explicit buffer(_In_
size_t read_buffer_size = default_buffer_size, _In_
size_t write_buffer_size = default_buffer_size) :
1397 m_read_buffer(read_buffer_size),
1398 m_write_buffer(write_buffer_size)
1410 buffer(_Inout_
basic& source, _In_
size_t read_buffer_size = default_buffer_size, _In_
size_t write_buffer_size = default_buffer_size) :
1412 m_read_buffer(read_buffer_size),
1413 m_write_buffer(write_buffer_size)
1422 virtual _Success_(
return != 0 || length == 0) size_t read(
1423 _Out_writes_bytes_to_opt_(length, return)
void* data, _In_
size_t length)
1425 stdex_assert(data || !length);
1426 for (
size_t to_read = length;;) {
1427 size_t buffer_size = m_read_buffer.tail - m_read_buffer.head;
1428 if (to_read <= buffer_size) {
1429 memcpy(data, m_read_buffer.data + m_read_buffer.head, to_read);
1430 m_read_buffer.head += to_read;
1431 m_state = state_t::ok;
1435 memcpy(data, m_read_buffer.data + m_read_buffer.head, buffer_size);
1436 reinterpret_cast<uint8_t*&
>(data) += buffer_size;
1437 to_read -= buffer_size;
1439 m_read_buffer.head = 0;
1440 if (to_read > m_read_buffer.capacity) {
1442 m_read_buffer.tail = 0;
1443 to_read -= m_source->read(data, to_read);
1444 m_state = to_read < length ? state_t::ok : m_source->state();
1445 return length - to_read;
1447 m_read_buffer.tail = m_source->read(m_read_buffer.data, m_read_buffer.capacity);
1448 if (m_read_buffer.tail < m_read_buffer.capacity && m_read_buffer.tail < to_read) _Unlikely_ {
1449 memcpy(data, m_read_buffer.data, m_read_buffer.tail);
1450 m_read_buffer.head = m_read_buffer.tail;
1451 to_read -= m_read_buffer.tail;
1452 m_state = to_read < length ? state_t::ok : m_source->state();
1453 return length - to_read;
1458 virtual _Success_(
return != 0) size_t write(
1459 _In_reads_bytes_opt_(length) const
void* data, _In_
size_t length)
1461 stdex_assert(data || !length);
1462 if (!length) _Unlikely_ {
1465 if (!ok()) _Unlikely_
1467 converter::write(
nullptr, 0);
1471 for (
size_t to_write = length;;) {
1472 size_t available_buffer = m_write_buffer.capacity - m_write_buffer.tail;
1473 if (to_write <= available_buffer) {
1474 memcpy(m_write_buffer.data + m_write_buffer.tail, data, to_write);
1475 m_write_buffer.tail += to_write;
1476 m_state = state_t::ok;
1479 if (available_buffer) {
1480 memcpy(m_write_buffer.data + m_write_buffer.tail, data, available_buffer);
1481 reinterpret_cast<const uint8_t*&
>(data) += available_buffer;
1482 to_write -= available_buffer;
1483 m_write_buffer.tail += available_buffer;
1485 size_t buffer_size = m_write_buffer.tail - m_write_buffer.head;
1487 m_write_buffer.head += converter::write(m_write_buffer.data + m_write_buffer.head, buffer_size);
1488 if (m_write_buffer.head == m_write_buffer.tail)
1489 m_write_buffer.head = m_write_buffer.tail = 0;
1491 return length - to_write;
1493 if (to_write > m_write_buffer.capacity) {
1495 to_write -= converter::write(data, to_write);
1496 return length - to_write;
1511 size_t buffer_size = m_write_buffer.tail - m_write_buffer.head;
1513 m_write_buffer.head += m_source->write(m_write_buffer.data + m_write_buffer.head, buffer_size);
1514 if (m_write_buffer.head == m_write_buffer.tail) {
1515 m_write_buffer.head = 0;
1516 m_write_buffer.tail = 0;
1519 m_state = m_source->state();
1523 m_state = state_t::ok;
1528 size_t head, tail, capacity;
1530 buffer_t(_In_
size_t buffer_size) :
1533 capacity(buffer_size),
1534 data(buffer_size ?
new uint8_t[buffer_size] :
nullptr)
1542 } m_read_buffer, m_write_buffer;
1551 limiter(_Inout_
basic& source, _In_ fsize_t _read_limit = 0, _In_ fsize_t _write_limit = 0) :
1553 read_limit(_read_limit),
1554 write_limit(_write_limit)
1557 virtual _Success_(
return != 0 || length == 0) size_t read(
1558 _Out_writes_bytes_to_opt_(length, return)
void* data, _In_
size_t length)
1561 if (read_limit == fsize_max)
1562 num_read = converter::read(data, length);
1563 else if (length <= read_limit) {
1564 num_read = converter::read(data, length);
1565 read_limit -= num_read;
1567 else if (length && !read_limit) {
1569 m_state = state_t::eof;
1572 num_read = converter::read(data,
static_cast<size_t>(read_limit));
1573 read_limit -= num_read;
1578 virtual _Success_(
return != 0) size_t write(
1579 _In_reads_bytes_opt_(length) const
void* data, _In_
size_t length)
1582 if (write_limit == fsize_max)
1583 num_written = converter::write(data, length);
1584 else if (length <= write_limit) {
1585 num_written = converter::write(data, length);
1586 write_limit -= num_written;
1588 else if (length && !write_limit) {
1590 m_state = state_t::fail;
1593 num_written = converter::write(data,
static_cast<size_t>(write_limit));
1594 write_limit -= num_written;
1611 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) :
1612 limiter(source, read_limit, write_limit),
1613 read_offset(_read_offset),
1614 write_offset(_write_offset)
1617 virtual _Success_(
return != 0 || length == 0) size_t read(
1618 _Out_writes_bytes_to_opt_(length, return)
void* data, _In_
size_t length)
1621 m_source->skip(read_offset);
1622 m_state = m_source->state();
1623 if (!ok()) _Unlikely_
1628 if (read_limit == fsize_max)
1629 num_read = converter::read(data, length);
1630 else if (length <= read_limit) {
1631 num_read = converter::read(data, length);
1632 read_limit -= num_read;
1634 else if (length && !read_limit) {
1636 m_source->skip(length);
1637 m_state = state_t::eof;
1640 num_read = converter::read(data,
static_cast<size_t>(read_limit));
1641 read_limit -= num_read;
1646 virtual _Success_(
return != 0) size_t write(
1647 _In_reads_bytes_opt_(length) const
void* data, _In_
size_t length)
1649 size_t num_skipped, num_written;
1650 if (length <= write_offset) {
1651 write_offset -= length;
1652 m_state = state_t::ok;
1656 reinterpret_cast<const uint8_t*&
>(data) +=
static_cast<size_t>(write_offset);
1657 length -=
static_cast<size_t>(write_offset);
1658 num_skipped =
static_cast<size_t>(write_offset);
1663 if (write_limit == fsize_max)
1664 num_written = converter::write(data, length);
1665 else if (length <= write_limit) {
1666 num_written = converter::write(data, length);
1667 write_limit -= num_written;
1669 else if (length && !write_limit) {
1670 num_skipped += length;
1672 m_state = state_t::ok;
1675 num_skipped += length -
static_cast<size_t>(write_limit);
1676 num_written = converter::write(data,
static_cast<size_t>(write_limit));
1677 write_limit -= num_written;
1679 return num_skipped + num_written;
1695 basic(source.state()),
1697 m_offset(source.tell()),
1698 m_region(offset, offset + length)
1701 virtual _Success_(
return != 0 || length == 0) size_t read(
1702 _Out_writes_bytes_to_opt_(length, return)
void* data, _In_
size_t length)
1704 stdex_assert(data || !length);
1705 if (m_region.contains(m_offset)) {
1706 size_t num_read = m_source.read(data,
static_cast<size_t>(std::min<fpos_t>(length, m_region.end - m_offset)));
1707 m_state = m_source.state();
1708 m_offset += num_read;
1711 m_state = length ? state_t::eof : state_t::ok;
1715 virtual _Success_(
return != 0) size_t write(
1716 _In_reads_bytes_opt_(length) const
void* data, _In_
size_t length)
1718 stdex_assert(data || !length);
1719 if (m_region.contains(m_offset)) {
1720 size_t num_written = m_source.write(data,
static_cast<size_t>(std::min<fpos_t>(length, m_region.end - m_offset)));
1721 m_state = m_source.state();
1722 m_offset += num_written;
1725 m_state = state_t::fail;
1732 m_state = m_source.state();
1738 m_state = m_source.state();
1741 virtual fpos_t
seek(_In_ foff_t offset, _In_ seek_t how = seek_t::beg)
1743 m_offset = m_source.seek(offset, how);
1744 m_state = m_source.state();
1745 return ok() ? m_offset - m_region.start : fpos_max;
1748 virtual void skip(_In_ fsize_t amount)
1750 m_source.skip(amount);
1751 m_state = m_source.state();
1756 fpos_t offset = m_source.tell();
1757 return m_region.contains(offset) ? offset - m_region.start : fpos_max;
1760 virtual void lock(_In_ fpos_t offset, _In_ fsize_t length)
1762 if (m_region.contains(offset)) {
1763 m_source.lock(m_region.start + offset, std::min<fsize_t>(length, m_region.end - offset));
1764 m_state = m_source.state();
1767 m_state = state_t::fail;
1770 virtual void unlock(_In_ fpos_t offset, _In_ fsize_t length)
1772 if (m_region.contains(offset)) {
1773 m_source.unlock(m_region.start + offset, std::min<fsize_t>(length, m_region.end - offset));
1774 m_state = m_source.state();
1777 m_state = state_t::fail;
1782 return m_region.size();
1787 m_state = state_t::fail;
1796 constexpr size_t default_cache_size = 0x1000;
1805#pragma warning(suppress: 26495)
1806 explicit cache(_In_
size_t cache_size = default_cache_size) :
1807 basic(state_t::fail),
1819 m_state = m_source->state();
1820 m_offset = m_source->tell();
1821#if SET_FILE_OP_TIMES
1822 m_atime = m_source->atime();
1823 m_mtime = m_source->mtime();
1831 if (!ok()) _Unlikely_
1832 throw std::system_error(sys_error(), std::system_category(),
"failed to flush cache");
1833 m_source->seekbeg(m_offset);
1834#if SET_FILE_OP_TIMES
1835 m_source->set_atime(m_atime);
1836 m_source->set_mtime(m_mtime);
1844 cache(_Inout_
basic_file& source, _In_
size_t cache_size = default_cache_size) :
1845 basic(source.state()),
1847 m_cache(cache_size),
1848 m_offset(source.tell())
1849#if SET_FILE_OP_TIMES
1850 , m_atime(source.atime())
1851 , m_mtime(source.mtime())
1855 virtual ~cache()
noexcept(
false)
1859 if (!ok()) _Unlikely_
1860 throw std::system_error(sys_error(), std::system_category(),
"failed to flush cache");
1861 m_source->seekbeg(m_offset);
1862#if SET_FILE_OP_TIMES
1863 m_source->set_atime(m_atime);
1864 m_source->set_mtime(m_mtime);
1869 virtual _Success_(
return != 0 || length == 0) size_t read(
1870 _Out_writes_bytes_to_opt_(length, return)
void* data, _In_
size_t length)
1872 stdex_assert(data || !length);
1873#if SET_FILE_OP_TIMES
1874 m_atime = time_point::now();
1876 for (
size_t to_read = length;;) {
1877 if (m_cache.status != cache_t::cache_t::status_t::empty) {
1878 if (m_cache.region.contains(m_offset)) {
1879 size_t remaining_cache =
static_cast<size_t>(m_cache.region.end - m_offset);
1880 if (to_read <= remaining_cache) {
1881 memcpy(data, m_cache.data +
static_cast<size_t>(m_offset - m_cache.region.start), to_read);
1882 m_offset += to_read;
1883 m_state = state_t::ok;
1886 memcpy(data, m_cache.data +
static_cast<size_t>(m_offset - m_cache.region.start), remaining_cache);
1887 reinterpret_cast<uint8_t*&
>(data) += remaining_cache;
1888 to_read -= remaining_cache;
1889 m_offset += remaining_cache;
1892 if (!ok()) _Unlikely_ {
1893 if (to_read < length)
1894 m_state = state_t::ok;
1895 return length - to_read;
1899 fpos_t end_max = m_offset + to_read;
1900 if (m_offset / m_cache.capacity < end_max / m_cache.capacity) {
1902 m_source->seekbeg(m_offset);
1903 if (!m_source->ok()) _Unlikely_ {
1904 m_state = to_read < length ? state_t::ok : state_t::fail;
1905 return length - to_read;
1907 size_t num_read = m_source->read(data, to_read -
static_cast<size_t>(end_max % m_cache.capacity));
1908 m_offset += num_read;
1909 to_read -= num_read;
1911 m_state = state_t::ok;
1914 reinterpret_cast<uint8_t*&
>(data) += num_read;
1915 m_state = m_source->state();
1917 if (to_read < length)
1918 m_state = state_t::ok;
1919 return length - to_read;
1923 load_cache(m_offset);
1924 if (!ok()) _Unlikely_ {
1925 m_state = to_read < length ? state_t::ok : state_t::fail;
1926 return length - to_read;
1928 if (m_cache.region.end <= m_offset) _Unlikely_ {
1929 m_state = to_read < length ? state_t::ok : state_t::eof;
1930 return length - to_read;
1935 virtual _Success_(
return != 0) size_t write(
1936 _In_reads_bytes_opt_(length) const
void* data, _In_
size_t length)
1938 stdex_assert(data || !length);
1939#if SET_FILE_OP_TIMES
1940 m_atime = m_mtime = time_point::now();
1942 for (
size_t to_write = length;;) {
1943 if (m_cache.status != cache_t::cache_t::status_t::empty) {
1944 fpos_t end_max = m_cache.region.start + m_cache.capacity;
1945 if (m_cache.region.start <= m_offset && m_offset < end_max) {
1946 size_t remaining_cache =
static_cast<size_t>(end_max - m_offset);
1947 if (to_write <= remaining_cache) {
1948 memcpy(m_cache.data +
static_cast<size_t>(m_offset - m_cache.region.start), data, to_write);
1949 m_offset += to_write;
1950 m_cache.status = cache_t::cache_t::status_t::dirty;
1951 m_cache.region.end = std::max(m_cache.region.end, m_offset);
1952 m_state = state_t::ok;
1955 memcpy(m_cache.data +
static_cast<size_t>(m_offset - m_cache.region.start), data, remaining_cache);
1956 reinterpret_cast<const uint8_t*&
>(data) += remaining_cache;
1957 to_write -= remaining_cache;
1958 m_offset += remaining_cache;
1959 m_cache.status = cache_t::cache_t::status_t::dirty;
1960 m_cache.region.end = end_max;
1963 if (!ok()) _Unlikely_
1964 return length - to_write;
1967 fpos_t end_max = m_offset + to_write;
1968 if (m_offset / m_cache.capacity < end_max / m_cache.capacity) {
1970 m_source->seekbeg(m_offset);
1971 if (!ok()) _Unlikely_
1972 return length - to_write;
1973 size_t num_written = m_source->write(data, to_write -
static_cast<size_t>(end_max % m_cache.capacity));
1974 m_offset += num_written;
1975 m_state = m_source->state();
1976 to_write -= num_written;
1977 if (!to_write || !ok())
1978 return length - to_write;
1979 reinterpret_cast<const uint8_t*&
>(data) += num_written;
1982 load_cache(m_offset);
1983 if (!ok()) _Unlikely_
1984 return length - to_write;
1991 if (!ok()) _Unlikely_
1992 throw std::system_error(sys_error(), std::system_category(),
"failed to flush cache");
1994 m_state = m_source->state();
1999#if SET_FILE_OP_TIMES
2000 m_atime = m_mtime = time_point::min();
2003 if (!ok()) _Unlikely_
2008 virtual fpos_t
seek(_In_ foff_t offset, _In_ seek_t how = seek_t::beg)
2010 m_state = state_t::ok;
2015 offset =
static_cast<foff_t
>(m_offset) + offset;
2019 if (n == fsize_max) _Unlikely_{
2020 m_state = state_t::fail;
2023 offset =
static_cast<foff_t
>(n) + offset;
2027 throw std::invalid_argument(
"unknown seek origin");
2029 if (offset < 0) _Unlikely_
2030 throw std::invalid_argument(
"negative file offset");
2031 return m_offset =
static_cast<fpos_t
>(offset);
2039 virtual void lock(_In_ fpos_t offset, _In_ fsize_t length)
2041 m_source->lock(offset, length);
2042 m_state = m_source->state();
2045 virtual void unlock(_In_ fpos_t offset, _In_ fsize_t length)
2047 m_source->unlock(offset, length);
2048 m_state = m_source->state();
2053 return m_cache.status != cache_t::cache_t::status_t::empty ?
2054 std::max(m_source->size(), m_cache.region.end) :
2060#if SET_FILE_OP_TIMES
2061 m_atime = m_mtime = time_point::now();
2063 m_source->seekbeg(m_offset);
2064 if (m_cache.region.end <= m_offset) {
2067 else if (m_cache.region.start <= m_offset) {
2069 m_cache.region.end = m_offset;
2073 m_cache.status = cache_t::cache_t::status_t::empty;
2075 m_source->truncate();
2076 m_state = m_source->state();
2081 return m_source->ctime();
2086#if SET_FILE_OP_TIMES
2087 return std::max(m_atime, m_source->atime());
2089 return m_source->atime();
2095#if SET_FILE_OP_TIMES
2096 return std::max(m_mtime, m_source->mtime());
2098 return m_source->mtime();
2104 m_source->set_ctime(date);
2109#if SET_FILE_OP_TIMES
2112 m_source->set_atime(date);
2117#if SET_FILE_OP_TIMES
2120 m_source->set_mtime(date);
2127 if (m_cache.status != cache_t::cache_t::status_t::dirty)
2128 m_state = state_t::ok;
2129 else if (!m_cache.region.empty()) {
2132 m_cache.status = cache_t::cache_t::status_t::loaded;
2135 m_state = state_t::ok;
2136 m_cache.status = cache_t::cache_t::status_t::loaded;
2140 void invalidate_cache()
2142 if (m_cache.status == cache_t::cache_t::status_t::dirty && !m_cache.region.empty()) {
2144 if (!ok()) _Unlikely_
2147 m_state = state_t::ok;
2148 m_cache.status = cache_t::cache_t::status_t::empty;
2151 void load_cache(_In_ fpos_t start)
2153 stdex_assert(m_cache.status != cache_t::cache_t::status_t::dirty);
2154 start -= start % m_cache.capacity;
2155 m_source->seekbeg(m_cache.region.start = start);
2156 if (m_source->ok()) {
2157 m_cache.region.end = start + m_source->read(m_cache.data, m_cache.capacity);
2158 m_cache.status = cache_t::cache_t::status_t::loaded;
2159 m_state = state_t::ok;
2162 m_state = state_t::fail;
2167 stdex_assert(m_cache.status == cache_t::cache_t::status_t::dirty);
2168 m_source->seekbeg(m_cache.region.start);
2169 m_source->write(m_cache.data,
static_cast<size_t>(m_cache.region.size()));
2170 m_state = m_source->state();
2173 basic_file* m_source;
2177 enum class status_t {
2182 interval<fpos_t> region;
2184 cache_t(_In_
size_t _capacity) :
2185 data(new uint8_t[_capacity]),
2186 capacity(_capacity),
2187 status(status_t::empty),
2197#if SET_FILE_OP_TIMES
2211 basic_sys(_In_opt_ sys_handle h = invalid_handle, _In_ state_t state = state_t::ok) :
2216 virtual _Success_(
return != 0 || length == 0) size_t read(
2217 _Out_writes_bytes_to_opt_(length, return)
void* data, _In_
size_t length)
2219 stdex_assert(data || !length);
2225 block_size = 0x1F80000;
2226#elif defined(_WIN32)
2227 block_size = 0x3f00000;
2229 block_size = SSIZE_MAX;
2231 for (
size_t to_read = length;;) {
2236 __try { succeeded = ReadFile(m_h, data,
static_cast<DWORD
>(std::min<size_t>(to_read, block_size)), &num_read,
nullptr); }
2237 __except (EXCEPTION_EXECUTE_HANDLER) { succeeded = FALSE; SetLastError(ERROR_UNHANDLED_EXCEPTION); num_read = 0; }
2238 if (!succeeded && GetLastError() == ERROR_NO_SYSTEM_RESOURCES && block_size > default_block_size) _Unlikely_ {
2241 block_size = default_block_size;
2244 if (!succeeded) _Unlikely_
2246 auto num_read = ::read(m_h, data, std::min<size_t>(to_read, block_size));
2247 if (num_read < 0) _Unlikely_
2250 m_state = to_read < length ? state_t::ok : state_t::fail;
2251 return length - to_read;
2253 if (!num_read) _Unlikely_ {
2254 m_state = to_read < length || !length ? state_t::ok : state_t::eof;
2255 return length - to_read;
2257 to_read -=
static_cast<size_t>(num_read);
2259 m_state = state_t::ok;
2262 reinterpret_cast<uint8_t*&
>(data) += num_read;
2266 virtual _Success_(
return != 0) size_t write(
2267 _In_reads_bytes_opt_(length) const
void* data, _In_
size_t length)
2274 block_size = 0x1F80000;
2275#elif defined(_WIN32)
2276 block_size = 0x3f00000;
2278 block_size = SSIZE_MAX;
2280 for (
size_t to_write = length;;) {
2285 __try { succeeded = WriteFile(m_h, data,
static_cast<DWORD
>(std::min<size_t>(to_write, block_size)), &num_written,
nullptr); }
2286 __except (EXCEPTION_EXECUTE_HANDLER) { succeeded = FALSE; SetLastError(ERROR_UNHANDLED_EXCEPTION); num_written = 0; }
2287 to_write -= num_written;
2289 m_state = state_t::ok;
2292 reinterpret_cast<const uint8_t*&
>(data) += num_written;
2293 if (!succeeded) _Unlikely_ {
2294 m_state = state_t::fail;
2295 return length - to_write;
2298 auto num_written = ::write(m_h, data, std::min<size_t>(to_write, block_size));
2299 if (num_written < 0) _Unlikely_ {
2300 m_state = state_t::fail;
2301 return length - to_write;
2303 to_write -=
static_cast<size_t>(num_written);
2305 m_state = state_t::ok;
2308 reinterpret_cast<const uint8_t*&
>(data) += num_written;
2316 sys_object::close();
2317 m_state = state_t::ok;
2320 m_state = state_t::fail;
2327 m_state = FlushFileBuffers(m_h) ? state_t::ok : state_t::fail;
2329 m_state = fsync(m_h) >= 0 ? state_t::ok : state_t::fail;
2340 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) :
2341 buffer(read_buffer_size, write_buffer_size),
2362 socket(_In_opt_ socket_t h = stdex::invalid_socket, _In_ state_t state = state_t::ok) :
2372 socket(_Inout_
socket&& other) noexcept : m_h(other.m_h)
2374 other.m_h = stdex::invalid_socket;
2379 if (
this != std::addressof(other)) {
2380 if (m_h != stdex::invalid_socket)
2383 other.m_h = stdex::invalid_socket;
2395 socket(_In_
int af, _In_
int type, _In_
int protocol)
2397 m_h = ::socket(af, type, protocol);
2398 if (m_h == stdex::invalid_socket) _Unlikely_
2399 m_state = state_t::fail;
2404 if (m_h != stdex::invalid_socket)
2411 operator bool() const noexcept {
return m_h != stdex::invalid_socket; }
2416 socket_t
get() const noexcept {
return m_h; }
2418 virtual _Success_(
return != 0 || length == 0) size_t read(
2419 _Out_writes_bytes_to_opt_(length, return)
void* data, _In_
size_t length)
2421 stdex_assert(data || !length);
2422 constexpr int block_size = 0x10000000;
2423 for (
size_t to_read = length;;) {
2424 auto num_read = recv(m_h,
reinterpret_cast<char*
>(data),
2426 static_cast<int>(std::min<size_t>(to_read, block_size)),
2428 std::min<size_t>(to_read, block_size),
2431 if (num_read < 0) _Unlikely_ {
2432 m_state = to_read < length ? state_t::ok : state_t::fail;
2433 return length - to_read;
2436 m_state = to_read < length || !length ? state_t::ok : state_t::eof;
2437 return length - to_read;
2439 to_read -=
static_cast<size_t>(num_read);
2441 m_state = state_t::ok;
2444 reinterpret_cast<uint8_t*&
>(data) += num_read;
2448 virtual _Success_(
return != 0) size_t write(
2449 _In_reads_bytes_opt_(length) const
void* data, _In_
size_t length)
2451 stdex_assert(data || !length);
2452 constexpr int block_size = 0x10000000;
2453 for (
size_t to_write = length;;) {
2454 auto num_written = send(m_h,
reinterpret_cast<const char*
>(data),
2456 static_cast<int>(std::min<size_t>(to_write, block_size)),
2458 std::min<size_t>(to_write, block_size),
2461 if (num_written < 0) _Unlikely_ {
2462 m_state = state_t::fail;
2463 return length - to_write;
2465 to_write -=
static_cast<size_t>(num_written);
2467 m_state = state_t::ok;
2470 reinterpret_cast<const uint8_t*&
>(data) += num_written;
2476 if (m_h != stdex::invalid_socket) {
2478 m_h = stdex::invalid_socket;
2480 m_state = state_t::ok;
2491 class sequential_stream :
public basic
2494 sequential_stream(_In_ ISequentialStream* source) : m_source(source)
2499 virtual ~sequential_stream()
2501 m_source->Release();
2504 virtual _Success_(
return != 0 || length == 0) size_t read(
2505 _Out_writes_bytes_to_opt_(length, return)
void* data, _In_
size_t length)
2507 stdex_assert(data || !length);
2508 for (
size_t to_read = length;;) {
2511 __try { hr = m_source->Read(data, (ULONG)std::min<size_t>(to_read, ULONG_MAX), &num_read); }
2512 __except (EXCEPTION_EXECUTE_HANDLER) { hr = E_FAIL; }
2513 if (FAILED(hr)) _Unlikely_ {
2514 m_state = to_read < length ? state_t::ok : state_t::fail;
2515 return length - to_read;
2517 to_read -= num_read;
2518 if (hr == S_FALSE) _Unlikely_ {
2519 m_state = to_read < length || !length ? state_t::ok : state_t::eof;
2520 return length - to_read;
2523 m_state = state_t::ok;
2526 reinterpret_cast<uint8_t*&
>(data) += num_read;
2530 virtual _Success_(
return != 0) size_t write(
2531 _In_reads_bytes_opt_(length) const
void* data, _In_
size_t length)
2533 stdex_assert(data || !length);
2534 for (
size_t to_write = length;;) {
2536 ULONG num_written = 0;
2537 __try { hr = m_source->Write(data,
static_cast<ULONG
>(std::min<size_t>(to_write, ULONG_MAX)), &num_written); }
2538 __except (EXCEPTION_EXECUTE_HANDLER) { hr = E_FAIL; }
2541 if (FAILED(hr)) _Unlikely_ {
2542 m_state = state_t::fail;
2543 return length - to_write;
2545 to_write -= num_written;
2547 m_state = state_t::ok;
2550 reinterpret_cast<const uint8_t*&
>(data) += num_written;
2555 ISequentialStream* m_source;
2561 class asp :
public basic
2564 asp(_In_opt_ IRequest* request, _In_opt_ IResponse* response) :
2566 m_response(response)
2569 m_request->AddRef();
2571 m_response->AddRef();
2577 m_request->Release();
2579 m_response->Release();
2582 virtual _Success_(
return != 0 || length == 0) size_t read(
2583 _Out_writes_bytes_to_opt_(length, return)
void* data, _In_
size_t length)
2585 stdex_assert(data || !length);
2586 if (!m_request) _Unlikely_ {
2587 m_state = state_t::fail;
2590 for (
size_t to_read = length;;) {
2591 VARIANT var_amount, var_data;
2592 V_VT(&var_amount) = VT_I4;
2593 V_I4(&var_amount) = (LONG)std::min<size_t>(to_read, LONG_MAX);
2594 V_VT(&var_data) = VT_EMPTY;
2595 HRESULT hr = [&]() {
2596 __try {
return m_request->BinaryRead(&var_amount, &var_data); }
2597 __except (EXCEPTION_EXECUTE_HANDLER) {
return E_FAIL; }
2599 if (FAILED(hr)) _Unlikely_ {
2600 m_state = to_read < length ? state_t::ok : state_t::fail;
2601 return length - to_read;
2603 stdex_assert(V_VT(&var_amount) == VT_I4);
2604 stdex_assert(V_VT(&var_data) == (VT_ARRAY | VT_UI1));
2605 std::unique_ptr<SAFEARRAY, SafeArrayDestroy_delete> sa(V_ARRAY(&var_data));
2606 if (!V_I4(&var_amount)) _Unlikely_ {
2607 m_state = to_read < length || !length ? state_t::ok : state_t::eof;
2608 return length - to_read;
2610 safearray_accessor<uint8_t> a(sa.get());
2611 memcpy(data, a.data(), V_I4(&var_amount));
2612 to_read -= V_I4(&var_amount);
2614 m_state = state_t::ok;
2617 reinterpret_cast<uint8_t*&
>(data) += V_I4(&var_amount);
2621 virtual _Success_(
return != 0) size_t write(
2622 _In_reads_bytes_opt_(length) const
void* data, _In_
size_t length)
2625 m_state = state_t::fail;
2628 for (
size_t to_write = length;;) {
2629 UINT num_written =
static_cast<UINT
>(std::min<size_t>(to_write, UINT_MAX));
2630 std::unique_ptr<OLECHAR, SysFreeString_delete> bstr_data(SysAllocStringByteLen(
reinterpret_cast<LPCSTR
>(data), num_written));
2632 V_VT(&var_data) = VT_BSTR;
2633 V_BSTR(&var_data) = bstr_data.get();
2634 HRESULT hr = [&]() {
2635 __try {
return m_response->BinaryWrite(var_data); }
2636 __except (EXCEPTION_EXECUTE_HANDLER) {
return E_FAIL; }
2638 if (FAILED(hr)) _Unlikely_ {
2639 m_state = state_t::fail;
2640 return length - to_write;
2642 to_write -= num_written;
2644 m_state = state_t::ok;
2647 reinterpret_cast<const uint8_t*&
>(data) += num_written;
2651 virtual void close()
2654 __try { m_response->End(); }
2655 __except (EXCEPTION_EXECUTE_HANDLER) {}
2657 m_state = state_t::ok;
2660 virtual void flush()
2664 __try { hr = m_response->Flush(); }
2665 __except (EXCEPTION_EXECUTE_HANDLER) { hr = E_FAIL; }
2666 m_state = SUCCEEDED(hr) ? state_t::ok : state_t::fail;
2671 IRequest* m_request;
2672 IResponse* m_response;
2681 mode_for_reading = 1 << 0,
2682 mode_for_writing = 1 << 1,
2683 mode_for_chmod = 1 << 2,
2685 mode_open_existing = 0 << 3,
2686 mode_truncate_existing = 1 << 3,
2687 mode_preserve_existing = 2 << 3,
2688 mode_create_new = 3 << 3,
2689 mode_create = 4 << 3,
2690 mode_disposition_mask = 7 << 3,
2692 mode_append = 1 << 6,
2694 mode_binary = 1 << 7,
2697 share_reading = 1 << 8,
2698 share_writing = 1 << 9,
2699 share_deleting = 1 << 10,
2700 share_all = share_reading | share_writing | share_deleting,
2702 inherit_handle = 1 << 11,
2704 hint_write_thru = 1 << 12,
2705 hint_no_buffering = 1 << 13,
2706 hint_random_access = 1 << 14,
2707 hint_sequential_access = 1 << 15,
2710#pragma warning(push)
2711#pragma warning(disable: 4250)
2718 file(_In_opt_ sys_handle h = invalid_handle, _In_ state_t state = state_t::ok) :
basic_sys(h, state) {}
2726 file(_In_z_
const schar_t* filename, _In_
int mode)
2728 open(filename, mode);
2737 template <
class TR = std::
char_traits<s
char_t>,
class AX = std::allocator<s
char_t>>
2738 file(_In_
const std::basic_string<TR, AX>& filename, _In_
int mode) :
file(filename.c_str(), mode) {}
2746 void open(_In_z_
const schar_t* filename, _In_
int mode)
2748 if (m_h != invalid_handle)
2752 DWORD dwDesiredAccess = 0;
2753 if (mode & mode_for_reading) dwDesiredAccess |= GENERIC_READ;
2754 if (mode & mode_for_writing) dwDesiredAccess |= GENERIC_WRITE;
2755 if (mode & mode_for_chmod) dwDesiredAccess |= FILE_WRITE_ATTRIBUTES;
2757 DWORD dwShareMode = 0;
2758 if (mode & share_reading) dwShareMode |= FILE_SHARE_READ;
2759 if (mode & share_writing) dwShareMode |= FILE_SHARE_WRITE;
2760 if (mode & share_deleting) dwShareMode |= FILE_SHARE_DELETE;
2762 SECURITY_ATTRIBUTES sa = {
sizeof(SECURITY_ATTRIBUTES) };
2763 sa.bInheritHandle = mode & inherit_handle ? true :
false;
2765 DWORD dwCreationDisposition;
2766 switch (mode & mode_disposition_mask) {
2767 case mode_open_existing: dwCreationDisposition = OPEN_EXISTING;
break;
2768 case mode_truncate_existing: dwCreationDisposition = TRUNCATE_EXISTING;
break;
2769 case mode_preserve_existing: dwCreationDisposition = OPEN_ALWAYS;
break;
2770 case mode_create_new: dwCreationDisposition = CREATE_NEW;
break;
2771 case mode_create: dwCreationDisposition = CREATE_ALWAYS;
break;
2772 default:
throw std::invalid_argument(
"invalid mode");
2775 DWORD dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
2776 if (mode & hint_write_thru) dwFlagsAndAttributes |= FILE_FLAG_WRITE_THROUGH;
2777 if (mode & hint_no_buffering) dwFlagsAndAttributes |= FILE_FLAG_NO_BUFFERING;
2778 if (mode & hint_random_access) dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
2779 if (mode & hint_sequential_access) dwFlagsAndAttributes |= FILE_FLAG_SEQUENTIAL_SCAN;
2781 m_h = CreateFile(filename, dwDesiredAccess, dwShareMode, &sa, dwCreationDisposition, dwFlagsAndAttributes, NULL);
2784 switch (mode & (mode_for_reading | mode_for_writing)) {
2785 case mode_for_reading: flags |= O_RDONLY;
break;
2786 case mode_for_writing: flags |= O_WRONLY;
break;
2787 case mode_for_reading | mode_for_writing: flags |= O_RDWR;
break;
2789 switch (mode & mode_disposition_mask) {
2790 case mode_open_existing:
break;
2791 case mode_truncate_existing: flags |= O_TRUNC;
break;
2792 case mode_preserve_existing: flags |= O_CREAT;
break;
2793 case mode_create_new: flags |= O_CREAT | O_EXCL;
break;
2794 case mode_create: flags |= O_CREAT | O_TRUNC;
break;
2795 default:
throw std::invalid_argument(
"invalid mode");
2797 if (mode & hint_write_thru) flags |= O_DSYNC;
2799 if (mode & hint_no_buffering) flags |= O_RSYNC;
2802 m_h = ::open(filename, flags, DEFFILEMODE);
2804 if (m_h != invalid_handle) {
2805 m_state = state_t::ok;
2806 if (mode & mode_append)
2807 seek(0, seek_t::end);
2810 m_state = state_t::fail;
2819 template <
class TR = std::
char_traits<s
char_t>,
class AX = std::allocator<s
char_t>>
2820 void open(_In_
const std::basic_string<TR, AX>& filename, _In_
int mode)
2822 open(filename.c_str(), mode);
2825 virtual fpos_t
seek(_In_ foff_t offset, _In_ seek_t how = seek_t::beg)
2829 li.QuadPart = offset;
2830 li.LowPart = SetFilePointer(m_h, li.LowPart, &li.HighPart,
static_cast<DWORD
>(how));
2831 if (li.LowPart != 0xFFFFFFFF || GetLastError() == NO_ERROR) {
2832 m_state = state_t::ok;
2836 off64_t result = lseek64(m_h, offset,
static_cast<int>(how));
2838 m_state = state_t::ok;
2839 return static_cast<fpos_t
>(result);
2842 m_state = state_t::fail;
2848 if (m_h != invalid_handle) {
2852 li.LowPart = SetFilePointer(m_h, 0, &li.HighPart, FILE_CURRENT);
2853 if (li.LowPart != 0xFFFFFFFF || GetLastError() == NO_ERROR)
2856 off64_t result = lseek64(m_h, 0, SEEK_CUR);
2858 return static_cast<fpos_t
>(result);
2864 virtual void lock(_In_ fpos_t offset, _In_ fsize_t length)
2867 LARGE_INTEGER liOffset;
2868 LARGE_INTEGER liSize;
2869 liOffset.QuadPart = offset;
2870 liSize.QuadPart = length;
2871 if (LockFile(m_h, liOffset.LowPart, liOffset.HighPart, liSize.LowPart, liSize.HighPart)) {
2872 m_state = state_t::ok;
2876 off64_t orig = lseek64(m_h, 0, SEEK_CUR);
2878 if (offset > std::numeric_limits<off64_t>::max())
2879 throw std::invalid_argument(
"file offset too big");
2880 if (length > std::numeric_limits<off64_t>::max())
2881 throw std::invalid_argument(
"file section length too big");
2882 m_state = lseek64(m_h,
static_cast<off64_t
>(offset), SEEK_SET) >= 0 && lockf64(m_h, F_LOCK,
static_cast<off64_t
>(length)) >= 0 ? state_t::ok : state_t::fail;
2883 lseek64(m_h, orig, SEEK_SET);
2884 m_state = state_t::ok;
2888 m_state = state_t::fail;
2891 virtual void unlock(_In_ fpos_t offset, _In_ fsize_t length)
2894 LARGE_INTEGER liOffset;
2895 LARGE_INTEGER liSize;
2896 liOffset.QuadPart = offset;
2897 liSize.QuadPart = length;
2898 if (UnlockFile(m_h, liOffset.LowPart, liOffset.HighPart, liSize.LowPart, liSize.HighPart)) {
2899 m_state = state_t::ok;
2903 off64_t orig = lseek64(m_h, 0, SEEK_CUR);
2905 if (offset > std::numeric_limits<off64_t>::max())
2906 throw std::invalid_argument(
"file offset too big");
2907 if (length > std::numeric_limits<off64_t>::max())
2908 throw std::invalid_argument(
"file section length too big");
2909 if (lseek64(m_h,
static_cast<off64_t
>(offset), SEEK_SET) >= 0 && lockf64(m_h, F_ULOCK,
static_cast<off64_t
>(length)) >= 0) {
2910 lseek64(m_h, orig, SEEK_SET);
2911 m_state = state_t::ok;
2914 lseek64(m_h, orig, SEEK_SET);
2917 m_state = state_t::fail;
2924 li.LowPart = GetFileSize(m_h, (LPDWORD)&li.HighPart);
2925 if (li.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR)
2929 off64_t orig = lseek64(m_h, 0, SEEK_CUR);
2931 off64_t length = lseek64(m_h, 0, SEEK_END);
2932 lseek64(m_h, orig, SEEK_SET);
2934 return static_cast<fsize_t
>(length);
2943 if (SetEndOfFile(m_h)) {
2944 m_state = state_t::ok;
2948 off64_t length = lseek64(m_h, 0, SEEK_CUR);
2949 if (length >= 0 && ftruncate64(m_h, length) >= 0) {
2950 m_state = state_t::ok;
2954 m_state = state_t::fail;
2958 static time_point ft2tp(_In_
const FILETIME& ft)
2961 uint64_t t = (
static_cast<int64_t
>(ft.dwHighDateTime) << 32) | ft.dwLowDateTime;
2963 uint64_t t = ((
static_cast<int64_t
>(ft.dwHighDateTime) << 32) | ft.dwLowDateTime) - 116444736000000000ll;
2965 return time_point(time_point::duration(t));
2968 static void tp2ft(_In_ time_point tp, _Out_ FILETIME& ft)
2971 uint64_t t = tp.time_since_epoch().count();
2973 uint64_t t = tp.time_since_epoch().count() + 116444736000000000ll;
2975 ft.dwHighDateTime =
static_cast<DWORD
>((t >> 32) & 0xffffffff);
2976 ft.dwLowDateTime =
static_cast<DWORD
>(t & 0xffffffff);
2984 if (GetFileTime(m_h, &ft,
nullptr,
nullptr))
2987 return time_point::min();
2994 if (GetFileTime(m_h,
nullptr, &ft,
nullptr))
2998 if (fstat(m_h, &buf) >= 0)
2999 return clock::from_time_t(buf.st_atime);
3001 return time_point::min();
3008 if (GetFileTime(m_h,
nullptr,
nullptr, &ft))
3012 if (fstat(m_h, &buf) >= 0)
3013 return clock::from_time_t(buf.st_mtime);
3015 return time_point::min();
3020 stdex_assert(m_h != invalid_handle);
3024 if (SetFileTime(m_h, &ft,
nullptr,
nullptr))
3026 throw std::system_error(GetLastError(), std::system_category(),
"SetFileTime failed");
3028 _Unreferenced_(date);
3029 throw std::runtime_error(
"not supported");
3035 stdex_assert(m_h != invalid_handle);
3039 if (SetFileTime(m_h,
nullptr, &ft,
nullptr))
3041 throw std::system_error(GetLastError(), std::system_category(),
"SetFileTime failed");
3043 struct timespec ts[2] = {
3044 { date.time_since_epoch().count(), 0 },
3047 if (futimens(m_h, ts) >= 0)
3049 throw std::system_error(errno, std::system_category(),
"futimens failed");
3058 if (SetFileTime(m_h,
nullptr,
nullptr, &ft))
3060 throw std::system_error(GetLastError(), std::system_category(),
"SetFileTime failed");
3062 struct timespec ts[2] = {
3064 { date.time_since_epoch().count(), 0 },
3066 if (futimens(m_h, ts) >= 0)
3068 throw std::system_error(errno, std::system_category(),
"futimens failed");
3077 static bool exists(_In_z_
const stdex::schar_t* filename)
3080 return GetFileAttributes(filename) != INVALID_FILE_ATTRIBUTES;
3083 return stat(filename, &s) == 0;
3092 template <
class TR = std::
char_traits<s
char_t>,
class AX = std::allocator<s
char_t>>
3093 static bool exists(_In_
const std::basic_string<TR, AX>& filename)
3095 return exists(filename.c_str());
3105 static bool readonly(_In_z_
const stdex::schar_t* filename)
3108 DWORD dwAttr = GetFileAttributes(filename);
3109 return dwAttr != INVALID_FILE_ATTRIBUTES && (dwAttr & FILE_ATTRIBUTE_READONLY) != 0;
3112 return stat(filename, &s) == 0 && (s.st_mode & (S_IWUSR|S_IWGRP|S_IWOTH)) == 0;
3123 template <
class TR = std::
char_traits<s
char_t>,
class AX = std::allocator<s
char_t>>
3124 static bool readonly(_In_
const std::basic_string<TR, AX>& filename)
3126 return readonly(filename.c_str());
3137 cached_file(_In_opt_ sys_handle h = invalid_handle, _In_ state_t state = state_t::ok, _In_
size_t cache_size = default_cache_size) :
3151 cached_file(_In_z_
const schar_t* filename, _In_
int mode, _In_
size_t cache_size = default_cache_size) :
3153 m_source(filename, mode & mode_for_writing ? mode | mode_for_reading : mode)
3165 template <
class TR = std::
char_traits<s
char_t>,
class AX = std::allocator<s
char_t>>
3166 cached_file(_In_
const std::basic_string<TR, AX>& filename, _In_
int mode, _In_
size_t cache_size = default_cache_size) :
cached_file(filename.c_str(), mode, cache_size) {}
3179 void open(_In_z_
const schar_t* filename, _In_
int mode)
3182 if (!ok()) _Unlikely_{
3183 m_state = state_t::fail;
3186 m_source.open(filename, mode & mode_for_writing ? mode | mode_for_reading : mode);
3187 if (m_source.ok()) {
3191 m_state = state_t::fail;
3200 template <
class TR = std::
char_traits<s
char_t>,
class AX = std::allocator<s
char_t>>
3201 void open(_In_
const std::basic_string<TR, AX>& filename, _In_
int mode)
3203 open(filename.c_str(), mode);
3209 operator bool() const noexcept {
return m_source; }
3229#if SET_FILE_OP_TIMES
3230 m_ctime = m_atime = m_mtime = time_point::now();
3242 m_data(reinterpret_cast<uint8_t*>(malloc(size))),
3249 m_state = state_t::fail;
3250 throw std::bad_alloc();
3252#if SET_FILE_OP_TIMES
3253 m_ctime = m_atime = m_mtime = time_point::now();
3266 memory_file(_Inout_
void* data, _In_
size_t size, _In_
size_t reserved, _In_
bool manage =
false, _In_ state_t state = state_t::ok) :
3268 m_data(reinterpret_cast<uint8_t*>(data)),
3271 m_reserved(reserved),
3274 stdex_assert(data || !size);
3275 stdex_assert(reserved >= size);
3276#if SET_FILE_OP_TIMES
3277 m_ctime = m_atime = m_mtime = time_point::now();
3289 memory_file(_Inout_
void* data, _In_
size_t size, _In_
bool manage =
false, _In_ state_t state = state_t::ok) :
3301 load(filename, mode);
3310 template <
class TR = std::
char_traits<s
char_t>,
class AX = std::allocator<s
char_t>>
3320 m_data(reinterpret_cast<uint8_t*>(malloc(other.m_size))),
3321 m_offset(other.m_offset),
3322 m_size(other.m_size),
3323 m_reserved(other.m_size),
3325#if SET_FILE_OP_TIMES
3326 , m_ctime(other.m_ctime)
3327 , m_atime(other.m_atime)
3328 , m_mtime(other.m_mtime)
3332 m_state = state_t::fail;
3333 throw std::bad_alloc();
3335 memcpy(m_data, other.m_data, other.m_size);
3345 if (
this != std::addressof(other)) {
3347 if (m_manage && m_data)
3349 m_data =
reinterpret_cast<uint8_t*
>(malloc(other.m_size));
3351 m_state = state_t::fail;
3352 throw std::bad_alloc();
3354 memcpy(m_data, other.m_data, other.m_size);
3355 m_offset = other.m_offset;
3356 m_size = other.m_size;
3357 m_reserved = other.m_size;
3359#if SET_FILE_OP_TIMES
3360 m_ctime = other.m_ctime;
3361 m_atime = other.m_atime;
3362 m_mtime = other.m_mtime;
3375 m_data(other.m_data),
3376 m_offset(other.m_offset),
3377 m_size(other.m_size),
3378 m_reserved(other.m_reserved),
3379 m_manage(other.m_manage)
3380#if SET_FILE_OP_TIMES
3381 , m_ctime(other.m_ctime)
3382 , m_atime(other.m_atime)
3383 , m_mtime(other.m_mtime)
3386 other.m_state = state_t::ok;
3387 other.m_data =
nullptr;
3390 other.m_reserved = 0;
3391 other.m_manage =
true;
3392#if SET_FILE_OP_TIMES
3393 other.m_ctime = other.m_atime = other.m_mtime = time_point::now();
3404 if (
this != std::addressof(other)) {
3405 *
static_cast<basic_file*
>(
this) = std::move(other);
3406 if (m_manage && m_data)
3408 m_data = other.m_data;
3409 other.m_data =
nullptr;
3410 m_offset = other.m_offset;
3412 m_size = other.m_size;
3414 m_reserved = other.m_reserved;
3415 other.m_reserved = 0;
3416 m_manage = other.m_manage;
3417 other.m_manage =
true;
3418#if SET_FILE_OP_TIMES
3419 m_ctime = other.m_ctime;
3420 m_atime = other.m_atime;
3421 m_mtime = other.m_mtime;
3422 other.m_ctime = other.m_atime = other.m_mtime = time_point::now();
3430 if (m_manage && m_data)
3440 void reserve(_In_
size_t required, _In_
bool tight =
false) noexcept
3442 if (required <= m_reserved && (!tight || required >= m_reserved)) {
3443 m_state = state_t::ok;
3447 m_state = state_t::fail;
3450 size_t reserved = tight ? required : ((required + required / 4 + (default_block_size - 1)) / default_block_size) * default_block_size;
3451 auto data =
reinterpret_cast<uint8_t*
>(realloc(m_data, reserved));
3452 if (!data && reserved) _Unlikely_ {
3453 m_state = state_t::fail;
3457 if (reserved < m_size)
3459 m_reserved = reserved;
3460 m_state = state_t::ok;
3469 void load(_In_z_
const schar_t* filename, _In_
int mode)
3471 file f(filename, (mode & ~hint_random_access) | mode_for_reading | hint_sequential_access);
3473 m_state = state_t::fail;
3476 fsize_t size = f.
size();
3477 if (size > SIZE_MAX) {
3478 m_state = state_t::fail;
3481 reserve(
static_cast<size_t>(size),
true);
3482 if (!ok()) _Unlikely_ {
3485 m_offset = m_size = 0;
3489#if SET_FILE_OP_TIMES
3490 m_ctime = f.
ctime();
3491 m_atime = f.
atime();
3492 m_mtime = f.
mtime();
3502 template <
class TR = std::
char_traits<s
char_t>,
class AX = std::allocator<s
char_t>>
3503 void load(_In_
const std::basic_string<TR, AX>& filename, _In_
int mode)
3505 load(filename.c_str(), mode);
3514 void save(_In_z_
const schar_t* filename, _In_
int mode)
3516 file f(filename, (mode & ~hint_random_access) | mode_for_writing | hint_sequential_access);
3518 m_state = state_t::fail;
3521 f.
write(m_data, m_size);
3523 m_state = state_t::fail;
3527#if SET_FILE_OP_TIMES
3540 template <
class TR = std::
char_traits<s
char_t>,
class AX = std::allocator<s
char_t>>
3541 void save(_In_
const std::basic_string<TR, AX>& filename, _In_
int mode)
3543 save(filename.c_str(), mode);
3549 const void*
data()
const {
return m_data; }
3551 virtual _Success_(
return != 0 || length == 0) size_t read(
3552 _Out_writes_bytes_to_opt_(length, return)
void* data, _In_
size_t length)
3554 stdex_assert(data || !length);
3555#if SET_FILE_OP_TIMES
3556 m_atime = time_point::now();
3558 size_t available = m_size - m_offset;
3559 if (length <= available) {
3560 memcpy(data, &m_data[m_offset], length);
3562 m_state = state_t::ok;
3565 if (length && !available) {
3566 m_state = state_t::eof;
3569 memcpy(data, &m_data[m_offset], available);
3570 m_offset += available;
3571 m_state = state_t::ok;
3592#if SET_FILE_OP_TIMES
3593 m_atime = time_point::now();
3595 if (CHECK_STREAM_STATE && !ok()) _Unlikely_ {
3599 size_t end_offset = m_offset +
sizeof(T);
3600 if (end_offset <= m_size) {
3601 data = LE2HE(*
reinterpret_cast<T*
>(&m_data[m_offset]));
3602 m_offset = end_offset;
3603#if !CHECK_STREAM_STATE
3604 m_state = state_t::ok;
3610 m_state = state_t::eof;
3629 template<
class T,
class TR = std::
char_traits<T>,
class AX = std::allocator<T>>
3632#if SET_FILE_OP_TIMES
3633 m_atime = time_point::now();
3635 if (CHECK_STREAM_STATE && !ok()) _Unlikely_ {
3639 size_t end_offset = m_offset +
sizeof(uint32_t);
3640 if (end_offset <= m_size) {
3641 uint32_t num_chars = LE2HE(*
reinterpret_cast<uint32_t*
>(&m_data[m_offset]));
3642 m_offset = end_offset;
3643 end_offset = stdex::add(m_offset, stdex::mul(num_chars,
sizeof(T)));
3644 T* start =
reinterpret_cast<T*
>(&m_data[m_offset]);
3645 if (end_offset <= m_size) {
3646 data.assign(start, start + num_chars);
3647 m_offset = end_offset;
3648#if !CHECK_STREAM_STATE
3649 m_state = state_t::ok;
3653 if (end_offset <= m_size)
3654 data.assign(start,
reinterpret_cast<T*
>(&m_data[m_size]));
3657 m_state = state_t::eof;
3661 virtual _Success_(
return != 0) size_t write(
3662 _In_reads_bytes_opt_(length) const
void* data, _In_
size_t length)
3664 stdex_assert(data || !length);
3665#if SET_FILE_OP_TIMES
3666 m_atime = m_mtime = time_point::now();
3668 size_t end_offset = m_offset + length;
3669 if (end_offset > m_reserved) {
3670 reserve(end_offset);
3671 if (!ok()) _Unlikely_
3674 memcpy(&m_data[m_offset], data, length);
3675 m_offset = end_offset;
3676 if (m_offset > m_size)
3678 m_state = state_t::ok;
3687#if SET_FILE_OP_TIMES
3688 m_atime = m_mtime = time_point::now();
3690 size_t end_offset = m_offset + amount;
3691 if (end_offset > m_reserved) {
3692 reserve(end_offset);
3693 if (!ok()) _Unlikely_
3696 memset(&m_data[m_offset],
byte, amount);
3697 m_offset = end_offset;
3698 if (m_offset > m_size)
3700 m_state = state_t::ok;
3720#if SET_FILE_OP_TIMES
3721 m_atime = m_mtime = time_point::now();
3723 if (CHECK_STREAM_STATE && !ok()) _Unlikely_
3725 size_t end_offset = m_offset +
sizeof(T);
3726 if (end_offset > m_reserved) {
3727 reserve(end_offset);
3728 if (!ok()) _Unlikely_
3731 (*
reinterpret_cast<T*
>(&m_data[m_offset])) = HE2LE(data);
3732 m_offset = end_offset;
3733 if (m_offset > m_size)
3735#if !CHECK_STREAM_STATE
3736 m_state = state_t::ok;
3758#if SET_FILE_OP_TIMES
3759 m_atime = m_mtime = time_point::now();
3761 if (CHECK_STREAM_STATE && !ok()) _Unlikely_
3763 size_t num_chars = stdex::strlen(data);
3764 if (num_chars > UINT32_MAX)
3765 throw std::invalid_argument(
"string too long");
3766 size_t size_chars = num_chars *
sizeof(T);
3767 size_t size =
sizeof(uint32_t) + size_chars;
3768 size_t end_offset = m_offset + size;
3769 if (end_offset > m_reserved) {
3770 reserve(end_offset);
3771 if (!ok()) _Unlikely_
3774 auto p = &m_data[m_offset];
3775 *
reinterpret_cast<uint32_t*
>(p) = HE2LE((uint32_t)num_chars);
3776 memcpy(p +
sizeof(uint32_t), data, size_chars);
3777 m_offset = end_offset;
3778 if (m_offset > m_size)
3780#if !CHECK_STREAM_STATE
3781 m_state = state_t::ok;
3800 template<
class T,
class TR = std::
char_traits<T>,
class AX = std::allocator<T>>
3803#if SET_FILE_OP_TIMES
3804 m_atime = m_mtime = time_point::now();
3806 if (CHECK_STREAM_STATE && !ok()) _Unlikely_
3808 size_t num_chars = data.size();
3809 if (num_chars > UINT32_MAX)
3810 throw std::invalid_argument(
"string too long");
3811 size_t size_chars = num_chars *
sizeof(T);
3812 size_t size =
sizeof(uint32_t) + size_chars;
3813 size_t end_offset = m_offset + size;
3814 if (end_offset > m_reserved) {
3815 reserve(end_offset);
3816 if (!ok()) _Unlikely_
3819 auto p = &m_data[m_offset];
3820 *
reinterpret_cast<uint32_t*
>(p) = HE2LE((uint32_t)num_chars);
3821 memcpy(p +
sizeof(uint32_t), data.data(), size_chars);
3822 m_offset = end_offset;
3823 if (m_offset > m_size)
3825#if !CHECK_STREAM_STATE
3826 m_state = state_t::ok;
3838#if SET_FILE_OP_TIMES
3839 m_atime = m_mtime = time_point::now();
3841 size_t num_read, dst_offset = m_offset, dst_size = m_offset;
3842 size_t num_copied = 0, to_write = amount;
3843 m_state = state_t::ok;
3844 if (amount != SIZE_MAX) {
3845 dst_size = stdex::add(dst_size, amount);
3847 if (!ok()) _Unlikely_
3850 num_read = stream.read(&m_data[dst_offset], to_write);
3851 dst_offset += num_read;
3852 num_copied += num_read;
3853 to_write -= num_read;
3855 if (stream.state() != state_t::eof)
3856 m_state = state_t::fail;
3864 block_size = std::min(to_write, default_block_size);
3865 dst_size = stdex::add(dst_size, block_size);
3867 if (!ok()) _Unlikely_
3869 num_read = stream.read(&m_data[dst_offset], block_size);
3870 dst_size = dst_offset += num_read;
3871 num_copied += num_read;
3872 to_write -= num_read;
3874 if (stream.state() != state_t::eof)
3875 m_state = state_t::fail;
3880 m_offset = dst_offset;
3881 if (m_offset > m_size)
3888 if (m_manage && m_data)
3893 m_size = m_reserved = 0;
3894#if SET_FILE_OP_TIMES
3895 m_ctime = m_atime = m_mtime = time_point::min();
3897 m_state = state_t::ok;
3900 virtual fpos_t
seek(_In_ foff_t offset, _In_ seek_t how = seek_t::beg)
3903 case seek_t::beg:
break;
3904 case seek_t::cur: offset =
static_cast<foff_t
>(m_offset) + offset;
break;
3905 case seek_t::end: offset =
static_cast<foff_t
>(m_size) + offset;
break;
3906 default:
throw std::invalid_argument(
"unknown seek origin");
3908 if (offset < 0) _Unlikely_
3909 throw std::invalid_argument(
"negative file offset");
3910 if (
static_cast<fpos_t
>(offset) > SIZE_MAX) _Unlikely_
3911 throw std::invalid_argument(
"file offset too big");
3912 m_state = state_t::ok;
3913 return m_offset =
static_cast<size_t>(offset);
3928#if SET_FILE_OP_TIMES
3929 m_atime = m_mtime = time_point::now();
3935#if SET_FILE_OP_TIMES
3936 virtual time_point ctime()
const
3941 virtual time_point atime()
const
3946 virtual time_point mtime()
const
3951 virtual void set_ctime(time_point date)
3956 virtual void set_atime(time_point date)
3961 virtual void set_mtime(time_point date)
3976 void set(_In_ fpos_t offset, _In_
const T data)
3978#if SET_FILE_OP_TIMES
3979 m_atime = m_mtime = time_point::now();
3981 stdex_assert(offset +
sizeof(T) < m_size);
3982 (*
reinterpret_cast<T*
>(&m_data[offset])) = HE2LE(data);
3986 void set(_In_ fpos_t offset, _In_
const int8_t data) { set<int8_t>(offset, data); }
3987 void set(_In_ fpos_t offset, _In_
const int16_t data) { set<int16_t>(offset, data); }
3988 void set(_In_ fpos_t offset, _In_
const int32_t data) { set<int32_t>(offset, data); }
3989 void set(_In_ fpos_t offset, _In_
const int64_t data) { set<int64_t>(offset, data); }
3990 void set(_In_ fpos_t offset, _In_
const uint8_t data) { set<uint8_t>(offset, data); }
3991 void set(_In_ fpos_t offset, _In_
const uint16_t data) { set<uint16_t>(offset, data); }
3992 void set(_In_ fpos_t offset, _In_
const uint32_t data) { set<uint32_t>(offset, data); }
3993 void set(_In_ fpos_t offset, _In_
const uint64_t data) { set<uint64_t>(offset, data); }
3994 void set(_In_ fpos_t offset, _In_
const float data) { set<float>(offset, data); }
3995 void set(_In_ fpos_t offset, _In_
const double data) { set<double>(offset, data); }
3996 void set(_In_ fpos_t offset, _In_
const char data) { set<char>(offset, data); }
3997#ifdef _NATIVE_WCHAR_T_DEFINED
3998 void set(_In_ fpos_t offset, _In_
const wchar_t data) { set<wchar_t>(offset, data); }
4010 void get(_In_ fpos_t offset, _Out_ T & data)
4012 stdex_assert(offset +
sizeof(T) < m_size);
4013 data = LE2HE(*(T*)(&m_data[offset]));
4014#if SET_FILE_OP_TIMES
4015 m_atime = time_point::now();
4020 void get(_In_ fpos_t offset, _Out_ int8_t & data) { get<int8_t>(offset, data); }
4021 void get(_In_ fpos_t offset, _Out_ int16_t & data) { get<int16_t>(offset, data); }
4022 void get(_In_ fpos_t offset, _Out_ int32_t & data) { get<int32_t>(offset, data); }
4023 void get(_In_ fpos_t offset, _Out_ int64_t & data) { get<int64_t>(offset, data); }
4024 void get(_In_ fpos_t offset, _Out_ uint8_t & data) { get<uint8_t>(offset, data); }
4025 void get(_In_ fpos_t offset, _Out_ uint16_t & data) { get<uint16_t>(offset, data); }
4026 void get(_In_ fpos_t offset, _Out_ uint32_t & data) { get<uint32_t>(offset, data); }
4027 void get(_In_ fpos_t offset, _Out_ uint64_t & data) { get<uint64_t>(offset, data); }
4028 void get(_In_ fpos_t offset, _Out_
float& data) { get<float>(offset, data); }
4029 void get(_In_ fpos_t offset, _Out_
double& data) { get<double>(offset, data); }
4030 void get(_In_ fpos_t offset, _Out_
char& data) { get<char>(offset, data); }
4031#ifdef _NATIVE_WCHAR_T_DEFINED
4032 void get(_In_ fpos_t offset, _Out_
wchar_t& data) { get<wchar_t>(offset, data); }
4035 memory_file& operator <<(_In_
const int8_t data) {
return write_data(data); }
4036 memory_file& operator >>(_Out_ int8_t & data) {
return read_data(data); }
4037 memory_file& operator <<(_In_
const int16_t data) {
return write_data(data); }
4038 memory_file& operator >>(_Out_ int16_t & data) {
return read_data(data); }
4039 memory_file& operator <<(_In_
const int32_t data) {
return write_data(data); }
4040 memory_file& operator >>(_Out_ int32_t & data) {
return read_data(data); }
4041 memory_file& operator <<(_In_
const int64_t data) {
return write_data(data); }
4042 memory_file& operator >>(_Out_ int64_t & data) {
return read_data(data); }
4043 memory_file& operator <<(_In_
const uint8_t data) {
return write_data(data); }
4044 memory_file& operator >>(_Out_ uint8_t & data) {
return read_data(data); }
4045 memory_file& operator <<(_In_
const uint16_t data) {
return write_data(data); }
4046 memory_file& operator >>(_Out_ uint16_t & data) {
return read_data(data); }
4047 memory_file& operator <<(_In_
const uint32_t data) {
return write_data(data); }
4048 memory_file& operator >>(_Out_ uint32_t & data) {
return read_data(data); }
4049 memory_file& operator <<(_In_
const uint64_t data) {
return write_data(data); }
4050 memory_file& operator >>(_Out_ uint64_t & data) {
return read_data(data); }
4051 memory_file& operator <<(_In_
const float data) {
return write_data(data); }
4052 memory_file& operator >>(_Out_
float& data) {
return read_data(data); }
4053 memory_file& operator <<(_In_
const double data) {
return write_data(data); }
4054 memory_file& operator >>(_Out_
double& data) {
return read_data(data); }
4055 memory_file& operator <<(_In_
const char data) {
return write_data(data); }
4056 memory_file& operator >>(_Out_
char& data) {
return read_data(data); }
4057#ifdef _NATIVE_WCHAR_T_DEFINED
4058 memory_file& operator <<(_In_
const wchar_t data) {
return write_data(data); }
4059 memory_file& operator >>(_Out_
wchar_t& data) {
return read_data(data); }
4061 template<
class T,
class TR = std::
char_traits<T>,
class AX = std::allocator<T>>
4062 memory_file& operator >>(_Out_ std::basic_string<T, TR, AX>&data) {
return read_str(data); }
4064 memory_file& operator <<(_In_
const T * data) {
return write_str(data); }
4065 template<
class T,
class TR = std::
char_traits<T>,
class AX = std::allocator<T>>
4066 memory_file& operator <<(_In_
const std::basic_string<T, TR, AX>& data) {
return write_str(data); }
4074#if SET_FILE_OP_TIMES
4103#pragma warning(suppress: 6101)
4104 virtual _Success_(
return != 0 || length == 0) size_t read(
4105 _Out_writes_bytes_to_opt_(length, return)
void* data, _In_
size_t length)
4107 stdex_assert(data || !length);
4108 for (
size_t to_read = length;;) {
4109 if (!m_head) _Unlikely_ {
4110 m_state = to_read < length || !length ? state_t::ok : state_t::eof;
4111 return length - to_read;
4113 size_t remaining = m_head->size - m_offset;
4114 if (remaining > to_read) {
4115 memcpy(data, m_head->data + m_offset, to_read);
4116 m_offset += to_read;
4118 m_state = state_t::ok;
4121 memcpy(data, m_head->data + m_offset, remaining);
4123 m_size -= remaining;
4124 reinterpret_cast<uint8_t*&
>(data) += remaining;
4125 to_read -= remaining;
4132 virtual _Success_(
return != 0) size_t write(
4133 _In_reads_bytes_opt_(length) const
void* data, _In_
size_t length)
4135 stdex_assert(data || !length);
4137 std::unique_ptr<node_t> n(
reinterpret_cast<node_t*
>(
new uint8_t[
sizeof(
node_t) + length]));
4140 memcpy(n->data, data, length);
4143 m_tail = m_tail->next = n.release();
4145 m_head = m_tail = n.release();
4146 m_state = state_t::ok;
4149 catch (
const std::bad_alloc&) {
4150 m_state = state_t::fail;
4157 m_size = m_offset = 0;
4163 m_state = state_t::ok;
4169 size_t size()
const {
return m_size; };
4172 size_t m_offset, m_size;
4176#pragma warning(suppress:4200)
4178 } *m_head, * m_tail;
4187 basic(num_files ? files[0]->state() : state_t::fail),
4188 m_files(files, files + num_files)
4191 virtual _Success_(
return != 0 || length == 0) size_t read(
4192 _Out_writes_bytes_to_opt_(length, return)
void* data, _In_
size_t length)
4194 stdex_assert(data || !length);
4195 if (m_files.empty()) {
4196 m_state = state_t::fail;
4199 size_t result = m_files[0]->read(data, length);
4200 stdex_assert(result <= length);
4201 m_state = m_files[0]->state();
4202 if (length > m_tmp.size())
4203 m_tmp.resize(length);
4204 for (
size_t i = 1, n = m_files.size(); i < n; ++i) {
4205 if (m_files[i]->read(m_tmp.data(), length) != result ||
4206 memcmp(m_tmp.data(), data, result))
4207 throw std::runtime_error(
"read mismatch");
4208 if (m_files[i]->state() != m_state)
4209 throw std::runtime_error(
"state mismatch");
4214 virtual _Success_(
return != 0) size_t write(
4215 _In_reads_bytes_opt_(length) const
void* data, _In_
size_t length)
4217 if (m_files.empty()) {
4218 m_state = state_t::fail;
4221 size_t result = m_files[0]->write(data, length);
4222 m_state = m_files[0]->state();
4223 for (
size_t i = 1, n = m_files.size(); i < n; ++i) {
4224 if (m_files[i]->write(data, length) != result)
4225 throw std::runtime_error(
"write mismatch");
4226 if (m_files[i]->state() != m_state)
4227 throw std::runtime_error(
"state mismatch");
4234 if (m_files.empty()) {
4235 m_state = state_t::ok;
4238 m_files[0]->flush();
4239 m_state = m_files[0]->state();
4240 for (
size_t i = 1, n = m_files.size(); i < n; ++i) {
4241 m_files[i]->flush();
4242 if (m_files[i]->state() != m_state)
4243 throw std::runtime_error(
"state mismatch");
4249 if (m_files.empty()) {
4250 m_state = state_t::ok;
4253 m_files[0]->close();
4254 m_state = m_files[0]->state();
4255 for (
size_t i = 1, n = m_files.size(); i < n; ++i) {
4256 m_files[i]->close();
4257 if (m_files[i]->state() != m_state)
4258 throw std::runtime_error(
"state mismatch");
4261 m_tmp.shrink_to_fit();
4264 virtual fpos_t
seek(_In_ foff_t offset, _In_ seek_t how = seek_t::beg)
4266 if (m_files.empty()) {
4267 m_state = state_t::fail;
4270 fpos_t result = m_files[0]->seek(offset, how);
4271 m_state = m_files[0]->state();
4272 for (
size_t i = 1, n = m_files.size(); i < n; ++i) {
4273 if (m_files[i]->seek(offset, how) != result)
4274 throw std::runtime_error(
"seek mismatch");
4275 if (m_files[i]->state() != m_state)
4276 throw std::runtime_error(
"state mismatch");
4283 if (m_files.empty())
4285 fpos_t result = m_files[0]->tell();
4286 for (
size_t i = 1, n = m_files.size(); i < n; ++i) {
4287 if (m_files[i]->tell() != result)
4288 throw std::runtime_error(
"tell mismatch");
4293 virtual void lock(_In_ fpos_t offset, _In_ fsize_t length)
4295 if (m_files.empty())
4296 m_state = state_t::fail;
4297 m_files[0]->lock(offset, length);
4298 m_state = m_files[0]->state();
4299 for (
size_t i = 1, n = m_files.size(); i < n; ++i) {
4300 m_files[i]->lock(offset, length);
4301 if (m_files[i]->state() != m_state)
4302 throw std::runtime_error(
"state mismatch");
4306 virtual void unlock(_In_ fpos_t offset, _In_ fsize_t length)
4308 if (m_files.empty())
4309 m_state = state_t::fail;
4310 m_files[0]->unlock(offset, length);
4311 m_state = m_files[0]->state();
4312 for (
size_t i = 1, n = m_files.size(); i < n; ++i) {
4313 m_files[i]->unlock(offset, length);
4314 if (m_files[i]->state() != m_state)
4315 throw std::runtime_error(
"state mismatch");
4321 if (m_files.empty())
4323 fsize_t result = m_files[0]->size();
4324 for (
size_t i = 1, n = m_files.size(); i < n; ++i) {
4325 if (m_files[i]->size() != result)
4326 throw std::runtime_error(
"size mismatch");
4333 if (m_files.empty())
4334 m_state = state_t::fail;
4335 m_files[0]->truncate();
4336 m_state = m_files[0]->state();
4337 for (
size_t i = 1, n = m_files.size(); i < n; ++i) {
4338 m_files[i]->truncate();
4339 if (m_files[i]->state() != m_state)
4340 throw std::runtime_error(
"state mismatch");
4345 std::vector<basic_file*> m_files;
4346 std::vector<uint8_t> m_tmp;
4351#if defined(__GNUC__)
4352#pragma GCC diagnostic pop
Operating system object base class.
Definition system.hpp:142
Encoding converter context.
Definition unicode.hpp:139
locale_t helper class to free_locale when going out of scope.
Definition locale.hpp:74
Provides read-ahead stream capability.
Definition stream.hpp:1254
Provides write-back stream capability.
Definition stream.hpp:1321
virtual void flush()
Persists volatile element data.
Definition stream.hpp:1358
Basic seekable stream operations.
Definition stream.hpp:815
virtual void skip(fsize_t amount)
Skips given amount of bytes of data on the stream.
Definition stream.hpp:861
virtual time_point ctime() const
Returns file creation time.
Definition stream.hpp:910
virtual void lock(fpos_t offset, fsize_t length)
Locks file section for exclusive access.
Definition stream.hpp:879
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 text-file charset based on UTF-32, UTF-16 or UTF-8 BOM.
Definition stream.hpp:985
fpos_t seekbeg(fpos_t offset)
Seeks to absolute file position.
Definition stream.hpp:842
virtual std::vector< uint8_t > read_remainder(size_t max_length=SIZE_MAX)
Reads and returns remainder of the stream.
Definition stream.hpp:817
virtual void set_mtime(time_point date)
Sets file modification time.
Definition stream.hpp:952
fpos_t seekcur(foff_t offset)
Seeks to relative from current file position.
Definition stream.hpp:852
virtual time_point atime() const
Returns file access time.
Definition stream.hpp:918
virtual void set_ctime(time_point date)
Sets file create time.
Definition stream.hpp:934
virtual void unlock(fpos_t offset, fsize_t length)
Unlocks file section for exclusive access.
Definition stream.hpp:889
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:926
fpos_t seekend(foff_t offset)
Seeks to relative from end file position.
Definition stream.hpp:859
virtual void set_atime(time_point date)
Sets file access time.
Definition stream.hpp:943
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:2209
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:2266
virtual void flush()
Persists volatile element data.
Definition stream.hpp:2324
virtual void close()
Closes the stream.
Definition stream.hpp:2313
Basic stream operations.
Definition stream.hpp:85
size_t write_array(const T_from *str, charset_encoder< T_from, T_to > &encoder)
Writes array of characters to the stream.
Definition stream.hpp:408
bool ok() const
Returns true if the stream state is clean i.e. previous operation was successful.
Definition stream.hpp:181
size_t readln_and_attach(std::basic_string< T, TR, AX > &str)
Reads stream to the end-of-line or end-of-file and append to str.
Definition stream.hpp:340
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:628
state_t state() const
Returns stream state after last operation.
Definition stream.hpp:176
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:614
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:641
virtual void flush()
Persists volatile element data.
Definition stream.hpp:132
virtual void skip(fsize_t amount)
Skips given amount of bytes of data on the stream.
Definition stream.hpp:148
virtual void close()
Closes the stream.
Definition stream.hpp:140
uint8_t read_byte()
Reads one byte of data.
Definition stream.hpp:216
virtual std::vector< uint8_t > read_remainder(size_t max_length=SIZE_MAX)
Reads and returns remainder of the stream.
Definition stream.hpp:190
size_t write_sprintf(_Printf_format_string_params_(2) const char *format, locale_t locale,...)
Writes formatted string to the stream.
Definition stream.hpp:600
size_t readln(std::basic_string< T_to, TR, AX > &str, charset_encoder< T_from, T_to > &encoder)
Reads stream to the end-of-line or end-of-file.
Definition stream.hpp:324
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:376
size_t readln(std::basic_string< T, TR, AX > &str)
Reads stream to the end-of-line or end-of-file.
Definition stream.hpp:312
basic & read_str(std::basic_string< T, TR, AX > &data)
Reads length-prefixed string from the stream.
Definition stream.hpp:471
basic & write_str(const T *data)
Writes string to the stream length-prefixed.
Definition stream.hpp:504
size_t readln_and_attach(std::basic_string< T_to, TR, AX > &str, 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:361
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:102
void write_charset(charset_id charset)
Writes UTF8, UTF-16 or UTF-32 byte-order-mark.
Definition stream.hpp:585
basic & write_data(const T data)
Writes one primitive data type.
Definition stream.hpp:293
fsize_t write_stream(basic &stream, fsize_t amount=fsize_max)
Writes content of another stream.
Definition stream.hpp:560
size_t write_array(const std::basic_string< T_from, TR, AX > &str, charset_encoder< T_from, T_to > &encoder)
Writes array of characters to the stream.
Definition stream.hpp:449
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:120
basic & write_str(const std::basic_string< T, TR, AX > &data)
Writes string to the stream length-prefixed.
Definition stream.hpp:529
size_t write_array(_In_reads_or_z_opt_(num_chars) const T_from *str, size_t num_chars, charset_encoder< T_from, T_to > &encoder)
Writes array of characters to the stream.
Definition stream.hpp:429
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:394
void write_byte(uint8_t byte, fsize_t amount=1)
Writes a byte of data.
Definition stream.hpp:227
basic & read_data(T &data)
Reads one primitive data type.
Definition stream.hpp:265
Buffered read/write stream.
Definition stream.hpp:1392
virtual void flush()
Persists volatile element data.
Definition stream.hpp:1501
Buffered OS data stream (file, pipe, socket...)
Definition stream.hpp:2338
Cached file.
Definition stream.hpp:1802
virtual time_point ctime() const
Returns file creation time.
Definition stream.hpp:2079
virtual void truncate()
Sets file size - truncates the remainder of file content from the current file position to the end of...
Definition stream.hpp:2058
virtual time_point atime() const
Returns file access time.
Definition stream.hpp:2084
virtual void unlock(fpos_t offset, fsize_t length)
Unlocks file section for exclusive access.
Definition stream.hpp:2045
virtual time_point mtime() const
Returns file modification time.
Definition stream.hpp:2093
virtual void close()
Closes the stream.
Definition stream.hpp:1988
virtual void set_mtime(time_point date)
Sets file modification time.
Definition stream.hpp:2115
virtual void lock(fpos_t offset, fsize_t length)
Locks file section for exclusive access.
Definition stream.hpp:2039
virtual void flush()
Persists volatile element data.
Definition stream.hpp:1997
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:2051
virtual void set_ctime(time_point date)
Sets file create time.
Definition stream.hpp:2102
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:2034
virtual void set_atime(time_point date)
Sets file access time.
Definition stream.hpp:2107
virtual fpos_t seek(foff_t offset, seek_t how=seek_t::beg)
Seeks to specified relative file position.
Definition stream.hpp:2008
Cached file-system file.
Definition stream.hpp:3135
void open(const std::basic_string< TR, AX > &filename, int mode)
Opens file.
Definition stream.hpp:3201
void open(const schar_t *filename, int mode)
Opens file.
Definition stream.hpp:3179
cached_file(const schar_t *filename, int mode, size_t cache_size=default_cache_size)
Opens file.
Definition stream.hpp:3151
cached_file(const std::basic_string< TR, AX > &filename, int mode, size_t cache_size=default_cache_size)
Opens file.
Definition stream.hpp:3166
Modifies data on the fly when reading from/writing to a source stream. Could also be used to modify r...
Definition stream.hpp:1020
virtual void flush()
Persists volatile element data.
Definition stream.hpp:1071
virtual void close()
Closes the stream.
Definition stream.hpp:1065
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:1049
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:1057
Compares multiple files to perform the same.
Definition stream.hpp:4184
virtual void truncate()
Sets file size - truncates the remainder of file content from the current file position to the end of...
Definition stream.hpp:4331
virtual void close()
Closes the stream.
Definition stream.hpp:4247
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:4319
virtual void lock(fpos_t offset, fsize_t length)
Locks file section for exclusive access.
Definition stream.hpp:4293
virtual void unlock(fpos_t offset, fsize_t length)
Unlocks file section for exclusive access.
Definition stream.hpp:4306
virtual fpos_t seek(foff_t offset, seek_t how=seek_t::beg)
Seeks to specified relative file position.
Definition stream.hpp:4264
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:4281
virtual void flush()
Persists volatile element data.
Definition stream.hpp:4232
In-memory FIFO queue.
Definition stream.hpp:4085
virtual void close()
Closes the stream.
Definition stream.hpp:4155
size_t size() const
Returns total size of pending data in the queue.
Definition stream.hpp:4169
Limits file reading/writing to a predefined window.
Definition stream.hpp:1692
virtual void truncate()
Sets file size - truncates the remainder of file content from the current file position to the end of...
Definition stream.hpp:1785
virtual void flush()
Persists volatile element data.
Definition stream.hpp:1735
virtual void skip(fsize_t amount)
Skips given amount of bytes of data on the stream.
Definition stream.hpp:1748
virtual fpos_t seek(foff_t offset, seek_t how=seek_t::beg)
Seeks to specified relative file position.
Definition stream.hpp:1741
virtual void lock(fpos_t offset, fsize_t length)
Locks file section for exclusive access.
Definition stream.hpp:1760
virtual void unlock(fpos_t offset, fsize_t length)
Unlocks file section for exclusive access.
Definition stream.hpp:1770
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:1754
virtual void close()
Closes the stream.
Definition stream.hpp:1729
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:1780
File-system file.
Definition stream.hpp:2716
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:2920
static bool readonly(const std::basic_string< TR, AX > &filename)
Checks if file/folder/symlink is read-only.
Definition stream.hpp:3124
virtual time_point mtime() const
Returns file modification time.
Definition stream.hpp:3004
virtual void unlock(fpos_t offset, fsize_t length)
Unlocks file section for exclusive access.
Definition stream.hpp:2891
file(const schar_t *filename, int mode)
Opens file.
Definition stream.hpp:2726
virtual void set_ctime(time_point date)
Sets file create time.
Definition stream.hpp:3018
static bool readonly(const stdex::schar_t *filename)
Checks if file/folder/symlink is read-only.
Definition stream.hpp:3105
virtual time_point atime() const
Returns file access time.
Definition stream.hpp:2990
static bool exists(const std::basic_string< TR, AX > &filename)
Checks if file/folder/symlink likely exists.
Definition stream.hpp:3093
void open(const schar_t *filename, int mode)
Opens file.
Definition stream.hpp:2746
virtual void set_mtime(time_point date)
Sets file modification time.
Definition stream.hpp:3053
virtual void set_atime(time_point date)
Sets file access time.
Definition stream.hpp:3033
virtual void lock(fpos_t offset, fsize_t length)
Locks file section for exclusive access.
Definition stream.hpp:2864
virtual void truncate()
Sets file size - truncates the remainder of file content from the current file position to the end of...
Definition stream.hpp:2940
void open(const std::basic_string< TR, AX > &filename, int mode)
Opens file.
Definition stream.hpp:2820
virtual time_point ctime() const
Returns file creation time.
Definition stream.hpp:2980
virtual fpos_t seek(foff_t offset, seek_t how=seek_t::beg)
Seeks to specified relative file position.
Definition stream.hpp:2825
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:2846
file(const std::basic_string< TR, AX > &filename, int mode)
Opens file.
Definition stream.hpp:2738
static bool exists(const stdex::schar_t *filename)
Checks if file/folder/symlink likely exists.
Definition stream.hpp:3077
Limits reading from/writing to stream to a predefined number of bytes.
Definition stream.hpp:1549
fsize_t read_limit
Number of bytes left that may be read from the stream.
Definition stream.hpp:1601
fsize_t write_limit
Number of bytes left, that can be written to the stream.
Definition stream.hpp:1602
In-memory file.
Definition stream.hpp:3219
void save(const std::basic_string< TR, AX > &filename, int mode)
Saves content to a file-system file.
Definition stream.hpp:3541
memory_file & operator=(memory_file &&other) noexcept
Moves content from another file.
Definition stream.hpp:3402
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:3921
memory_file(const schar_t *filename, int mode)
Loads content from file-system file.
Definition stream.hpp:3299
memory_file & read_str(std::basic_string< T, TR, AX > &data)
Reads length-prefixed string from the stream.
Definition stream.hpp:3630
memory_file(const memory_file &other)
Copies content from another file.
Definition stream.hpp:3318
memory_file & write_str(const std::basic_string< T, TR, AX > &data)
Writes string to the stream length-prefixed.
Definition stream.hpp:3801
void load(const std::basic_string< TR, AX > &filename, int mode)
Loads content from a file-system file.
Definition stream.hpp:3503
size_t m_size
file size
Definition stream.hpp:4072
void get(fpos_t offset, T &data)
Reads data from specified file location This does not move file pointer. It checks for data size stde...
Definition stream.hpp:4010
size_t write_stream(basic &stream, size_t amount=SIZE_MAX)
Writes content of another stream.
Definition stream.hpp:3836
uint8_t * m_data
file data
Definition stream.hpp:4069
memory_file & read_data(T &data)
Reads one primitive data type.
Definition stream.hpp:3590
virtual void close()
Closes the stream.
Definition stream.hpp:3886
memory_file(const std::basic_string< TR, AX > &filename, int mode)
Loads content from file-system file.
Definition stream.hpp:3311
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:3916
size_t m_reserved
reserved file size
Definition stream.hpp:4073
memory_file(size_t size, state_t state=state_t::ok)
Creates an empty file of reserved size.
Definition stream.hpp:3240
void reserve(size_t required, bool tight=false) noexcept
Reallocates memory.
Definition stream.hpp:3440
memory_file(memory_file &&other) noexcept
Moves content from another file.
Definition stream.hpp:3373
void write_byte(uint8_t byte, size_t amount=1)
Writes a byte of data.
Definition stream.hpp:3685
memory_file & operator=(const memory_file &other)
Copies content from another file.
Definition stream.hpp:3343
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:3976
size_t m_offset
file pointer
Definition stream.hpp:4071
void save(const schar_t *filename, int mode)
Saves content to a file-system file.
Definition stream.hpp:3514
void load(const schar_t *filename, int mode)
Loads content from a file-system file.
Definition stream.hpp:3469
virtual fpos_t seek(foff_t offset, seek_t how=seek_t::beg)
Seeks to specified relative file position.
Definition stream.hpp:3900
virtual void truncate()
Sets file size - truncates the remainder of file content from the current file position to the end of...
Definition stream.hpp:3926
memory_file & write_data(const T data)
Writes one primitive data type.
Definition stream.hpp:3718
memory_file & write_str(const T *data)
Writes string to the stream length-prefixed.
Definition stream.hpp:3756
bool m_manage
may reallocate m_data?
Definition stream.hpp:4070
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:3289
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:3266
const void * data() const
Returns pointer to data.
Definition stream.hpp:3549
Definition stream.hpp:1168
size_t num_written
Number of bytes written.
Definition stream.hpp:1217
size_t length
Byte limit of data to write.
Definition stream.hpp:1216
const void * data
Data to write.
Definition stream.hpp:1215
Replicates writing of the same data to multiple streams.
Definition stream.hpp:1085
void push_back(basic *source)
Adds stream on the list.
Definition stream.hpp:1104
virtual void flush()
Persists volatile element data.
Definition stream.hpp:1161
void remove(basic *source)
Removes stream from the list.
Definition stream.hpp:1112
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:1129
virtual void close()
Closes the stream.
Definition stream.hpp:1156
Socket stream.
Definition stream.hpp:2360
socket_t get() const noexcept
Returns socket handle.
Definition stream.hpp:2416
virtual void close()
Closes the stream.
Definition stream.hpp:2474
socket(int af, int type, int protocol)
Creates a socket.
Definition stream.hpp:2395
Limits reading from/writing to stream to a predefined window.
Definition stream.hpp:1609
fpos_t write_offset
Number of bytes to discard on write.
Definition stream.hpp:1685
fpos_t read_offset
Number of bytes to skip on read.
Definition stream.hpp:1684
Numerical interval.
Definition interval.hpp:18
Definition stream.hpp:1526
Definition stream.hpp:4173