Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
INI/Writer.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/Streams/TextToBinary.h"
7
8#include "Writer.h"
9
10using std::byte;
11
12using namespace Stroika::Foundation;
15using namespace Stroika::Foundation::Streams;
16
20using namespace DataExchange::Variant;
21
22/*
23 ********************************************************************************
24 *********************** DataExchange::Variant::INI::Writer *********************
25 ********************************************************************************
26 */
27class INI::Writer::Rep_ : public Variant::Writer::_IRep, public Memory::UseBlockAllocationIfAppropriate<Rep_> {
28public:
29 Rep_ ()
30 {
31 }
32 virtual _SharedPtrIRep Clone () const override
33 {
34 return make_shared<Rep_> (); // no instance data
35 }
36 virtual optional<filesystem::path> GetDefaultFileSuffix () const override
37 {
38 return ".ini"sv;
39 }
40 virtual void Write (const VariantValue& v, const OutputStream::Ptr<byte>& out) const override
41 {
42 Write (v, TextToBinary::Writer::New (out, UnicodeExternalEncodings::eUTF8, ByteOrderMark::eDontInclude));
43 }
44 virtual void Write (const VariantValue& v, const OutputStream::Ptr<Character>& out) const override
45 {
46 Write (Convert (v), out);
47 }
48 nonvirtual void Write (const Profile& profile, const OutputStream::Ptr<Characters::Character>& out) const
49 {
50#if USE_NOISY_TRACE_IN_THIS_MODULE_
51 Debug::TraceContextBumper ctx{"DataExchange::Variant::INI::Writer::Rep_::Write"};
52#endif
53 Write (nullopt, profile.fUnnamedSection, out);
54 for (const auto& sectionKVP : profile.fNamedSections) {
55 Write (sectionKVP.fKey, sectionKVP.fValue, out);
56 }
57 }
58 nonvirtual void Write (const optional<String>& sectionName, const Section& profile, const OutputStream::Ptr<Characters::Character>& out) const
59 {
61 if (sectionName) {
62 sb << "["sv << *sectionName << "]"sv << Characters::kEOL<wchar_t>;
63 }
64 for (const Common::KeyValuePair<String, String>& kvp : profile.fProperties) {
65 sb << kvp.fKey << "="sv << kvp.fValue << Characters::kEOL<wchar_t>;
66 }
67 out.WriteLn (sb.str ());
68 }
69};
70
71INI::Writer::Writer ()
72 : inherited{make_shared<Rep_> ()}
73{
74}
75
76void INI::Writer::Write (const Profile& profile, const OutputStream::Ptr<byte>& out)
77{
78 return Write (profile, Streams::TextToBinary::Writer::New (out));
79}
80
81void INI::Writer::Write (const Profile& profile, const OutputStream::Ptr<Characters::Character>& out)
82{
83 Debug::UncheckedDynamicCast<Rep_&> (_GetRep ()).Write (profile, out);
84}
85
86Memory::BLOB INI::Writer::WriteAsBLOB (const Profile& profile)
87{
88 return _WriteAsBLOBHelper ([&profile, this] (const OutputStream::Ptr<byte>& out) { Write (profile, out); });
89}
90
91String INI::Writer::WriteAsString (const Profile& profile)
92{
93 return _WriteAsStringHelper ([&profile, this] (const OutputStream::Ptr<Characters::Character>& out) { Write (profile, out); });
94}
conditional_t< qStroika_Foundation_Memory_PreferBlockAllocation and andTrueCheck, BlockAllocationUseHelper< T >, Common::Empty > UseBlockAllocationIfAppropriate
Use this to enable block allocation for a particular class. Beware of subclassing.
Similar to String, but intended to more efficiently construct a String. Mutable type (String is large...
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
Simple variant-value (case variant union) object, with (variant) basic types analogous to a value in ...
OutputStream<>::Ptr is Smart pointer to a stream-based sink of data.
nonvirtual void WriteLn(ELT_2_WRITE &&arg) const
Iterable<T> is a base class for containers which easily produce an Iterator<T> to traverse them.
Definition Iterable.h:237
An Iterator<T> is a copyable object which allows traversing the contents of some container....
Definition Iterator.h:225