ALib C++ Framework
by
Library Version: 2605 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
inifilefeeder.cpp
1
2namespace alib::variables {
3
4//##################################################################################################
5// helpers
6//##################################################################################################
8 if(iniFile == nullptr) {
9 ALIB_ERROR( "VARIABLES", "No INI-file loaded when trying to search data." )
10 return IniFile::Handle{nullptr, nullptr};
11 }
12
13 // separate section/entry name
14 auto sectionSeparator= path.LastIndexOf( configuration.Separator() );
15 String sectionName= (sectionSeparator != -1) ? path.Substring<NC>(0, sectionSeparator ) : EMPTY_STRING;
16 String entryName = (sectionSeparator != -1) ? path.Substring<NC>(sectionSeparator + 1, path.Length() - sectionSeparator - 1) : path;
17
18 // search for existing entry
19 return iniFile->SearchEntry(sectionName, entryName);
20}
21
23 ALIB_ASSERT_ERROR( var.IsDeclared(), "VARIABLES", "Given Variable not declared." )
24 ALIB_ASSERT_ERROR( &var.GetConfiguration() == &configuration, "VARIABLES",
25 "Variable belongs to different configuration: ", var )
26 return SearchEntry( String256( var ) );
27}
28
29//##################################################################################################
30// Import interface
31//##################################################################################################
32int IniFileFeeder::ImportSection( const String& sectionName, const String& typeName ) {
33 if(iniFile == nullptr) {
34 ALIB_ERROR( "VARIABLES", "No INI-file loaded when trying to import data." )
35 return 0;
36 }
37
38 auto* section= iniFile->SearchSection( sectionName );
39 if(section == nullptr) {
40 ALIB_WARNING( "VARIABLES", "Section named \"{}\" not found in INI-file.", sectionName )
41 return 0;
42 }
43
44 int cnt= 0;
45 String256 varName;
46
47 varName.Reset(sectionName);
48 if( varName.IsNotEmpty())
49 varName << configuration.Separator();
50
51 // loop over all entries
52 for ( auto& entry : section->Entries ) {
53 StringLengthResetter sectionNameResetter(varName);
54 varName << entry.Name;
55 Variable var(configuration, varName, typeName);
56 if( var.Define( priority) ) {
57 var.Import(entry.Value, priority, &configuration.Escaper );
58 ++cnt;
59 } }
60
61 return cnt;
62}
63
65 int cnt= 0;
66 String256 varName;
67
68 varName.Reset(section.Name);
69 if( varName.IsNotEmpty())
70 varName << configuration.Separator();
71
72 // loop over all entries
73 for ( auto& entry : section.Entries ) {
74 // Try if variable is declared and has lower or equal priority than us.
75 StringLengthResetter sectionNameResetter(varName);
76 varName << entry.Name;
78 Substring value= entry.Value;
79 if( var.Try(varName) ) {
80 var.Import(value, priority, &configuration.Escaper );
81 ++cnt;
82 continue;
83 }
84
85 // Variable not declared. Copy value to undeclared input variables
86 configuration.PresetImportString(varName, value, &configuration.Escaper, priority);
87 }
88
89 return cnt;
90}
91
92int IniFileFeeder::ImportSection( const String& sectionName ) {
93 if(iniFile == nullptr) {
94 ALIB_ERROR( "VARIABLES", "No INI-file loaded when trying to import data." )
95 return 0;
96 }
97
98 auto* section= iniFile->SearchSection( sectionName );
99 if(section == nullptr) {
100 ALIB_WARNING( "VARIABLES", "Section name \"{}\" not found in INI-file.", sectionName )
101 return 0;
102 }
103
104 return importSection(*section);
105}
106
108 if(iniFile == nullptr) {
109 ALIB_ERROR( "VARIABLES", "No INI-file loaded when trying to import data." )
110 return 0;
111 }
112
113 int cnt= 0;
114 String256 varName;
115
116 // loop over all sections
117 for ( IniFile::Section& section : iniFile->Sections )
118 cnt+= importSection( section );
119
120 return cnt;
121}
122
123//##################################################################################################
124// Export interface
125//##################################################################################################
127 if(iniFile == nullptr) {
128 ALIB_ERROR( "VARIABLES", "No INI-file loaded when trying to export data." )
129 return false;
130 }
131 ALIB_ASSERT_ERROR(var.IsDeclared(), "VARIABLES", "Variable to export not declared: ", var)
132
133 String256 name(var);
134
135 // separate section/entry name
136 auto sectionSeparator= name.LastIndexOf( var.GetConfiguration().Separator() );
137 String sectionName= (sectionSeparator != -1) ? name.Substring<NC>(0, sectionSeparator ) : EMPTY_STRING;
138 String entryName = (sectionSeparator != -1) ? name.Substring<NC>(sectionSeparator + 1, name.Length() - sectionSeparator - 1) : name;
139
140 // search for existing entry
141 auto handle= iniFile->SearchEntry(sectionName, entryName);
142 auto* entry= handle.EntryPointer;
143 if( entry ) {
144 // exists and no write back?
145 if( !entry ->WriteBack
146 && !handle.SectionPointer->WriteBack )
147 return false;
148 } else {
149 // create entry
150 auto sectionIt= iniFile->SearchOrCreateSection( sectionName );
151 entry = iniFile->CreateEntry( sectionIt.first, entryName );
152 if ( var.IsWriteBack() )
153 entry->WriteBack= true;
154 }
155
156 {String4K buf;
158 var.Export( buf, &configuration.Escaper );
159 entry->NewValue.Allocate(iniFile->Allocator, buf );
160 }
161
162 // add comments
163 if(entry->Comments.IsNull()) {
164 auto* decl= var.GetDeclaration();
165 if( decl && decl->Comments().IsNotEmpty() )
166 iniFile->AddComments( entry->Comments, decl->Comments(), DefaultCommentPrefix );
167 }
168
169 return true;
170}
171
172int IniFileFeeder::ExportSubTree( Configuration::Cursor cursor, bool directChildrenOnly ) {
173 if(iniFile == nullptr) {
174 ALIB_ERROR( "VARIABLES", "No INI-file loaded when trying to export data." )
175 return 0;
176 }
177 int cnt= 0;
180 stit.SetMaxDepth( directChildrenOnly ? 1 : (std::numeric_limits<unsigned>::max)() );
181 stit.Initialize( cursor, lang::Inclusion::Include );
182 while ( stit.IsValid() ) {
183 if ( stit.Node().IsRoot()) {
184 stit.Next();
185 continue;
186 }
187 if( stit.Node().Name().Equals(A_CHAR("$PRESETS")) ) {
188 stit.NextSibling();
189 continue;
190 }
191
192 Variable var(stit.Node());
193 if( var.IsDeclared() && var.IsDefined() ) {
194 if( Export( var ) )
195 cnt++;
196 }
197 stit.Next();
198 }
199 return cnt;
200}
201
202#if ALIB_RESOURCES
204 const NString& resourceCategory,
205 const NString& resourceNamePrefix ) {
206 if(iniFile == nullptr) {
207 ALIB_ERROR( "VARIABLES", "No INI-file loaded when trying to import data." )
208 return 0;
209 }
210
211 // add section comments from resources to INI-file
212 int cnt= 0;
213 for( auto& section : iniFile->Sections )
214 if( section.Comments.IsNull() ) {
215 auto& comment= resourcePool.Get( resourceCategory,
216 NString128() << resourceNamePrefix << section.Name
217 ALIB_DBG(, false));
218 if( comment.IsNull() )
219 continue;
220
221 ++cnt;
222 Paragraphs text;
224 text.LineWidth= LineWidth;
225 text.Buffer._(NEW_LINE);
226 text.AddMarked( comment );
227 }
228 section.Comments.Allocate(iniFile->Allocator, text.Buffer);
229 }
230 return cnt;
231}
232#endif
233
235 auto handle= SearchEntry( path );
236 ALIB_ASSERT_WARNING( handle.EntryPointer , "VARIABLES",
237 "Variable \"{}\" to be marked as 'writeback' not found.", path )
238
239 if( handle.EntryPointer && handle.EntryPointer->RawValue.IsEmpty() ) {
240 handle.EntryPointer->WriteBack= true;
241 return true;
242 }
243 return false;
244}
245
247 ALIB_ASSERT_ERROR( var.IsDeclared(), "VARIABLES", "Given Variable not declared." )
248 ALIB_ASSERT_ERROR( &var.GetConfiguration() == &configuration, "VARIABLES",
249 "Variable belongs to different configuration: ", var)
250 return SetWriteBackFlag( String256(var) );
251}
252
253
254} // namespace [alib::variables]
#define A_CHAR(STR)
#define ALIB_WARNING(domain,...)
#define ALIB_ASSERT_WARNING(cond, domain,...)
#define ALIB_ERROR(domain,...)
#define ALIB_LOCK_RECURSIVE_WITH(lock)
#define ALIB_DBG(...)
#define ALIB_ASSERT_ERROR(cond, domain,...)
void SetMaxDepth(unsigned int newMaxDepth=(std::numeric_limits< unsigned >::max)())
void Initialize(CursorType startNode, lang::Inclusion includeStartNode)
void SetPathGeneration(lang::Switch pathGeneration)
constexpr CharacterType Separator() const noexcept
static threads::RecursiveLock DEFAULT_LOCK
void AddMarked(boxing::TBoxes< TAllocatorArgs > &args)
integer LineWidth
Used as parameter lineWidth of static method invocations.
virtual const String & Get(const NString &category, const NString &name, bool dbgAssert)=0
TAString & _(const TAppendable &src)
void DbgDisableBufferReplacementWarning()
Definition tastring.hpp:236
constexpr integer Length() const
Definition string.hpp:300
void Allocate(TAllocator &allocator, const TString< TChar > &copy)
Definition string.hpp:1729
constexpr bool IsNotEmpty() const
Definition string.hpp:353
integer LastIndexOf(TChar needle, integer startIndex=MAX_LEN) const
Definition string.hpp:909
TString< TChar > Substring(integer regionStart, integer regionLength=MAX_LEN) const
Definition string.hpp:368
int AddResourcedSectionComments(ResourcePool &resourcePool, const NString &resourceCategory, const NString &resourceNamePrefix)
int ExportSubTree(Configuration::Cursor cursor, bool directChildrenOnly=false)
IniFile * iniFile
The INI-file. Created with methods #"ImportStart" and #"ExportStart".
bool SetWriteBackFlag(const String &path)
Configuration & configuration
The configuration to work with. Set with construction.
int ImportSection(const String &sectionName)
int importSection(IniFile::Section &section)
Priority priority
The priority to use to define variables. Set with construction.
bool Export(const Variable &var)
IniFile::Handle SearchEntry(const Variable &variable)
bool Define(Priority requestedPriority=Priority::Standard)
Definition variable.cpp:237
void Import(const String &src, Priority priority, const StringEscaper *escaper=nullptr)
Definition variable.cpp:307
AString & Export(AString &dest, const StringEscaper *escaper=nullptr) const
bool Try(const String &name)
const Declaration * GetDeclaration() const
Configuration & GetConfiguration() const
@ On
Switch it on, switched on, etc.
@ Include
Chooses inclusion.
strings::TString< nchar > NString
Type alias in namespace #"%alib".
Definition string.hpp:2174
resources::ResourcePool ResourcePool
Type alias in namespace #"%alib".
constexpr CString NEW_LINE
A zero-terminated string containing the new-line character sequence.
Definition cstring.hpp:536
constexpr const String EMPTY_STRING
An empty string of the default character type.
Definition string.hpp:2227
LocalString< 4096 > String4K
Type alias name for #"TLocalString;TLocalString<character,4096>".
strings::TString< character > String
Type alias in namespace #"%alib".
Definition string.hpp:2165
strings::TSubstring< character > Substring
Type alias in namespace #"%alib".
LocalString< 256 > String256
Type alias name for #"TLocalString;TLocalString<character,256>".
NLocalString< 128 > NString128
Type alias name for #"TLocalString;TLocalString<nchar,128>".
format::Paragraphs Paragraphs
Type alias in namespace #"%alib".
strings::TStringLengthResetter< character,lang::HeapAllocator > StringLengthResetter
Type alias in namespace #"%alib".
containers::StringTreeIterator< TTree > StringTreeIterator
Type alias in namespace #"%alib".
A pair of pointers to a section and an entry in the section.
Definition inifile.hpp:165
A section of the INI-file.
Definition inifile.hpp:148
ListMA< Entry, Recycling::None > Entries
The list of variables of the section.
Definition inifile.hpp:160
String Name
The name of the section.
Definition inifile.hpp:158