e2c8e72e22
- aggiunta gestione attributi Level, Mode e Status - aggiunti comandi per assegnarli.
335 lines
9.6 KiB
C++
335 lines
9.6 KiB
C++
//----------------------------------------------------------------------------
|
|
// EgalTech 2014-2014
|
|
//----------------------------------------------------------------------------
|
|
// File : Attribs.cpp Data : 05.03.14 Versione : 1.5c1
|
|
// Contenuto : Implementazione della classe Attribs.
|
|
//
|
|
//
|
|
//
|
|
// Modifiche : 05.03.14 DS Creazione modulo.
|
|
//
|
|
//
|
|
//----------------------------------------------------------------------------
|
|
|
|
//--------------------------- Include ----------------------------------------
|
|
#include "stdafx.h"
|
|
#include "Attribs.h"
|
|
#include "/EgtDev/Include/EGnStringUtils.h"
|
|
#include "/EgtDev/Include/EGkStringUtils3d.h"
|
|
|
|
using namespace std ;
|
|
|
|
//----------------------------------------------------------------------------
|
|
static const string NAME = "N" ;
|
|
static const char EQUAL = '=' ;
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
Attribs::Dump( string& sOut, const char* szNewLine) const
|
|
{
|
|
// livello
|
|
sOut += "Lev=" ;
|
|
switch ( m_Data[LEVEL]) {
|
|
default : /* GDB_LV_USER */ sOut += "user" ; break ;
|
|
case GDB_LV_SYSTEM : sOut += "system" ; break ;
|
|
case GDB_LV_TEMP : sOut += "temp" ; break ;
|
|
}
|
|
// modo
|
|
sOut += " Mod=" ;
|
|
switch ( m_Data[MODE]) {
|
|
default : /* GDB_MD_STD */ sOut += "std" ; break ;
|
|
case GDB_MD_LOCKED : sOut += "locked" ; break ;
|
|
case GDB_MD_HIDDEN : sOut += "hidden" ; break ;
|
|
}
|
|
// stato
|
|
sOut += " Sta=" ;
|
|
switch ( m_Data[STATUS]) {
|
|
default : /* GDB_ST_ON */ sOut += "on" ; break ;
|
|
case GDB_ST_SEL : sOut += "sel" ; break ;
|
|
case GDB_ST_OFF : sOut += "off" ; break ;
|
|
}
|
|
sOut += szNewLine ;
|
|
// materiale
|
|
sOut += "Mat=" ;
|
|
switch ( m_Material) {
|
|
case GDB_MT_COLOR : sOut += "color" ; break ;
|
|
case GDB_MT_PARENT : sOut += "by parent" ; break ;
|
|
default : sOut += ToString( m_Material) ; break ;
|
|
}
|
|
// eventuale colore
|
|
if ( m_Material == GDB_MT_COLOR) {
|
|
sOut += " Col=" ;
|
|
string sColName ;
|
|
if ( GetNameOfStdColor( m_Color, sColName))
|
|
sOut += sColName ;
|
|
sOut += "(" + ToString( m_Color.GetIntRed()) ;
|
|
sOut += "," + ToString( m_Color.GetIntGreen()) ;
|
|
sOut += "," + ToString( m_Color.GetIntBlue()) ;
|
|
if ( m_Color.GetIntAlpha() != 100)
|
|
sOut += "," + ToString( m_Color.GetIntAlpha()) ;
|
|
sOut += ")" ;
|
|
}
|
|
sOut += szNewLine ;
|
|
// eventuali nome e stringhe informative
|
|
STRLIST::const_iterator iIter ;
|
|
for ( iIter = m_slInfo.begin() ; iIter != m_slInfo.end() ; ++ iIter)
|
|
sOut += *iIter + szNewLine ;
|
|
|
|
return true ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
Attribs::Save( ostream& osOut) const
|
|
{
|
|
// flag presenza attributi
|
|
osOut << "A" << endl ;
|
|
|
|
// livello
|
|
osOut << ToString( m_Data[LEVEL]) << "," ;
|
|
// modo
|
|
osOut << ToString( m_Data[MODE]) << "," ;
|
|
// stato (se SEL viene convertito in ON)
|
|
int nStat = (( m_Data[STATUS] == GDB_ST_SEL) ? GDB_ST_ON : m_Data[STATUS]) ;
|
|
osOut << ToString( nStat) << "," ;
|
|
// altro
|
|
osOut << ToString( m_Data[OTHER]) << ";" ;
|
|
// materiale
|
|
osOut << ToString( m_Material) << ";" ;
|
|
// colore
|
|
osOut << ToString( m_Color) << ";" ;
|
|
// numero di stringhe di info (nelle linee successive)
|
|
osOut << ToString( int( m_slInfo.size())) << ";" << endl ;
|
|
// stringhe di info
|
|
STRLIST::const_iterator iIter ;
|
|
for ( iIter = m_slInfo.begin() ; iIter != m_slInfo.end() ; ++ iIter)
|
|
osOut << *iIter << endl ;
|
|
|
|
return true ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
Attribs::Load( Scanner& TheScanner)
|
|
{
|
|
// leggo la prossima linea
|
|
string sLine ;
|
|
if ( ! TheScanner.GetLine( sLine))
|
|
return false ;
|
|
// la divido in parametri
|
|
STRVECTOR vsParams ;
|
|
Tokenize( sLine, ";", vsParams) ;
|
|
// 4 parametri
|
|
if ( vsParams.size() != 4)
|
|
return false ;
|
|
// Data (Level,Mode,Status,Other)
|
|
if ( ! DataFromString( vsParams[0]))
|
|
return false ;
|
|
// OldData è inizializzato con Data
|
|
for ( int i = 0 ; i < DIM ; ++ i)
|
|
m_OldData[i] = m_Data[i] ;
|
|
// materiale
|
|
int nMat ;
|
|
if ( ! FromString( vsParams[1], nMat))
|
|
return false ;
|
|
m_Material = __max( nMat, GDB_MT_COLOR) ;
|
|
// colore
|
|
if ( ! FromString( vsParams[2], m_Color))
|
|
return false ;
|
|
// numero di stringhe (nelle linee successive)
|
|
int nNext ;
|
|
if ( ! FromString( vsParams[3], nNext))
|
|
return false ;
|
|
// leggo le eventuali stringhe
|
|
for ( int i = 0 ; i < nNext ; ++i) {
|
|
// leggo la linea
|
|
if ( ! TheScanner.GetLine( sLine))
|
|
return false ;
|
|
// la carico in lista
|
|
m_slInfo.push_back( sLine) ;
|
|
}
|
|
|
|
return true ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
Attribs::DataFromString( const string& sParam)
|
|
{
|
|
// il primo parametro è diviso in 4 parti
|
|
STRVECTOR vsParams ;
|
|
Tokenize( sParam, ",", vsParams) ;
|
|
// 4 parti
|
|
if ( vsParams.size() != 4)
|
|
return false ;
|
|
// livello
|
|
int nLev ;
|
|
if ( ! FromString( vsParams[0], nLev))
|
|
return false ;
|
|
m_Data[LEVEL] = CLIP( nLev, GDB_LV_USER, GDB_LV_TEMP) ;
|
|
// modo
|
|
int nMode ;
|
|
if ( ! FromString( vsParams[1], nMode))
|
|
return false ;
|
|
m_Data[MODE] = CLIP( nMode, GDB_MD_STD, GDB_MD_HIDDEN) ;
|
|
// stato
|
|
int nStat ;
|
|
if ( ! FromString( vsParams[2], nStat))
|
|
return false ;
|
|
m_Data[STATUS] = CLIP( nStat, GDB_ST_ON, GDB_ST_OFF) ;
|
|
// altro
|
|
int nOldStatus ;
|
|
if ( ! FromString( vsParams[3], nOldStatus))
|
|
return false ;
|
|
m_Data[OTHER] = CLIP( nOldStatus, 0, 255) ;
|
|
|
|
return true ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
Attribs::SetName( const string& sName)
|
|
{
|
|
// se nome non valido, esco con errore
|
|
if ( ! ValidString( sName))
|
|
return false ;
|
|
|
|
// può essere solo la prima stringa
|
|
STRLIST::iterator iIter ;
|
|
if ( ( iIter = m_slInfo.begin()) != m_slInfo.end() &&
|
|
FindKey( *iIter, NAME)) {
|
|
*iIter = NAME + EQUAL + sName ;
|
|
return true ;
|
|
}
|
|
|
|
// altrimenti devo inserire la stringa prima dell'inizio
|
|
try {
|
|
m_slInfo.push_front( NAME + EQUAL + sName) ;
|
|
}
|
|
catch(...) {
|
|
return false ;
|
|
}
|
|
return true ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
Attribs::GetName( string& sName) const
|
|
{
|
|
// può essere solo la prima stringa
|
|
STRLIST::const_iterator iIter ;
|
|
if ( ( iIter = m_slInfo.begin()) != m_slInfo.end() &&
|
|
FindKey( *iIter, NAME)) {
|
|
sName = iIter->substr( NAME.length() + 1) ;
|
|
return true ;
|
|
}
|
|
|
|
return false ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
Attribs::RemoveName( void)
|
|
{
|
|
// può essere solo la prima stringa
|
|
STRLIST::const_iterator iIter ;
|
|
if ( ( iIter = m_slInfo.begin()) != m_slInfo.end() &&
|
|
FindKey( *iIter, NAME)) {
|
|
m_slInfo.pop_front() ;
|
|
return true ;
|
|
}
|
|
|
|
// non trovato, il nome non richiede rimozione
|
|
return true ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
Attribs::SetInfo( const string& sKey, const string& sVal)
|
|
{
|
|
// se chiave o valore non validi, esco con errore
|
|
if ( ! ValidString( sKey) || ! ValidString( sVal))
|
|
return false ;
|
|
|
|
// se è il nome
|
|
if ( sKey == NAME)
|
|
return SetName( sVal) ;
|
|
|
|
// se esiste già una stringa con quella chiave la sostituisco
|
|
STRLIST::iterator iIter ;
|
|
for ( iIter = m_slInfo.begin() ; iIter != m_slInfo.end() ; ++ iIter) {
|
|
if ( FindKey( *iIter, sKey)) {
|
|
*iIter = sKey + EQUAL + sVal ;
|
|
return true ;
|
|
}
|
|
}
|
|
// altrimenti la appendo
|
|
try {
|
|
m_slInfo.push_back( sKey + EQUAL + sVal) ;
|
|
}
|
|
catch(...) {
|
|
return false ;
|
|
}
|
|
return true ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
Attribs::GetInfo( const string& sKey, string& sVal) const
|
|
{
|
|
// se chiave non valida, esco con errore
|
|
if ( ! ValidString( sKey))
|
|
return false ;
|
|
|
|
// cerco una stringa con la chiave
|
|
STRLIST::const_iterator iIter ;
|
|
for ( iIter = m_slInfo.begin() ; iIter != m_slInfo.end() ; ++ iIter) {
|
|
if ( FindKey( *iIter, sKey)) {
|
|
sVal = iIter->substr( sKey.length() + 1) ; ;
|
|
return true ;
|
|
}
|
|
}
|
|
|
|
return false ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
Attribs::RemoveInfo( const string& sKey)
|
|
{
|
|
// se chiave non valida, esco con errore
|
|
if ( ! ValidString( sKey))
|
|
return false ;
|
|
|
|
// cerco una stringa con la chiave
|
|
STRLIST::iterator iIter ;
|
|
for ( iIter = m_slInfo.begin() ; iIter != m_slInfo.end() ; ++ iIter) {
|
|
if ( FindKey( *iIter, sKey)) {
|
|
m_slInfo.erase( iIter) ;
|
|
return true ;
|
|
}
|
|
}
|
|
|
|
// non trovata, la info non richiede rimozione
|
|
return true ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
Attribs::FindKey( const string& sString, const string& sKey) const
|
|
{
|
|
return ( sString.compare( 0, sKey.length(), sKey) == 0 &&
|
|
sString.length() > sKey.length() &&
|
|
sString[sKey.length()] == EQUAL) ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
Attribs::ValidString( const string& sString) const
|
|
{
|
|
return ( sString.length() > 0 &&
|
|
sString.find( '\n') == string::npos &&
|
|
sString.find_first_not_of( " \t\r\n") != string::npos) ;
|
|
}
|