12#include "internal.hpp"
28 template <
class _Elem,
class _Traits>
32 std::basic_ostream<_Elem, _Traits> &sp;
34 inline basic_ostreamfmt(_Inout_ std::basic_ostream<_Elem, _Traits> &stream) : sp(stream) {}
36 using pos_type =
typename _Traits::pos_type;
37 using off_type =
typename _Traits::off_type;
38 inline pos_type tellp() {
return sp.tellp(); }
41 inline bool good()
const noexcept {
return sp.good(); }
42 inline bool eof()
const noexcept {
return sp.eof(); }
43 inline bool fail()
const noexcept {
return sp.fail(); }
44 inline bool bad()
const noexcept {
return sp.bad(); }
48 sp.write(
reinterpret_cast<const _Elem*
>(data), size/
sizeof(_Elem));
56 sp.write(
reinterpret_cast<const _Elem*
>(&value),
sizeof(T)/
sizeof(_Elem));
62 size_t count = strlen(value);
63 if (count > UINT32_MAX)
64 throw std::invalid_argument(
"string too big");
65 sp.write(
static_cast<uint32_t
>(count));
66 sp.write(
reinterpret_cast<const _Elem*
>(value), (std::streamsize)count *
sizeof(
char)/
sizeof(_Elem));
72 size_t count = strlen(value);
73 if (count > UINT32_MAX)
74 throw std::invalid_argument(
"string too big");
75 sp.write(
static_cast<uint32_t
>(count));
77 for (
size_t i = 0; i < count; ++i)
80 sp.write(
reinterpret_cast<const _Elem*
>(value), (std::streamsize)count *
sizeof(
wchar_t)/
sizeof(_Elem));
97 template <
class _Elem2>
98 void vprintf(_In_z_ _Printf_format_string_
const _Elem2 *format, _In_opt_ locale_t locale, _In_ va_list arg)
100 std::basic_string<_Elem2> str;
101 vappendf(str, format, locale, arg);
102 sp.write(
reinterpret_cast<const _Elem*
>(str.c_str()), str.size() *
sizeof(_Elem2)/
sizeof(_Elem));
111 template <
class _Elem2>
112 void printf(_In_z_ _Printf_format_string_
const _Elem2 *format, _In_opt_ locale_t locale, ...)
115 va_start(arg, locale);
121 inline basic_ostreamfmt<_Elem, _Traits>& operator <<(_In_ int16_t value) {
return write(value); }
122 inline basic_ostreamfmt<_Elem, _Traits>& operator <<(_In_ int32_t value) {
return write(value); }
123 inline basic_ostreamfmt<_Elem, _Traits>& operator <<(_In_ int64_t value) {
return write(value); }
124 inline basic_ostreamfmt<_Elem, _Traits>& operator <<(_In_ uint8_t value) {
return write(value); }
125 inline basic_ostreamfmt<_Elem, _Traits>& operator <<(_In_ uint16_t value) {
return write(value); }
126 inline basic_ostreamfmt<_Elem, _Traits>& operator <<(_In_ uint32_t value) {
return write(value); }
127 inline basic_ostreamfmt<_Elem, _Traits>& operator <<(_In_ uint64_t value) {
return write(value); }
128#if defined(_NATIVE_SIZE_T_DEFINED) && defined(_WIN64)
129 inline basic_ostreamfmt<_Elem, _Traits>& operator <<(_In_
size_t value) {
return write(value); }
131 inline basic_ostreamfmt<_Elem, _Traits>& operator <<(_In_
float value) {
return write(value); }
132 inline basic_ostreamfmt<_Elem, _Traits>& operator <<(_In_
double value) {
return write(value); }
133 inline basic_ostreamfmt<_Elem, _Traits>& operator <<(_In_
char value) {
return write(value); }
134#ifdef _NATIVE_WCHAR_T_DEFINED
135 inline basic_ostreamfmt<_Elem, _Traits>& operator <<(_In_
wchar_t value) {
return write(value); }
137 inline basic_ostreamfmt<_Elem, _Traits>& operator <<(_In_z_
const char* value) {
return write(value); }
138 inline basic_ostreamfmt<_Elem, _Traits>& operator <<(_In_z_
const wchar_t* value) {
return write(value); }
141 using ostreamfmt = basic_ostreamfmt<char, std::char_traits<char>>;
142 using wostreamfmt = basic_ostreamfmt<wchar_t, std::char_traits<wchar_t>>;
147 template <
class _Elem,
class _Traits>
151 std::basic_istream<_Elem, _Traits> &sg;
153 inline basic_istreamfmt(_Inout_ std::basic_istream<_Elem, _Traits> &stream) : sg(stream) {}
155 using pos_type =
typename _Traits::pos_type;
156 using off_type =
typename _Traits::off_type;
157 inline pos_type tellg() {
return sg.tellg(); }
160 inline bool good()
const noexcept {
return sg.good(); }
161 inline bool eof()
const noexcept {
return sg.eof(); }
162 inline bool fail()
const noexcept {
return sg.fail(); }
163 inline bool bad()
const noexcept {
return sg.bad(); }
164 inline std::streamsize gcount()
const noexcept {
return sg.gcount(); }
168 sg.read(
reinterpret_cast<_Elem*
>(data), size/
sizeof(_Elem));
175 sg.read(
reinterpret_cast<_Elem*
>(&value),
sizeof(T)/
sizeof(_Elem));
181 template <
class _Traits = std::
char_traits<
char>,
class _Alloc = std::allocator<
char>>
188 sg.read(
reinterpret_cast<_Elem*
>(&value[0]), (std::streamsize)count *
sizeof(
char)/
sizeof(_Elem));
193 template <
class _Traits = std::
char_traits<
wchar_t>,
class _Alloc = std::allocator<
wchar_t>>
201 for (
size_t i = 0; i < count; ++i)
204 sg.read(
reinterpret_cast<_Elem*
>(&value[0]), (std::streamsize)count *
sizeof(
wchar_t)/
sizeof(_Elem));
210 inline uint8_t read_byte()
225#if defined(_NATIVE_SIZE_T_DEFINED) && defined(_WIN64)
231#ifdef _NATIVE_WCHAR_T_DEFINED
234 template <
class _Traits = std::
char_traits<
char>,
class _Alloc = std::allocator<
char>>
236 template <
class _Traits = std::
char_traits<
wchar_t>,
class _Alloc = std::allocator<
wchar_t>>
246 template <
class _Elem,
class _Traits>
262 template <
class _Elem,
class _Traits>
268 std::basic_streambuf<_Elem, _Traits>::setg(
const_cast<_Elem*
>(data),
const_cast<_Elem*
>(data),
const_cast<_Elem*
>(data + size));
273 std::basic_streambuf<_Elem, _Traits>::setg(other.eback(), other.gptr(), other.egptr());
278 if (
this != std::addressof(other))
279 std::basic_streambuf<_Elem, _Traits>::operator =(other);
288 virtual pos_type seekoff(off_type off, std::ios_base::seekdir way, std::ios_base::openmode which = std::ios_base::in | std::ios_base::out)
290 if (which & std::ios_base::in) {
293 case std::ios_base::beg: target = eback() +
static_cast<ptrdiff_t
>(off);
break;
294 case std::ios_base::cur: target = gptr() +
static_cast<ptrdiff_t
>(off);
break;
295 case std::ios_base::end: target = egptr() +
static_cast<ptrdiff_t
>(off);
break;
296 default:
throw std::invalid_argument(
"invalid seek reference");
298 if (eback() <= target && target <= egptr()) {
299 gbump(
static_cast<int>(target - gptr()));
300 return pos_type{ off_type{ target - eback() } };
303 return pos_type{ off_type{-1} };
306 virtual pos_type __CLR_OR_THIS_CALL seekpos(pos_type pos, std::ios_base::openmode which = std::ios_base::in | std::ios_base::out)
309 if (which & std::ios_base::in) {
310 _Elem* target = eback() +
static_cast<size_t>(pos);
311 if (eback() <= target && target <= egptr()) {
312 gbump(
static_cast<int>(target - gptr()));
313 return pos_type{ off_type{ target - eback() } };
316 return pos_type{ off_type{-1} };
320 template <
class _Elem,
class _Traits>
326 std::basic_istream<_Elem, _Traits>(&m_buf)
341 inline FILE* filebuf_fhandle(_In_ std::filebuf* rb)
346 inline FILE* filebuf_fhandle(_In_ std::wfilebuf* rb)
348 return (*rb).*get(getter<FILE*, std::wfilebuf>());
356 template <
class _Elem,
class _Traits>
360 using _Mybase = std::basic_fstream<_Elem, _Traits>;
362 using time_point = std::chrono::time_point<std::chrono::file_clock>;
364 using time_point = std::chrono::time_point<std::chrono::system_clock>;
370 _In_z_
const char* file_name,
371 _In_ ios_base::openmode mode = ios_base::in | ios_base::out,
372 _In_
int prot = ios_base::_Default_open_prot) : _Mybase(file_name, mode, prot) {}
375 _In_z_
const wchar_t* file_name,
376 _In_ ios_base::openmode mode = ios_base::in | ios_base::out,
377 _In_
int prot = ios_base::_Default_open_prot) : _Mybase(file_name, mode, prot) {}
379 template<
class _Elem2,
class _Traits2,
class _Ax>
381 _In_
const std::basic_string<_Elem2, _Traits2, _Ax>& str,
382 _In_ ios_base::openmode mode = ios_base::in | ios_base::out,
383 _In_
int prot = ios_base::_Default_open_prot) :
basic_fstream(str.c_str(), mode, prot) {}
395 auto h = os_fhandle();
397 if (h == INVALID_HANDLE_VALUE)
398 throw std::runtime_error(
"invalid handle");
401 pos_lo =
static_cast<LONG
>(pos & 0xffffffff),
402 pos_hi =
static_cast<LONG
>((pos >> 32) & 0xffffffff);
403 if (SetFilePointer(h, pos_lo, &pos_hi, FILE_BEGIN) == INVALID_SET_FILE_POINTER)
404 throw std::runtime_error(
"failed to seek");
405 if (!SetEndOfFile(h))
406 throw std::runtime_error(
"failed to truncate");
419 auto h = os_fhandle();
421 if (h == INVALID_HANDLE_VALUE)
422 throw std::runtime_error(
"invalid handle");
424 if (!GetFileTime(h, NULL, NULL, &ft))
425 throw std::runtime_error(
"failed to get mtime");
427 return time_point(time_point::duration(((
static_cast<int64_t
>(ft.dwHighDateTime) << 32) | ft.dwLowDateTime)));
430 return time_point(time_point::duration(((
static_cast<int64_t
>(ft.dwHighDateTime) << 32) | ft.dwLowDateTime) - 116444736000000000ll));
439 HANDLE os_fhandle()
const
441 FILE* f = filebuf_fhandle(rdbuf());
443 return INVALID_HANDLE_VALUE;
447 return INVALID_HANDLE_VALUE;
449 return (HANDLE)_get_osfhandle(fd);
456 using fstream = basic_fstream<char, std::char_traits<char>>;
457 using wfstream = basic_fstream<wchar_t, std::char_traits<wchar_t>>;
462 template <
class _Elem,
class _Traits,
class _Alloc>
465 using _Mybase = std::basic_stringstream<_Elem, _Traits, _Alloc>;
466 using _Mystr = std::basic_string<_Elem, _Traits, _Alloc>;
470 explicit basic_stringstream(_In_
const _Mystr& str, _In_ std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) : _Mybase(str, mode) {}
481 explicit basic_stringstream(_In_z_
const T* filename, _In_ std::ios_base::openmode mode = std::ios_base::in, _In_
int prot = std::ios_base::_Default_open_prot) :
482 _Mybase(std::ios_base::in | std::ios_base::out | (mode & std::ios_base::binary | std::ios_base::app))
484 std::basic_ifstream<_Elem, _Traits> input(filename, mode & ~(std::ios_base::ate | std::ios_base::app), prot);
485 input.seekg(0, input.end);
486 auto size = input.tellg();
488 throw std::runtime_error(
"file too big to fit into memory");
489 str().reserve(
static_cast<size_t>(size));
493 input.read(buf, _countof(buf));
494 write(buf, input.gcount());
495 }
while (!input.eof());
496 if (!(mode & (std::ios_base::ate | std::ios_base::app)))
507 template <
class _Elem2,
class _Traits2 = std::
char_traits<_Elem2>,
class _Alloc2 = std::allocator<_Elem2>>
508 explicit basic_stringstream(_In_
const std::basic_string<_Elem2, _Traits2, _Alloc2>& filename, _In_ std::ios_base::openmode mode = std::ios_base::in, _In_
int prot = std::ios_base::_Default_open_prot) :
520 void save(_In_z_
const T* filename, _In_ std::ios_base::openmode mode = std::ios_base::out, _In_
int prot = std::ios_base::_Default_open_prot)
522 std::basic_ofstream<_Elem, _Traits> output(filename, mode, prot);
523 auto origin = tellg();
528 read(buf, _countof(buf));
529 output.write(buf, gcount());
534 template <
class _Elem2,
class _Traits2 = std::
char_traits<T>,
class _Alloc2 = std::allocator<T>>
535 void save(_In_
const std::basic_string<_Elem2, _Traits2, _Alloc2>& filename, _In_ std::ios_base::openmode mode = std::ios_base::out, _In_
int prot = std::ios_base::_Default_open_prot)
537 save(filename.data(), mode, prot);
541 using stringstream = basic_stringstream<char, std::char_traits<char>, std::allocator<char>>;
542 using wstringstream = basic_stringstream<wchar_t, std::char_traits<wchar_t>, std::allocator<char>>;
File stream with additional std::filesystem features.
Definition ios.hpp:358
time_point mtime() const
Returns file modification time.
Definition ios.hpp:417
void truncate()
Sets end of file at current put position.
Definition ios.hpp:392
Binary stream reader/writer.
Definition ios.hpp:248
Binary stream reader.
Definition ios.hpp:149
Binary stream writer.
Definition ios.hpp:30
void vprintf(const _Elem2 *format, locale_t locale, va_list arg)
Formats string using printf() and write it to stream.
Definition ios.hpp:98
void printf(const _Elem2 *format, locale_t locale,...)
Formats string using printf() and write it to stream.
Definition ios.hpp:112
Shared-memory string buffer.
Definition ios.hpp:264
String stream.
Definition ios.hpp:463
basic_stringstream(const T *filename, std::ios_base::openmode mode=std::ios_base::in, int prot=std::ios_base::_Default_open_prot)
Initializes stream with content from file.
Definition ios.hpp:481
void save(const T *filename, std::ios_base::openmode mode=std::ios_base::out, int prot=std::ios_base::_Default_open_prot)
Saves stream content to a file.
Definition ios.hpp:520
basic_stringstream(const std::basic_string< _Elem2, _Traits2, _Alloc2 > &filename, std::ios_base::openmode mode=std::ios_base::in, int prot=std::ios_base::_Default_open_prot)
Initializes stream with content from file.
Definition ios.hpp:508
Helper template to allow access to internal std C++ private members.
Definition internal.hpp:30
Helper template to allow access to internal std C++ private members.
Definition internal.hpp:18