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));
92 template <
class _Elem2>
93 void vprintf(_In_z_ _Printf_format_string_
const _Elem2 *format, _In_opt_ locale_t locale, _In_ va_list arg)
95 std::basic_string<_Elem2> str;
96 vappendf(str, format, locale, arg);
97 sp.write(
reinterpret_cast<const _Elem*
>(str.c_str()), str.size() *
sizeof(_Elem2)/
sizeof(_Elem));
106 template <
class _Elem2>
107 void printf(_In_z_ _Printf_format_string_
const _Elem2 *format, _In_opt_ locale_t locale, ...)
110 va_start(arg, locale);
116 inline basic_ostreamfmt<_Elem, _Traits>& operator <<(_In_ int16_t value) {
return write(value); }
117 inline basic_ostreamfmt<_Elem, _Traits>& operator <<(_In_ int32_t value) {
return write(value); }
118 inline basic_ostreamfmt<_Elem, _Traits>& operator <<(_In_ int64_t value) {
return write(value); }
119 inline basic_ostreamfmt<_Elem, _Traits>& operator <<(_In_ uint8_t value) {
return write(value); }
120 inline basic_ostreamfmt<_Elem, _Traits>& operator <<(_In_ uint16_t value) {
return write(value); }
121 inline basic_ostreamfmt<_Elem, _Traits>& operator <<(_In_ uint32_t value) {
return write(value); }
122 inline basic_ostreamfmt<_Elem, _Traits>& operator <<(_In_ uint64_t value) {
return write(value); }
123#ifdef _NATIVE_SIZE_T_DEFINED
124 inline basic_ostreamfmt<_Elem, _Traits>& operator <<(_In_
size_t value) {
return write(value); }
126 inline basic_ostreamfmt<_Elem, _Traits>& operator <<(_In_
float value) {
return write(value); }
127 inline basic_ostreamfmt<_Elem, _Traits>& operator <<(_In_
double value) {
return write(value); }
128 inline basic_ostreamfmt<_Elem, _Traits>& operator <<(_In_
char value) {
return write(value); }
129#ifdef _NATIVE_WCHAR_T_DEFINED
130 inline basic_ostreamfmt<_Elem, _Traits>& operator <<(_In_
wchar_t value) {
return write(value); }
132 inline basic_ostreamfmt<_Elem, _Traits>& operator <<(_In_z_
const char* value) {
return write(value); }
133 inline basic_ostreamfmt<_Elem, _Traits>& operator <<(_In_z_
const wchar_t* value) {
return write(value); }
136 using ostreamfmt = basic_ostreamfmt<char, std::char_traits<char>>;
137 using wostreamfmt = basic_ostreamfmt<wchar_t, std::char_traits<wchar_t>>;
142 template <
class _Elem,
class _Traits>
146 std::basic_istream<_Elem, _Traits> &sg;
148 inline basic_istreamfmt(_Inout_ std::basic_istream<_Elem, _Traits> &stream) : sg(stream) {}
150 using pos_type =
typename _Traits::pos_type;
151 using off_type =
typename _Traits::off_type;
152 inline pos_type tellg() {
return sg.tellg(); }
155 inline bool good()
const noexcept {
return sg.good(); }
156 inline bool eof()
const noexcept {
return sg.eof(); }
157 inline bool fail()
const noexcept {
return sg.fail(); }
158 inline bool bad()
const noexcept {
return sg.bad(); }
159 inline std::streamsize gcount()
const noexcept {
return sg.gcount(); }
163 sg.read(
reinterpret_cast<_Elem*
>(data), size/
sizeof(_Elem));
170 sg.read(
reinterpret_cast<_Elem*
>(&value),
sizeof(T)/
sizeof(_Elem));
176 template <
class _Traits = std::
char_traits<
char>,
class _Alloc = std::allocator<
char>>
183 sg.read(
reinterpret_cast<_Elem*
>(&value[0]), (std::streamsize)count *
sizeof(
char)/
sizeof(_Elem));
188 template <
class _Traits = std::
char_traits<
wchar_t>,
class _Alloc = std::allocator<
wchar_t>>
196 for (
size_t i = 0; i < count; ++i)
199 sg.read(
reinterpret_cast<_Elem*
>(&value[0]), (std::streamsize)count *
sizeof(
wchar_t)/
sizeof(_Elem));
213#ifdef _NATIVE_SIZE_T_DEFINED
219#ifdef _NATIVE_WCHAR_T_DEFINED
222 template <
class _Traits = std::
char_traits<
char>,
class _Alloc = std::allocator<
char>>
224 template <
class _Traits = std::
char_traits<
wchar_t>,
class _Alloc = std::allocator<
wchar_t>>
234 template <
class _Elem,
class _Traits>
250 template <
class _Elem,
class _Traits>
256 std::basic_streambuf<_Elem, _Traits>::setg(
const_cast<_Elem*
>(data),
const_cast<_Elem*
>(data),
const_cast<_Elem*
>(data + size));
261 std::basic_streambuf<_Elem, _Traits>::setg(other.eback(), other.gptr(), other.egptr());
266 if (
this != std::addressof(other))
267 std::basic_streambuf<_Elem, _Traits>::operator =(other);
276 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)
278 if (which & std::ios_base::in) {
281 case std::ios_base::beg: target = eback() + off;
break;
282 case std::ios_base::cur: target = gptr() + off;
break;
283 case std::ios_base::end: target = egptr() + off;
break;
284 default:
throw std::invalid_argument(
"invalid seek reference");
286 if (eback() <= target && target <= egptr()) {
287 gbump(
static_cast<int>(target - gptr()));
288 return pos_type{ off_type{ target - eback() } };
291 return pos_type{ off_type{-1} };
294 virtual pos_type __CLR_OR_THIS_CALL seekpos(pos_type pos, std::ios_base::openmode which = std::ios_base::in | std::ios_base::out)
297 if (which & std::ios_base::in) {
298 _Elem* target = eback() + pos;
299 if (eback() <= target && target <= egptr()) {
300 gbump(
static_cast<int>(target - gptr()));
301 return pos_type{ off_type{ target - eback() } };
304 return pos_type{ off_type{-1} };
308 template <
class _Elem,
class _Traits>
314 std::basic_istream<_Elem, _Traits>(&m_buf)
329 inline FILE* filebuf_fhandle(_In_ std::filebuf* rb)
334 inline FILE* filebuf_fhandle(_In_ std::wfilebuf* rb)
336 return (*rb).*get(getter<FILE*, std::wfilebuf>());
344 template <
class _Elem,
class _Traits>
348 using _Mybase = std::basic_fstream<_Elem, _Traits>;
353 _In_z_
const char* file_name,
354 _In_ ios_base::openmode mode = ios_base::in | ios_base::out,
355 _In_
int prot = ios_base::_Default_open_prot) : _Mybase(file_name, mode, prot) {}
358 _In_z_
const wchar_t* file_name,
359 _In_ ios_base::openmode mode = ios_base::in | ios_base::out,
360 _In_
int prot = ios_base::_Default_open_prot) : _Mybase(file_name, mode, prot) {}
362 template<
class _Elem2,
class _Traits2,
class _Ax>
364 _In_
const std::basic_string<_Elem2, _Traits2, _Ax>& str,
365 _In_ ios_base::openmode mode = ios_base::in | ios_base::out,
366 _In_
int prot = ios_base::_Default_open_prot) :
basic_fstream(str.c_str(), mode, prot) {}
378 auto h = os_fhandle();
380 if (h == INVALID_HANDLE_VALUE)
381 throw std::runtime_error(
"invalid handle");
384 pos_lo =
static_cast<LONG
>(pos & 0xffffffff),
385 pos_hi =
static_cast<LONG
>((pos >> 32) & 0xffffffff);
386 if (SetFilePointer(h, pos_lo, &pos_hi, FILE_BEGIN) == INVALID_SET_FILE_POINTER)
387 throw std::runtime_error(
"failed to seek");
388 if (!SetEndOfFile(h))
389 throw std::runtime_error(
"failed to truncate");
396 using time_type = std::chrono::time_point<std::chrono::file_clock>;
398 using time_type = std::chrono::time_point<std::chrono::system_clock>;
408 auto h = os_fhandle();
410 if (h == INVALID_HANDLE_VALUE)
411 throw std::runtime_error(
"invalid handle");
413 if (!GetFileTime(h, NULL, NULL, &ft))
414 throw std::runtime_error(
"failed to get mtime");
416 return time_type(time_type::duration(((
static_cast<int64_t
>(ft.dwHighDateTime) << 32) | ft.dwLowDateTime)));
419 return time_type(time_type::duration(((
static_cast<int64_t
>(ft.dwHighDateTime) << 32) | ft.dwLowDateTime) - 116444736000000000ll));
428 HANDLE os_fhandle()
const
430 FILE* f = filebuf_fhandle(rdbuf());
432 return INVALID_HANDLE_VALUE;
436 return INVALID_HANDLE_VALUE;
438 return (HANDLE)_get_osfhandle(fd);
445 using fstream = basic_fstream<char, std::char_traits<char>>;
446 using wfstream = basic_fstream<wchar_t, std::char_traits<wchar_t>>;
451 template <
class _Elem,
class _Traits,
class _Alloc>
454 using _Mybase = std::basic_stringstream<_Elem, _Traits, _Alloc>;
455 using _Mystr = std::basic_string<_Elem, _Traits, _Alloc>;
459 explicit basic_stringstream(_In_
const _Mystr& str, _In_ std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) : _Mybase(str, mode) {}
470 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) :
471 _Mybase(std::ios_base::in | std::ios_base::out | (mode & std::ios_base::bin | std::ios_base::app))
473 std::basic_ifstream<_Elem, _Traits> input(filename, mode & ~(std::ios_base::ate | std::ios_base::app), prot);
474 input.seekg(0, input.end);
475 auto size = input.tellg();
477 throw std::runtime_error(
"file too big to fit into memory");
478 str.reserve(
static_cast<size_t>(size));
482 input.read(buf, _countof(buf));
483 write(buf, input.gcount());
484 }
while (!input.eof());
485 if (!(mode & (std::ios_base::ate | std::ios_base::app)))
497 explicit basic_stringstream(_In_
const std::basic_string<T>& filename, _In_ std::ios_base::openmode mode = std::ios_base::in, _In_
int prot = std::ios_base::_Default_open_prot) :
502 using stringstream = basic_stringstream<char, std::char_traits<char>, std::allocator<char>>;
503 using wstringstream = basic_stringstream<wchar_t, std::char_traits<wchar_t>, std::allocator<char>>;
File stream with additional std::filesystem features.
Definition ios.hpp:346
void truncate()
Sets end of file at current put position.
Definition ios.hpp:375
time_type mtime() const
Returns file modification time.
Definition ios.hpp:406
Binary stream reader/writer.
Definition ios.hpp:236
Binary stream reader.
Definition ios.hpp:144
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:93
void printf(const _Elem2 *format, locale_t locale,...)
Formats string using printf() and write it to stream.
Definition ios.hpp:107
Shared-memory string buffer.
Definition ios.hpp:252
String stream.
Definition ios.hpp:452
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:470
basic_stringstream(const std::basic_string< 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:497
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