Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Profile.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include "Stroika/Foundation/StroikaPreComp.h"
5
7
8#include "Profile.h"
9
10using namespace Stroika::Foundation;
13using namespace Stroika::Foundation::DataExchange::Variant;
14using namespace Stroika::Foundation::DataExchange::Variant::INI;
15
16/*
17 ********************************************************************************
18 ************************** DataExchange::INI::Section **************************
19 ********************************************************************************
20 */
22{
23 return "{ properties: "sv + Characters::ToString (fProperties) + "}"sv;
24}
25
26/*
27 ********************************************************************************
28 ************************** DataExchange::INI::Profile **************************
29 ********************************************************************************
30 */
32{
34 sb << "{"sv;
35 if (not fUnnamedSection.fProperties.empty ()) {
36 sb << "UnnamedSection: "sv << fUnnamedSection << ","sv;
37 }
38 if (not fNamedSections.empty ()) {
39 sb << "NamedSections: "sv << fNamedSections;
40 }
41 sb << "}"sv;
42 return sb;
43}
44
45/*
46 ********************************************************************************
47 ************************** DataExchange::INI::Convert **************************
48 ********************************************************************************
49 */
50Profile INI::Convert (const VariantValue& v)
51{
52 Profile profile;
53 Mapping<String, VariantValue> mv = v.As<Mapping<String, VariantValue>> (); // throws if format mismatch
54 for (const KeyValuePair<String, VariantValue>& kvi : mv) {
55 if (kvi.fValue.GetType () == VariantValue::eMap) {
56 Section newSection;
57 for (const KeyValuePair<String, VariantValue>& namedSectionElt : kvi.fValue.As<Mapping<String, VariantValue>> ()) {
58 newSection.fProperties.Add (namedSectionElt.fKey, namedSectionElt.fValue.As<String> ());
59 }
60 profile.fNamedSections.Add (kvi.fKey, newSection);
61 }
62 else {
63 // goes into global unnamed section
64 profile.fUnnamedSection.fProperties.Add (kvi.fKey, kvi.fValue.As<String> ());
65 }
66 }
67 return profile;
68}
69
70VariantValue INI::Convert (const Profile& p)
71{
72 auto sec2VV = [] (const Section& s) -> VariantValue {
74 for (const KeyValuePair<String, String>& k : s.fProperties) {
75 m.Add (k.fKey, k.fValue);
76 }
77 return VariantValue{m};
78 };
80 for (const KeyValuePair<String, Section>& kvp : p.fNamedSections) {
81 mv.Add (kvp.fKey, sec2VV (kvp.fValue));
82 }
83 for (const KeyValuePair<String, String>& kvp : p.fUnnamedSection.fProperties) {
84 mv.Add (kvp.fKey, kvp.fValue);
85 }
86 return VariantValue{mv};
87}
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
nonvirtual bool Add(ArgByValueType< key_type > key, ArgByValueType< mapped_type > newElt, AddReplaceMode addReplaceMode=AddReplaceMode::eAddReplaces)
Definition Mapping.inl:190
Simple variant-value (case variant union) object, with (variant) basic types analogous to a value in ...
String ToString(T &&t, ARGS... args)
Return a debug-friendly, display version of the argument: not guaranteed parsable or usable except fo...
Definition ToString.inl:465