Stroika Library 3.0d23x
 
Loading...
Searching...
No Matches
Variant/CharacterDelimitedLines/Reader.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. All rights reserved
3 */
4#include "Stroika/Foundation/StroikaPreComp.h"
5
6#include "Stroika/Foundation/Characters/FloatConversion.h"
8#include "Stroika/Foundation/Characters/String2Int.h"
9#include "Stroika/Foundation/DataExchange/BadFormatException.h"
12
13#include "Reader.h"
14
15using namespace Stroika::Foundation;
17using namespace Stroika::Foundation::DataExchange::Variant;
18using namespace Stroika::Foundation::DataExchange::Variant::CharacterDelimitedLines;
19using namespace Stroika::Foundation::Streams;
20
24using Containers::Set;
25using Memory::MakeSharedPtr;
27
28// Comment this in to turn on aggressive noisy DbgTrace in this module
29//#define USE_NOISY_TRACE_IN_THIS_MODULE_ 1
30
31/*
32 ********************************************************************************
33 ************** DataExchange::::CharacterDelimitedLines::Reader *****************
34 ********************************************************************************
35 */
36class CharacterDelimitedLines::Reader::Rep_ final : public Variant::Reader::_IRep, public Memory::UseBlockAllocationIfAppropriate<Rep_> {
37public:
38 Set<Character> fDelimiters_;
39 bool fTrimTokens_{false};
40 Rep_ (const Set<Character>& columnDelimiters, bool trimTokens)
41 : fDelimiters_{columnDelimiters}
42 , fTrimTokens_{trimTokens}
43 {
44 }
45 virtual _SharedPtrIRep Clone () const override
46 {
47 return MakeSharedPtr<Rep_> (fDelimiters_, fTrimTokens_);
48 }
49 virtual optional<filesystem::path> GetDefaultFileSuffix () const override
50 {
51 return ".txt"sv;
52 }
53 virtual VariantValue Read (const InputStream::Ptr<byte>& in) const override
54 {
55 return Read (Streams::BinaryToText::Reader::New (in));
56 }
57 virtual VariantValue Read (const InputStream::Ptr<Character>& in) const override
58 {
59 // @todo consider if this functional style is more clear than a nested for-loop. Was harder for me to
60 // write this way, but that could be my inexperience... --LGP 2022-12-04
61 return VariantValue{ReadMatrix (in).Map<Sequence<VariantValue>> ([] (const Sequence<String>& line) -> VariantValue {
62 return VariantValue{line.Map<Iterable<VariantValue>> ([] (const String& i) { return VariantValue{i}; })};
63 })};
64 }
65 nonvirtual Iterable<Sequence<String>> ReadMatrix (const InputStream::Ptr<Character>& in) const
66 {
67#if USE_NOISY_TRACE_IN_THIS_MODULE_
68 Debug::TraceContextBumper ctx{"DataExchange::Variant::CharacterDelimitedLines::Reader::Rep_::ReadMatrix"};
69#endif
71 for (const String& line : in.ReadLines ()) {
72 Sequence<String> tokens{line.Tokenize (fDelimiters_)};
73 if (fTrimTokens_) {
74 tokens = tokens.Map<Sequence<String>> ([] (auto i) { return i.Trim (); });
75 }
76#if USE_NOISY_TRACE_IN_THIS_MODULE_
77 DbgTrace ("DataExchange::Variant::CharacterDelimitedLines::Reader::ReadMatrix: line={}, tokenCount={}"_f, line, tokens.size ());
78 for ([[maybe_unused]] const auto& i : tokens) {
79 DbgTrace ("token='{}'"_f, i);
80 }
81#endif
82 result.Append (tokens);
83 }
84 return result;
85 }
86};
87CharacterDelimitedLines::Reader::Reader (const Set<Character>& columnDelimiters, bool trimTokens)
88 : inherited{MakeSharedPtr<Rep_> (columnDelimiters, trimTokens)}
89{
90}
91
96
98{
99 return Debug::UncheckedDynamicCast<const Rep_&> (_GetRep ()).ReadMatrix (in);
100}
auto MakeSharedPtr(ARGS_TYPE &&... args) -> shared_ptr< T >
same as make_shared, but if type T has block allocation, then use block allocation for the 'shared pa...
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.
#define DbgTrace
Definition Trace.h:309
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
A generalization of a vector: a container whose elements are keyed by the natural numbers.
nonvirtual RESULT_CONTAINER Map(ELEMENT_MAPPER &&elementMapper) const
'override' Iterable<>::Map () function so RESULT_CONTAINER defaults to Sequence, and improve that cas...
Definition Sequence.inl:174
nonvirtual void Append(ArgByValueType< value_type > item)
Definition Sequence.inl:330
Set<T> is a container of T, where once an item is added, additionally adds () do nothing.
nonvirtual Iterable< Sequence< String > > ReadMatrix(const Streams::InputStream::Ptr< byte > &in) const
Simple variant-value (case variant union) object, with (variant) basic types analogous to a value in ...
InputStream<>::Ptr is Smart pointer (with abstract Rep) class defining the interface to reading from ...
Iterable<T> is a base class for containers which easily produce an Iterator<T> to traverse them.
Definition Iterable.h:237
Ptr New(const InputStream::Ptr< byte > &src, optional< AutomaticCodeCvtFlags > codeCvtFlags={}, optional< SeekableFlag > seekable={}, ReadAhead readAhead=eReadAheadAllowed)
Create an InputStream::Ptr<Character> from the arguments (usually binary source) - which can be used ...