5a3ca60a3a
- aggiunte funzioni Set/Get/RemoveValInNotes - aggiornati prototipi e costanti - for each (.. in ..) sostituito da for ( .. : ..) vero costrutto C++.
183 lines
5.6 KiB
C++
183 lines
5.6 KiB
C++
//----------------------------------------------------------------------------
|
|
// EgalTech 2015-2018
|
|
//----------------------------------------------------------------------------
|
|
// File : EGnStringKeyVal.h Data : 27.11.18 Versione : 1.9k1
|
|
// Contenuto : Funzioni per gestione coppie chiave-valore in stringhe.
|
|
//
|
|
//
|
|
//
|
|
// Modifiche : 23.05.15 DS Creazione modulo.
|
|
// 27.11.18 DS Aggiunte Set/Get/RemoveValInNotes.
|
|
//
|
|
//----------------------------------------------------------------------------
|
|
|
|
#pragma once
|
|
|
|
#include "/EgtDev/Include/EgnStringUtils.h"
|
|
|
|
//----------------------------------------------------------------------------
|
|
static const char EQUAL = '=' ;
|
|
|
|
//----------------------------------------------------------------------------
|
|
inline bool
|
|
IsValidKey( const std::string& sKey)
|
|
{
|
|
return ( ! sKey.empty() &&
|
|
sKey.find( '\n') == std::string::npos &&
|
|
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)
|
|
{
|
|
if ( sString.empty() || ! IsValidKey( sKey))
|
|
return false ;
|
|
return ( sString.compare( 0, sKey.length(), sKey) == 0 &&
|
|
sString.length() > sKey.length() &&
|
|
sString[sKey.length()] == EQUAL) ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
inline bool
|
|
IsValidVal( const std::string& sString)
|
|
{
|
|
return ( sString.empty() ||
|
|
( sString.find( '\n') == std::string::npos &&
|
|
sString.find_first_not_of( " \t\r\n") != std::string::npos)) ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
inline bool
|
|
ValidateVal( std::string& sString)
|
|
{
|
|
std::string::size_type i ;
|
|
while ( ( i = sString.find_first_of( "\t\r\n")) != std::string::npos)
|
|
sString[i] = '_' ;
|
|
return true ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
inline bool
|
|
SetVal( const std::string& sKey, const std::string& sVal, std::string& sString)
|
|
{
|
|
if ( ! IsValidKey( sKey) || ! IsValidVal( sVal))
|
|
return false ;
|
|
sString = sKey + EQUAL + sVal ;
|
|
return true ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
template <class T>
|
|
inline bool
|
|
SetVal( const std::string& sKey, T& Val, std::string& sString)
|
|
{
|
|
return SetVal( sKey, ToString( Val), sString) ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
inline bool
|
|
GetVal( const std::string& sString, const std::string& sKey, std::string& sVal)
|
|
{
|
|
if ( ! FindKey( sString, sKey))
|
|
return false ;
|
|
sVal = sString.substr( sKey.length() + 1) ;
|
|
return true ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
template <class T>
|
|
inline bool
|
|
GetVal( const std::string& sString, const std::string& sKey, T& Val)
|
|
{
|
|
std::string sVal ;
|
|
return ( GetVal( sString, sKey, sVal) && FromString( sVal, Val)) ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
inline bool
|
|
SetValInNotes( const std::string& sKey, const std::string& sVal, std::string& sNotes)
|
|
{
|
|
if ( ! IsValidKey( sKey) || ! IsValidVal( sVal))
|
|
return false ;
|
|
STRVECTOR vsTokens ;
|
|
Tokenize( sNotes, ";", vsTokens) ;
|
|
bool bFound = false ;
|
|
for ( auto& sToken : vsTokens) {
|
|
if ( FindKey( sToken, sKey)) {
|
|
sToken = sKey + EQUAL + sVal ;
|
|
bFound = true ;
|
|
break ;
|
|
}
|
|
}
|
|
if ( ! bFound)
|
|
vsTokens.emplace_back( sKey + EQUAL + sVal) ;
|
|
sNotes.clear() ;
|
|
for ( const auto& sToken : vsTokens) {
|
|
if ( ! IsEmptyOrSpaces( sToken))
|
|
sNotes += sToken + ";" ;
|
|
}
|
|
return true ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
template <class T>
|
|
inline bool
|
|
SetValInNotes( const std::string& sKey, T& Val, std::string& sNotes)
|
|
{
|
|
return SetValInNotes( sKey, ToString( Val), sNotes) ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
inline bool
|
|
GetValInNotes( const std::string& sNotes, const std::string& sKey, std::string& sVal)
|
|
{
|
|
if ( sNotes.empty() || ! IsValidKey( sKey))
|
|
return false ;
|
|
STRVECTOR vsTokens ;
|
|
Tokenize( sNotes, ";", vsTokens) ;
|
|
for ( const auto& sToken : vsTokens) {
|
|
if ( FindKey( sToken, sKey)) {
|
|
sVal = sToken.substr( sKey.length() + 1) ;
|
|
return true ;
|
|
}
|
|
}
|
|
return false ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
template <class T>
|
|
inline bool
|
|
GetValInNotes( const std::string& sNotes, const std::string& sKey, T& Val)
|
|
{
|
|
std::string sVal ;
|
|
return ( GetValInNotes( sNotes, sKey, sVal) && FromString( sVal, Val)) ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
inline bool
|
|
RemoveValInNotes( const std::string& sKey, std::string& sNotes)
|
|
{
|
|
if ( sNotes.empty() || ! IsValidKey( sKey))
|
|
return false ;
|
|
STRVECTOR vsTokens ;
|
|
Tokenize( sNotes, ";", vsTokens) ;
|
|
bool bFound = false ;
|
|
for ( size_t i = 0; i < vsTokens.size() ; ++ i) {
|
|
if ( FindKey( vsTokens[i], sKey)) {
|
|
vsTokens.erase( vsTokens.begin() + i) ;
|
|
bFound ;
|
|
break ;
|
|
}
|
|
}
|
|
if ( bFound) {
|
|
sNotes.clear() ;
|
|
for ( const auto& sToken : vsTokens) {
|
|
if ( ! IsEmptyOrSpaces( sToken))
|
|
sNotes += sToken + ";" ;
|
|
}
|
|
}
|
|
return true ;
|
|
}
|