Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Streams/iostream/Utilities.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include "Stroika/Foundation/StroikaPreComp.h"
5
6#include "Stroika/Foundation/Containers/Common.h"
7#include "Stroika/Foundation/Execution/Exceptions.h"
8#include "Stroika/Foundation/Execution/Throw.h"
10
11#include "Utilities.h"
12
13// FILE DEPRECATED IN STROIKA v3.0d2
14
15using std::byte;
16
17using namespace Stroika::Foundation;
20using namespace Stroika::Foundation::Execution;
21using namespace Stroika::Foundation::Memory;
22using namespace Stroika::Foundation::Streams;
24
26DISABLE_COMPILER_GCC_WARNING_START ("GCC diagnostic ignored \"-Wdeprecated-declarations\"");
27DISABLE_COMPILER_CLANG_WARNING_START ("clang diagnostic ignored \"-Wdeprecated-declarations\"");
28
29/*
30 ********************************************************************************
31 ********************* Streams::iostream::ReadTextStream ************************
32 ********************************************************************************
33 */
34wstring Streams::iostream::ReadTextStream (istream& in)
35{
36 streamoff start = in.tellg ();
37 in.seekg (0, ios_base::end);
38 streamoff end = in.tellg ();
39 Assert (start <= end);
42 if ((sizeof (streamoff) > sizeof (size_t)) and ((end - start) > static_cast<streamoff> (numeric_limits<ptrdiff_t>::max ()))) [[unlikely]] {
43 static const auto kException_ = Execution::RuntimeErrorException{"stream too large"sv};
44 Execution::Throw (kException_);
45 }
46 size_t bufLen = static_cast<size_t> (end - start);
47 Memory::StackBuffer<byte> buf{Memory::eUninitialized, bufLen};
48 in.seekg (start, ios_base::beg);
49 in.read (reinterpret_cast<char*> (buf.begin ()), bufLen);
50 size_t readLen = static_cast<size_t> (in.gcount ());
51 Assert (readLen <= bufLen);
52 const char* startOfBuf = reinterpret_cast<const char*> (static_cast<const byte*> (buf));
53 return Characters::MapUNICODETextWithMaybeBOMTowstring (startOfBuf, startOfBuf + readLen);
54 DISABLE_COMPILER_MSC_WARNING_END (6237)
55 DISABLE_COMPILER_MSC_WARNING_END (4127)
56}
57
58wstring Streams::iostream::ReadTextStream (wistream& in)
59{
60 streamoff start = in.tellg ();
61 in.seekg (0, ios_base::end);
62 streamoff end = in.tellg ();
63 Assert (start <= end);
66 if ((sizeof (streamoff) > sizeof (size_t)) and ((end - start) > static_cast<streamoff> (numeric_limits<ptrdiff_t>::max ()))) [[unlikely]] {
67 static const auto kException_ = Execution::RuntimeErrorException{"stream too large"sv};
68 Execution::Throw (kException_);
69 }
70 size_t bufLen = static_cast<size_t> (end - start);
71 Memory::StackBuffer<wchar_t> buf{Memory::eUninitialized, bufLen};
72 in.seekg (start, ios_base::beg);
73 in.read (reinterpret_cast<wchar_t*> (buf.begin ()), bufLen);
74 size_t readLen = static_cast<size_t> (in.gcount ());
75 Assert (readLen <= bufLen);
76 const wchar_t* startOfBuf = reinterpret_cast<const wchar_t*> (static_cast<const wchar_t*> (buf));
77 return wstring (startOfBuf, startOfBuf + readLen);
78 DISABLE_COMPILER_MSC_WARNING_END (6237)
79 DISABLE_COMPILER_MSC_WARNING_END (4127)
80}
81
82/*
83 ********************************************************************************
84 *********************** Streams::iostream::ReadBytes ***************************
85 ********************************************************************************
86 */
87vector<byte> Streams::iostream::ReadBytes (istream& in)
88{
89 streamoff start = in.tellg ();
90 in.seekg (0, ios_base::end);
91 streamoff end = in.tellg ();
92 Assert (start <= end);
95 if ((sizeof (streamoff) > sizeof (size_t)) and ((end - start) > static_cast<streamoff> (numeric_limits<ptrdiff_t>::max ()))) [[unlikely]] {
96 static const auto kException_ = Execution::RuntimeErrorException{"stream too large"sv};
97 Execution::Throw (kException_);
98 }
99 size_t len = static_cast<size_t> (end - start);
100 StackBuffer<byte> buf{Memory::eUninitialized, len};
101 in.seekg (start, ios_base::beg);
102 in.read (reinterpret_cast<char*> (buf.begin ()), len);
103 size_t xxx = static_cast<size_t> (in.gcount ());
104 Assert (xxx <= len);
105 return vector<byte> (static_cast<const byte*> (buf), static_cast<const byte*> (buf) + xxx);
106 DISABLE_COMPILER_MSC_WARNING_END (6237)
107 DISABLE_COMPILER_MSC_WARNING_END (4127)
108}
109
110/*
111 ********************************************************************************
112 ********************** Streams::iostream::WriteBytes ***************************
113 ********************************************************************************
114 */
115void Streams::iostream::WriteBytes (ostream& out, const vector<byte>& s)
116{
117 out.write (reinterpret_cast<const char*> (Containers::Start (s)), s.size ());
118}
119
120DISABLE_COMPILER_MSC_WARNING_END (4996);
121DISABLE_COMPILER_GCC_WARNING_END ("GCC diagnostic ignored \"-Wdeprecated-declarations\"");
122DISABLE_COMPILER_CLANG_WARNING_END ("clang diagnostic ignored \"-Wdeprecated-declarations\"");
Logically halfway between std::array and std::vector; Smart 'direct memory array' - which when needed...
CONTAINER::value_type * Start(CONTAINER &c)
For a contiguous container (such as a vector or basic_string) - find the pointer to the start of the ...
void Throw(T &&e2Throw)
identical to builtin C++ 'throw' except that it does helpful, type dependent DbgTrace() messages firs...
Definition Throw.inl:43