Files
Include/EGnStringKeyVal.h
T
Dario Sassi 96952c35f8 Include :
- aggiornamento prototipi
- aggiunte per ObjUser.
2015-05-26 06:52:11 +00:00

86 lines
2.7 KiB
C++

//----------------------------------------------------------------------------
// EgalTech 2015-2015
//----------------------------------------------------------------------------
// File : EgnStringKeyVal.h Data : 23.05.15 Versione : 1.6e3
// Contenuto : Funzioni per gestione coppie chiave-valore in stringhe.
//
//
//
// Modifiche : 23.05.15 DS Creazione modulo.
//
//
//----------------------------------------------------------------------------
#pragma once
#include "/EgtDev/Include/EgnStringUtils.h"
//----------------------------------------------------------------------------
static const char EQUAL = '=' ;
//----------------------------------------------------------------------------
inline bool
FindKey( const std::string& sString, const std::string& sKey)
{
return ( sString.compare( 0, sKey.length(), sKey) == 0 &&
sString.length() > sKey.length() &&
sString[sKey.length()] == EQUAL) ;
}
//----------------------------------------------------------------------------
inline bool
IsValidKey( 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
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
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 ( ! IsValidKey( sKey))
return false ;
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)) ;
}