Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
TextToBinary.h
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Streams_TextToBinary_h_
5#define _Stroika_Foundation_Streams_TextToBinary_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
13
14namespace Stroika::Foundation::Streams {
16 using Characters::Character;
18}
19
20namespace Stroika::Foundation::Streams::TextToBinary {
21
22 /**
23 * \brief Take some binary output stream, and make it look like an output stream of (UNICODE) characters (using New - argument - encoding).
24 *
25 * Obviously todo this, there may be some character set mapping/conversion needed. The object
26 * takes constructor arguments to decide how this will he handled.
27 *
28 * TextToBinary::Writer is not seekable. It's possible to implement, but complicated, and performance costly. Very unlikely
29 * to ever be useful.
30 *
31 * \note This API was called TextOutputStreamAdapter
32 *
33 * \note TextToBinary::Writer aggregates its owned sub-stream, so that a Close () on TextToBinary::Writer
34 * will Close that sub-stream.
35 *
36 * Ptr is a copyable smart pointer to a TextToBinary::Writer stream.
37 *
38 * \par Example Usage
39 * \code
40 * Streams::TextToBinary::Writer::Ptr textOut = Streams::TextToBinary::Writer::New (out, UnicodeExternalEncodings::eUTF8, ByteOrderMark::eDoneInclude);
41 * textOut.Write ("{}\r\n"_f (headLine));
42 * ...
43 * \endcode
44 *
45 * \par Example Usage
46 * \code
47 * Streams::TextToBinary::Writer::Ptr textOut = Streams::TextToBinary::Writer::New (binOut);
48 * textOut.Write ("Hello World\n");
49 * \endcode
50 *
51 * \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>
52 *
53 * If TextToBinary::Writer given an OutStream<Bytes>, it maps the characters according to the given code page info (@todo improve so generic code page support).
54 * If handled an OutputStream<Character> - it just passes through characters.
55 *
56 * \par Example Usage
57 * \code
58 * Streams::TextToBinary::Writer::Ptr textOut = Streams::TextToBinary::Writer::New (out, UnicodeExternalEncodings::eUTF8, ByteOrderMark::eInclude);
59 * textOut.Write ("{}\r\n"_f (headLine));
60 * ...
61 * \endcode
62 *
63 * \par Example Usage
64 * \code
65 * Streams::TextToBinary::Writer::Ptr textOut = Streams::TextToBinary::Writer::New (binOut);
66 * textOut.Write ("Hello World\n");
67 * \endcode
68 */
69 namespace Writer {
70
71 /**
72 * \brief TextToBinary::Writer wrap some sink (typically a binary stream), and produce a text sink you can Write() text to
73 */
75
76 /**
77 */
78 Ptr New (const Streams::OutputStream::Ptr<byte>& src, const Characters::CodeCvt<>& char2OutputConverter);
79 Ptr New (const Streams::OutputStream::Ptr<byte>& src, UnicodeExternalEncodings e = UnicodeExternalEncodings::eDEFAULT,
80 ByteOrderMark bom = ByteOrderMark::eDontInclude);
82 template <typename... ARGS>
83 Ptr New (Execution::InternallySynchronized internallySynchronized, ARGS... args);
84
85 }
86
87 namespace Reader {
88
89 /**
90 * \brief TextToBinary::Reader wrap some source of text, and produce an binary input stream you can read (converted) bytes from.
91 */
93
94 /**
95 * \brief Stream wrapper that takes an InputStream<Character> and transforms it into an
96 * InputStream<byte> (like TextToBinary::Writer does, but pull rather than push based).
97 *
98 * Draft implementation (not very performant, but doesn't seem used much and easy to tweak)
99 *
100 * DOC CONNECTION TO TextToBinary::Writer and maybe share output/format flags?
101 *
102 * @todo NOTE - this CURRENTLY HARDWIRES converting to UTF-8
103 *
104 * @todo take optional CodeCvt argument, or things you would pass to CodeCvt (character coding) for what binary rep to create!
105 *
106 * WONT change this part of the API - just adding overloads, so OK to release as-is - 2023-07-10
107 */
108 Ptr New (const InputStream::Ptr<Character>& srcStream);
109 Ptr New (const Traversal::Iterable<Character>& srcText);
110
111 }
112
113}
114
115/*
116 ********************************************************************************
117 ***************************** Implementation Details ***************************
118 ********************************************************************************
119 */
120#include "TextToBinary.inl"
121
122#endif /*_Stroika_Foundation_Streams_TextToBinary_h_*/
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 ...
OutputStream<>::Ptr is Smart pointer to a stream-based sink of data.
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:237
UnicodeExternalEncodings
list of external UNICODE character encodings, for file IO (eDEFAULT = eUTF8)
Definition UTFConvert.h:31