Include :

- modifiche a SetValInNotes, GetValInNotes e RemoveValInNotes per gestire chiavi con terminatore ':' già compreso a cui non aggiungere '='.
This commit is contained in:
DarioS
2022-05-14 11:41:40 +02:00
parent c0cecd27ae
commit c8faafd10b
+37 -14
View File
@@ -1,13 +1,14 @@
//----------------------------------------------------------------------------
// EgalTech 2015-2018
// EgalTech 2015-2022
//----------------------------------------------------------------------------
// File : EGnStringKeyVal.h Data : 27.11.18 Versione : 1.9k1
// File : EGnStringKeyVal.h Data : 13.05.22 Versione : 2.4e2
// Contenuto : Funzioni per gestione coppie chiave-valore in stringhe.
//
//
//
// Modifiche : 23.05.15 DS Creazione modulo.
// 27.11.18 DS Aggiunte Set/Get/RemoveValInNotes.
// 13.05.22 DS In SetValInNotes e GetValInNotes aggiunta gestione separatore ':'.
//
//----------------------------------------------------------------------------
@@ -17,26 +18,27 @@
//----------------------------------------------------------------------------
static const char EQUAL = '=' ;
static const char COLON = ':' ;
//----------------------------------------------------------------------------
inline bool
IsValidKey( const std::string& sKey)
IsValidKey( const std::string& sKey, bool bFull = false)
{
return ( ! sKey.empty() &&
sKey.find( '\n') == std::string::npos &&
sKey.find( EQUAL) == std::string::npos &&
(( bFull && ( sKey.back() == EQUAL || sKey.back() == COLON)) ||
sKey.find( EQUAL) == std::string::npos) &&
sKey.find_first_not_of( " \t\r\n") != std::string::npos) ;
}
//----------------------------------------------------------------------------
inline bool
FindKey( const std::string& sString, const std::string& sKey)
FindKey( const std::string& sString, const std::string& sKey, bool bFull = false)
{
if ( sString.empty() || ! IsValidKey( sKey))
if ( sString.empty() || ! IsValidKey( sKey, bFull))
return false ;
return ( sString.compare( 0, sKey.length(), sKey) == 0 &&
sString.length() > sKey.length() &&
sString[sKey.length()] == EQUAL) ;
( bFull || ( sString.length() > sKey.length() && sString[sKey.length()] == EQUAL))) ;
}
//----------------------------------------------------------------------------
@@ -99,20 +101,28 @@ GetVal( const std::string& sString, const std::string& sKey, T& Val)
inline bool
SetValInNotes( const std::string& sKey, const std::string& sVal, std::string& sNotes)
{
// verifiche validità chiave
if ( ! IsValidKey( sKey) || ! IsValidVal( sVal))
return false ;
// chiave con carattere finale speciale
std::string sTkey = sKey ;
if ( sKey.back() != COLON)
sTkey += EQUAL ;
// ricerca se già presente
STRVECTOR vsTokens ;
Tokenize( sNotes, ";", vsTokens) ;
bool bFound = false ;
for ( auto& sToken : vsTokens) {
if ( FindKey( sToken, sKey)) {
sToken = sKey + EQUAL + sVal ;
if ( FindKey( sToken, sTkey, true)) {
sToken = sTkey + sVal ;
bFound = true ;
break ;
}
}
// altrimenti aggiunta
if ( ! bFound)
vsTokens.emplace_back( sKey + EQUAL + sVal) ;
vsTokens.emplace_back( sTkey + sVal) ;
// ricostruzione delle note
sNotes.clear() ;
for ( const auto& sToken : vsTokens) {
if ( ! IsEmptyOrSpaces( sToken))
@@ -133,13 +143,19 @@ SetValInNotes( const std::string& sKey, T& Val, std::string& sNotes)
inline bool
GetValInNotes( const std::string& sNotes, const std::string& sKey, std::string& sVal)
{
// verifiche validità chiave
if ( sNotes.empty() || ! IsValidKey( sKey))
return false ;
// chiave con carattere finale speciale
std::string sTkey = sKey ;
if ( sKey.back() != COLON)
sTkey += EQUAL ;
// ricerca
STRVECTOR vsTokens ;
Tokenize( sNotes, ";", vsTokens) ;
for ( const auto& sToken : vsTokens) {
if ( FindKey( sToken, sKey)) {
sVal = sToken.substr( sKey.length() + 1) ;
if ( FindKey( sToken, sTkey, true)) {
sVal = sToken.substr( sTkey.length()) ;
return true ;
}
}
@@ -159,18 +175,25 @@ GetValInNotes( const std::string& sNotes, const std::string& sKey, T& Val)
inline bool
RemoveValInNotes( const std::string& sKey, std::string& sNotes)
{
// verifiche validità chiave
if ( sNotes.empty() || ! IsValidKey( sKey))
return false ;
// chiave con carattere finale speciale
std::string sTkey = sKey ;
if ( sKey.back() != COLON)
sTkey += EQUAL ;
// ricerca
STRVECTOR vsTokens ;
Tokenize( sNotes, ";", vsTokens) ;
bool bFound = false ;
for ( size_t i = 0; i < vsTokens.size() ; ++ i) {
if ( FindKey( vsTokens[i], sKey)) {
if ( FindKey( vsTokens[i], sTkey, true)) {
vsTokens.erase( vsTokens.begin() + i) ;
bFound = true ;
break ;
}
}
// se trovato e cancellato, devo ricostruire le note
if ( bFound) {
sNotes.clear() ;
for ( const auto& sToken : vsTokens) {