Stroika Library 3.0d25
 
Loading...
Searching...
No Matches
BinaryToText.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Streams_BinaryToText_h_
5#define _Stroika_Foundation_Streams_BinaryToText_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <optional>
10
15
16/**
17 * \file
18 *
19 * \note Code-Status: <a href="Code-Status.md#Beta">Beta</a>
20 */
21
22namespace Stroika::Foundation::Memory {
23 class BLOB;
24}
25namespace Stroika::Foundation::Streams {
26 using Characters::Character;
27}
28
29namespace Stroika::Foundation::Streams::BinaryToText {
30
31 /**
32 * \brief Decode by the text's byte order mark where it has one, and otherwise with fIfNone - as against a CodeCvt alone,
33 * which decodes with exactly that, byte order mark or not
34 *
35 * Given neither, Reader::New and Convert decode by the byte order mark, and otherwise as UTF-8 - as Stroika writes text
36 * (@see Characters::ByteOrderMark).
37 *
38 * \par Example Usage
39 * \code
40 * // a text file a Windows program may have written: as its byte order mark says, else in the ANSI code page
41 * String text = BinaryToText::Reader::New (FileInputStream::New (path), ByteOrderMarkOr{CodeCvt<>::PlatformDefault ()}).ReadAll ();
42 * \endcode
43 */
46 };
47
48 /**
49 * How text was decoded with no CodeCvt named - @see ByteOrderMarkOr, and Reader::New (src) for the byte order mark, else UTF-8.
50 */
51 enum class [[deprecated ("Since Stroika v3.0d25 - use Reader::New (src) or Convert (src) - a byte order mark, else UTF-8 - or "
52 "ByteOrderMarkOr{codeCvt}")]] AutomaticCodeCvtFlags {
53 eReadBOMAndIfNotPresentUseUTF8,
54 eReadBOMAndIfNotPresentUseCurrentLocale,
55
56 eDEFAULT = eReadBOMAndIfNotPresentUseUTF8
57 };
58 DISABLE_COMPILER_MSC_WARNING_START (4996)
59 DISABLE_COMPILER_GCC_WARNING_START ("GCC diagnostic ignored \"-Wdeprecated-declarations\"");
60 DISABLE_COMPILER_CLANG_WARNING_START ("clang diagnostic ignored \"-Wdeprecated-declarations\"");
61 using AutomaticCodeCvtFlags::eReadBOMAndIfNotPresentUseCurrentLocale;
62 using AutomaticCodeCvtFlags::eReadBOMAndIfNotPresentUseUTF8;
63 DISABLE_COMPILER_MSC_WARNING_END (4996)
64 DISABLE_COMPILER_GCC_WARNING_END ("GCC diagnostic ignored \"-Wdeprecated-declarations\"");
65 DISABLE_COMPILER_CLANG_WARNING_END ("clang diagnostic ignored \"-Wdeprecated-declarations\"");
66
67 /**
68 * \brief BinaryToText::Reader::Ptr is an InputStream::Ptr<Character>, usually constructed wrapping some binary object or binary stream
69 *
70 * \note This was called TextInputStreamBinaryAdapter
71 * \note This was called TextReader
72 *
73 * \note This is similar to the .net TextReader (https://msdn.microsoft.com/en-us/library/system.io.textreader(v=vs.110).aspx) except that
74 * much of the 'reading' API is baked into InputStream::Ptr<Character>.
75 *
76 * \note BinaryToText::Reader's are smart about not reading more than they need to from the source Stream (unless you make that stream buffered, in
77 * which case the buffering can cause it to read ahead)
78 *
79 * But BinaryToText::Reader itself doesn't read ahead more than it needs to to complete requested methods.
80 *
81 * \par Example Usage
82 * \code
83 * for (String line : BinaryToText::Reader::New (FileInputStream::New ("/tmp/foo")).ReadLines ()) {
84 * }
85 * \endcode
86 *
87 * \par Example Usage
88 * \code
89 * Assert (BinaryToText::Reader::New (String{"hello world"}).ReadAll () == "hello world");
90 * \endcode
91 *
92 * \note Reading improperly encoded text may result in a RuntimeException indicating improperly encoded characters.
93 *
94 * \note \em Thread-Safety <a href="Thread-Safety.md#C++-Standard-Thread-Safety-For-Envelope-Plus-Must-Externally-Synchronize-Letter">C++-Standard-Thread-Safety-For-Envelope-Plus-Must-Externally-Synchronize-Letter</a>
95 *
96 * Seekable defaults to the same value as that of the underlying stream wrapped.
97 * For the constructor taking const InputStream::Ptr<Character>& src, the seekability mimics that of the original source.
98 * Constructors taking a BLOB, the resulting stream will be seekable..
99 *
100 * But when you specify it explicitly, the given value will be used.
101 *
102 * \note Depending on the underlying source (e.g. binary stream) - maintaining seekability may be expensive in terms
103 * of memory usage.
104 */
105 namespace Reader {
106
107 /**
108 * \brief BinaryToText::Readers produce text in the form of an InputStream of 'Character' objects (so you might get the text with ReadAll()).
109 */
111
112 /**
113 * This flag controls whether the (BinaryToText::Reader) TextReader instance will try to read-ahead (typically in order to cache). This is generally
114 * a good thing, but for some stream uses, its important to not read ahead - e.g. in the underlying binary stream contains multiple objects and we
115 * are just reading one).
116 *
117 * \note eReadAheadAllowed doesn't mean the underlying class actually WILL read ahead, only that it is permitted to.
118 */
119 enum class ReadAhead {
120 eReadAheadNever,
121 eReadAheadAllowed,
122 };
123 using ReadAhead::eReadAheadAllowed;
124 using ReadAhead::eReadAheadNever;
125
126 /**
127 * \brief Create an InputStream::Ptr<Character> from the arguments (usually binary source) - which can be used to Read out the text as a string
128 *
129 * \par Example Usage
130 * \code
131 * for (String line : BinaryToText::Reader::New (FileInputStream::New (kProcCPUInfoFileName_, FileInputStream::eNotSeekable)).ReadLines ()) {
132 * DbgTrace ("***in Common::GetSystemConfiguration_CPU capture_ line={}"_f, line);
133 * }
134 * \endcode
135 *
136 * \note New (const InputStream::Ptr<byte>& src,... overloads)
137 * o Seekability
138 * o if not specified, its copied from the src binary stream.
139 * o New (const Traversal::Iterable<Character>& src) produces a seekable stream
140 * o Decoding
141 * o given a CodeCvt, with exactly that
142 * o otherwise by its byte order mark, where src.IsSeekable () and it has one (a stream that cannot seek
143 * is not looked at for one), and failing that with ByteOrderMarkOr's fIfNone, or as UTF-8
144 */
145 Ptr New (const InputStream::Ptr<byte>& src, optional<SeekableFlag> seekable = {}, ReadAhead readAhead = eReadAheadAllowed);
146 Ptr New (const InputStream::Ptr<byte>& src, const ByteOrderMarkOr& ifNoByteOrderMark, optional<SeekableFlag> seekable = {},
147 ReadAhead readAhead = eReadAheadAllowed); ///< \brief ... by its byte order mark, else with ifNoByteOrderMark.fIfNone
148 Ptr New (const InputStream::Ptr<byte>& src, const Characters::CodeCvt<>& codeConverter, optional<SeekableFlag> seekable = {},
149 ReadAhead readAhead = eReadAheadAllowed); ///< \brief ... with exactly codeConverter
150 Ptr New (const InputStream::Ptr<Character>& src); ///< \brief ... text already
151 Ptr New (const Traversal::Iterable<Character>& src); ///< \brief ... text already
152 template <typename... ARGS>
153 Ptr New (Execution::InternallySynchronized internallySynchronized, ARGS... args); ///< \brief ... internally synchronized
154 DISABLE_COMPILER_MSC_WARNING_START (4996)
155 DISABLE_COMPILER_GCC_WARNING_START ("GCC diagnostic ignored \"-Wdeprecated-declarations\"");
156 DISABLE_COMPILER_CLANG_WARNING_START ("clang diagnostic ignored \"-Wdeprecated-declarations\"");
157 [[deprecated ("Since Stroika v3.0d25 - use New (src, seekable, readAhead) - a byte order mark, else UTF-8 - or New (src, "
158 "ByteOrderMarkOr{codeCvt}, ...)")]] Ptr
159 New (const InputStream::Ptr<byte>& src, optional<AutomaticCodeCvtFlags> codeCvtFlags, optional<SeekableFlag> seekable = {},
160 ReadAhead readAhead = eReadAheadAllowed);
161 DISABLE_COMPILER_MSC_WARNING_END (4996)
162 DISABLE_COMPILER_GCC_WARNING_END ("GCC diagnostic ignored \"-Wdeprecated-declarations\"");
163 DISABLE_COMPILER_CLANG_WARNING_END ("clang diagnostic ignored \"-Wdeprecated-declarations\"");
164
165 }
166
167 // note - @todo COULD do this as an OutStream::Ptr/Writer - but never found a need/use
168 namespace Writer {
169 }
170
171 /**
172 * \brief convert BLOB to a String - decoded as Reader::New decodes: by its byte order mark, else as UTF-8 (or with
173 * ifNoByteOrderMark's), or with exactly codeConverter
174 *
175 * \note shorthand for BinaryToText::Reader::New (src, ...).ReadAll ())
176 *
177 * \note could have done with stream, but the API names suggest side-effect free, and that wouldn't be,
178 * since it seeks the src input stream.
179 */
180 Characters::String Convert (const Memory::BLOB& src);
181 Characters::String Convert (const Memory::BLOB& src, const ByteOrderMarkOr& ifNoByteOrderMark); ///< \brief convert BLOB to a String
182 Characters::String Convert (const Memory::BLOB& src, const Characters::CodeCvt<>& codeConverter); ///< \brief convert BLOB to a String
183 DISABLE_COMPILER_MSC_WARNING_START (4996)
184 DISABLE_COMPILER_GCC_WARNING_START ("GCC diagnostic ignored \"-Wdeprecated-declarations\"");
185 DISABLE_COMPILER_CLANG_WARNING_START ("clang diagnostic ignored \"-Wdeprecated-declarations\"");
186 [[deprecated ("Since Stroika v3.0d25 - use Convert (src) - a byte order mark, else UTF-8 - or Convert (src, "
187 "ByteOrderMarkOr{codeCvt})")]] Characters::String
188 Convert (const Memory::BLOB& src, optional<AutomaticCodeCvtFlags> codeCvtFlags);
189 DISABLE_COMPILER_MSC_WARNING_END (4996)
190 DISABLE_COMPILER_GCC_WARNING_END ("GCC diagnostic ignored \"-Wdeprecated-declarations\"");
191 DISABLE_COMPILER_CLANG_WARNING_END ("clang diagnostic ignored \"-Wdeprecated-declarations\"");
192
193}
194
195/*
196 ********************************************************************************
197 ***************************** Implementation Details ***************************
198 ********************************************************************************
199 */
200#include "BinaryToText.inl"
201
202#endif /*_Stroika_Foundation_Streams_BinaryToText_h_*/
Profile Convert(const VariantValue &v)
Definition Profile.cpp:50
CodeCvt unifies byte <-> unicode conversions, vaguely inspired by (and wraps) std::codecvt,...
Definition CodeCvt.h:118
InputStream<>::Ptr is Smart pointer (with abstract Rep) class defining the interface to reading from ...
A Streams::Ptr<ELEMENT_TYPE> is a smart-pointer to a stream of elements of type T.
Definition Stream.h:170
Iterable<T> is a base class for containers which easily produce an Iterator<T> to traverse them.
Definition Iterable.h:234
Decode by the text's byte order mark where it has one, and otherwise with fIfNone - as against a Code...