stdex
Additional custom or not Standard C++ covered algorithms
Loading...
Searching...
No Matches
stream.hpp
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 2023 Amebis
4*/
5
6#pragma once
7
8#include "compat.hpp"
9#include "endian.hpp"
10#include "interval.hpp"
11#include "locale.hpp"
12#include "math.hpp"
13#include "ring.hpp"
14#include "socket.hpp"
15#include "string.hpp"
16#include "unicode.hpp"
17#include <stdint.h>
18#include <stdlib.h>
19#if defined(_WIN32)
20#include "windows.h"
21#include <asptlb.h>
22#include <objidl.h>
23#else
24#include <fcntl.h>
25#include <unistd.h>
26#include <sys/stat.h>
27#endif
28#include <chrono>
29#include <condition_variable>
30#include <list>
31#include <memory>
32#include <set>
33#include <string>
34#include <thread>
35#include <vector>
36
37#if !defined(SET_FILE_OP_TIMES) && defined(RDAT_BELEZI_CAS_DOSTOPA_VER)
38#define SET_FILE_OP_TIMES 1
39#pragma message("RDAT_BELEZI_CAS_DOSTOPA_VER is deprecated. Use SET_FILE_OP_TIMES instead.")
40#elif !defined(SET_FILE_OP_TIMES)
41#define SET_FILE_OP_TIMES 0
42#endif
43#if !defined(CHECK_STREAM_STATE) && defined(RDAT_NE_PREVERJAJ_STANJA_VER)
44#define CHECK_STREAM_STATE 0
45#pragma message("RDAT_NE_PREVERJAJ_EOF_VER is deprecated. Use CHECK_STREAM_STATE=0 instead.")
46#else
47#define CHECK_STREAM_STATE 1
48#endif
49
50namespace stdex
51{
52 namespace stream
53 {
57 enum class state_t {
58 ok = 0,
59 eof,
60 fail,
61 };
62
66 using fsize_t = uint64_t;
67 constexpr fsize_t fsize_max = UINT64_MAX;
68
69 constexpr size_t iterate_count = 0x10;
70 constexpr size_t default_block_size = 0x10000;
71 constexpr char16_t utf16_bom = u'\ufeff';
72 constexpr char32_t utf32_bom = U'\ufeff';
73 constexpr const char utf8_bom[3] = { '\xef', '\xbb', '\xbf' };
74
78 class basic
79 {
80 public:
81 basic(_In_ state_t state = state_t::ok) : m_state(state) {}
82
83 virtual ~basic() noexcept(false) {}
84
96 virtual _Success_(return != 0 || length == 0) size_t read(
97 _Out_writes_bytes_to_opt_(length, return) void* data, _In_ size_t length)
98 {
99 _Unreferenced_(data);
100 _Unreferenced_(length);
101 m_state = state_t::fail;
102 return 0;
103 }
104
114 virtual _Success_(return != 0) size_t write(
115 _In_reads_bytes_opt_(length) const void* data, _In_ size_t length)
116 {
117 _Unreferenced_(data);
118 _Unreferenced_(length);
119 m_state = state_t::fail;
120 return 0;
121 }
122
126 virtual void flush()
127 {
128 m_state = state_t::ok;
129 }
130
134 virtual void close()
135 {
136 m_state = state_t::ok;
137 }
138
142 virtual void skip(_In_ fsize_t amount)
143 {
144 if (amount == 1)
145 read_byte();
146 else if (amount < iterate_count) {
147 for (size_t i = 0; i < static_cast<size_t>(amount); i++) {
148 read_byte();
149 if (!ok()) _Unlikely_
150 break;
151 }
152 }
153 else {
154 size_t block = static_cast<size_t>(std::min<fsize_t>(amount, default_block_size));
155 try {
156 std::unique_ptr<uint8_t[]> dummy(new uint8_t[block]);
157 while (amount) {
158 amount -= read_array(dummy.get(), sizeof(uint8_t), static_cast<size_t>(std::min<fsize_t>(amount, block)));
159 if (!ok()) _Unlikely_
160 break;
161 }
162 }
163 catch (const std::bad_alloc&) { m_state = state_t::fail; }
164 }
165 }
166
170 inline state_t state() const { return m_state; };
171
175 inline bool ok() const { return m_state == state_t::ok; };
176
184 virtual std::vector<uint8_t> read_remainder(_In_ size_t max_length = SIZE_MAX)
185 {
186 std::vector<uint8_t> result;
187 size_t offset, length;
188 offset = 0;
189 length = default_block_size;
190 while (offset < max_length) {
191 length = std::min(length, max_length);
192 try { result.resize(length); }
193 catch (const std::bad_alloc&) {
194 m_state = state_t::fail;
195 return result;
196 }
197 auto num_read = read_array(result.data() + offset, sizeof(uint8_t), length - offset);
198 offset += num_read;
199 if (!ok()) _Unlikely_
200 break;
201 length += default_block_size;
202 }
203 result.resize(offset);
204 return result;
205 }
206
210 inline uint8_t read_byte()
211 {
212 uint8_t byte;
213 if (read_array(&byte, sizeof(byte), 1) == 1)
214 return byte;
215 throw std::system_error(sys_error(), std::system_category(), "failed to read");
216 }
217
221 void write_byte(_In_ uint8_t byte, _In_ fsize_t amount = 1)
222 {
223 if (amount == 1)
224 write(&byte, sizeof(uint8_t));
225 else if (amount < iterate_count) {
226 for (size_t i = 0; i < static_cast<size_t>(amount); i++) {
227 write(&byte, sizeof(uint8_t));
228 if (!ok()) _Unlikely_
229 break;
230 }
231 }
232 else {
233 size_t block = static_cast<size_t>(std::min<fsize_t>(amount, default_block_size));
234 try {
235 std::unique_ptr<uint8_t[]> dummy(new uint8_t[block]);
236 memset(dummy.get(), byte, block);
237 while (amount) {
238 amount -= write_array(dummy.get(), sizeof(uint8_t), static_cast<size_t>(std::min<fsize_t>(amount, block)));
239 if (!ok()) _Unlikely_
240 break;
241 }
242 }
243 catch (const std::bad_alloc&) { m_state = state_t::fail; }
244 }
245 }
246
258 template <class T>
259 inline basic& read_data(_Out_ T& data)
260 {
261 if (!ok()) _Unlikely_ {
262 data = 0;
263 return *this;
264 }
265 if (read_array(&data, sizeof(T), 1) == 1)
266 (void)LE2HE(&data);
267 else {
268 data = 0;
269 if (ok())
270 m_state = state_t::eof;
271 }
272 return *this;
273 }
274
286 template <class T>
287 inline basic& write_data(_In_ const T data)
288 {
289 if (!ok()) _Unlikely_
290 return *this;
291#if BYTE_ORDER == BIG_ENDIAN
292 T data_le = HE2LE(data);
293 write(&data_le, sizeof(T));
294#else
295 write(&data, sizeof(T));
296#endif
297 return *this;
298 }
299
305 template<class _Traits = std::char_traits<char>, class _Ax = std::allocator<char>>
306 inline size_t readln(_Inout_ std::basic_string<char, _Traits, _Ax>& str)
307 {
308 str.clear();
309 return readln_and_attach(str);
310 }
311
317 template<class _Traits = std::char_traits<wchar_t>, class _Ax = std::allocator<wchar_t>>
318 inline size_t readln(_Inout_ std::basic_string<wchar_t, _Traits, _Ax>& wstr)
319 {
320 wstr.clear();
321 return readln_and_attach(wstr);
322 }
323
329 template<class T_from, class T_to, class _Traits = std::char_traits<T_to>, class _Ax = std::allocator<T_to>>
330 size_t readln(_Inout_ std::basic_string<T_to, _Traits, _Ax>& wstr, _In_ charset_encoder<T_from, T_to>& encoder)
331 {
332 if (encoder.from_encoding() == encoder.to_encoding())
333 return readln(wstr);
334 std::string str;
336 encoder.strcpy(wstr, str);
337 return wstr.size();
338 }
339
345 template<class _Elem, class _Traits = std::char_traits<_Elem>, class _Ax = std::allocator<_Elem>>
346 size_t readln_and_attach(_Inout_ std::basic_string<_Elem, _Traits, _Ax>& str)
347 {
348 bool initial = true;
349 _Elem chr, previous = (_Elem)0;
350 do {
351 read_array(&chr, sizeof(_Elem), 1);
352 if (!initial && !(previous == static_cast<_Elem>('\r') && chr == static_cast<_Elem>('\n')))
353 str += previous;
354 else
355 initial = false;
356 previous = chr;
357 } while (ok() && chr != static_cast<_Elem>('\n'));
358 return str.size();
359 }
360
366 template<class T_from, class T_to, class _Traits = std::char_traits<T_to>, class _Ax = std::allocator<T_to>>
367 size_t readln_and_attach(_Inout_ std::basic_string<T_to, _Traits, _Ax>& wstr, _In_ charset_encoder<T_from, T_to>& encoder)
368 {
369 if (encoder.from_encoding() == encoder.to_encoding())
370 return readln_and_attach(wstr);
371 std::string str;
373 encoder.strcat(wstr, str);
374 return wstr.size();
375 }
376
382 size_t read_array(_Out_writes_bytes_(size* count) void* array, _In_ size_t size, _In_ size_t count)
383 {
384 for (size_t to_read = mul(size, count);;) {
385 size_t num_read = read(array, to_read);
386 to_read -= num_read;
387 if (!to_read)
388 return count;
389 if (!ok()) _Unlikely_
390 return count - to_read / size;
391 reinterpret_cast<uint8_t*&>(array) += num_read;
392 }
393 }
394
400 inline size_t write_array(_In_reads_bytes_opt_(size* count) const void* array, _In_ size_t size, _In_ size_t count)
401 {
402 return write(array, mul(size, count)) / size;
403 }
404
413 template <class T_from, class T_to>
414 size_t write_array(_In_z_ const T_from* wstr, _In_ charset_encoder<T_from, T_to>& encoder)
415 {
416 if (!ok()) _Unlikely_
417 return 0;
418 size_t num_chars = stdex::strlen(wstr);
419 if (encoder.from_encoding() == encoder.to_encoding())
420 return write_array(wstr, sizeof(T_from), num_chars);
421 std::basic_string<T_to> str(encoder.convert(wstr, num_chars));
422 return write_array(str.data(), sizeof(T_to), str.size());
423 }
424
434 template <class T_from, class T_to>
435 size_t write_array(_In_reads_or_z_opt_(num_chars) const T_from* wstr, _In_ size_t num_chars, _In_ charset_encoder<T_from, T_to>& encoder)
436 {
437 if (!ok()) _Unlikely_
438 return 0;
439 num_chars = stdex::strnlen(wstr, num_chars);
440 if (encoder.from_encoding() == encoder.to_encoding())
441 return write_array(wstr, sizeof(T_from), num_chars);
442 std::basic_string<T_to> str(encoder.convert(wstr, num_chars));
443 return write_array(str.data(), sizeof(T_to), str.size());
444 }
445
454 template<class T_from, class T_to, class _Traits = std::char_traits<T_from>, class _Ax = std::allocator<T_from>>
455 size_t write_array(_In_ const std::basic_string<T_from, _Traits, _Ax>& wstr, _In_ charset_encoder<T_from, T_to>& encoder)
456 {
457 if (!ok()) _Unlikely_
458 return 0;
459 if (encoder.from_encoding() == encoder.to_encoding())
460 return write_array(wstr.data(), sizeof(T_from), wstr.size());
461 std::basic_string<T_to> str(encoder.convert(wstr));
462 return write_array(str.data(), sizeof(T_to), str.size());
463 }
464
476 template<class _Elem, class _Traits = std::char_traits<_Elem>, class _Ax = std::allocator<_Elem>>
477 inline basic& read_str(_Out_ std::basic_string<_Elem, _Traits, _Ax>& data)
478 {
479 data.clear();
480 if (!ok()) _Unlikely_
481 return *this;
482 uint32_t num_chars;
483 read_data(num_chars);
484 if (!ok()) _Unlikely_
485 return *this;
486 data.reserve(num_chars);
487 for (;;) {
488 _Elem buf[0x400];
489 uint32_t num_read = static_cast<uint32_t>(read_array(buf, sizeof(_Elem), std::min<uint32_t>(num_chars, _countof(buf))));
490 data.append(buf, buf + num_read);
491 num_chars -= num_read;
492 if (!num_chars || !ok())
493 return *this;
494 }
495 }
496
508 template <class T>
509 inline basic& write_str(_In_z_ const T* data)
510 {
511 // Stream state will be checked in write_data.
512 size_t num_chars = stdex::strlen(data);
513 if (num_chars > UINT32_MAX)
514 throw std::invalid_argument("string too long");
515 write_data(static_cast<uint32_t>(num_chars));
516 if (!ok()) _Unlikely_
517 return *this;
518 write_array(data, sizeof(T), num_chars);
519 return *this;
520 }
521
533 template<class _Elem, class _Traits = std::char_traits<_Elem>, class _Ax = std::allocator<_Elem>>
534 inline basic& write_str(_In_ const std::basic_string<_Elem, _Traits, _Ax>& data)
535 {
536 // Stream state will be checked in write_data.
537 size_t num_chars = data.size();
538 if (num_chars > UINT32_MAX)
539 throw std::invalid_argument("string too long");
540 write_data(static_cast<uint32_t>(num_chars));
541 if (!ok()) _Unlikely_
542 return *this;
543 write_array(data.data(), sizeof(_Elem), num_chars);
544 return *this;
545 }
546
547#ifdef _WIN32
553 size_t write_sa(_In_ LPSAFEARRAY sa)
554 {
555 safearray_accessor<void> a(sa);
556 long ubound, lbound;
557 if (FAILED(SafeArrayGetUBound(sa, 1, &ubound)) ||
558 FAILED(SafeArrayGetLBound(sa, 1, &lbound)))
559 throw std::invalid_argument("SafeArrayGet[UL]Bound failed");
560 return write(a.data(), static_cast<size_t>(ubound) - lbound + 1);
561 }
562#endif
563
569 fsize_t write_stream(_Inout_ basic& stream, _In_ fsize_t amount = fsize_max)
570 {
571 std::unique_ptr<uint8_t[]> data(new uint8_t[static_cast<size_t>(std::min<fsize_t>(amount, default_block_size))]);
572 fsize_t num_copied = 0, to_write = amount;
573 m_state = state_t::ok;
574 while (to_write) {
575 size_t num_read = stream.read(data.get(), static_cast<size_t>(std::min<fsize_t>(default_block_size, to_write)));
576 size_t num_written = write(data.get(), num_read);
577 num_copied += num_written;
578 to_write -= num_written;
579 if (stream.m_state == state_t::eof) {
580 // EOF is not an error.
581 m_state = state_t::ok;
582 break;
583 }
584 m_state = stream.m_state;
585 if (!ok())
586 break;
587 }
588 return num_copied;
589 }
590
594 void write_charset(_In_ charset_id charset)
595 {
596 if (charset == charset_id::utf32)
597 write_data(utf32_bom);
598 else if (charset == charset_id::utf16)
599 write_data(utf16_bom);
600 else if (charset == charset_id::utf8)
601 write_array(utf8_bom, sizeof(utf8_bom), 1);
602 }
603
609 size_t write_sprintf(_In_z_ _Printf_format_string_params_(2) const char* format, _In_opt_ locale_t locale, ...)
610 {
611 va_list params;
612 va_start(params, locale);
613 size_t num_chars = write_vsprintf(format, locale, params);
614 va_end(params);
615 return num_chars;
616 }
617
623 size_t write_sprintf(_In_z_ _Printf_format_string_params_(2) const wchar_t* format, _In_opt_ locale_t locale, ...)
624 {
625 va_list params;
626 va_start(params, locale);
627 size_t num_chars = write_vsprintf(format, locale, params);
628 va_end(params);
629 return num_chars;
630 }
631
637 size_t write_vsprintf(_In_z_ _Printf_format_string_params_(2) const char* format, _In_opt_ locale_t locale, _In_ va_list params)
638 {
639 std::string str;
640 str.reserve(default_block_size);
641 vappendf(str, format, locale, params);
642 return write_array(str.data(), sizeof(char), str.size());
643 }
644
650 size_t write_vsprintf(_In_z_ _Printf_format_string_params_(2) const wchar_t* format, _In_opt_ locale_t locale, _In_ va_list params)
651 {
652 std::wstring str;
653 str.reserve(default_block_size);
654 vappendf(str, format, locale, params);
655 return write_array(str.data(), sizeof(wchar_t), str.size());
656 }
657
658 inline basic& operator >>(_Out_ int8_t& data) { return read_data(data); }
659 inline basic& operator <<(_In_ const int8_t data) { return write_data(data); }
660 inline basic& operator >>(_Out_ int16_t& data) { return read_data(data); }
661 inline basic& operator <<(_In_ const int16_t data) { return write_data(data); }
662 inline basic& operator >>(_Out_ int32_t& data) { return read_data(data); }
663 inline basic& operator <<(_In_ const int32_t data) { return write_data(data); }
664 inline basic& operator >>(_Out_ int64_t& data) { return read_data(data); }
665 inline basic& operator <<(_In_ const int64_t data) { return write_data(data); }
666 inline basic& operator >>(_Out_ uint8_t& data) { return read_data(data); }
667 inline basic& operator <<(_In_ const uint8_t data) { return write_data(data); }
668 inline basic& operator >>(_Out_ uint16_t& data) { return read_data(data); }
669 inline basic& operator <<(_In_ const uint16_t data) { return write_data(data); }
670 inline basic& operator >>(_Out_ uint32_t& data) { return read_data(data); }
671 inline basic& operator <<(_In_ const uint32_t data) { return write_data(data); }
672 inline basic& operator >>(_Out_ uint64_t& data) { return read_data(data); }
673 inline basic& operator <<(_In_ const uint64_t data) { return write_data(data); }
674 inline basic& operator >>(_Out_ float& data) { return read_data(data); }
675 inline basic& operator <<(_In_ const float data) { return write_data(data); }
676 inline basic& operator >>(_Out_ double& data) { return read_data(data); }
677 inline basic& operator <<(_In_ const double data) { return write_data(data); }
678 inline basic& operator >>(_Out_ char& data) { return read_data(data); }
679 inline basic& operator <<(_In_ const char data) { return write_data(data); }
680#ifdef _NATIVE_WCHAR_T_DEFINED
681 inline basic& operator >>(_Out_ wchar_t& data) { return read_data(data); }
682 inline basic& operator <<(_In_ const wchar_t data) { return write_data(data); }
683#endif
684 template<class _Elem, class _Traits = std::char_traits<_Elem>, class _Ax = std::allocator<_Elem>>
685 inline basic& operator >>(_Out_ std::basic_string<_Elem, _Traits, _Ax>& data) { return read_str(data); }
686 template <class T>
687 inline basic& operator <<(_In_ const T* data) { return write_str(data); }
688 template<class _Elem, class _Traits = std::char_traits<_Elem>, class _Ax = std::allocator<_Elem>>
689 inline basic& operator <<(_In_ const std::basic_string<_Elem, _Traits, _Ax>& data) { return write_str(data); }
690
691 template <class _Ty, class _Alloc = std::allocator<_Ty>>
692 basic& operator <<(_In_ const std::vector<_Ty, _Alloc>& data)
693 {
694 size_t num = data.size();
695 if (num > UINT32_MAX) _Unlikely_
696 throw std::invalid_argument("collection too big");
697 *this << static_cast<uint32_t>(num);
698 for (auto& el : data)
699 *this << el;
700 return *this;
701 }
702
703 template <class _Ty, class _Alloc = std::allocator<_Ty>>
704 basic& operator >>(_Out_ std::vector<_Ty, _Alloc>& data)
705 {
706 data.clear();
707 uint32_t num;
708 *this >> num;
709 if (!ok()) _Unlikely_
710 return *this;
711 data.reserve(num);
712 for (uint32_t i = 0; i < num; ++i) {
713 _Ty el;
714 *this >> el;
715 if (!ok()) _Unlikely_
716 return *this;
717 data.push_back(std::move(el));
718 }
719 }
720
721 template <class _Kty, class _Pr = std::less<_Kty>, class _Alloc = std::allocator<_Kty>>
722 basic& operator <<(_In_ const std::set<_Kty, _Pr, _Alloc>& data)
723 {
724 size_t num = data.size();
725 if (num > UINT32_MAX) _Unlikely_
726 throw std::invalid_argument("collection too big");
727 *this << static_cast<uint32_t>(num);
728 for (auto& el : data)
729 *this << el;
730 return *this;
731 }
732
733 template <class _Kty, class _Pr = std::less<_Kty>, class _Alloc = std::allocator<_Kty>>
734 basic& operator >>(_Out_ std::set<_Kty, _Pr, _Alloc>& data)
735 {
736 data.clear();
737 uint32_t num;
738 *this >> num;
739 if (!ok()) _Unlikely_
740 return *this;
741 for (uint32_t i = 0; i < num; ++i) {
742 _Kty el;
743 *this >> el;
744 if (!ok()) _Unlikely_
745 return *this;
746 data.insert(std::move(el));
747 }
748 }
749
750 template <class _Kty, class _Pr = std::less<_Kty>, class _Alloc = std::allocator<_Kty>>
751 basic& operator <<(_In_ const std::multiset<_Kty, _Pr, _Alloc>& data)
752 {
753 size_t num = data.size();
754 if (num > UINT32_MAX) _Unlikely_
755 throw std::invalid_argument("collection too big");
756 *this << static_cast<uint32_t>(num);
757 for (auto& el : data)
758 *this << el;
759 return *this;
760 }
761
762 template <class _Kty, class _Pr = std::less<_Kty>, class _Alloc = std::allocator<_Kty>>
763 basic& operator >>(_Out_ std::multiset<_Kty, _Pr, _Alloc>& data)
764 {
765 data.clear();
766 uint32_t num;
767 *this >> num;
768 if (!ok()) _Unlikely_
769 return *this;
770 for (uint32_t i = 0; i < num; ++i) {
771 _Kty el;
772 *this >> el;
773 if (!ok()) _Unlikely_
774 return *this;
775 data.insert(std::move(el));
776 }
777 return *this;
778 }
779
780 protected:
781 state_t m_state;
782 };
783
787 using fpos_t = uint64_t;
788 constexpr fpos_t fpos_max = UINT64_MAX;
789 constexpr fpos_t fpos_min = 0;
790
794 using foff_t = int64_t;
795 constexpr foff_t foff_max = INT64_MAX;
796 constexpr foff_t foff_min = INT64_MIN;
797
801 enum class seek_t {
802#ifdef _WIN32
803 beg = FILE_BEGIN,
804 cur = FILE_CURRENT,
805 end = FILE_END
806#else
807 beg = SEEK_SET,
808 cur = SEEK_CUR,
809 end = SEEK_END
810#endif
811 };
812
813#if _HAS_CXX20
814 using clock = std::chrono::file_clock;
815#else
816 using clock = std::chrono::system_clock;
817#endif
818 using time_point = std::chrono::time_point<clock>;
819
823 class basic_file : virtual public basic
824 {
825 public:
826 virtual std::vector<uint8_t> read_remainder(_In_ size_t max_length = SIZE_MAX)
827 {
828 size_t length = std::min<size_t>(max_length, static_cast<size_t>(size() - tell()));
829 std::vector<uint8_t> result;
830 try { result.resize(length); }
831 catch (const std::bad_alloc&) {
832 m_state = state_t::fail;
833 return result;
834 }
835 result.resize(read_array(result.data(), sizeof(uint8_t), length));
836 return result;
837 }
838
844 virtual fpos_t seek(_In_ foff_t offset, _In_ seek_t how = seek_t::beg) = 0;
845
851 inline fpos_t seekbeg(_In_ fpos_t offset) { return seek(offset, seek_t::beg); }
852
858 inline fpos_t seekcur(_In_ foff_t offset) { return seek(offset, seek_t::cur); }
859
865 inline fpos_t seekend(_In_ foff_t offset) { return seek(offset, seek_t::end); }
866
867 virtual void skip(_In_ fsize_t amount)
868 {
869 seek(amount, seek_t::cur);
870 }
871
878 virtual fpos_t tell() const = 0;
879
883 virtual void lock(_In_ fpos_t offset, _In_ fsize_t length)
884 {
885 _Unreferenced_(offset);
886 _Unreferenced_(length);
887 throw std::domain_error("not implemented");
888 }
889
893 virtual void unlock(_In_ fpos_t offset, _In_ fsize_t length)
894 {
895 _Unreferenced_(offset);
896 _Unreferenced_(length);
897 throw std::domain_error("not implemented");
898 }
899
904 virtual fsize_t size() = 0;
905
909 virtual void truncate() = 0;
910
914 virtual time_point ctime() const
915 {
916 return time_point::min();
917 }
918
922 virtual time_point atime() const
923 {
924 return time_point::min();
925 }
926
930 virtual time_point mtime() const
931 {
932 return time_point::min();
933 }
934
938 virtual void set_ctime(time_point date)
939 {
940 _Unreferenced_(date);
941 throw std::domain_error("not implemented");
942 }
943
947 virtual void set_atime(time_point date)
948 {
949 _Unreferenced_(date);
950 throw std::domain_error("not implemented");
951 }
952
956 virtual void set_mtime(time_point date)
957 {
958 _Unreferenced_(date);
959 throw std::domain_error("not implemented");
960 }
961
962#ifdef _WIN32
966 LPSAFEARRAY read_sa()
967 {
968 _Assume_(size() <= SIZE_MAX);
969 size_t length = static_cast<size_t>(size());
970 std::unique_ptr<SAFEARRAY, SafeArrayDestroy_delete> sa(SafeArrayCreateVector(VT_UI1, 0, (ULONG)length));
971 if (!sa) _Unlikely_
972 throw std::runtime_error("SafeArrayCreateVector failed");
973 safearray_accessor<void> a(sa.get());
974 if (seek(0) != 0) _Unlikely_
975 throw std::system_error(sys_error(), std::system_category(), "failed to seek");
976 if (read_array(a.data(), 1, length) != length)
977 throw std::system_error(sys_error(), std::system_category(), "failed to read");
978 return sa.release();
979 }
980#endif
981
987 charset_id read_charset(_In_ charset_id default_charset = charset_id::system)
988 {
989 if (seek(0) != 0) _Unlikely_
990 throw std::system_error(sys_error(), std::system_category(), "failed to seek");
991 char32_t id_utf32;
992 read_array(&id_utf32, sizeof(char32_t), 1);
993 if (ok() && id_utf32 == utf32_bom)
994 return charset_id::utf32;
995
996 if (seek(0) != 0) _Unlikely_
997 throw std::system_error(sys_error(), std::system_category(), "failed to seek");
998 char16_t id_utf16;
999 read_array(&id_utf16, sizeof(char16_t), 1);
1000 if (ok() && id_utf16 == utf16_bom)
1001 return charset_id::utf16;
1002
1003 if (seek(0) != 0) _Unlikely_
1004 throw std::system_error(sys_error(), std::system_category(), "failed to seek");
1005 char id_utf8[3] = { 0 };
1006 read_array(id_utf8, sizeof(id_utf8), 1);
1007 if (ok() && strncmp(id_utf8, _countof(id_utf8), utf8_bom, _countof(utf8_bom)) == 0)
1008 return charset_id::utf8;
1009
1010 if (seek(0) != 0) _Unlikely_
1011 throw std::system_error(sys_error(), std::system_category(), "failed to seek");
1012 return default_charset;
1013 }
1014 };
1015
1021 class converter : public basic
1022 {
1023 protected:
1025#pragma warning(suppress: 26495) // The delayed init call will finish initializing the class.
1026 explicit converter() : basic(state_t::fail) {}
1027
1028 void init(_Inout_ basic& source)
1029 {
1030 m_source = &source;
1031 init();
1032 }
1033
1034 void init()
1035 {
1036 m_state = m_source->state();
1037 }
1038
1039 void done()
1040 {
1041 m_source = nullptr;
1042 }
1044
1045 public:
1046 converter(_Inout_ basic& source) :
1047 basic(source.state()),
1048 m_source(&source)
1049 {}
1050
1051 virtual _Success_(return != 0 || length == 0) size_t read(
1052 _Out_writes_bytes_to_opt_(length, return) void* data, _In_ size_t length)
1053 {
1054 size_t num_read = m_source->read(data, length);
1055 m_state = m_source->state();
1056 return num_read;
1057 }
1058
1059 virtual _Success_(return != 0) size_t write(
1060 _In_reads_bytes_opt_(length) const void* data, _In_ size_t length)
1061 {
1062 size_t num_written = m_source->write(data, length);
1063 m_state = m_source->state();
1064 return num_written;
1065 }
1066
1067 virtual void close()
1068 {
1069 m_source->close();
1070 m_state = m_source->state();
1071 }
1072
1073 virtual void flush()
1074 {
1075 m_source->flush();
1076 m_state = m_source->state();
1077 }
1078
1079 protected:
1080 basic* m_source;
1081 };
1082
1086 class replicator : public basic
1087 {
1088 public:
1089 virtual ~replicator()
1090 {
1091 for (auto w = m_workers.begin(), w_end = m_workers.end(); w != w_end; ++w) {
1092 auto _w = w->get();
1093 {
1094 const std::lock_guard<std::mutex> lk(_w->mutex);
1095 _w->op = worker::op_t::quit;
1096 }
1097 _w->cv.notify_one();
1098 }
1099 for (auto w = m_workers.begin(), w_end = m_workers.end(); w != w_end; ++w)
1100 w->get()->join();
1101 }
1102
1106 void push_back(_In_ basic* source)
1107 {
1108 m_workers.push_back(std::unique_ptr<worker>(new worker(source)));
1109 }
1110
1114 void remove(basic* source)
1115 {
1116 for (auto w = m_workers.begin(), w_end = m_workers.end(); w != w_end; ++w) {
1117 auto _w = w->get();
1118 if (_w->source == source) {
1119 {
1120 const std::lock_guard<std::mutex> lk(_w->mutex);
1121 _w->op = worker::op_t::quit;
1122 }
1123 _w->cv.notify_one();
1124 _w->join();
1125 m_workers.erase(w);
1126 return;
1127 }
1128 }
1129 }
1130
1131 virtual _Success_(return != 0) size_t write(
1132 _In_reads_bytes_opt_(length) const void* data, _In_ size_t length)
1133 {
1134 for (auto w = m_workers.begin(), w_end = m_workers.end(); w != w_end; ++w) {
1135 auto _w = w->get();
1136 {
1137 const std::lock_guard<std::mutex> lk(_w->mutex);
1138 _w->op = worker::op_t::write;
1139 _w->data = data;
1140 _w->length = length;
1141 }
1142 _w->cv.notify_one();
1143 }
1144 size_t num_written = length;
1145 m_state = state_t::ok;
1146 for (auto w = m_workers.begin(), w_end = m_workers.end(); w != w_end; ++w) {
1147 auto _w = w->get();
1148 std::unique_lock<std::mutex> lk(_w->mutex);
1149 _w->cv.wait(lk, [&] {return _w->op == worker::op_t::noop; });
1150 if (_w->num_written < num_written)
1151 num_written = _w->num_written;
1152 if (ok() && !_w->source->ok())
1153 m_state = _w->source->state();
1154 }
1155 return num_written;
1156 }
1157
1158 virtual void close()
1159 {
1160 foreach_worker(worker::op_t::close);
1161 }
1162
1163 virtual void flush()
1164 {
1165 foreach_worker(worker::op_t::flush);
1166 }
1167
1168 protected:
1169 class worker : public std::thread
1170 {
1171 public:
1172 worker(_In_ basic* _source) :
1173 source(_source),
1174 op(op_t::noop),
1175 data(nullptr),
1176 length(0),
1177 num_written(0)
1178 {
1179 *static_cast<std::thread*>(this) = std::thread([](_Inout_ worker& w) { w.process_op(); }, std::ref(*this));
1180 }
1181
1182 protected:
1183 void process_op()
1184 {
1185 for (;;) {
1186 std::unique_lock<std::mutex> lk(mutex);
1187 cv.wait(lk, [&] {return op != op_t::noop; });
1188 switch (op) {
1189 case op_t::quit:
1190 return;
1191 case op_t::write:
1192 num_written = source->write(data, length);
1193 break;
1194 case op_t::close:
1195 source->close();
1196 break;
1197 case op_t::flush:
1198 source->flush();
1199 break;
1200 case op_t::noop:;
1201 }
1202 op = op_t::noop;
1203 lk.unlock();
1204 cv.notify_one();
1205 }
1206 }
1207
1208 public:
1209 basic* source;
1210 enum class op_t {
1211 noop = 0,
1212 quit,
1213 write,
1214 close,
1215 flush,
1216 } op;
1217 const void* data;
1218 size_t length;
1220 std::mutex mutex;
1221 std::condition_variable cv;
1222 };
1223
1224 void foreach_worker(_In_ worker::op_t op)
1225 {
1226 for (auto w = m_workers.begin(), w_end = m_workers.end(); w != w_end; ++w) {
1227 auto _w = w->get();
1228 {
1229 const std::lock_guard<std::mutex> lk(_w->mutex);
1230 _w->op = op;
1231 }
1232 _w->cv.notify_one();
1233 }
1234 m_state = state_t::ok;
1235 for (auto w = m_workers.begin(), w_end = m_workers.end(); w != w_end; ++w) {
1236 auto _w = w->get();
1237 std::unique_lock<std::mutex> lk(_w->mutex);
1238 _w->cv.wait(lk, [&] {return _w->op == worker::op_t::noop; });
1239 if (ok())
1240 m_state = _w->source->state();
1241 }
1242 }
1243
1244 std::list<std::unique_ptr<worker>> m_workers;
1245 };
1246
1247 constexpr size_t default_async_limit = 0x100000;
1248
1254 template <size_t CAPACITY = default_async_limit>
1256 {
1257 public:
1258 async_reader(_Inout_ basic& source) :
1259 converter(source),
1260 m_worker([](_Inout_ async_reader& w) { w.process(); }, std::ref(*this))
1261 {}
1262
1263 virtual ~async_reader()
1264 {
1265 m_ring.quit();
1266 m_worker.join();
1267 }
1268
1269#pragma warning(suppress: 6101) // See [1] below
1270 virtual _Success_(return != 0 || length == 0) size_t read(
1271 _Out_writes_bytes_to_opt_(length, return) void* data, _In_ size_t length)
1272 {
1273 _Assume_(data || !length);
1274 for (size_t to_read = length;;) {
1275 uint8_t* ptr; size_t num_read;
1276 std::tie(ptr, num_read) = m_ring.front();
1277 if (!ptr) _Unlikely_ {
1278 m_state = to_read < length || !length ? state_t::ok : m_source->state();
1279 return length - to_read; // [1] Code analysis misses `length - to_read` bytes were written to data in previous loop iterations.
1280 }
1281 if (to_read < num_read)
1282 num_read = to_read;
1283 memcpy(data, ptr, num_read);
1284 m_ring.pop(num_read);
1285 to_read -= num_read;
1286 if (!to_read) {
1287 m_state = state_t::ok;
1288 return length;
1289 }
1290 reinterpret_cast<uint8_t*&>(data) += num_read;
1291 }
1292 }
1293
1294 protected:
1295 void process()
1296 {
1297 for (;;) {
1298 uint8_t* ptr; size_t num_write;
1299 std::tie(ptr, num_write) = m_ring.back();
1300 if (!ptr) _Unlikely_
1301 break;
1302 num_write = m_source->read(ptr, num_write);
1303 m_ring.push(num_write);
1304 if (!m_source->ok()) {
1305 m_ring.quit();
1306 break;
1307 }
1308 }
1309 }
1310
1311 protected:
1312 ring<uint8_t, CAPACITY> m_ring;
1313 std::thread m_worker;
1314 };
1315
1321 template <size_t CAPACITY = default_async_limit>
1323 {
1324 public:
1325 async_writer(_Inout_ basic& source) :
1326 converter(source),
1327 m_worker([](_Inout_ async_writer& w) { w.process(); }, std::ref(*this))
1328 {}
1329
1330 virtual ~async_writer()
1331 {
1332 m_ring.quit();
1333 m_worker.join();
1334 }
1335
1336 virtual _Success_(return != 0) size_t write(
1337 _In_reads_bytes_opt_(length) const void* data, _In_ size_t length)
1338 {
1339 _Assume_(data || !length);
1340 for (size_t to_write = length;;) {
1341 uint8_t* ptr; size_t num_write;
1342 std::tie(ptr, num_write) = m_ring.back();
1343 if (!ptr) _Unlikely_ {
1344 m_state = state_t::fail;
1345 return length - to_write;
1346 }
1347 if (to_write < num_write)
1348 num_write = to_write;
1349 memcpy(ptr, data, num_write);
1350 m_ring.push(num_write);
1351 to_write -= num_write;
1352 if (!to_write) {
1353 m_state = state_t::ok;
1354 return length;
1355 }
1356 reinterpret_cast<const uint8_t*&>(data) += num_write;
1357 }
1358 }
1359
1360 virtual void flush()
1361 {
1362 m_ring.sync();
1364 }
1365
1366 protected:
1367 void process()
1368 {
1369 for (;;) {
1370 uint8_t* ptr; size_t num_read;
1371 std::tie(ptr, num_read) = m_ring.front();
1372 if (!ptr)
1373 break;
1374 num_read = m_source->write(ptr, num_read);
1375 m_ring.pop(num_read);
1376 if (!m_source->ok()) {
1377 m_ring.quit();
1378 break;
1379 }
1380 }
1381 }
1382
1383 protected:
1384 ring<uint8_t, CAPACITY> m_ring;
1385 std::thread m_worker;
1386 };
1387
1388 constexpr size_t default_buffer_size = 0x400;
1389
1393 class buffer : public converter
1394 {
1395 protected:
1397 explicit buffer(_In_ size_t read_buffer_size = default_buffer_size, _In_ size_t write_buffer_size = default_buffer_size) :
1398 converter(),
1399 m_read_buffer(read_buffer_size),
1400 m_write_buffer(write_buffer_size)
1401 {}
1402
1403 void done()
1404 {
1405 if (m_source)
1406 flush_write();
1407 converter::done();
1408 }
1410
1411 public:
1412 buffer(_Inout_ basic& source, _In_ size_t read_buffer_size = default_buffer_size, _In_ size_t write_buffer_size = default_buffer_size) :
1413 converter(source),
1414 m_read_buffer(read_buffer_size),
1415 m_write_buffer(write_buffer_size)
1416 {}
1417
1418 virtual ~buffer()
1419 {
1420 if (m_source)
1421 flush_write();
1422 }
1423
1424 virtual _Success_(return != 0 || length == 0) size_t read(
1425 _Out_writes_bytes_to_opt_(length, return) void* data, _In_ size_t length)
1426 {
1427 _Assume_(data || !length);
1428 for (size_t to_read = length;;) {
1429 size_t buffer_size = m_read_buffer.tail - m_read_buffer.head;
1430 if (to_read <= buffer_size) {
1431 memcpy(data, m_read_buffer.data + m_read_buffer.head, to_read);
1432 m_read_buffer.head += to_read;
1433 m_state = state_t::ok;
1434 return length;
1435 }
1436 if (buffer_size) {
1437 memcpy(data, m_read_buffer.data + m_read_buffer.head, buffer_size);
1438 reinterpret_cast<uint8_t*&>(data) += buffer_size;
1439 to_read -= buffer_size;
1440 }
1441 m_read_buffer.head = 0;
1442 if (to_read > m_read_buffer.capacity) {
1443 // When needing to read more data than buffer capacity, bypass the buffer.
1444 m_read_buffer.tail = 0;
1445 to_read -= m_source->read(data, to_read);
1446 m_state = to_read < length ? state_t::ok : m_source->state();
1447 return length - to_read;
1448 }
1449 m_read_buffer.tail = m_source->read(m_read_buffer.data, m_read_buffer.capacity);
1450 if (m_read_buffer.tail < m_read_buffer.capacity && m_read_buffer.tail < to_read) _Unlikely_ {
1451 memcpy(data, m_read_buffer.data, m_read_buffer.tail);
1452 m_read_buffer.head = m_read_buffer.tail;
1453 to_read -= m_read_buffer.tail;
1454 m_state = to_read < length ? state_t::ok : m_source->state();
1455 return length - to_read;
1456 }
1457 }
1458 }
1459
1460 virtual _Success_(return != 0) size_t write(
1461 _In_reads_bytes_opt_(length) const void* data, _In_ size_t length)
1462 {
1463 _Assume_(data || !length);
1464 if (!length) _Unlikely_ {
1465 // Pass null writes (zero-byte length). Null write operations have special meaning with with Windows pipes.
1466 flush_write();
1467 if (!ok()) _Unlikely_
1468 return 0;
1469 converter::write(nullptr, 0);
1470 return 0;
1471 }
1472
1473 for (size_t to_write = length;;) {
1474 size_t available_buffer = m_write_buffer.capacity - m_write_buffer.tail;
1475 if (to_write <= available_buffer) {
1476 memcpy(m_write_buffer.data + m_write_buffer.tail, data, to_write);
1477 m_write_buffer.tail += to_write;
1478 m_state = state_t::ok;
1479 return length;
1480 }
1481 if (available_buffer) {
1482 memcpy(m_write_buffer.data + m_write_buffer.tail, data, available_buffer);
1483 reinterpret_cast<const uint8_t*&>(data) += available_buffer;
1484 to_write -= available_buffer;
1485 m_write_buffer.tail += available_buffer;
1486 }
1487 size_t buffer_size = m_write_buffer.tail - m_write_buffer.head;
1488 if (buffer_size) {
1489 m_write_buffer.head += converter::write(m_write_buffer.data + m_write_buffer.head, buffer_size);
1490 if (m_write_buffer.head == m_write_buffer.tail)
1491 m_write_buffer.head = m_write_buffer.tail = 0;
1492 else
1493 return length - to_write;
1494 }
1495 if (to_write > m_write_buffer.capacity) {
1496 // When needing to write more data than buffer capacity, bypass the buffer.
1497 to_write -= converter::write(data, to_write);
1498 return length - to_write;
1499 }
1500 }
1501 }
1502
1503 virtual void flush()
1504 {
1505 flush_write();
1506 if (ok())
1508 }
1509
1510 protected:
1511 void flush_write()
1512 {
1513 size_t buffer_size = m_write_buffer.tail - m_write_buffer.head;
1514 if (buffer_size) {
1515 m_write_buffer.head += m_source->write(m_write_buffer.data + m_write_buffer.head, buffer_size);
1516 if (m_write_buffer.head == m_write_buffer.tail) {
1517 m_write_buffer.head = 0;
1518 m_write_buffer.tail = 0;
1519 }
1520 else {
1521 m_state = m_source->state();
1522 return;
1523 }
1524 }
1525 m_state = state_t::ok;
1526 }
1527
1528 struct buffer_t {
1529 uint8_t* data;
1530 size_t head, tail, capacity;
1531
1532 buffer_t(_In_ size_t buffer_size) :
1533 head(0),
1534 tail(0),
1535 capacity(buffer_size),
1536 data(buffer_size ? new uint8_t[buffer_size] : nullptr)
1537 {}
1538
1539 ~buffer_t()
1540 {
1541 if (data)
1542 delete[] data;
1543 }
1544 } m_read_buffer, m_write_buffer;
1545 };
1546
1550 class limiter : public converter
1551 {
1552 public:
1553 limiter(_Inout_ basic& source, _In_ fsize_t _read_limit = 0, _In_ fsize_t _write_limit = 0) :
1554 converter(source),
1555 read_limit(_read_limit),
1556 write_limit(_write_limit)
1557 {}
1558
1559 virtual _Success_(return != 0 || length == 0) size_t read(
1560 _Out_writes_bytes_to_opt_(length, return) void* data, _In_ size_t length)
1561 {
1562 size_t num_read;
1563 if (read_limit == fsize_max)
1564 num_read = converter::read(data, length);
1565 else if (length <= read_limit) {
1566 num_read = converter::read(data, length);
1567 read_limit -= num_read;
1568 }
1569 else if (length && !read_limit) {
1570 num_read = 0;
1571 m_state = state_t::eof;
1572 }
1573 else {
1574 num_read = converter::read(data, static_cast<size_t>(read_limit));
1575 read_limit -= num_read;
1576 }
1577 return num_read;
1578 }
1579
1580 virtual _Success_(return != 0) size_t write(
1581 _In_reads_bytes_opt_(length) const void* data, _In_ size_t length)
1582 {
1583 size_t num_written;
1584 if (write_limit == fsize_max)
1585 num_written = converter::write(data, length);
1586 else if (length <= write_limit) {
1587 num_written = converter::write(data, length);
1588 write_limit -= num_written;
1589 }
1590 else if (length && !write_limit) {
1591 num_written = 0;
1592 m_state = state_t::fail;
1593 }
1594 else {
1595 num_written = converter::write(data, static_cast<size_t>(write_limit));
1596 write_limit -= num_written;
1597 }
1598 return num_written;
1599 }
1600
1601 public:
1602 fsize_t
1605 };
1606
1610 class window : public limiter
1611 {
1612 public:
1613 window(_Inout_ basic& source, _In_ fpos_t _read_offset = 0, _In_ fsize_t read_limit = fsize_max, _In_ fpos_t _write_offset = 0, _In_ fsize_t write_limit = fsize_max) :
1614 limiter(source, read_limit, write_limit),
1615 read_offset(_read_offset),
1616 write_offset(_write_offset)
1617 {}
1618
1619 virtual _Success_(return != 0 || length == 0) size_t read(
1620 _Out_writes_bytes_to_opt_(length, return) void* data, _In_ size_t length)
1621 {
1622 if (read_offset) {
1623 m_source->skip(read_offset);
1624 m_state = m_source->state();
1625 if (!ok()) _Unlikely_
1626 return 0;
1627 read_offset = 0;
1628 }
1629 size_t num_read;
1630 if (read_limit == fsize_max)
1631 num_read = converter::read(data, length);
1632 else if (length <= read_limit) {
1633 num_read = converter::read(data, length);
1634 read_limit -= num_read;
1635 }
1636 else if (length && !read_limit) {
1637 num_read = 0;
1638 m_source->skip(length);
1639 m_state = state_t::eof;
1640 }
1641 else {
1642 num_read = converter::read(data, static_cast<size_t>(read_limit));
1643 read_limit -= num_read;
1644 }
1645 return num_read;
1646 }
1647
1648 virtual _Success_(return != 0) size_t write(
1649 _In_reads_bytes_opt_(length) const void* data, _In_ size_t length)
1650 {
1651 size_t num_skipped, num_written;
1652 if (length <= write_offset) {
1653 write_offset -= length;
1654 m_state = state_t::ok;
1655 return length;
1656 }
1657 if (write_offset) {
1658 reinterpret_cast<const uint8_t*&>(data) += static_cast<size_t>(write_offset);
1659 length -= static_cast<size_t>(write_offset);
1660 num_skipped = static_cast<size_t>(write_offset);
1661 write_offset = 0;
1662 }
1663 else
1664 num_skipped = 0;
1665 if (write_limit == fsize_max)
1666 num_written = converter::write(data, length);
1667 else if (length <= write_limit) {
1668 num_written = converter::write(data, length);
1669 write_limit -= num_written;
1670 }
1671 else if (length && !write_limit) {
1672 num_skipped += length;
1673 num_written = 0;
1674 m_state = state_t::ok;
1675 }
1676 else {
1677 num_skipped += length - static_cast<size_t>(write_limit);
1678 num_written = converter::write(data, static_cast<size_t>(write_limit));
1679 write_limit -= num_written;
1680 }
1681 return num_skipped + num_written;
1682 }
1683
1684 public:
1685 fpos_t
1688 };
1689
1694 {
1695 public:
1696 file_window(_Inout_ basic_file& source, fpos_t offset = 0, fsize_t length = 0) :
1697 basic(source.state()),
1698 m_source(source),
1699 m_offset(source.tell()),
1700 m_region(offset, offset + length)
1701 {}
1702
1703 virtual _Success_(return != 0 || length == 0) size_t read(
1704 _Out_writes_bytes_to_opt_(length, return) void* data, _In_ size_t length)
1705 {
1706 _Assume_(data || !length);
1707 if (m_region.contains(m_offset)) {
1708 size_t num_read = m_source.read(data, static_cast<size_t>(std::min<fpos_t>(length, m_region.end - m_offset)));
1709 m_state = m_source.state();
1710 m_offset += num_read;
1711 return num_read;
1712 }
1713 m_state = length ? state_t::eof : state_t::ok;
1714 return 0;
1715 }
1716
1717 virtual _Success_(return != 0) size_t write(
1718 _In_reads_bytes_opt_(length) const void* data, _In_ size_t length)
1719 {
1720 _Assume_(data || !length);
1721 if (m_region.contains(m_offset)) {
1722 size_t num_written = m_source.write(data, static_cast<size_t>(std::min<fpos_t>(length, m_region.end - m_offset)));
1723 m_state = m_source.state();
1724 m_offset += num_written;
1725 return num_written;
1726 }
1727 m_state = state_t::fail;
1728 return 0;
1729 }
1730
1731 virtual void close()
1732 {
1733 m_source.close();
1734 m_state = m_source.state();
1735 }
1736
1737 virtual void flush()
1738 {
1739 m_source.flush();
1740 m_state = m_source.state();
1741 }
1742
1743 virtual fpos_t seek(_In_ foff_t offset, _In_ seek_t how = seek_t::beg)
1744 {
1745 m_offset = m_source.seek(offset, how);
1746 m_state = m_source.state();
1747 return ok() ? m_offset - m_region.start : fpos_max;
1748 }
1749
1750 virtual void skip(_In_ fsize_t amount)
1751 {
1752 m_source.skip(amount);
1753 m_state = m_source.state();
1754 }
1755
1756 virtual fpos_t tell() const
1757 {
1758 fpos_t offset = m_source.tell();
1759 return m_region.contains(offset) ? offset - m_region.start : fpos_max;
1760 }
1761
1762 virtual void lock(_In_ fpos_t offset, _In_ fsize_t length)
1763 {
1764 if (m_region.contains(offset)) {
1765 m_source.lock(m_region.start + offset, std::min<fsize_t>(length, m_region.end - offset));
1766 m_state = m_source.state();
1767 }
1768 else
1769 m_state = state_t::fail;
1770 }
1771
1772 virtual void unlock(_In_ fpos_t offset, _In_ fsize_t length)
1773 {
1774 if (m_region.contains(offset)) {
1775 m_source.unlock(m_region.start + offset, std::min<fsize_t>(length, m_region.end - offset));
1776 m_state = m_source.state();
1777 }
1778 else
1779 m_state = state_t::fail;
1780 }
1781
1782 virtual fsize_t size()
1783 {
1784 return m_region.size();
1785 }
1786
1787 virtual void truncate()
1788 {
1789 m_state = state_t::fail;
1790 }
1791
1792 protected:
1793 basic_file& m_source;
1794 fpos_t m_offset;
1795 interval<fpos_t> m_region;
1796 };
1797
1798 constexpr size_t default_cache_size = 0x1000;
1799
1803 class cache : public basic_file
1804 {
1805 protected:
1807#pragma warning(suppress: 26495) // The delayed init call will finish initializing the class.
1808 explicit cache(_In_ size_t cache_size = default_cache_size) :
1809 basic(state_t::fail),
1810 m_cache(cache_size)
1811 {}
1812
1813 void init(_Inout_ basic_file& source)
1814 {
1815 m_source = &source;
1816 init();
1817 }
1818
1819 void init()
1820 {
1821 m_state = m_source->state();
1822 m_offset = m_source->tell();
1823#if SET_FILE_OP_TIMES
1824 m_atime = m_source->atime();
1825 m_mtime = m_source->mtime();
1826#endif
1827 }
1828
1829 void done()
1830 {
1831 if (m_source) {
1832 flush_cache();
1833 if (!ok()) _Unlikely_
1834 throw std::system_error(sys_error(), std::system_category(), "failed to flush cache"); // Data loss occured
1835 m_source->seek(m_offset);
1836#if SET_FILE_OP_TIMES
1837 m_source->set_atime(m_atime);
1838 m_source->set_mtime(m_mtime);
1839#endif
1840 m_source = nullptr;
1841 }
1842 }
1844
1845 public:
1846 cache(_Inout_ basic_file& source, _In_ size_t cache_size = default_cache_size) :
1847 basic(source.state()),
1848 m_source(&source),
1849 m_cache(cache_size),
1850 m_offset(source.tell())
1851#if SET_FILE_OP_TIMES
1852 , m_atime(source.atime())
1853 , m_mtime(source.mtime())
1854#endif
1855 {}
1856
1857 virtual ~cache() noexcept(false)
1858 {
1859 if (m_source) {
1860 flush_cache();
1861 if (!ok()) _Unlikely_
1862 throw std::system_error(sys_error(), std::system_category(), "failed to flush cache"); // Data loss occured
1863 m_source->seek(m_offset);
1864#if SET_FILE_OP_TIMES
1865 m_source->set_atime(m_atime);
1866 m_source->set_mtime(m_mtime);
1867#endif
1868 }
1869 }
1870
1871 virtual _Success_(return != 0 || length == 0) size_t read(
1872 _Out_writes_bytes_to_opt_(length, return) void* data, _In_ size_t length)
1873 {
1874 _Assume_(data || !length);
1875#if SET_FILE_OP_TIMES
1876 m_atime = time_point::now();
1877#endif
1878 for (size_t to_read = length;;) {
1879 if (m_cache.status != cache_t::cache_t::status_t::empty) {
1880 if (m_cache.region.contains(m_offset)) {
1881 size_t remaining_cache = static_cast<size_t>(m_cache.region.end - m_offset);
1882 if (to_read <= remaining_cache) {
1883 memcpy(data, m_cache.data + static_cast<size_t>(m_offset - m_cache.region.start), to_read);
1884 m_offset += to_read;
1885 m_state = state_t::ok;
1886 return length;
1887 }
1888 memcpy(data, m_cache.data + static_cast<size_t>(m_offset - m_cache.region.start), remaining_cache);
1889 reinterpret_cast<uint8_t*&>(data) += remaining_cache;
1890 to_read -= remaining_cache;
1891 m_offset += remaining_cache;
1892 }
1893 flush_cache();
1894 if (!ok()) _Unlikely_ {
1895 if (to_read < length)
1896 m_state = state_t::ok;
1897 return length - to_read;
1898 }
1899 }
1900 {
1901 fpos_t end_max = m_offset + to_read;
1902 if (m_offset / m_cache.capacity < end_max / m_cache.capacity) {
1903 // Read spans multiple cache blocks. Bypass cache to the last block.
1904 m_source->seek(m_offset);
1905 if (!m_source->ok()) _Unlikely_ {
1906 m_state = to_read < length ? state_t::ok : state_t::fail;
1907 return length - to_read;
1908 }
1909 size_t num_read = m_source->read(data, to_read - static_cast<size_t>(end_max % m_cache.capacity));
1910 m_offset += num_read;
1911 to_read -= num_read;
1912 if (!to_read) {
1913 m_state = state_t::ok;
1914 return length;
1915 }
1916 reinterpret_cast<uint8_t*&>(data) += num_read;
1917 m_state = m_source->state();
1918 if (!ok()) {
1919 if (to_read < length)
1920 m_state = state_t::ok;
1921 return length - to_read;
1922 }
1923 }
1924 }
1925 load_cache(m_offset);
1926 if (!ok() || m_cache.region.end <= m_offset) _Unlikely_ {
1927 m_state = to_read < length ? state_t::ok : state_t::fail;
1928 return length - to_read;
1929 }
1930 }
1931 }
1932
1933 virtual _Success_(return != 0) size_t write(
1934 _In_reads_bytes_opt_(length) const void* data, _In_ size_t length)
1935 {
1936 _Assume_(data || !length);
1937#if SET_FILE_OP_TIMES
1938 m_atime = m_mtime = time_point::now();
1939#endif
1940 for (size_t to_write = length;;) {
1941 if (m_cache.status != cache_t::cache_t::status_t::empty) {
1942 fpos_t end_max = m_cache.region.start + m_cache.capacity;
1943 if (m_cache.region.start <= m_offset && m_offset < end_max) {
1944 size_t remaining_cache = static_cast<size_t>(end_max - m_offset);
1945 if (to_write <= remaining_cache) {
1946 memcpy(m_cache.data + static_cast<size_t>(m_offset - m_cache.region.start), data, to_write);
1947 m_offset += to_write;
1948 m_cache.status = cache_t::cache_t::status_t::dirty;
1949 m_cache.region.end = std::max(m_cache.region.end, m_offset);
1950 m_state = state_t::ok;
1951 return length;
1952 }
1953 memcpy(m_cache.data + static_cast<size_t>(m_offset - m_cache.region.start), data, remaining_cache);
1954 reinterpret_cast<const uint8_t*&>(data) += remaining_cache;
1955 to_write -= remaining_cache;
1956 m_offset += remaining_cache;
1957 m_cache.status = cache_t::cache_t::status_t::dirty;
1958 m_cache.region.end = end_max;
1959 }
1960 flush_cache();
1961 if (!ok()) _Unlikely_
1962 return length - to_write;
1963 }
1964 {
1965 fpos_t end_max = m_offset + to_write;
1966 if (m_offset / m_cache.capacity < end_max / m_cache.capacity) {
1967 // Write spans multiple cache blocks. Bypass cache to the last block.
1968 m_source->seek(m_offset);
1969 if (!ok()) _Unlikely_
1970 return length - to_write;
1971 size_t num_written = m_source->write(data, to_write - static_cast<size_t>(end_max % m_cache.capacity));
1972 m_offset += num_written;
1973 m_state = m_source->state();
1974 to_write -= num_written;
1975 if (!to_write || !ok())
1976 return length - to_write;
1977 reinterpret_cast<const uint8_t*&>(data) += num_written;
1978 }
1979 }
1980 load_cache(m_offset);
1981 if (!ok()) _Unlikely_
1982 return length - to_write;
1983 }
1984 }
1985
1986 virtual void close()
1987 {
1988 invalidate_cache();
1989 if (!ok()) _Unlikely_
1990 throw std::system_error(sys_error(), std::system_category(), "failed to flush cache"); // Data loss occured
1991 m_source->close();
1992 m_state = m_source->state();
1993 }
1994
1995 virtual void flush()
1996 {
1997#if SET_FILE_OP_TIMES
1998 m_atime = m_mtime = time_point::min();
1999#endif
2000 flush_cache();
2001 if (!ok()) _Unlikely_
2002 return;
2003 m_source->flush();
2004 }
2005
2006 virtual fpos_t seek(_In_ foff_t offset, _In_ seek_t how = seek_t::beg)
2007 {
2008 m_state = state_t::ok;
2009 switch (how) {
2010 case seek_t::beg:
2011 return m_offset = offset;
2012 case seek_t::cur:
2013 return m_offset += offset;
2014 case seek_t::end:
2015 return m_offset = size() + offset;
2016 default:
2017 throw std::invalid_argument("unknown seek origin");
2018 }
2019 }
2020
2021 virtual fpos_t tell() const
2022 {
2023 return m_offset;
2024 }
2025
2026 virtual void lock(_In_ fpos_t offset, _In_ fsize_t length)
2027 {
2028 m_source->lock(offset, length);
2029 m_state = m_source->state();
2030 }
2031
2032 virtual void unlock(_In_ fpos_t offset, _In_ fsize_t length)
2033 {
2034 m_source->unlock(offset, length);
2035 m_state = m_source->state();
2036 }
2037
2038 virtual fsize_t size()
2039 {
2040 return m_cache.status != cache_t::cache_t::status_t::empty ?
2041 std::max(m_source->size(), m_cache.region.end) :
2042 m_source->size();
2043 }
2044
2045 virtual void truncate()
2046 {
2047#if SET_FILE_OP_TIMES
2048 m_atime = m_mtime = time_point::now();
2049#endif
2050 m_source->seek(m_offset);
2051 if (m_cache.region.end <= m_offset) {
2052 // Truncation does not affect cache.
2053 }
2054 else if (m_cache.region.start <= m_offset) {
2055 // Truncation truncates cache.
2056 m_cache.region.end = m_offset;
2057 }
2058 else {
2059 // Truncation invalidates cache.
2060 m_cache.status = cache_t::cache_t::status_t::empty;
2061 }
2062 m_source->truncate();
2063 m_state = m_source->state();
2064 }
2065
2066 virtual time_point ctime() const
2067 {
2068 return m_source->ctime();
2069 }
2070
2071 virtual time_point atime() const
2072 {
2073#if SET_FILE_OP_TIMES
2074 return std::max(m_atime, m_source->atime());
2075#else
2076 return m_source->atime();
2077#endif
2078 }
2079
2080 virtual time_point mtime() const
2081 {
2082#if SET_FILE_OP_TIMES
2083 return std::max(m_mtime, m_source->mtime());
2084#else
2085 return m_source->mtime();
2086#endif
2087 }
2088
2089 virtual void set_ctime(time_point date)
2090 {
2091 m_source->set_ctime(date);
2092 }
2093
2094 virtual void set_atime(time_point date)
2095 {
2096#if SET_FILE_OP_TIMES
2097 m_atime = date;
2098#endif
2099 m_source->set_atime(date);
2100 }
2101
2102 virtual void set_mtime(time_point date)
2103 {
2104#if SET_FILE_OP_TIMES
2105 m_mtime = date;
2106#endif
2107 m_source->set_mtime(date);
2108 }
2109
2110 protected:
2112 void flush_cache()
2113 {
2114 if (m_cache.status != cache_t::cache_t::status_t::dirty)
2115 m_state = state_t::ok;
2116 else if (!m_cache.region.empty()) {
2117 write_cache();
2118 if (ok())
2119 m_cache.status = cache_t::cache_t::status_t::loaded;
2120 }
2121 else {
2122 m_state = state_t::ok;
2123 m_cache.status = cache_t::cache_t::status_t::loaded;
2124 }
2125 }
2126
2127 void invalidate_cache()
2128 {
2129 if (m_cache.status == cache_t::cache_t::status_t::dirty && !m_cache.region.empty()) {
2130 write_cache();
2131 if (!ok()) _Unlikely_
2132 return;
2133 } else
2134 m_state = state_t::ok;
2135 m_cache.status = cache_t::cache_t::status_t::empty;
2136 }
2137
2138 void load_cache(_In_ fpos_t start)
2139 {
2140 _Assume_(m_cache.status != cache_t::cache_t::status_t::dirty);
2141 start -= start % m_cache.capacity; // Align to cache block size.
2142 m_source->seek(m_cache.region.start = start);
2143 if (m_source->ok()) {
2144 m_cache.region.end = start + m_source->read(m_cache.data, m_cache.capacity);
2145 m_cache.status = cache_t::cache_t::status_t::loaded;
2146 m_state = state_t::ok; // Regardless the read failure, we still might have cached some data.
2147 }
2148 else
2149 m_state = state_t::fail;
2150 }
2151
2152 void write_cache()
2153 {
2154 _Assume_(m_cache.status == cache_t::cache_t::status_t::dirty);
2155 m_source->seek(m_cache.region.start);
2156 m_source->write(m_cache.data, static_cast<size_t>(m_cache.region.size()));
2157 m_state = m_source->state();
2158 }
2159
2160 basic_file* m_source;
2161 struct cache_t {
2162 uint8_t* data;
2163 size_t capacity;
2164 enum class status_t {
2165 empty = 0,
2166 loaded,
2167 dirty,
2168 } status;
2169 interval<fpos_t> region;
2170
2171 cache_t(_In_ size_t _capacity) :
2172 data(new uint8_t[_capacity]),
2173 capacity(_capacity),
2174 status(status_t::empty),
2175 region(0)
2176 {}
2177
2178 ~cache_t()
2179 {
2180 delete[] data;
2181 }
2182 } m_cache;
2183 fpos_t m_offset;
2184#if SET_FILE_OP_TIMES
2185 time_point
2186 m_atime,
2187 m_mtime;
2188#endif
2190 };
2191
2195 class basic_sys : virtual public basic, public sys_object
2196 {
2197 public:
2198 basic_sys(_In_opt_ sys_handle h = invalid_handle, _In_ state_t state = state_t::ok) :
2199 basic(state),
2200 sys_object(h)
2201 {}
2202
2203 virtual _Success_(return != 0 || length == 0) size_t read(
2204 _Out_writes_bytes_to_opt_(length, return) void* data, _In_ size_t length)
2205 {
2206 _Assume_(data || !length);
2207 // Windows Server 2003 and Windows XP: Pipe write operations across a network are limited in size per write.
2208 // The amount varies per platform. For x86 platforms it's 63.97 MB. For x64 platforms it's 31.97 MB. For Itanium
2209 // it's 63.95 MB. For more information regarding pipes, see the Remarks section.
2210 size_t
2211#if defined(_WIN64)
2212 block_size = 0x1F80000;
2213#elif defined(_WIN32)
2214 block_size = 0x3f00000;
2215#else
2216 block_size = SSIZE_MAX;
2217#endif
2218 for (size_t to_read = length;;) {
2219#ifdef _WIN32
2220 // ReadFile() might raise exception (e.g. STATUS_FILE_BAD_FORMAT/0xE0000002).
2221 BOOL succeeded;
2222 DWORD num_read;
2223 __try { succeeded = ReadFile(m_h, data, static_cast<DWORD>(std::min<size_t>(to_read, block_size)), &num_read, nullptr); }
2224 __except (EXCEPTION_EXECUTE_HANDLER) { succeeded = FALSE; SetLastError(ERROR_UNHANDLED_EXCEPTION); num_read = 0; }
2225 if (!succeeded && GetLastError() == ERROR_NO_SYSTEM_RESOURCES && block_size > default_block_size) _Unlikely_ {
2226 // Error "Insufficient system resources exist to complete the requested service." occurs
2227 // ocasionally, when attempting to read too much data at once (e.g. over \\TSClient).
2228 block_size = default_block_size;
2229 continue;
2230 }
2231 if (!succeeded) _Unlikely_
2232#else
2233 ssize_t num_read = ::read(m_h, data, static_cast<ssize_t>(std::min<size_t>(to_read, block_size)));
2234 if (num_read < 0) _Unlikely_
2235#endif
2236 {
2237 m_state = to_read < length ? state_t::ok : state_t::fail;
2238 return length - to_read;
2239 }
2240 if (!num_read) _Unlikely_ {
2241 m_state = to_read < length || !length ? state_t::ok : state_t::eof;
2242 return length - to_read;
2243 }
2244 to_read -= num_read;
2245 if (!to_read) {
2246 m_state = state_t::ok;
2247 return length;
2248 }
2249 reinterpret_cast<uint8_t*&>(data) += num_read;
2250 }
2251 }
2252
2253 virtual _Success_(return != 0) size_t write(
2254 _In_reads_bytes_opt_(length) const void* data, _In_ size_t length)
2255 {
2256 // Windows Server 2003 and Windows XP: Pipe write operations across a network are limited in size per write.
2257 // The amount varies per platform. For x86 platforms it's 63.97 MB. For x64 platforms it's 31.97 MB. For Itanium
2258 // it's 63.95 MB. For more information regarding pipes, see the Remarks section.
2259 constexpr size_t
2260#if defined(_WIN64)
2261 block_size = 0x1F80000;
2262#elif defined(_WIN32)
2263 block_size = 0x3f00000;
2264#else
2265 block_size = SSIZE_MAX;
2266#endif
2267 for (size_t to_write = length;;) {
2268#ifdef _WIN32
2269 // ReadFile() might raise an exception. Be cautious with WriteFile() too.
2270 BOOL succeeded;
2271 DWORD num_written;
2272 __try { succeeded = WriteFile(m_h, data, static_cast<DWORD>(std::min<size_t>(to_write, block_size)), &num_written, nullptr); }
2273 __except (EXCEPTION_EXECUTE_HANDLER) { succeeded = FALSE; SetLastError(ERROR_UNHANDLED_EXCEPTION); num_written = 0; }
2274 to_write -= num_written;
2275 if (!to_write) {
2276 m_state = state_t::ok;
2277 return length;
2278 }
2279 reinterpret_cast<const uint8_t*&>(data) += num_written;
2280 if (!succeeded) _Unlikely_ {
2281 m_state = state_t::fail;
2282 return length - to_write;
2283 }
2284#else
2285 ssize_t num_written = ::write(m_h, data, static_cast<ssize_t>(std::min<size_t>(to_write, block_size)));
2286 if (num_written < 0) _Unlikely_ {
2287 m_state = state_t::fail;
2288 return length - to_write;
2289 }
2290 to_write -= num_written;
2291 if (!to_write) {
2292 m_state = state_t::ok;
2293 return length;
2294 }
2295 reinterpret_cast<const uint8_t*&>(data) += num_written;
2296#endif
2297 }
2298 }
2299
2300 virtual void close()
2301 {
2302 try {
2304 m_state = state_t::ok;
2305 }
2306 catch (...) {
2307 m_state = state_t::fail;
2308 }
2309 }
2310
2311 virtual void flush()
2312 {
2313#ifdef _WIN32
2314 m_state = FlushFileBuffers(m_h) ? state_t::ok : state_t::fail;
2315#else
2316 m_state = fsync(m_h) >= 0 ? state_t::ok : state_t::fail;
2317#endif
2318 }
2319 };
2320
2324 class buffered_sys : public buffer
2325 {
2326 public:
2327 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) :
2328 buffer(read_buffer_size, write_buffer_size),
2329 m_source(h)
2330 {
2331 init(m_source);
2332 }
2333
2334 virtual ~buffered_sys()
2335 {
2336 done();
2337 }
2338
2339 protected:
2340 basic_sys m_source;
2341 };
2342
2346 class socket : public basic
2347 {
2348 public:
2349 socket(_In_opt_ socket_t h = invalid_socket, _In_ state_t state = state_t::ok) :
2350 basic(state),
2351 m_h(h)
2352 {}
2353
2354 private:
2355 socket(_In_ const socket& other);
2356 socket& operator =(_In_ const socket& other);
2357
2358 public:
2359 socket(_Inout_ socket&& other) noexcept : m_h(other.m_h)
2360 {
2361 other.m_h = invalid_socket;
2362 }
2363
2364 socket& operator =(_Inout_ socket&& other) noexcept
2365 {
2366 if (this != std::addressof(other)) {
2367 if (m_h != invalid_socket)
2368 closesocket(m_h);
2369 m_h = other.m_h;
2370 other.m_h = invalid_socket;
2371 }
2372 return *this;
2373 }
2374
2382 socket(_In_ int af, _In_ int type, _In_ int protocol)
2383 {
2384 m_h = ::socket(af, type, protocol);
2385 if (m_h == invalid_socket) _Unlikely_
2386 m_state = state_t::fail;
2387 }
2388
2389 virtual ~socket()
2390 {
2391 if (m_h != invalid_socket)
2392 closesocket(m_h);
2393 }
2394
2398 inline operator bool() const noexcept { return m_h != invalid_socket; }
2399
2403 inline socket_t get() const noexcept { return m_h; }
2404
2405 virtual _Success_(return != 0 || length == 0) size_t read(
2406 _Out_writes_bytes_to_opt_(length, return) void* data, _In_ size_t length)
2407 {
2408 _Assume_(data || !length);
2409 constexpr int block_size = 0x10000000;
2410 for (size_t to_read = length;;) {
2411 int num_read = recv(m_h, reinterpret_cast<char*>(data), static_cast<int>(std::min<size_t>(to_read, block_size)), 0);
2412 if (num_read == SOCKET_ERROR) _Unlikely_ {
2413 m_state = to_read < length ? state_t::ok : state_t::fail;
2414 return length - to_read;
2415 }
2416 if (!num_read) {
2417 m_state = to_read < length || !length ? state_t::ok : state_t::eof;
2418 return length - to_read;
2419 }
2420 to_read -= num_read;
2421 if (!to_read) {
2422 m_state = state_t::ok;
2423 return length;
2424 }
2425 reinterpret_cast<uint8_t*&>(data) += num_read;
2426 }
2427 }
2428
2429 virtual _Success_(return != 0) size_t write(
2430 _In_reads_bytes_opt_(length) const void* data, _In_ size_t length)
2431 {
2432 _Assume_(data || !length);
2433 constexpr int block_size = 0x10000000;
2434 for (size_t to_write = length;;) {
2435 int num_written = send(m_h, reinterpret_cast<const char*>(data), static_cast<int>(std::min<size_t>(to_write, block_size)), 0);
2436 if (num_written == SOCKET_ERROR) _Unlikely_ {
2437 m_state = state_t::fail;
2438 return length - to_write;
2439 }
2440 to_write -= num_written;
2441 if (!to_write) {
2442 m_state = state_t::ok;
2443 return length;
2444 }
2445 reinterpret_cast<const uint8_t*&>(data) += num_written;
2446 }
2447 }
2448
2449 virtual void close()
2450 {
2451 if (m_h != invalid_socket) {
2452 closesocket(m_h);
2453 m_h = invalid_socket;
2454 }
2455 m_state = state_t::ok;
2456 }
2457
2458 protected:
2459 socket_t m_h;
2460 };
2461
2462#ifdef _WIN32
2466 class sequential_stream : public basic
2467 {
2468 public:
2469 sequential_stream(_In_ ISequentialStream* source) : m_source(source)
2470 {
2471 m_source->AddRef();
2472 }
2473
2474 virtual ~sequential_stream()
2475 {
2476 m_source->Release();
2477 }
2478
2479 virtual _Success_(return != 0 || length == 0) size_t read(
2480 _Out_writes_bytes_to_opt_(length, return) void* data, _In_ size_t length)
2481 {
2482 _Assume_(data || !length);
2483 for (size_t to_read = length;;) {
2484 HRESULT hr;
2485 ULONG num_read = 0;
2486 __try { hr = m_source->Read(data, (ULONG)std::min<size_t>(to_read, ULONG_MAX), &num_read); }
2487 __except (EXCEPTION_EXECUTE_HANDLER) { hr = E_FAIL; }
2488 if (FAILED(hr)) _Unlikely_ {
2489 m_state = to_read < length ? state_t::ok : state_t::fail;
2490 return length - to_read;
2491 }
2492 to_read -= num_read;
2493 if (hr == S_FALSE) _Unlikely_ {
2494 m_state = to_read < length || !length ? state_t::ok : state_t::eof;
2495 return length - to_read;
2496 }
2497 if (!to_read) {
2498 m_state = state_t::ok;
2499 return length;
2500 }
2501 reinterpret_cast<uint8_t*&>(data) += num_read;
2502 }
2503 }
2504
2505 virtual _Success_(return != 0) size_t write(
2506 _In_reads_bytes_opt_(length) const void* data, _In_ size_t length)
2507 {
2508 _Assume_(data || !length);
2509 for (size_t to_write = length;;) {
2510 HRESULT hr;
2511 ULONG num_written = 0;
2512 __try { hr = m_source->Write(data, static_cast<ULONG>(std::min<size_t>(to_write, ULONG_MAX)), &num_written); }
2513 __except (EXCEPTION_EXECUTE_HANDLER) { hr = E_FAIL; }
2514 // In abscence of documentation whether num_written gets set when FAILED(hr) (i.e. partially succesful writes),
2515 // assume write failed completely.
2516 if (FAILED(hr)) _Unlikely_ {
2517 m_state = state_t::fail;
2518 return length - to_write;
2519 }
2520 to_write -= num_written;
2521 if (!to_write) {
2522 m_state = state_t::ok;
2523 return length;
2524 }
2525 reinterpret_cast<const uint8_t*&>(data) += num_written;
2526 }
2527 }
2528
2529 protected:
2530 ISequentialStream* m_source;
2531 };
2532
2536 class asp : public basic
2537 {
2538 public:
2539 asp(_In_opt_ IRequest* request, _In_opt_ IResponse* response) :
2540 m_request(request),
2541 m_response(response)
2542 {
2543 if (m_request)
2544 m_request->AddRef();
2545 if (m_response)
2546 m_response->AddRef();
2547 }
2548
2549 virtual ~asp()
2550 {
2551 if (m_request)
2552 m_request->Release();
2553 if (m_response)
2554 m_response->Release();
2555 }
2556
2557 virtual _Success_(return != 0 || length == 0) size_t read(
2558 _Out_writes_bytes_to_opt_(length, return) void* data, _In_ size_t length)
2559 {
2560 _Assume_(data || !length);
2561 if (!m_request) _Unlikely_ {
2562 m_state = state_t::fail;
2563 return 0;
2564 }
2565 for (size_t to_read = length;;) {
2566 VARIANT var_amount, var_data;
2567 V_VT(&var_amount) = VT_I4;
2568 V_I4(&var_amount) = (LONG)std::min<size_t>(to_read, LONG_MAX);
2569 V_VT(&var_data) = VT_EMPTY;
2570 HRESULT hr = [&]() {
2571 __try { return m_request->BinaryRead(&var_amount, &var_data); }
2572 __except (EXCEPTION_EXECUTE_HANDLER) { return E_FAIL; }
2573 }();
2574 if (FAILED(hr)) _Unlikely_ {
2575 m_state = to_read < length ? state_t::ok : state_t::fail;
2576 return length - to_read;
2577 }
2578 _Assume_(V_VT(&var_amount) == VT_I4);
2579 _Assume_(V_VT(&var_data) == (VT_ARRAY | VT_UI1));
2580 std::unique_ptr<SAFEARRAY, SafeArrayDestroy_delete> sa(V_ARRAY(&var_data));
2581 if (!V_I4(&var_amount)) _Unlikely_ {
2582 m_state = to_read < length || !length ? state_t::ok : state_t::eof;
2583 return length - to_read;
2584 }
2585 safearray_accessor<uint8_t> a(sa.get());
2586 memcpy(data, a.data(), V_I4(&var_amount));
2587 to_read -= V_I4(&var_amount);
2588 if (!to_read) {
2589 m_state = state_t::ok;
2590 return length;
2591 }
2592 reinterpret_cast<uint8_t*&>(data) += V_I4(&var_amount);
2593 }
2594 }
2595
2596 virtual _Success_(return != 0) size_t write(
2597 _In_reads_bytes_opt_(length) const void* data, _In_ size_t length)
2598 {
2599 if (!m_response) {
2600 m_state = state_t::fail;
2601 return 0;
2602 }
2603 for (size_t to_write = length;;) {
2604 UINT num_written = static_cast<UINT>(std::min<size_t>(to_write, UINT_MAX));
2605 std::unique_ptr<OLECHAR, SysFreeString_delete> bstr_data(SysAllocStringByteLen(reinterpret_cast<LPCSTR>(data), num_written));
2606 VARIANT var_data;
2607 V_VT(&var_data) = VT_BSTR;
2608 V_BSTR(&var_data) = bstr_data.get();
2609 HRESULT hr = [&]() {
2610 __try { return m_response->BinaryWrite(var_data); }
2611 __except (EXCEPTION_EXECUTE_HANDLER) { return E_FAIL; }
2612 }();
2613 if (FAILED(hr)) _Unlikely_ {
2614 m_state = state_t::fail;
2615 return length - to_write;
2616 }
2617 to_write -= num_written;
2618 if (!to_write) {
2619 m_state = state_t::ok;
2620 return length;
2621 }
2622 reinterpret_cast<const uint8_t*&>(data) += num_written;
2623 }
2624 }
2625
2626 virtual void close()
2627 {
2628 if (m_response) {
2629 __try { m_response->End(); }
2630 __except (EXCEPTION_EXECUTE_HANDLER) {}
2631 }
2632 m_state = state_t::ok;
2633 }
2634
2635 virtual void flush()
2636 {
2637 if (m_response) {
2638 HRESULT hr;
2639 __try { hr = m_response->Flush(); }
2640 __except (EXCEPTION_EXECUTE_HANDLER) { hr = E_FAIL; }
2641 m_state = SUCCEEDED(hr) ? state_t::ok : state_t::fail;
2642 }
2643 }
2644
2645 protected:
2646 IRequest* m_request;
2647 IResponse* m_response;
2648 };
2649#endif
2650
2654 enum mode_t
2655 {
2656 mode_for_reading = 1 << 0,
2657 mode_for_writing = 1 << 1,
2658 mode_for_chmod = 1 << 2,
2659
2660 mode_open_existing = 0 << 3,
2661 mode_truncate_existing = 1 << 3,
2662 mode_preserve_existing = 2 << 3,
2663 mode_create_new = 3 << 3,
2664 mode_create = 4 << 3,
2665 mode_disposition_mask = 7 << 3,
2666
2667 mode_append = 1 << 6,
2668 mode_text = 0,
2669 mode_binary = 1 << 7,
2670
2671 share_none = 0,
2672 share_reading = 1 << 8,
2673 share_writing = 1 << 9,
2674 share_deleting = 1 << 10,
2675 share_all = share_reading | share_writing | share_deleting, // Allow others all operations on our file
2676
2677 inherit_handle = 1 << 11,
2678
2679 hint_write_thru = 1 << 12,
2680 hint_no_buffering = 1 << 13,
2681 hint_random_access = 1 << 14,
2682 hint_sequential_access = 1 << 15,
2683 };
2684
2685#pragma warning(push)
2686#pragma warning(disable: 4250)
2690 class file : virtual public basic_file, virtual public basic_sys
2691 {
2692 public:
2693 file(_In_opt_ sys_handle h = invalid_handle, _In_ state_t state = state_t::ok) : basic_sys(h, state) {}
2694
2701 file(_In_z_ const schar_t* filename, _In_ int mode)
2702 {
2703 open(filename, mode);
2704 }
2705
2712 inline file(_In_ const stdex::sstring& filename, _In_ int mode) : file(filename.c_str(), mode) {}
2713
2720 void open(_In_z_ const schar_t* filename, _In_ int mode)
2721 {
2722 if (m_h != invalid_handle)
2723 close();
2724
2725#ifdef _WIN32
2726 DWORD dwDesiredAccess = 0;
2727 if (mode & mode_for_reading) dwDesiredAccess |= GENERIC_READ;
2728 if (mode & mode_for_writing) dwDesiredAccess |= GENERIC_WRITE;
2729 if (mode & mode_for_chmod) dwDesiredAccess |= FILE_WRITE_ATTRIBUTES;
2730
2731 DWORD dwShareMode = 0;
2732 if (mode & share_reading) dwShareMode |= FILE_SHARE_READ;
2733 if (mode & share_writing) dwShareMode |= FILE_SHARE_WRITE;
2734 if (mode & share_deleting) dwShareMode |= FILE_SHARE_DELETE;
2735
2736 SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES) };
2737 sa.bInheritHandle = mode & inherit_handle ? true : false;
2738
2739 DWORD dwCreationDisposition;
2740 switch (mode & mode_disposition_mask) {
2741 case mode_open_existing: dwCreationDisposition = OPEN_EXISTING; break;
2742 case mode_truncate_existing: dwCreationDisposition = TRUNCATE_EXISTING; break;
2743 case mode_preserve_existing: dwCreationDisposition = OPEN_ALWAYS; break;
2744 case mode_create_new: dwCreationDisposition = CREATE_NEW; break;
2745 case mode_create: dwCreationDisposition = CREATE_ALWAYS; break;
2746 default: throw std::invalid_argument("invalid mode");
2747 }
2748
2749 DWORD dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
2750 if (mode & hint_write_thru) dwFlagsAndAttributes |= FILE_FLAG_WRITE_THROUGH;
2751 if (mode & hint_no_buffering) dwFlagsAndAttributes |= FILE_FLAG_NO_BUFFERING;
2752 if (mode & hint_random_access) dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
2753 if (mode & hint_sequential_access) dwFlagsAndAttributes |= FILE_FLAG_SEQUENTIAL_SCAN;
2754
2755 m_h = CreateFile(filename, dwDesiredAccess, dwShareMode, &sa, dwCreationDisposition, dwFlagsAndAttributes, NULL);
2756#else
2757 int flags = 0;
2758 switch (mode & (mode_for_reading | mode_for_writing)) {
2759 case mode_for_reading: flags |= O_RDONLY; break;
2760 case mode_for_writing: flags |= O_WRONLY; break;
2761 case mode_for_reading | mode_for_writing: flags |= O_RDWR; break;
2762 }
2763 switch (mode & mode_disposition_mask) {
2764 case mode_open_existing: break;
2765 case mode_truncate_existing: flags |= O_TRUNC; break;
2766 case mode_preserve_existing: flags |= O_CREAT; break;
2767 case mode_create_new: flags |= O_CREAT | O_EXCL; break;
2768 case mode_create: flags |= O_CREAT | O_TRUNC; break;
2769 default: throw std::invalid_argument("invalid mode");
2770 }
2771 if (mode & hint_write_thru) flags |= O_DSYNC;
2772#ifndef __APPLE__
2773 if (mode & hint_no_buffering) flags |= O_RSYNC;
2774#endif
2775
2776 m_h = ::open(filename, flags, DEFFILEMODE);
2777#endif
2778 if (m_h != invalid_handle) {
2779 m_state = state_t::ok;
2780 if (mode & mode_append)
2781 seek(0, seek_t::end);
2782 }
2783 else
2784 m_state = state_t::fail;
2785 }
2786
2793 inline void open(_In_ const stdex::sstring& filename, _In_ int mode)
2794 {
2795 open(filename.c_str(), mode);
2796 }
2797
2798 virtual fpos_t seek(_In_ foff_t offset, _In_ seek_t how = seek_t::beg)
2799 {
2800#ifdef _WIN32
2801 LARGE_INTEGER li;
2802 li.QuadPart = offset;
2803 li.LowPart = SetFilePointer(m_h, li.LowPart, &li.HighPart, static_cast<DWORD>(how));
2804 if (li.LowPart != 0xFFFFFFFF || GetLastError() == NO_ERROR) {
2805 m_state = state_t::ok;
2806 return li.QuadPart;
2807 }
2808#else
2809 off64_t result = lseek64(m_h, offset, static_cast<int>(how));
2810 if (result >= 0) {
2811 m_state = state_t::ok;
2812 return result;
2813 }
2814#endif
2815 m_state = state_t::fail;
2816 return fpos_max;
2817 }
2818
2819 virtual fpos_t tell() const
2820 {
2821 if (m_h != invalid_handle) {
2822#ifdef _WIN32
2823 LARGE_INTEGER li;
2824 li.QuadPart = 0;
2825 li.LowPart = SetFilePointer(m_h, 0, &li.HighPart, FILE_CURRENT);
2826 if (li.LowPart != 0xFFFFFFFF || GetLastError() == NO_ERROR)
2827 return li.QuadPart;
2828#else
2829 off64_t result = lseek64(m_h, 0, SEEK_CUR);
2830 if (result >= 0)
2831 return result;
2832#endif
2833 }
2834 return fpos_max;
2835 }
2836
2837 virtual void lock(_In_ fpos_t offset, _In_ fsize_t length)
2838 {
2839#ifdef _WIN32
2840 LARGE_INTEGER liOffset;
2841 LARGE_INTEGER liSize;
2842 liOffset.QuadPart = offset;
2843 liSize.QuadPart = length;
2844 if (LockFile(m_h, liOffset.LowPart, liOffset.HighPart, liSize.LowPart, liSize.HighPart)) {
2845 m_state = state_t::ok;
2846 return;
2847 }
2848#else
2849 off64_t orig = lseek64(m_h, 0, SEEK_CUR);
2850 if (orig >= 0) {
2851 m_state = lseek64(m_h, offset, SEEK_SET) >= 0 && lockf64(m_h, F_LOCK, length) >= 0 ? state_t::ok : state_t::fail;
2852 lseek64(m_h, orig, SEEK_SET);
2853 m_state = state_t::ok;
2854 return;
2855 }
2856#endif
2857 m_state = state_t::fail;
2858 }
2859
2860 virtual void unlock(_In_ fpos_t offset, _In_ fsize_t length)
2861 {
2862#ifdef _WIN32
2863 LARGE_INTEGER liOffset;
2864 LARGE_INTEGER liSize;
2865 liOffset.QuadPart = offset;
2866 liSize.QuadPart = length;
2867 if (UnlockFile(m_h, liOffset.LowPart, liOffset.HighPart, liSize.LowPart, liSize.HighPart)) {
2868 m_state = state_t::ok;
2869 return;
2870 }
2871#else
2872 off64_t orig = lseek64(m_h, 0, SEEK_CUR);
2873 if (orig >= 0) {
2874 if (lseek64(m_h, offset, SEEK_SET) >= 0 && lockf64(m_h, F_ULOCK, length) >= 0) {
2875 lseek64(m_h, orig, SEEK_SET);
2876 m_state = state_t::ok;
2877 return;
2878 }
2879 lseek64(m_h, orig, SEEK_SET);
2880 }
2881#endif
2882 m_state = state_t::fail;
2883 }
2884
2885 virtual fsize_t size()
2886 {
2887#ifdef _WIN32
2888 LARGE_INTEGER li;
2889 li.LowPart = GetFileSize(m_h, (LPDWORD)&li.HighPart);
2890 if (li.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR)
2891 li.QuadPart = -1;
2892 return li.QuadPart;
2893#else
2894 off64_t length = -1, orig = lseek64(m_h, 0, SEEK_CUR);
2895 if (orig >= 0) {
2896 length = lseek64(m_h, 0, SEEK_END);
2897 lseek64(m_h, orig, SEEK_SET);
2898 }
2899 return length;
2900#endif
2901 }
2902
2903 virtual void truncate()
2904 {
2905#ifdef _WIN32
2906 if (SetEndOfFile(m_h)) {
2907 m_state = state_t::ok;
2908 return;
2909 }
2910#else
2911 off64_t length = lseek64(m_h, 0, SEEK_CUR);
2912 if (length >= 0 && ftruncate64(m_h, length) >= 0) {
2913 m_state = state_t::ok;
2914 return;
2915 }
2916#endif
2917 m_state = state_t::fail;
2918 }
2919
2920#ifdef _WIN32
2921 static inline time_point ft2tp(_In_ const FILETIME& ft)
2922 {
2923#if _HAS_CXX20
2924 uint64_t t = (static_cast<int64_t>(ft.dwHighDateTime) << 32) | ft.dwLowDateTime;
2925#else
2926 uint64_t t = ((static_cast<int64_t>(ft.dwHighDateTime) << 32) | ft.dwLowDateTime) - 116444736000000000ll;
2927#endif
2928 return time_point(time_point::duration(t));
2929 }
2930
2931 static inline void tp2ft(_In_ time_point tp, _Out_ FILETIME& ft)
2932 {
2933#if _HAS_CXX20
2934 uint64_t t = tp.time_since_epoch().count();
2935#else
2936 uint64_t t = tp.time_since_epoch().count() + 116444736000000000ll;
2937#endif
2938 ft.dwHighDateTime = static_cast<DWORD>((t >> 32) & 0xffffffff);
2939 ft.dwLowDateTime = static_cast<DWORD>(t & 0xffffffff);
2940 }
2941#endif
2942
2943 virtual time_point ctime() const
2944 {
2945#ifdef _WIN32
2946 FILETIME ft;
2947 if (GetFileTime(m_h, &ft, nullptr, nullptr))
2948 return ft2tp(ft);
2949#endif
2950 return time_point::min();
2951 }
2952
2953 virtual time_point atime() const
2954 {
2955#ifdef _WIN32
2956 FILETIME ft;
2957 if (GetFileTime(m_h, nullptr, &ft, nullptr))
2958 return ft2tp(ft);
2959#else
2960 struct stat buf;
2961 if (fstat(m_h, &buf) >= 0)
2962 return clock::from_time_t(buf.st_atime);
2963#endif
2964 return time_point::min();
2965 }
2966
2967 virtual time_point mtime() const
2968 {
2969#ifdef _WIN32
2970 FILETIME ft;
2971 if (GetFileTime(m_h, nullptr, nullptr, &ft))
2972 return ft2tp(ft);
2973#else
2974 struct stat buf;
2975 if (fstat(m_h, &buf) >= 0)
2976 return clock::from_time_t(buf.st_mtime);
2977#endif
2978 return time_point::min();
2979 }
2980
2981 virtual void set_ctime(time_point date)
2982 {
2983 _Assume_(m_h != invalid_handle);
2984#ifdef _WIN32
2985 FILETIME ft;
2986 tp2ft(date, ft);
2987 if (SetFileTime(m_h, &ft, nullptr, nullptr))
2988 return;
2989 throw std::system_error(GetLastError(), std::system_category(), "SetFileTime failed");
2990#else
2991 throw std::runtime_error("not supported");
2992#endif
2993 }
2994
2995 virtual void set_atime(time_point date)
2996 {
2997 _Assume_(m_h != invalid_handle);
2998#ifdef _WIN32
2999 FILETIME ft;
3000 tp2ft(date, ft);
3001 if (SetFileTime(m_h, nullptr, &ft, nullptr))
3002 return;
3003 throw std::system_error(GetLastError(), std::system_category(), "SetFileTime failed");
3004#else
3005 struct timespec ts[2] = {
3006 { date.time_since_epoch().count(), 0 },
3007 { 0, UTIME_OMIT },
3008 };
3009 if (futimens(m_h, ts) >= 0)
3010 return;
3011 throw std::system_error(errno, std::system_category(), "futimens failed");
3012#endif
3013 }
3014
3015 virtual void set_mtime(time_point date)
3016 {
3017#ifdef _WIN32
3018 FILETIME ft;
3019 tp2ft(date, ft);
3020 if (SetFileTime(m_h, nullptr, nullptr, &ft))
3021 return;
3022 throw std::system_error(GetLastError(), std::system_category(), "SetFileTime failed");
3023#else
3024 struct timespec ts[2] = {
3025 { 0, UTIME_OMIT },
3026 { date.time_since_epoch().count(), 0 },
3027 };
3028 if (futimens(m_h, ts) >= 0)
3029 return;
3030 throw std::system_error(errno, std::system_category(), "futimens failed");
3031#endif
3032 }
3033
3039 static bool exists(_In_z_ const stdex::schar_t* filename)
3040 {
3041#ifdef _WIN32
3042 return GetFileAttributes(filename) != INVALID_FILE_ATTRIBUTES;
3043#else
3044 struct stat s;
3045 return stat(filename, &s) == 0;
3046#endif
3047 }
3048
3054 static inline bool exists(_In_ const stdex::sstring& filename)
3055 {
3056 return exists(filename.c_str());
3057 }
3058
3066 static bool readonly(_In_z_ const stdex::schar_t* filename)
3067 {
3068#ifdef _WIN32
3069 DWORD dwAttr = GetFileAttributes(filename);
3070 return dwAttr != INVALID_FILE_ATTRIBUTES && (dwAttr & FILE_ATTRIBUTE_READONLY) != 0;
3071#else
3072 struct stat s;
3073 return stat(filename, &s) == 0 && (s.st_mode & (S_IWUSR|S_IWGRP|S_IWOTH)) == 0;
3074#endif
3075 }
3076
3084 static inline bool readonly(_In_ const stdex::sstring& filename)
3085 {
3086 return readonly(filename.c_str());
3087 }
3088 };
3089#pragma warning(pop)
3090
3094 class cached_file : public cache
3095 {
3096 public:
3097 cached_file(_In_opt_ sys_handle h = invalid_handle, _In_ state_t state = state_t::ok, _In_ size_t cache_size = default_cache_size) :
3098 cache(cache_size),
3099 m_source(h, state)
3100 {
3101 init(m_source);
3102 }
3103
3111 cached_file(_In_z_ const schar_t* filename, _In_ int mode, _In_ size_t cache_size = default_cache_size) :
3112 cache(cache_size),
3113 m_source(filename, mode & mode_for_writing ? mode | mode_for_reading : mode)
3114 {
3115 init(m_source);
3116 }
3117
3125 inline cached_file(_In_ const stdex::sstring& filename, _In_ int mode, _In_ size_t cache_size = default_cache_size) : cached_file(filename.c_str(), mode, cache_size) {}
3126
3127 virtual ~cached_file()
3128 {
3129 done();
3130 }
3131
3138 void open(_In_z_ const schar_t* filename, _In_ int mode)
3139 {
3140 invalidate_cache();
3141 if (!ok()) _Unlikely_{
3142 m_state = state_t::fail;
3143 return;
3144 }
3145 m_source.open(filename, mode & mode_for_writing ? mode | mode_for_reading : mode);
3146 if (m_source.ok()) {
3147 init();
3148 return;
3149 }
3150 m_state = state_t::fail;
3151 }
3152
3159 inline void open(_In_ const stdex::sstring& filename, _In_ int mode)
3160 {
3161 open(filename.c_str(), mode);
3162 }
3163
3164 protected:
3165 file m_source;
3166 };
3167
3172 {
3173 public:
3174 memory_file(_In_ state_t state = state_t::ok) :
3175 basic(state),
3176 m_data(nullptr),
3177 m_offset(0),
3178 m_size(0),
3179 m_reserved(0),
3180 m_manage(true)
3181 {
3182#if SET_FILE_OP_TIMES
3183 m_ctime = m_atime = m_mtime = time_point::now();
3184#endif
3185 }
3186
3193 memory_file(_In_ size_t size, _In_ state_t state = state_t::ok) :
3194 basic(state),
3195 m_data(reinterpret_cast<uint8_t*>(malloc(size))),
3196 m_offset(0),
3197 m_size(0),
3199 m_manage(true)
3200 {
3201 if (!m_data)
3202 throw std::bad_alloc();
3203#if SET_FILE_OP_TIMES
3204 m_ctime = m_atime = m_mtime = time_point::now();
3205#endif
3206 }
3207
3217 memory_file(_Inout_ void* data, _In_ size_t size, _In_ size_t reserved, _In_ bool manage = false, _In_ state_t state = state_t::ok) :
3218 basic(state),
3219 m_data(reinterpret_cast<uint8_t*>(data)),
3220 m_offset(0),
3221 m_size(size),
3222 m_reserved(reserved),
3223 m_manage(manage)
3224 {
3225 _Assume_(data || !size);
3226 _Assume_(reserved >= size);
3227#if SET_FILE_OP_TIMES
3228 m_ctime = m_atime = m_mtime = time_point::now();
3229#endif
3230 }
3231
3240 memory_file(_Inout_ void* data, _In_ size_t size, _In_ bool manage = false, _In_ state_t state = state_t::ok) :
3241 memory_file(data, size, size, manage, state)
3242 {}
3243
3250 memory_file(_In_z_ const schar_t* filename, _In_ int mode) : memory_file()
3251 {
3252 load(filename, mode);
3253 }
3254
3261 inline memory_file(_In_ const stdex::sstring& filename, _In_ int mode) : memory_file(filename.c_str(), mode) {}
3262
3263 virtual ~memory_file()
3264 {
3265 if (m_manage && m_data)
3266 free(m_data);
3267 }
3268
3275 void reserve(_In_ size_t required, _In_ bool tight = false) noexcept
3276 {
3277 if (required <= m_reserved && (!tight || required >= m_reserved)) {
3278 m_state = state_t::ok;
3279 return;
3280 }
3281 if (!m_manage) {
3282 m_state = state_t::fail;
3283 return;
3284 }
3285 size_t reserved = tight ? required : ((required + required / 4 + (default_block_size - 1)) / default_block_size) * default_block_size;
3286 auto data = reinterpret_cast<uint8_t*>(realloc(m_data, reserved));
3287 if (!data && reserved) _Unlikely_ {
3288 m_state = state_t::fail;
3289 return;
3290 }
3291 m_data = data;
3292 if (reserved < m_size)
3293 m_size = reserved;
3294 m_reserved = reserved;
3295 m_state = state_t::ok;
3296 }
3297
3304 void load(_In_z_ const schar_t* filename, _In_ int mode)
3305 {
3306 file f(filename, (mode & ~hint_random_access) | mode_for_reading | hint_sequential_access);
3307 if (!f.ok()) {
3308 m_state = state_t::fail;
3309 return;
3310 }
3311 fsize_t size = f.size();
3312 if (size > SIZE_MAX) {
3313 m_state = state_t::fail;
3314 return;
3315 }
3316 reserve(static_cast<size_t>(size), true);
3317 if (!ok()) _Unlikely_ {
3318 return;
3319 }
3320 m_offset = m_size = 0;
3321 write_stream(f);
3322 if (ok())
3323 m_offset = 0;
3324#if SET_FILE_OP_TIMES
3325 m_ctime = f.ctime();
3326 m_atime = f.atime();
3327 m_mtime = f.mtime();
3328#endif
3329 }
3330
3337 inline void load(_In_ const stdex::sstring& filename, _In_ int mode)
3338 {
3339 load(filename.c_str(), mode);
3340 }
3341
3348 void save(_In_z_ const schar_t* filename, _In_ int mode)
3349 {
3350 file f(filename, (mode & ~hint_random_access) | mode_for_writing | hint_sequential_access);
3351 if (!f.ok()) {
3352 m_state = state_t::fail;
3353 return;
3354 }
3355 f.write(m_data, m_size);
3356 if (!f.ok()) {
3357 m_state = state_t::fail;
3358 return;
3359 }
3360 f.truncate();
3361#if SET_FILE_OP_TIMES
3362 f.set_ctime(m_ctime);
3363 f.set_atime(m_atime);
3364 f.set_mtime(m_mtime);
3365#endif
3366 }
3367
3374 inline void save(_In_ const stdex::sstring& filename, _In_ int mode)
3375 {
3376 save(filename.c_str(), mode);
3377 }
3378
3382 inline const void* data() const { return m_data; }
3383
3384 virtual _Success_(return != 0 || length == 0) size_t read(
3385 _Out_writes_bytes_to_opt_(length, return) void* data, _In_ size_t length)
3386 {
3387 _Assume_(data || !length);
3388#if SET_FILE_OP_TIMES
3389 m_atime = time_point::now();
3390#endif
3391 size_t available = m_size - m_offset;
3392 if (length <= available) {
3393 memcpy(data, m_data + m_offset, length);
3394 m_offset += length;
3395 m_state = state_t::ok;
3396 return length;
3397 }
3398 if (length && !available) {
3399 m_state = state_t::eof;
3400 return 0;
3401 }
3402 memcpy(data, m_data + m_offset, available);
3403 m_offset += available;
3404 m_state = state_t::ok;
3405 return available;
3406 }
3407
3422 template <class T>
3423 inline memory_file& read_data(_Out_ T& data)
3424 {
3425#if SET_FILE_OP_TIMES
3426 m_atime = time_point::now();
3427#endif
3428 if (CHECK_STREAM_STATE && !ok()) _Unlikely_ {
3429 data = 0;
3430 return *this;
3431 }
3432 size_t end_offset = m_offset + sizeof(T);
3433 if (end_offset <= m_size) {
3434 data = LE2HE(*reinterpret_cast<T*>(m_data + m_offset));
3435 m_offset = end_offset;
3436#if !CHECK_STREAM_STATE
3437 m_state = state_t::ok;
3438#endif
3439 }
3440 else {
3441 data = 0;
3442 m_offset = m_size;
3443 m_state = state_t::eof;
3444 }
3445 return *this;
3446 }
3447
3462 template<class _Elem, class _Traits = std::char_traits<_Elem>, class _Ax = std::allocator<_Elem>>
3463 memory_file& read_str(_Inout_ std::basic_string<_Elem, _Traits, _Ax>&data)
3464 {
3465#if SET_FILE_OP_TIMES
3466 m_atime = time_point::now();
3467#endif
3468 if (CHECK_STREAM_STATE && !ok()) _Unlikely_ {
3469 data.clear();
3470 return *this;
3471 }
3472 size_t end_offset = m_offset + sizeof(uint32_t);
3473 if (end_offset <= m_size) {
3474 uint32_t num_chars = LE2HE(*reinterpret_cast<uint32_t*>(m_data + m_offset));
3475 m_offset = end_offset;
3476 end_offset = stdex::add(m_offset, stdex::mul(num_chars, sizeof(_Elem)));
3477 _Elem* start = reinterpret_cast<_Elem*>(m_data + m_offset);
3478 if (end_offset <= m_size) {
3479 data.assign(start, start + num_chars);
3480 m_offset = end_offset;
3481#if !CHECK_STREAM_STATE
3482 m_state = state_t::ok;
3483#endif
3484 return *this;
3485 }
3486 if (end_offset <= m_size)
3487 data.assign(start, reinterpret_cast<_Elem*>(m_data + m_size));
3488 }
3489 m_offset = m_size;
3490 m_state = state_t::eof;
3491 return *this;
3492 }
3493
3494 virtual _Success_(return != 0) size_t write(
3495 _In_reads_bytes_opt_(length) const void* data, _In_ size_t length)
3496 {
3497 _Assume_(data || !length);
3498#if SET_FILE_OP_TIMES
3499 m_atime = m_mtime = time_point::now();
3500#endif
3501 size_t end_offset = m_offset + length;
3502 if (end_offset > m_reserved) {
3503 reserve(end_offset);
3504 if (!ok()) _Unlikely_
3505 return 0;
3506 }
3507 memcpy(m_data + m_offset, data, length);
3508 m_offset = end_offset;
3509 if (m_offset > m_size)
3510 m_size = m_offset;
3511 m_state = state_t::ok;
3512 return length;
3513 }
3514
3518 void write_byte(_In_ uint8_t byte, _In_ size_t amount = 1)
3519 {
3520#if SET_FILE_OP_TIMES
3521 m_atime = m_mtime = time_point::now();
3522#endif
3523 size_t end_offset = m_offset + amount;
3524 if (end_offset > m_reserved) {
3525 reserve(end_offset);
3526 if (!ok()) _Unlikely_
3527 return;
3528 }
3529 memset(m_data + m_offset, byte, amount);
3530 m_offset = end_offset;
3531 if (m_offset > m_size)
3532 m_size = m_offset;
3533 m_state = state_t::ok;
3534 }
3535
3550 template <class T>
3551 inline memory_file& write_data(const T data)
3552 {
3553#if SET_FILE_OP_TIMES
3554 m_atime = m_mtime = time_point::now();
3555#endif
3556 if (CHECK_STREAM_STATE && !ok()) _Unlikely_
3557 return *this;
3558 size_t end_offset = m_offset + sizeof(T);
3559 if (end_offset > m_reserved) {
3560 reserve(end_offset);
3561 if (!ok()) _Unlikely_
3562 return *this;
3563 }
3564 (*reinterpret_cast<T*>(m_data + m_offset)) = HE2LE(data);
3565 m_offset = end_offset;
3566 if (m_offset > m_size)
3567 m_size = m_offset;
3568#if !CHECK_STREAM_STATE
3569 m_state = state_t::ok;
3570#endif
3571 return *this;
3572 }
3573
3588 template <class T>
3589 inline memory_file& write_str(_In_z_ const T * data)
3590 {
3591#if SET_FILE_OP_TIMES
3592 m_atime = m_mtime = time_point::now();
3593#endif
3594 if (CHECK_STREAM_STATE && !ok()) _Unlikely_
3595 return *this;
3596 size_t num_chars = stdex::strlen(data);
3597 if (num_chars > UINT32_MAX)
3598 throw std::invalid_argument("string too long");
3599 size_t size_chars = num_chars * sizeof(T);
3600 size_t size = sizeof(uint32_t) + size_chars;
3601 size_t end_offset = m_offset + size;
3602 if (end_offset > m_reserved) {
3603 reserve(end_offset);
3604 if (!ok()) _Unlikely_
3605 return *this;
3606 }
3607 auto p = m_data + m_offset;
3608 *reinterpret_cast<uint32_t*>(p) = HE2LE((uint32_t)num_chars);
3609 memcpy(p + sizeof(uint32_t), data, size_chars);
3610 m_offset = end_offset;
3611 if (m_offset > m_size)
3612 m_size = m_offset;
3613#if !CHECK_STREAM_STATE
3614 m_state = state_t::ok;
3615#endif
3616 return *this;
3617 }
3618
3633 template<class _Elem, class _Traits = std::char_traits<_Elem>, class _Ax = std::allocator<_Elem>>
3634 inline memory_file& write_str(_In_ const std::basic_string<_Elem, _Traits, _Ax>& data)
3635 {
3636#if SET_FILE_OP_TIMES
3637 m_atime = m_mtime = time_point::now();
3638#endif
3639 if (CHECK_STREAM_STATE && !ok()) _Unlikely_
3640 return *this;
3641 size_t num_chars = data.size();
3642 if (num_chars > UINT32_MAX)
3643 throw std::invalid_argument("string too long");
3644 size_t size_chars = num_chars * sizeof(_Elem);
3645 size_t size = sizeof(uint32_t) + size_chars;
3646 size_t end_offset = m_offset + size;
3647 if (end_offset > m_reserved) {
3648 reserve(end_offset);
3649 if (!ok()) _Unlikely_
3650 return *this;
3651 }
3652 auto p = m_data + m_offset;
3653 *reinterpret_cast<uint32_t*>(p) = HE2LE((uint32_t)num_chars);
3654 memcpy(p + sizeof(uint32_t), data.data(), size_chars);
3655 m_offset = end_offset;
3656 if (m_offset > m_size)
3657 m_size = m_offset;
3658#if !CHECK_STREAM_STATE
3659 m_state = state_t::ok;
3660#endif
3661 return *this;
3662 }
3663
3669 size_t write_stream(_Inout_ basic & stream, _In_ size_t amount = SIZE_MAX)
3670 {
3671#if SET_FILE_OP_TIMES
3672 m_atime = m_mtime = time_point::now();
3673#endif
3674 size_t num_read, dst_offset = m_offset, dst_size = m_offset;
3675 size_t num_copied = 0, to_write = amount;
3676 m_state = state_t::ok;
3677 if (amount != SIZE_MAX) {
3678 dst_size = stdex::add(dst_size, amount);
3679 reserve(dst_size);
3680 if (!ok()) _Unlikely_
3681 return 0;
3682 while (to_write) {
3683 num_read = stream.read(m_data + dst_offset, to_write);
3684 dst_size = dst_offset += num_read;
3685 num_copied += num_read;
3686 to_write -= num_read;
3687 if (!stream.ok()) {
3688 if (stream.state() != state_t::eof)
3689 m_state = state_t::fail;
3690 break;
3691 }
3692 };
3693 }
3694 else {
3695 size_t block_size;
3696 while (to_write) {
3697 block_size = std::min(to_write, default_block_size);
3698 dst_size = stdex::add(dst_size, block_size);
3699 reserve(dst_size);
3700 if (!ok()) _Unlikely_
3701 break;
3702 num_read = stream.read(m_data + dst_offset, block_size);
3703 dst_size = dst_offset += num_read;
3704 num_copied += num_read;
3705 to_write -= num_read;
3706 if (!stream.ok()) {
3707 if (stream.state() != state_t::eof)
3708 m_state = state_t::fail;
3709 break;
3710 }
3711 };
3712 }
3713 m_offset = dst_offset;
3714 if (m_offset > m_size)
3715 m_size = m_offset;
3716 return num_copied;
3717 }
3718
3719 virtual void close()
3720 {
3721 if (m_manage && m_data)
3722 free(m_data);
3723 m_data = nullptr;
3724 m_manage = true;
3725 m_offset = 0;
3726 m_size = m_reserved = 0;
3727#if SET_FILE_OP_TIMES
3728 m_ctime = m_atime = m_mtime = time_point::min();
3729#endif
3730 m_state = state_t::ok;
3731 }
3732
3733 virtual fpos_t seek(_In_ foff_t offset, _In_ seek_t how = seek_t::beg)
3734 {
3735 fpos_t target;
3736 switch (how) {
3737 case seek_t::beg: target = offset; break;
3738 case seek_t::cur: target = static_cast<fpos_t>(m_offset) + offset; break;
3739 case seek_t::end: target = static_cast<fpos_t>(m_size) + offset; break;
3740 default: throw std::invalid_argument("unknown seek origin");
3741 }
3742 if (target <= SIZE_MAX) {
3743 m_state = state_t::ok;
3744 return m_offset = static_cast<size_t>(target);
3745 }
3746 m_state = state_t::fail;
3747 return fpos_max;
3748 }
3749
3750 virtual fpos_t tell() const
3751 {
3752 return m_offset;
3753 }
3754
3755 virtual fsize_t size()
3756 {
3757 return m_size;
3758 }
3759
3760 virtual void truncate()
3761 {
3762#if SET_FILE_OP_TIMES
3763 m_atime = m_mtime = time_point::now();
3764#endif
3765 m_size = m_offset;
3767 }
3768
3769#if SET_FILE_OP_TIMES
3770 virtual time_point ctime() const
3771 {
3772 return m_ctime;
3773 }
3774
3775 virtual time_point atime() const
3776 {
3777 return m_atime;
3778 }
3779
3780 virtual time_point mtime() const
3781 {
3782 return m_mtime;
3783 }
3784
3785 virtual void set_ctime(time_point date)
3786 {
3787 m_ctime = date;
3788 }
3789
3790 virtual void set_atime(time_point date)
3791 {
3792 m_atime = date;
3793 }
3794
3795 virtual void set_mtime(time_point date)
3796 {
3797 m_mtime = date;
3798 }
3799#endif
3800
3801 protected:
3809 template <class T>
3810 inline void set(_In_ fpos_t offset, _In_ const T data)
3811 {
3812#if SET_FILE_OP_TIMES
3813 m_atime = m_mtime = time_point::now();
3814#endif
3815 _Assume_(offset + sizeof(T) < m_size);
3816 (*reinterpret_cast<T*>(m_data + offset)) = HE2LE(data);
3817 }
3818
3819 public:
3820 inline void set(_In_ fpos_t offset, _In_ const int8_t data) { set<int8_t>(offset, data); }
3821 inline void set(_In_ fpos_t offset, _In_ const int16_t data) { set<int16_t>(offset, data); }
3822 inline void set(_In_ fpos_t offset, _In_ const int32_t data) { set<int32_t>(offset, data); }
3823 inline void set(_In_ fpos_t offset, _In_ const int64_t data) { set<int64_t>(offset, data); }
3824 inline void set(_In_ fpos_t offset, _In_ const uint8_t data) { set<uint8_t>(offset, data); }
3825 inline void set(_In_ fpos_t offset, _In_ const uint16_t data) { set<uint16_t>(offset, data); }
3826 inline void set(_In_ fpos_t offset, _In_ const uint32_t data) { set<uint32_t>(offset, data); }
3827 inline void set(_In_ fpos_t offset, _In_ const uint64_t data) { set<uint64_t>(offset, data); }
3828 inline void set(_In_ fpos_t offset, _In_ const float data) { set<float>(offset, data); }
3829 inline void set(_In_ fpos_t offset, _In_ const double data) { set<double>(offset, data); }
3830 inline void set(_In_ fpos_t offset, _In_ const char data) { set<char>(offset, data); }
3831#ifdef _NATIVE_WCHAR_T_DEFINED
3832 inline void set(_In_ fpos_t offset, _In_ const wchar_t data) { set<wchar_t>(offset, data); }
3833#endif
3834
3842 protected:
3843 template <class T>
3844 inline void get(_In_ fpos_t offset, _Out_ T & data)
3845 {
3846 _Assume_(offset + sizeof(T) < m_size);
3847 data = LE2HE(*(T*)(m_data + offset));
3848#if SET_FILE_OP_TIMES
3849 m_atime = time_point::now();
3850#endif
3851 }
3852
3853 public:
3854 inline void get(_In_ fpos_t offset, _Out_ int8_t & data) { get<int8_t>(offset, data); }
3855 inline void get(_In_ fpos_t offset, _Out_ int16_t & data) { get<int16_t>(offset, data); }
3856 inline void get(_In_ fpos_t offset, _Out_ int32_t & data) { get<int32_t>(offset, data); }
3857 inline void get(_In_ fpos_t offset, _Out_ int64_t & data) { get<int64_t>(offset, data); }
3858 inline void get(_In_ fpos_t offset, _Out_ uint8_t & data) { get<uint8_t>(offset, data); }
3859 inline void get(_In_ fpos_t offset, _Out_ uint16_t & data) { get<uint16_t>(offset, data); }
3860 inline void get(_In_ fpos_t offset, _Out_ uint32_t & data) { get<uint32_t>(offset, data); }
3861 inline void get(_In_ fpos_t offset, _Out_ uint64_t & data) { get<uint64_t>(offset, data); }
3862 inline void get(_In_ fpos_t offset, _Out_ float& data) { get<float>(offset, data); }
3863 inline void get(_In_ fpos_t offset, _Out_ double& data) { get<double>(offset, data); }
3864 inline void get(_In_ fpos_t offset, _Out_ char& data) { get<char>(offset, data); }
3865#ifdef _NATIVE_WCHAR_T_DEFINED
3866 inline void get(_In_ fpos_t offset, _Out_ wchar_t& data) { get<wchar_t>(offset, data); }
3867#endif
3868
3869 inline memory_file& operator <<(_In_ const int8_t data) { return write_data(data); }
3870 inline memory_file& operator >>(_Out_ int8_t & data) { return read_data(data); }
3871 inline memory_file& operator <<(_In_ const int16_t data) { return write_data(data); }
3872 inline memory_file& operator >>(_Out_ int16_t & data) { return read_data(data); }
3873 inline memory_file& operator <<(_In_ const int32_t data) { return write_data(data); }
3874 inline memory_file& operator >>(_Out_ int32_t & data) { return read_data(data); }
3875 inline memory_file& operator <<(_In_ const int64_t data) { return write_data(data); }
3876 inline memory_file& operator >>(_Out_ int64_t & data) { return read_data(data); }
3877 inline memory_file& operator <<(_In_ const uint8_t data) { return write_data(data); }
3878 inline memory_file& operator >>(_Out_ uint8_t & data) { return read_data(data); }
3879 inline memory_file& operator <<(_In_ const uint16_t data) { return write_data(data); }
3880 inline memory_file& operator >>(_Out_ uint16_t & data) { return read_data(data); }
3881 inline memory_file& operator <<(_In_ const uint32_t data) { return write_data(data); }
3882 inline memory_file& operator >>(_Out_ uint32_t & data) { return read_data(data); }
3883 inline memory_file& operator <<(_In_ const uint64_t data) { return write_data(data); }
3884 inline memory_file& operator >>(_Out_ uint64_t & data) { return read_data(data); }
3885 inline memory_file& operator <<(_In_ const float data) { return write_data(data); }
3886 inline memory_file& operator >>(_Out_ float& data) { return read_data(data); }
3887 inline memory_file& operator <<(_In_ const double data) { return write_data(data); }
3888 inline memory_file& operator >>(_Out_ double& data) { return read_data(data); }
3889 inline memory_file& operator <<(_In_ const char data) { return write_data(data); }
3890 inline memory_file& operator >>(_Out_ char& data) { return read_data(data); }
3891#ifdef _NATIVE_WCHAR_T_DEFINED
3892 inline memory_file& operator <<(_In_ const wchar_t data) { return write_data(data); }
3893 inline memory_file& operator >>(_Out_ wchar_t& data) { return read_data(data); }
3894#endif
3895 template<class _Elem, class _Traits = std::char_traits<_Elem>, class _Ax = std::allocator<_Elem>>
3896 inline memory_file& operator >>(_Out_ std::basic_string<_Elem, _Traits, _Ax>&data) { return read_str(data); }
3897 template <class T>
3898 inline memory_file& operator <<(_In_ const T * data) { return write_str(data); }
3899 template<class _Elem, class _Traits = std::char_traits<_Elem>, class _Ax = std::allocator<_Elem>>
3900 inline memory_file& operator <<(_In_ const std::basic_string<_Elem, _Traits, _Ax>& data) { return write_str(data); }
3901
3902 protected:
3903 uint8_t* m_data;
3905 size_t m_offset;
3906 size_t m_size;
3907 size_t m_reserved;
3908#if SET_FILE_OP_TIMES
3909 time_point
3910 m_ctime,
3911 m_atime,
3912 m_mtime;
3913#endif
3914 };
3915
3919 class fifo : public basic {
3920 public:
3921 fifo() :
3922 m_offset(0),
3923 m_size(0),
3924 m_head(nullptr),
3925 m_tail(nullptr)
3926 {}
3927
3928 virtual ~fifo()
3929 {
3930 while (m_head) {
3931 auto p = m_head;
3932 m_head = p->next;
3933 delete p;
3934 }
3935 }
3936
3937#pragma warning(suppress: 6101) // See [2] below
3938 virtual _Success_(return != 0 || length == 0) size_t read(
3939 _Out_writes_bytes_to_opt_(length, return) void* data, _In_ size_t length)
3940 {
3941 _Assume_(data || !length);
3942 for (size_t to_read = length;;) {
3943 if (!m_head) _Unlikely_ {
3944 m_state = to_read < length || !length ? state_t::ok : state_t::eof;
3945 return length - to_read; // [2] Code analysis misses `length - to_read` bytes were written to data in previous loop iterations.
3946 }
3947 size_t remaining = m_head->size - m_offset;
3948 if (remaining > to_read) {
3949 memcpy(data, m_head->data + m_offset, to_read);
3950 m_offset += to_read;
3951 m_size -= to_read;
3952 m_state = state_t::ok;
3953 return length;
3954 }
3955 memcpy(data, m_head->data + m_offset, remaining);
3956 m_offset = 0;
3957 m_size -= remaining;
3958 reinterpret_cast<uint8_t*&>(data) += remaining;
3959 to_read -= remaining;
3960 auto p = m_head;
3961 m_head = p->next;
3962 delete p;
3963 }
3964 }
3965
3966 virtual _Success_(return != 0) size_t write(
3967 _In_reads_bytes_opt_(length) const void* data, _In_ size_t length)
3968 {
3969 _Assume_(data || !length);
3970 try {
3971 std::unique_ptr<node_t> n(reinterpret_cast<node_t*>(new uint8_t[sizeof(node_t) + length]));
3972 n->next = nullptr;
3973 n->size = length;
3974 memcpy(n->data, data, length);
3975 m_size += length;
3976 if (m_head)
3977 m_tail = m_tail->next = n.release();
3978 else
3979 m_head = m_tail = n.release();
3980 m_state = state_t::ok;
3981 return length;
3982 }
3983 catch (const std::bad_alloc&) {
3984 m_state = state_t::fail;
3985 return 0;
3986 }
3987 }
3988
3989 virtual void close()
3990 {
3991 m_size = m_offset = 0;
3992 while (m_head) {
3993 auto p = m_head;
3994 m_head = p->next;
3995 delete p;
3996 }
3997 m_state = state_t::ok;
3998 }
3999
4003 inline size_t size() const { return m_size; };
4004
4005 protected:
4006 size_t m_offset, m_size;
4007 struct node_t {
4008 node_t* next;
4009 size_t size;
4010#pragma warning(suppress:4200)
4011 uint8_t data[0];
4012 } *m_head, * m_tail;
4013 };
4014
4018 class diag_file : public basic_file {
4019 public:
4020 diag_file(_In_count_(num_files) basic_file* const* files, _In_ size_t num_files) :
4021 basic(num_files ? files[0]->state() : state_t::fail),
4022 m_files(files, files + num_files)
4023 {}
4024
4025 virtual _Success_(return != 0 || length == 0) size_t read(
4026 _Out_writes_bytes_to_opt_(length, return) void* data, _In_ size_t length)
4027 {
4028 _Assume_(data || !length);
4029 if (m_files.empty()) {
4030 m_state = state_t::fail;
4031 return 0;
4032 }
4033 size_t result = m_files[0]->read(data, length);
4034 _Assume_(result <= length);
4035 m_state = m_files[0]->state();
4036 if (length > m_tmp.size())
4037 m_tmp.resize(length);
4038 for (size_t i = 1, n = m_files.size(); i < n; ++i) {
4039 if (m_files[i]->read(m_tmp.data(), length) != result ||
4040 memcmp(m_tmp.data(), data, result))
4041 throw std::runtime_error("read mismatch");
4042 if (m_files[i]->state() != m_state)
4043 throw std::runtime_error("state mismatch");
4044 }
4045 return result;
4046 }
4047
4048 virtual _Success_(return != 0) size_t write(
4049 _In_reads_bytes_opt_(length) const void* data, _In_ size_t length)
4050 {
4051 if (m_files.empty()) {
4052 m_state = state_t::fail;
4053 return 0;
4054 }
4055 size_t result = m_files[0]->write(data, length);
4056 m_state = m_files[0]->state();
4057 for (size_t i = 1, n = m_files.size(); i < n; ++i) {
4058 if (m_files[i]->write(data, length) != result)
4059 throw std::runtime_error("write mismatch");
4060 if (m_files[i]->state() != m_state)
4061 throw std::runtime_error("state mismatch");
4062 }
4063 return result;
4064 }
4065
4066 virtual void flush()
4067 {
4068 if (m_files.empty()) {
4069 m_state = state_t::ok;
4070 return;
4071 }
4072 m_files[0]->flush();
4073 m_state = m_files[0]->state();
4074 for (size_t i = 1, n = m_files.size(); i < n; ++i) {
4075 m_files[i]->flush();
4076 if (m_files[i]->state() != m_state)
4077 throw std::runtime_error("state mismatch");
4078 }
4079 }
4080
4081 virtual void close()
4082 {
4083 if (m_files.empty()) {
4084 m_state = state_t::ok;
4085 return;
4086 }
4087 m_files[0]->close();
4088 m_state = m_files[0]->state();
4089 for (size_t i = 1, n = m_files.size(); i < n; ++i) {
4090 m_files[i]->close();
4091 if (m_files[i]->state() != m_state)
4092 throw std::runtime_error("state mismatch");
4093 }
4094 m_tmp.clear();
4095 m_tmp.shrink_to_fit();
4096 }
4097
4098 virtual fpos_t seek(_In_ foff_t offset, _In_ seek_t how = seek_t::beg)
4099 {
4100 if (m_files.empty()) {
4101 m_state = state_t::fail;
4102 return fpos_max;
4103 }
4104 fpos_t result = m_files[0]->seek(offset, how);
4105 m_state = m_files[0]->state();
4106 for (size_t i = 1, n = m_files.size(); i < n; ++i) {
4107 if (m_files[i]->seek(offset, how) != result)
4108 throw std::runtime_error("seek mismatch");
4109 if (m_files[i]->state() != m_state)
4110 throw std::runtime_error("state mismatch");
4111 }
4112 return result;
4113 }
4114
4115 virtual fpos_t tell() const
4116 {
4117 if (m_files.empty())
4118 return fpos_max;
4119 fpos_t result = m_files[0]->tell();
4120 for (size_t i = 1, n = m_files.size(); i < n; ++i) {
4121 if (m_files[i]->tell() != result)
4122 throw std::runtime_error("tell mismatch");
4123 }
4124 return result;
4125 }
4126
4127 virtual void lock(_In_ fpos_t offset, _In_ fsize_t length)
4128 {
4129 if (m_files.empty())
4130 m_state = state_t::fail;
4131 m_files[0]->lock(offset, length);
4132 m_state = m_files[0]->state();
4133 for (size_t i = 1, n = m_files.size(); i < n; ++i) {
4134 m_files[i]->lock(offset, length);
4135 if (m_files[i]->state() != m_state)
4136 throw std::runtime_error("state mismatch");
4137 }
4138 }
4139
4140 virtual void unlock(_In_ fpos_t offset, _In_ fsize_t length)
4141 {
4142 if (m_files.empty())
4143 m_state = state_t::fail;
4144 m_files[0]->unlock(offset, length);
4145 m_state = m_files[0]->state();
4146 for (size_t i = 1, n = m_files.size(); i < n; ++i) {
4147 m_files[i]->unlock(offset, length);
4148 if (m_files[i]->state() != m_state)
4149 throw std::runtime_error("state mismatch");
4150 }
4151 }
4152
4153 virtual fsize_t size()
4154 {
4155 if (m_files.empty()) {
4156 m_state = state_t::fail;
4157 return 0;
4158 }
4159 fsize_t result = m_files[0]->size();
4160 m_state = m_files[0]->state();
4161 for (size_t i = 1, n = m_files.size(); i < n; ++i) {
4162 if (m_files[i]->size() != result)
4163 throw std::runtime_error("size mismatch");
4164 if (m_files[i]->state() != m_state)
4165 throw std::runtime_error("state mismatch");
4166 }
4167 return result;
4168 }
4169
4170 virtual void truncate()
4171 {
4172 if (m_files.empty())
4173 m_state = state_t::fail;
4174 m_files[0]->truncate();
4175 m_state = m_files[0]->state();
4176 for (size_t i = 1, n = m_files.size(); i < n; ++i) {
4177 m_files[i]->truncate();
4178 if (m_files[i]->state() != m_state)
4179 throw std::runtime_error("state mismatch");
4180 }
4181 }
4182
4183 protected:
4184 std::vector<basic_file*> m_files;
4185 std::vector<uint8_t> m_tmp;
4186 };
4187 }
4188}
Encoding converter context.
Definition unicode.hpp:133
Provides read-ahead stream capability.
Definition stream.hpp:1256
virtual size_t read(_Out_writes_bytes_to_opt_(length, return) void *data, size_t length)
Reads block of data from the stream.
Definition stream.hpp:1270
Provides write-back stream capability.
Definition stream.hpp:1323
virtual void flush()
Persists volatile element data.
Definition stream.hpp:1360
virtual size_t write(_In_reads_bytes_opt_(length) const void *data, size_t length)
Writes block of data to the stream.
Definition stream.hpp:1336
Basic seekable stream operations.
Definition stream.hpp:824
virtual void skip(fsize_t amount)
Skips given amount of bytes of data on the stream.
Definition stream.hpp:867
virtual time_point ctime() const
Returns file creation time.
Definition stream.hpp:914
virtual void lock(fpos_t offset, fsize_t length)
Locks file section for exclusive access.
Definition stream.hpp:883
virtual void truncate()=0
Sets file size - truncates the remainder of file content from the current file position to the end of...
charset_id read_charset(charset_id default_charset=charset_id::system)
Attempts to detect textfile charset based on UTF-32, UTF-16 or UTF-8 BOM.
Definition stream.hpp:987
fpos_t seekbeg(fpos_t offset)
Seeks to absolute file position.
Definition stream.hpp:851
virtual std::vector< uint8_t > read_remainder(size_t max_length=SIZE_MAX)
Reads and returns remainder of the stream.
Definition stream.hpp:826
virtual void set_mtime(time_point date)
Sets file modification time.
Definition stream.hpp:956
fpos_t seekcur(foff_t offset)
Seeks to relative from current file position.
Definition stream.hpp:858
virtual time_point atime() const
Returns file access time.
Definition stream.hpp:922
virtual void set_ctime(time_point date)
Sets file create time.
Definition stream.hpp:938
virtual fsize_t size()=0
Returns file size Should the file size cannot be determined, the method returns fsize_max and it does...
virtual void unlock(fpos_t offset, fsize_t length)
Unlocks file section for exclusive access.
Definition stream.hpp:893
virtual fpos_t tell() const =0
Returns absolute file position in file or fpos_max if fails. This method does not update stream state...
virtual time_point mtime() const
Returns file modification time.
Definition stream.hpp:930
fpos_t seekend(foff_t offset)
Seeks to relative from end file position.
Definition stream.hpp:865
virtual void set_atime(time_point date)
Sets file access time.
Definition stream.hpp:947
virtual fpos_t seek(foff_t offset, seek_t how=seek_t::beg)=0
Seeks to specified relative file position.
OS data stream (file, pipe, socket...)
Definition stream.hpp:2196
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:2253
virtual void flush()
Persists volatile element data.
Definition stream.hpp:2311
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:2203
virtual void close()
Closes the stream.
Definition stream.hpp:2300
‍UTF-8 byte-order-mark
Definition stream.hpp:79
bool ok() const
Returns true if the stream state is clean i.e. previous operation was succesful.
Definition stream.hpp:175
size_t write_vsprintf(_Printf_format_string_params_(2) const char *format, locale_t locale, va_list params)
Writes formatted string to the stream.
Definition stream.hpp:637
size_t write_array(const std::basic_string< T_from, _Traits, _Ax > &wstr, charset_encoder< T_from, T_to > &encoder)
Writes array of characters to the stream.
Definition stream.hpp:455
state_t state() const
Returns stream state after last operation.
Definition stream.hpp:170
basic & read_str(std::basic_string< _Elem, _Traits, _Ax > &data)
Reads length-prefixed string from the stream.
Definition stream.hpp:477
size_t write_sprintf(_Printf_format_string_params_(2) const wchar_t *format, locale_t locale,...)
Writes formatted string to the stream.
Definition stream.hpp:623
size_t write_vsprintf(_Printf_format_string_params_(2) const wchar_t *format, locale_t locale, va_list params)
Writes formatted string to the stream.
Definition stream.hpp:650
virtual void flush()
Persists volatile element data.
Definition stream.hpp:126
virtual void skip(fsize_t amount)
Skips given amount of bytes of data on the stream.
Definition stream.hpp:142
virtual void close()
Closes the stream.
Definition stream.hpp:134
uint8_t read_byte()
Reads one byte of data.
Definition stream.hpp:210
virtual std::vector< uint8_t > read_remainder(size_t max_length=SIZE_MAX)
Reads and returns remainder of the stream.
Definition stream.hpp:184
size_t write_sprintf(_Printf_format_string_params_(2) const char *format, locale_t locale,...)
Writes formatted string to the stream.
Definition stream.hpp:609
size_t readln(std::basic_string< char, _Traits, _Ax > &str)
Reads stream to the end-of-line or end-of-file.
Definition stream.hpp:306
size_t readln_and_attach(std::basic_string< _Elem, _Traits, _Ax > &str)
Reads stream to the end-of-line or end-of-file and append to str.
Definition stream.hpp:346
size_t read_array(_Out_writes_bytes_(size *count) void *array, size_t size, size_t count)
Reads an array of data from the stream.
Definition stream.hpp:382
basic & write_str(const T *data)
Writes string to the stream length-prefixed.
Definition stream.hpp:509
virtual size_t read(_Out_writes_bytes_to_opt_(length, return) void *data, size_t length)
Reads block of data from the stream.
Definition stream.hpp:96
size_t readln(std::basic_string< wchar_t, _Traits, _Ax > &wstr)
Reads stream to the end-of-line or end-of-file.
Definition stream.hpp:318
void write_charset(charset_id charset)
Writes UTF8, UTF-16 or UTF-32 byte-order-mark.
Definition stream.hpp:594
basic & write_str(const std::basic_string< _Elem, _Traits, _Ax > &data)
Writes string to the stream length-prefixed.
Definition stream.hpp:534
basic & write_data(const T data)
Writes one primitive data type.
Definition stream.hpp:287
size_t write_array(const T_from *wstr, charset_encoder< T_from, T_to > &encoder)
Writes array of characters to the stream.
Definition stream.hpp:414
size_t readln_and_attach(std::basic_string< T_to, _Traits, _Ax > &wstr, charset_encoder< T_from, T_to > &encoder)
Reads stream to the end-of-line or end-of-file and append to str.
Definition stream.hpp:367
fsize_t write_stream(basic &stream, fsize_t amount=fsize_max)
Writes content of another stream.
Definition stream.hpp:569
virtual size_t write(_In_reads_bytes_opt_(length) const void *data, size_t length)
Writes block of data to the stream.
Definition stream.hpp:114
size_t write_array(_In_reads_or_z_opt_(num_chars) const T_from *wstr, size_t num_chars, charset_encoder< T_from, T_to > &encoder)
Writes array of characters to the stream.
Definition stream.hpp:435
size_t readln(std::basic_string< T_to, _Traits, _Ax > &wstr, charset_encoder< T_from, T_to > &encoder)
Reads stream to the end-of-line or end-of-file.
Definition stream.hpp:330
size_t write_array(_In_reads_bytes_opt_(size *count) const void *array, size_t size, size_t count)
Writes an array of data to the stream.
Definition stream.hpp:400
void write_byte(uint8_t byte, fsize_t amount=1)
Writes a byte of data.
Definition stream.hpp:221
basic & read_data(T &data)
Reads one primitive data type.
Definition stream.hpp:259
Buffered read/write stream.
Definition stream.hpp:1394
virtual void flush()
Persists volatile element data.
Definition stream.hpp:1503
virtual size_t read(_Out_writes_bytes_to_opt_(length, return) void *data, size_t length)
Reads block of data from the stream.
Definition stream.hpp:1424
virtual size_t write(_In_reads_bytes_opt_(length) const void *data, size_t length)
Writes block of data to the stream.
Definition stream.hpp:1460
Buffered OS data stream (file, pipe, socket...)
Definition stream.hpp:2325
Cached file.
Definition stream.hpp:1804
virtual time_point ctime() const
Returns file creation time.
Definition stream.hpp:2066
virtual void truncate()
Sets file size - truncates the remainder of file content from the current file position to the end of...
Definition stream.hpp:2045
virtual size_t read(_Out_writes_bytes_to_opt_(length, return) void *data, size_t length)
Reads block of data from the stream.
Definition stream.hpp:1871
virtual time_point atime() const
Returns file access time.
Definition stream.hpp:2071
virtual fsize_t size()
Returns file size Should the file size cannot be determined, the method returns fsize_max and it does...
Definition stream.hpp:2038
virtual void unlock(fpos_t offset, fsize_t length)
Unlocks file section for exclusive access.
Definition stream.hpp:2032
virtual time_point mtime() const
Returns file modification time.
Definition stream.hpp:2080
virtual void close()
Closes the stream.
Definition stream.hpp:1986
virtual void set_mtime(time_point date)
Sets file modification time.
Definition stream.hpp:2102
virtual void lock(fpos_t offset, fsize_t length)
Locks file section for exclusive access.
Definition stream.hpp:2026
virtual void flush()
Persists volatile element data.
Definition stream.hpp:1995
virtual size_t write(_In_reads_bytes_opt_(length) const void *data, size_t length)
Writes block of data to the stream.
Definition stream.hpp:1933
virtual void set_ctime(time_point date)
Sets file create time.
Definition stream.hpp:2089
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:2021
virtual void set_atime(time_point date)
Sets file access time.
Definition stream.hpp:2094
virtual fpos_t seek(foff_t offset, seek_t how=seek_t::beg)
Seeks to specified relative file position.
Definition stream.hpp:2006
Cached file-system file.
Definition stream.hpp:3095
cached_file(const stdex::sstring &filename, int mode, size_t cache_size=default_cache_size)
Opens file.
Definition stream.hpp:3125
void open(const stdex::sstring &filename, int mode)
Opens file.
Definition stream.hpp:3159
void open(const schar_t *filename, int mode)
Opens file.
Definition stream.hpp:3138
cached_file(const schar_t *filename, int mode, size_t cache_size=default_cache_size)
Opens file.
Definition stream.hpp:3111
Modifies data on the fly when reading from/writing to a source stream. Could also be used to modify r...
Definition stream.hpp:1022
virtual void flush()
Persists volatile element data.
Definition stream.hpp:1073
virtual void close()
Closes the stream.
Definition stream.hpp:1067
virtual size_t read(_Out_writes_bytes_to_opt_(length, return) void *data, size_t length)
Reads block of data from the stream.
Definition stream.hpp:1051
virtual size_t write(_In_reads_bytes_opt_(length) const void *data, size_t length)
Writes block of data to the stream.
Definition stream.hpp:1059
Compares multiple files to perform the same.
Definition stream.hpp:4018
virtual fsize_t size()
Returns file size Should the file size cannot be determined, the method returns fsize_max and it does...
Definition stream.hpp:4153
virtual void truncate()
Sets file size - truncates the remainder of file content from the current file position to the end of...
Definition stream.hpp:4170
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:4048
virtual void close()
Closes the stream.
Definition stream.hpp:4081
virtual void lock(fpos_t offset, fsize_t length)
Locks file section for exclusive access.
Definition stream.hpp:4127
virtual void unlock(fpos_t offset, fsize_t length)
Unlocks file section for exclusive access.
Definition stream.hpp:4140
virtual fpos_t seek(foff_t offset, seek_t how=seek_t::beg)
Seeks to specified relative file position.
Definition stream.hpp:4098
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:4115
virtual void flush()
Persists volatile element data.
Definition stream.hpp:4066
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:4025
In-memory FIFO queue.
Definition stream.hpp:3919
virtual void close()
Closes the stream.
Definition stream.hpp:3989
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:3966
size_t size() const
Returns total size of pending data in the queue.
Definition stream.hpp:4003
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:3938
Limits file reading/writing to a predefined window.
Definition stream.hpp:1694
virtual void truncate()
Sets file size - truncates the remainder of file content from the current file position to the end of...
Definition stream.hpp:1787
virtual void flush()
Persists volatile element data.
Definition stream.hpp:1737
virtual void skip(fsize_t amount)
Skips given amount of bytes of data on the stream.
Definition stream.hpp:1750
virtual fpos_t seek(foff_t offset, seek_t how=seek_t::beg)
Seeks to specified relative file position.
Definition stream.hpp:1743
virtual fsize_t size()
Returns file size Should the file size cannot be determined, the method returns fsize_max and it does...
Definition stream.hpp:1782
virtual size_t write(_In_reads_bytes_opt_(length) const void *data, size_t length)
Writes block of data to the stream.
Definition stream.hpp:1717
virtual void lock(fpos_t offset, fsize_t length)
Locks file section for exclusive access.
Definition stream.hpp:1762
virtual size_t read(_Out_writes_bytes_to_opt_(length, return) void *data, size_t length)
Reads block of data from the stream.
Definition stream.hpp:1703
virtual void unlock(fpos_t offset, fsize_t length)
Unlocks file section for exclusive access.
Definition stream.hpp:1772
virtual fpos_t tell() const
Returns absolute file position in file or fpos_max if fails. This method does not update stream state...
Definition stream.hpp:1756
virtual void close()
Closes the stream.
Definition stream.hpp:1731
File-system file.
Definition stream.hpp:2691
file(const stdex::sstring &filename, int mode)
Opens file.
Definition stream.hpp:2712
static bool readonly(const stdex::sstring &filename)
Checks if file/folder/symlink is read-only.
Definition stream.hpp:3084
virtual time_point mtime() const
Returns file modification time.
Definition stream.hpp:2967
virtual void unlock(fpos_t offset, fsize_t length)
Unlocks file section for exclusive access.
Definition stream.hpp:2860
file(const schar_t *filename, int mode)
Opens file.
Definition stream.hpp:2701
virtual void set_ctime(time_point date)
Sets file create time.
Definition stream.hpp:2981
static bool readonly(const stdex::schar_t *filename)
Checks if file/folder/symlink is read-only.
Definition stream.hpp:3066
static bool exists(const stdex::sstring &filename)
Checks if file/folder/symlink likely exists.
Definition stream.hpp:3054
virtual time_point atime() const
Returns file access time.
Definition stream.hpp:2953
void open(const schar_t *filename, int mode)
Opens file.
Definition stream.hpp:2720
virtual void set_mtime(time_point date)
Sets file modification time.
Definition stream.hpp:3015
virtual void set_atime(time_point date)
Sets file access time.
Definition stream.hpp:2995
virtual void lock(fpos_t offset, fsize_t length)
Locks file section for exclusive access.
Definition stream.hpp:2837
virtual void truncate()
Sets file size - truncates the remainder of file content from the current file position to the end of...
Definition stream.hpp:2903
virtual time_point ctime() const
Returns file creation time.
Definition stream.hpp:2943
void open(const stdex::sstring &filename, int mode)
Opens file.
Definition stream.hpp:2793
virtual fsize_t size()
Returns file size Should the file size cannot be determined, the method returns fsize_max and it does...
Definition stream.hpp:2885
virtual fpos_t seek(foff_t offset, seek_t how=seek_t::beg)
Seeks to specified relative file position.
Definition stream.hpp:2798
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:2819
static bool exists(const stdex::schar_t *filename)
Checks if file/folder/symlink likely exists.
Definition stream.hpp:3039
Limits reading from/writing to stream to a predefined number of bytes.
Definition stream.hpp:1551
fsize_t read_limit
Number of bytes left that may be read from the stream.
Definition stream.hpp:1603
virtual size_t read(_Out_writes_bytes_to_opt_(length, return) void *data, size_t length)
Reads block of data from the stream.
Definition stream.hpp:1559
virtual size_t write(_In_reads_bytes_opt_(length) const void *data, size_t length)
Writes block of data to the stream.
Definition stream.hpp:1580
fsize_t write_limit
Number of bytes left, that can be written to the stream.
Definition stream.hpp:1604
In-memory file.
Definition stream.hpp:3172
memory_file(const schar_t *filename, int mode)
Loads content from file-system file.
Definition stream.hpp:3250
memory_file & write_str(const std::basic_string< _Elem, _Traits, _Ax > &data)
Writes string to the stream length-prefixed.
Definition stream.hpp:3634
size_t m_size
file size
Definition stream.hpp:3906
void get(fpos_t offset, T &data)
Reads data from specified file location This does not move file pointer. It checks for data size Assu...
Definition stream.hpp:3844
size_t write_stream(basic &stream, size_t amount=SIZE_MAX)
Writes content of another stream.
Definition stream.hpp:3669
uint8_t * m_data
file data
Definition stream.hpp:3903
memory_file & read_data(T &data)
Reads one primitive data type.
Definition stream.hpp:3423
virtual void close()
Closes the stream.
Definition stream.hpp:3719
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:3384
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:3750
size_t m_reserved
reserved file size
Definition stream.hpp:3907
memory_file(size_t size, state_t state=state_t::ok)
Creates an empty file of reserved size.
Definition stream.hpp:3193
void reserve(size_t required, bool tight=false) noexcept
Reallocates memory.
Definition stream.hpp:3275
memory_file(const stdex::sstring &filename, int mode)
Loads content from file-system file.
Definition stream.hpp:3261
memory_file & read_str(std::basic_string< _Elem, _Traits, _Ax > &data)
Reads length-prefixed string from the stream.
Definition stream.hpp:3463
void write_byte(uint8_t byte, size_t amount=1)
Writes a byte of data.
Definition stream.hpp:3518
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:3810
void load(const stdex::sstring &filename, int mode)
Loads content from a file-system file.
Definition stream.hpp:3337
size_t m_offset
file pointer
Definition stream.hpp:3905
void save(const schar_t *filename, int mode)
Saves content to a file-system file.
Definition stream.hpp:3348
void load(const schar_t *filename, int mode)
Loads content from a file-system file.
Definition stream.hpp:3304
virtual fsize_t size()
Returns file size Should the file size cannot be determined, the method returns fsize_max and it does...
Definition stream.hpp:3755
virtual fpos_t seek(foff_t offset, seek_t how=seek_t::beg)
Seeks to specified relative file position.
Definition stream.hpp:3733
virtual void truncate()
Sets file size - truncates the remainder of file content from the current file position to the end of...
Definition stream.hpp:3760
memory_file & write_data(const T data)
Writes one primitive data type.
Definition stream.hpp:3551
memory_file & write_str(const T *data)
Writes string to the stream length-prefixed.
Definition stream.hpp:3589
void save(const stdex::sstring &filename, int mode)
Saves content to a file-system file.
Definition stream.hpp:3374
bool m_manage
may reallocate m_data?
Definition stream.hpp:3904
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:3240
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:3494
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:3217
const void * data() const
Returns pointer to data.
Definition stream.hpp:3382
Definition stream.hpp:1170
enum stdex::stream::replicator::worker::op_t op
Operation to perform.
size_t num_written
Number of bytes written.
Definition stream.hpp:1219
size_t length
Byte limit of data to write.
Definition stream.hpp:1218
const void * data
Data to write.
Definition stream.hpp:1217
Replicates writing of the same data to multiple streams.
Definition stream.hpp:1087
void push_back(basic *source)
Adds stream on the list.
Definition stream.hpp:1106
virtual void flush()
Persists volatile element data.
Definition stream.hpp:1163
void remove(basic *source)
Removes stream from the list.
Definition stream.hpp:1114
virtual size_t write(_In_reads_bytes_opt_(length) const void *data, size_t length)
Writes block of data to the stream.
Definition stream.hpp:1131
virtual void close()
Closes the stream.
Definition stream.hpp:1158
Socket stream.
Definition stream.hpp:2347
socket_t get() const noexcept
Returns socket handle.
Definition stream.hpp:2403
virtual void close()
Closes the stream.
Definition stream.hpp:2449
socket(int af, int type, int protocol)
Creates a socket.
Definition stream.hpp:2382
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:2405
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:2429
Limits reading from/writing to stream to a predefined window.
Definition stream.hpp:1611
fpos_t write_offset
Number of bytes to discard on write.
Definition stream.hpp:1687
virtual size_t write(_In_reads_bytes_opt_(length) const void *data, size_t length)
Writes block of data to the stream.
Definition stream.hpp:1648
virtual size_t read(_Out_writes_bytes_to_opt_(length, return) void *data, size_t length)
Reads block of data from the stream.
Definition stream.hpp:1619
fpos_t read_offset
Number of bytes to skip on read.
Definition stream.hpp:1686
Operating system object (file, pipe, anything with an OS handle etc.)
Definition system.hpp:93
virtual void close()
Closes object.
Definition system.hpp:134
Numerical interval.
Definition interval.hpp:18
bool contains(T x) const
Is value in interval?
Definition interval.hpp:79
T size() const
Returns interval size.
Definition interval.hpp:47
T end
interval end
Definition interval.hpp:20
T start
interval start
Definition interval.hpp:19
Definition stream.hpp:1528
Definition stream.hpp:4007