21namespace alib {
namespace config {
31 || ( c ==
'/' && subs.
Length() > 1 && subs.
CharAt(1) ==
'/' );
69 if( secIt->Name.Equals(name))
71 auto* section= &*secIt;
75 if( entryIt->second.first == section )
92 for(
auto entryIt= section->
Entries.begin() ; entryIt != section->
Entries.end(); ++entryIt )
95 if( entryIt->Name.Equals(name))
97 auto* entry= &*entryIt;
111 std::make_pair(section, &newEntry) );
121 return std::pair<Section*, Entry*>(
nullptr,
nullptr);
137std::pair<IniFile::Section*, bool>
142 return std::make_pair( s,
false );
153 auto tok= tknzr.
Next().TrimEnd();
154 tok.ConsumeCharFromEnd(
'\r');
168 std::ifstream file( nPath );
170 if ( !file.is_open() )
175 if ( errNo == UnderlyingIntegral(SystemErrors::enoent) )
190 String4K actComments;
ALIB_DBG( actComments.DbgDisableBufferReplacementWarning(); )
193 String8 equalSignOrWhitespace(
'=');
203 bool writebackFlag =
false;
204 int qtyEntriesRead = 0;
206 bool fileHeaderRead =
false;
207 while( !reader.
IsEOF() )
209 reader.
Read( actLine ); ++lineNo;
215 if ( !fileHeaderRead && lineTrimmed.
IsEmpty() )
217 fileHeaderRead=
true;
225 actComments._(actLine).NewLine();
239 fileHeaderRead=
true;
248 if( actSection->Comments.IsEmpty())
250 actSection->WriteBack= writebackFlag;
251 writebackFlag=
false;
265 actName << lineTrimmed;
270 actName << lineTrimmed.
Substring( 0, idx );
273 actRawValue.
_(lineTrimmed);
281 reader.
Read( actLine );
282 if ( reader.
IsEOF() )
289 actRawValue << (actLine);
290 lineTrimmed= actLine;
295 auto* entry=
SearchEntry( actSection->Name, actName ).second;
296 if( entry ==
nullptr)
303 ALIB_WARNING(
"CONFIG",
"Variable was found twice in INI-file. First value will be discarded\n"
304 "on writing. Variable name: ", actName )
306 entry->Comments.Allocate(
Allocator, actComments );
307 entry->RawValue.Allocate(
Allocator, actRawValue );
308 entry->WriteBack= writebackFlag;
309 writebackFlag=
false;
317 while((parser= tknzr.
Next()).IsNotNull())
325 trimmedValue << parser;
327 entry->Value.Allocate(
Allocator, trimmedValue );
336 return qtyEntriesRead;
347 "Given Path is empty and no known filename from previous Read() operation available.")
350 std::ofstream outputFileStream( path.
Terminate(), std::ios::binary );
351 if ( !outputFileStream.is_open() )
373 if( section.Name.IsNotEmpty())
376 if(section.Comments.CharAtStart() !=
'\n' && section.Comments.CharAtStart() !=
'\r')
378 writer.
Write( section.Comments );
379 if(section.WriteBack)
381 writer.
Write( writeBackAttribute );
389 for (
auto& entry : section.Entries )
390 maxVarLength= (std::max)( maxVarLength, entry.Name.Length() );
392 for (
auto& entry : section.Entries )
395 if( entry.Comments.IsNotEmpty())
397 if (entry.Comments.CharAtStart() !=
'\n' && entry.Comments.CharAtStart() !=
'\r')
399 writer.
Write( entry.Comments );
404 writer.
Write( writeBackAttribute );
407 writer.
Write( entry.Name );
410 if( entry.NewValue.IsNull())
412 writer.
Write( entry.RawValue );
413 if(!entry.NewValue.EndsWith(
NEW_LINE))
425 writer.
WriteChars(
' ', maxVarLength - ( cntLine == 0 ? entry.Name.Length() - 1
433 writer.
Write(
"\\" );
435 writer.
Write( actual );
442 if( actual.
Length() > maxValLength )
443 maxValLength= actual.
Length() + 2;
446 writer.
Write(
"\\" );
456 outputFileStream.close();
lang::system::PathString FileName
The file name.
ALIB_API void AddComments(String &dest, const String &comments, const String &prefix=A_CHAR("# "))
List< MonoAllocator, Section > Sections
The list of sections.
ALIB_API void Reset()
Clears all data, resets the internal mono allocator.
ALIB_API Section * CreateSection(const String &name)
ALIB_API std::pair< Section *, bool > SearchOrCreateSection(const String §ionName)
String FileComments
The file header which will be written out as a comment lines with "# " prefixes.
HashMap< MonoAllocator, EntryKey, std::pair< Section *, Entry * >, EntryKey::Hash, EntryKey::EqualTo > entryTable
ALIB_API void Write(const lang::system::PathString &path=lang::system::NULL_PATH)
ALIB_API Entry * CreateEntry(Section *section, const String &name)
ALIB_API Section * DeleteSection(const String &name)
ALIB_API Section * SearchSection(const String §ionName)
ALIB_API Entry * DeleteEntry(Section *section, const String &name)
List< MonoAllocator, integer > LinesWithReadErrors
ALIB_API integer Read(const lang::system::CPathString &path)
ALIB_API IniFile()
Default constructor.
bool startsWithCommentSymbol(String &subs)
ALIB_API std::pair< Section *, Entry * > SearchEntry(const String §ion, const String &name)
const String & GetResource(const NString &name)
Exception & Add(const lang::CallerInfo &ci, TEnum type, TArgs &&... args)
constexpr const TChar * Terminate() const
TAString & TrimEnd(const TCString< TChar > &trimChars=TT_CStringConstants< TChar >::DefaultWhitespaces())
TAString & _(const TString< TChar > &src, integer regionStart, integer regionLength=MAX_LEN)
constexpr bool IsEmpty() const
TChar CharAt(integer idx) const
void Allocate(TAllocator &allocator, const TString< TChar > ©)
constexpr bool IsNotEmpty() const
constexpr integer Length() const
TChar CharAtStart() const
constexpr bool IsNotNull() const
integer IndexOfAny(const TString &needles, integer startIdx=0) const
TString< TChar > Substring(integer regionStart, integer regionLength=MAX_LEN) const
bool Equals(const TString< TChar > &rhs) const
TSubstring & TrimEnd(const TCString< TChar > &whiteSpaces=TT_CStringConstants< TChar >::DefaultWhitespaces())
integer ConsumeChars(integer regionLength, TSubstring *target=nullptr)
TSubstring & TrimStart(const TCString< TChar > &whiteSpaces=TT_CStringConstants< TChar >::DefaultWhitespaces())
TString< TChar > ConsumeToken(TChar separator=',', lang::Inclusion includeSeparator=lang::Inclusion::Include)
bool ConsumeCharFromEnd(TChar consumable)
TSubstring & Trim(const TCString< TChar > &whiteSpaces=TT_CStringConstants< TChar >::DefaultWhitespaces())
void Read(NAString &target)
void SetStream(::std::istream *is)
ALIB_API void Write(const NString &src)
ALIB_API void WriteChars(const nchar fillChar, integer count)
void SetStream(::std::ostream *os)
ALIB_API TSubstring< TChar > & Next(lang::Whitespaces trimming=lang::Whitespaces::Trim, TChar newDelim='\0')
#define ALIB_WARNING(...)
#define ALIB_CALLER_NULLED
#define ALIB_STRINGS_TO_NARROW( src, dest, bufSize)
#define ALIB_ASSERT_RESULT_GREATER_THAN(func, value)
#define ALIB_ASSERT_ERROR(cond,...)
@ ErrorWritingFile
An error occurred writing the file .
@ ErrorOpeningFile
File not found when reading.
This is the reference documentation of sub-namespace system of module ALib BaseCamp.
Exception CreateSystemException(const CallerInfo &ci, int errNo)
@ Include
Chooses inclusion.
config::ConfigCamp CONFIG
The singleton instance of ALib Camp class ConfigCamp.
constexpr CString DEFAULT_WHITESPACES
A zero-terminated string of default whitespace characters.
constexpr const String EMPTY_STRING
An empty string of the default character type.
NLocalString< 256 > NString256
Type alias name for TLocalString<nchar,256>.
constexpr CString NEW_LINE
A zero-terminated string containing the new-line character sequence.
lang::integer integer
Type alias in namespace alib.
Hash functor for nodes hashed in field entryTable. Ignores letter case.
String Name
The entry's name.
A section of the INI-file.
String Name
The name of the section.
List< MonoAllocator, Entry, Recycling::None > Entries
The list of variables of the section.