ALib C++ Framework
by
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
inifilefeeder.cpp
1//##################################################################################################
2// ALib C++ Framework
3//
4// Copyright 2013-2025 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6//##################################################################################################
7#include "alib_precompile.hpp"
8#if !defined(ALIB_C20_MODULES) || ((ALIB_C20_MODULES != 0) && (ALIB_C20_MODULES != 1))
9# error "Configuration MACRO ALIB_C20_MODULES has to be given to the compiler as either 0 or 1"
10#endif
11#if ALIB_C20_MODULES
12 module;
13#endif
14//========================================= Global Fragment ========================================
16#include <fstream>
17//============================================== Module ============================================
18#if ALIB_C20_MODULES
19 module ALib.Variables.IniFile;
20 import ALib.Strings.StdIOStream;
21# if ALIB_EXCEPTIONS
22 import ALib.Exceptions;
23# endif
24 import ALib.System;
25 import ALib.Variables;
26# if ALIB_FORMAT
27 import ALib.Format;
28 import ALib.Format.Paragraphs;
29# endif
30#else
32# include "ALib.Exceptions.H"
35# include "ALib.System.H"
36# include "ALib.Variables.H"
37# include "ALib.Format.H"
40#endif
41//========================================== Implementation ========================================
42
43namespace alib::variables {
44
45//##################################################################################################
46// helpers
47//##################################################################################################
49 if(iniFile == nullptr) {
50 ALIB_ERROR( "VARIABLES", "No INI-file loaded when trying to search data." )
51 return IniFile::Handle{nullptr, nullptr};
52 }
53
54 // separate section/entry name
55 auto sectionSeparator= path.LastIndexOf( configuration.Separator() );
56 String sectionName= (sectionSeparator != -1) ? path.Substring<NC>(0, sectionSeparator ) : EMPTY_STRING;
57 String entryName = (sectionSeparator != -1) ? path.Substring<NC>(sectionSeparator + 1, path.Length() - sectionSeparator - 1) : path;
58
59 // search for existing entry
60 return iniFile->SearchEntry(sectionName, entryName);
61}
62
64 ALIB_ASSERT_ERROR( var.IsDeclared(), "VARIABLES", "Given Variable not declared." )
65 ALIB_ASSERT_ERROR( &var.GetConfiguration() == &configuration, "VARIABLES",
66 "Variable belongs to different configuration: ", var )
67 return SearchEntry( String256( var ) );
68}
69
70//##################################################################################################
71// Import interface
72//##################################################################################################
73int IniFileFeeder::ImportSection( const String& sectionName, const String& typeName ) {
74 if(iniFile == nullptr) {
75 ALIB_ERROR( "VARIABLES", "No INI-file loaded when trying to import data." )
76 return 0;
77 }
78
79 auto* section= iniFile->SearchSection( sectionName );
80 if(section == nullptr) {
81 ALIB_WARNING( "VARIABLES", "Section named \"{}\" not found in INI-file.", sectionName )
82 return 0;
83 }
84
85 int cnt= 0;
86 String256 varName;
87
88 varName.Reset(sectionName);
89 if( varName.IsNotEmpty())
90 varName << configuration.Separator();
91
92 // loop over all entries
93 for ( auto& entry : section->Entries ) {
94 StringLengthResetter sectionNameResetter(varName);
95 varName << entry.Name;
96 Variable var(configuration, varName, typeName);
97 if( var.Define( priority) ) {
98 var.Import(entry.Value, priority, &configuration.Escaper );
99 ++cnt;
100 } }
101
102 return cnt;
103}
104
106 int cnt= 0;
107 String256 varName;
108
109 varName.Reset(section.Name);
110 if( varName.IsNotEmpty())
111 varName << configuration.Separator();
112
113 // loop over all entries
114 for ( auto& entry : section.Entries ) {
115 // Try if variable is declared and has lower or equal priority than us.
116 StringLengthResetter sectionNameResetter(varName);
117 varName << entry.Name;
119 Substring value= entry.Value;
120 if( var.Try(varName) ) {
121 var.Import(value, priority, &configuration.Escaper );
122 ++cnt;
123 continue;
124 }
125
126 // Variable not declared. Copy value to undeclared input variables
127 configuration.PresetImportString(varName, value, &configuration.Escaper, priority);
128 }
129
130 return cnt;
131}
132
133int IniFileFeeder::ImportSection( const String& sectionName ) {
134 if(iniFile == nullptr) {
135 ALIB_ERROR( "VARIABLES", "No INI-file loaded when trying to import data." )
136 return 0;
137 }
138
139 auto* section= iniFile->SearchSection( sectionName );
140 if(section == nullptr) {
141 ALIB_WARNING( "VARIABLES", "Section name \"{}\" not found in INI-file.", sectionName )
142 return 0;
143 }
144
145 return importSection(*section);
146}
147
149 if(iniFile == nullptr) {
150 ALIB_ERROR( "VARIABLES", "No INI-file loaded when trying to import data." )
151 return 0;
152 }
153
154 int cnt= 0;
155 String256 varName;
156
157 // loop over all sections
158 for ( IniFile::Section& section : iniFile->Sections )
159 cnt+= importSection( section );
160
161 return cnt;
162}
163
164//##################################################################################################
165// Export interface
166//##################################################################################################
168 if(iniFile == nullptr) {
169 ALIB_ERROR( "VARIABLES", "No INI-file loaded when trying to export data." )
170 return false;
171 }
172 ALIB_ASSERT_ERROR(var.IsDeclared(), "VARIABLES", "Variable to export not declared: ", var)
173
174 String256 name(var);
175
176 // separate section/entry name
177 auto sectionSeparator= name.LastIndexOf( var.GetConfiguration().Separator() );
178 String sectionName= (sectionSeparator != -1) ? name.Substring<NC>(0, sectionSeparator ) : EMPTY_STRING;
179 String entryName = (sectionSeparator != -1) ? name.Substring<NC>(sectionSeparator + 1, name.Length() - sectionSeparator - 1) : name;
180
181 // search for existing entry
182 auto handle= iniFile->SearchEntry(sectionName, entryName);
183 auto* entry= handle.EntryPointer;
184 if( entry ) {
185 // exists and no write back?
186 if( !entry ->WriteBack
187 && !handle.SectionPointer->WriteBack )
188 return false;
189 } else {
190 // create entry
191 auto sectionIt= iniFile->SearchOrCreateSection( sectionName );
192 entry = iniFile->CreateEntry( sectionIt.first, entryName );
193 if ( var.IsWriteBack() )
194 entry->WriteBack= true;
195 }
196
197 {String4K buf;
199 var.Export( buf, &configuration.Escaper );
200 entry->NewValue.Allocate(iniFile->Allocator, buf );
201 }
202
203 // add comments
204 if(entry->Comments.IsNull()) {
205 auto* decl= var.GetDeclaration();
206 if( decl && decl->Comments().IsNotEmpty() )
207 iniFile->AddComments( entry->Comments, decl->Comments(), DefaultCommentPrefix );
208 }
209
210 return true;
211}
212
213int IniFileFeeder::ExportSubTree( Configuration::Cursor cursor, bool directChildrenOnly ) {
214 if(iniFile == nullptr) {
215 ALIB_ERROR( "VARIABLES", "No INI-file loaded when trying to export data." )
216 return 0;
217 }
218 int cnt= 0;
221 stit.SetMaxDepth( directChildrenOnly ? 1 : (std::numeric_limits<unsigned>::max)() );
222 stit.Initialize( cursor, lang::Inclusion::Include );
223 while ( stit.IsValid() ) {
224 if ( stit.Node().IsRoot()) {
225 stit.Next();
226 continue;
227 }
228 if( stit.Node().Name().Equals(A_CHAR("$PRESETS")) ) {
229 stit.NextSibling();
230 continue;
231 }
232
233 Variable var(stit.Node());
234 if( var.IsDeclared() && var.IsDefined() ) {
235 if( Export( var ) )
236 cnt++;
237 }
238 stit.Next();
239 }
240 return cnt;
241}
242
243#if ALIB_RESOURCES
245 const NString& resourceCategory,
246 const NString& resourceNamePrefix ) {
247 if(iniFile == nullptr) {
248 ALIB_ERROR( "VARIABLES", "No INI-file loaded when trying to import data." )
249 return 0;
250 }
251
252 // add section comments from resources to INI-file
253 int cnt= 0;
254 for( auto& section : iniFile->Sections )
255 if( section.Comments.IsNull() ) {
256 auto& comment= resourcePool.Get( resourceCategory,
257 NString128() << resourceNamePrefix << section.Name
258 ALIB_DBG(, false));
259 if( comment.IsNull() )
260 continue;
261
262 ++cnt;
263 Paragraphs text;
265 text.LineWidth= LineWidth;
266 text.Buffer._(NEW_LINE);
267 text.AddMarked( comment );
268 }
269 section.Comments.Allocate(iniFile->Allocator, text.Buffer);
270 }
271 return cnt;
272}
273#endif
274
276 auto handle= SearchEntry( path );
277 ALIB_ASSERT_WARNING( handle.EntryPointer , "VARIABLES",
278 "Variable \"{}\" to be marked as 'writeback' not found.", path )
279
280 if( handle.EntryPointer && handle.EntryPointer->RawValue.IsEmpty() ) {
281 handle.EntryPointer->WriteBack= true;
282 return true;
283 }
284 return false;
285}
286
288 ALIB_ASSERT_ERROR( var.IsDeclared(), "VARIABLES", "Given Variable not declared." )
289 ALIB_ASSERT_ERROR( &var.GetConfiguration() == &configuration, "VARIABLES",
290 "Variable belongs to different configuration: ", var)
291 return SetWriteBackFlag( String256(var) );
292}
293
294
295} // namespace [alib::variables]
#define A_CHAR(STR)
Definition alib.inl:1325
#define ALIB_WARNING(domain,...)
Definition alib.inl:1141
#define ALIB_ASSERT_WARNING(cond, domain,...)
Definition alib.inl:1145
#define ALIB_ERROR(domain,...)
Definition alib.inl:1140
#define ALIB_LOCK_RECURSIVE_WITH(lock)
Definition alib.inl:1414
#define ALIB_DBG(...)
Definition alib.inl:931
#define ALIB_ASSERT_ERROR(cond, domain,...)
Definition alib.inl:1144
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.inl:241
constexpr integer Length() const
Definition string.inl:304
void Allocate(TAllocator &allocator, const TString< TChar > &copy)
Definition string.inl:1734
constexpr bool IsNotEmpty() const
Definition string.inl:357
integer LastIndexOf(TChar needle, integer startIndex=MAX_LEN) const
Definition string.inl:913
TString< TChar > Substring(integer regionStart, integer regionLength=MAX_LEN) const
Definition string.inl:372
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:260
void Import(const String &src, Priority priority, const StringEscaper *escaper=nullptr)
Definition variable.cpp:330
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.inl:2181
resources::ResourcePool ResourcePool
Type alias in namespace alib.
constexpr CString NEW_LINE
A zero-terminated string containing the new-line character sequence.
Definition cstring.inl:540
constexpr const String EMPTY_STRING
An empty string of the default character type.
Definition string.inl:2234
LocalString< 4096 > String4K
Type alias name for #"TLocalString;TLocalString<character,4096>".
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2172
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.inl:162
A section of the INI-file.
Definition inifile.inl:145
ListMA< Entry, Recycling::None > Entries
The list of variables of the section.
Definition inifile.inl:157
String Name
The name of the section.
Definition inifile.inl:155