bjone Insert booze to continue | Bon j'ai testé une approche qui permet d'écrire comme ça:
ça me plait pas trop mal.
avec un fichier xmeul pas compliant style:
Code :
- <Yop A="84" B="95.776505" Test="20" wesh="7139.703064" zorg="true" />
- <Youpi rg="24" />
- <Yop A="90" Test="20" wesh="-9577.649780" B="-119.720622" zorg="false" />
- <Youpa rg="644" />
- <Yop B="-167.608908" zorg="false" Test="20" wesh="-14018.199585" A="94" />
- <zz a="98" />
|
Code :
- #include <iostream>
- #include "XmlElement.h"
- using namespace std;
- int main()
- {
- TiXmlDocument Doc;
- if( Doc.LoadFile("test.xml" ) )
- {
- XmlElement Xml=Doc.FirstChildElement("Yop" );
- while( Xml )
- {
- int A=12;
- float B=-20;
- bool zorg=false;
- Xml["A"]>>A;
- Xml["B"]>>B;
- Xml["zorg"]>>zorg;
- cout<<A<<" "<<B<<" : "<<zorg<<"\n";
- Xml["A"]<<A+2;
- Xml["B"]<<B*1.1;
- Xml["wesh"]<<A*B;
- Xml["zorg"]<<(B>A);
- ++Xml;
- }
- Doc.SaveFile();
- }
- }
|
bon c'est une surcouche à TinyXml, en mode cracra const char * et pas STL, demain je fais la variante STL et je m'y colle, sinon coté code ça donne ça:
XmlElement.h
Code :
- #pragma once
- #include <tinyxml.h>
- struct XmlElementAttribute
- {
- TiXmlElement *Element;
- const char *Attribute;
- XmlElementAttribute( TiXmlElement *Elem, const char *Attr ) : Element(Elem), Attribute(Attr) {};
- const char *GetValue() const
- {
- if( !Element || !Attribute )
- return NULL;
- return Element->Attribute(Attribute);
- }
- operator const char*() const
- {
- return GetValue();
- }
- };
- class XmlElement
- {
- public:
- TiXmlElement *Element;
- XmlElement( TiXmlElement *Elem=NULL ) : Element(Elem) {};
- XmlElement &operator = (TiXmlElement *Elem )
- {
- Element=Elem;
- return *this;
- }
- operator TiXmlElement * () const
- {
- return Element;
- }
- TiXmlElement * operator -> () const
- {
- return Element;
- }
- XmlElement &operator ++ ();
- const XmlElementAttribute operator [] ( const char *AttributeName ) const
- {
- return XmlElementAttribute( Element, AttributeName );
- }
- };
- int operator >> ( const XmlElementAttribute &ElemAttr, int &Value );
- float operator >> ( const XmlElementAttribute &ElemAttr, float &Value );
- double operator >> ( const XmlElementAttribute &ElemAttr, double &Value );
- bool operator >> ( const XmlElementAttribute &ElemAttr, bool &Value );
- const char * operator << ( const XmlElementAttribute &ElemAttr, const char *Value );
- int operator << ( const XmlElementAttribute &ElemAttr, int Value );
- double operator << ( const XmlElementAttribute &ElemAttr, double Value );
- bool operator << ( const XmlElementAttribute &ElemAttr, bool Value );
|
XmlElement.cpp
Code :
- include <assert.h>
- #include "XmlElement.h"
- #include "Size.h"
- XmlElement &XmlElement::operator ++ ()
- {
- if( Element )
- {
- const char *Name=Element->Value();
- if( Name )
- Element=Element->NextSiblingElement(Name);
- else
- Element=NULL;
- }
- return *this;
- }
- //////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////
- bool ExtractInt( const char *Str, int &Value )
- {
- assert(Str);
- if( sscanf(Str,"%d",&Value) == 1 || sscanf( Str,"%x",&Value) == 1 )
- return true;
- return false;
- }
- bool ExtractFloat( const char *Str, float &Value )
- {
- assert(Str);
- if( sscanf(Str,"%f",&Value) == 1 )
- return true;
- else
- {
- int iValue;
- if( ExtractInt( Str, iValue ) )
- {
- Value=(float)iValue;
- return true;
- }
- }
- return false;
- }
- bool ExtractDouble( const char *Str, double &Value )
- {
- assert(Str);
- if( sscanf(Str,"%lf",&Value) == 1 )
- return true;
- else
- {
- float fValue;
- if( ExtractFloat( Str, fValue) )
- {
- Value=fValue;
- return true;
- }
- }
- return false;
- }
- int operator >> ( const XmlElementAttribute &ElemAttr, int &Value )
- {
- if( const char *Attribute=ElemAttr.GetValue() )
- ExtractInt( Attribute, Value );
- return Value;
- }
- float operator >> ( const XmlElementAttribute &ElemAttr, float &Value )
- {
- if( const char *Attribute=ElemAttr.GetValue() )
- ExtractFloat( Attribute, Value );
- return Value;
- }
- double operator >> ( const XmlElementAttribute &ElemAttr, double &Value )
- {
- if( const char *Attribute=ElemAttr.GetValue() )
- ExtractDouble( Attribute, Value );
- return Value;
- }
- bool operator >> ( const XmlElementAttribute &ElemAttr, bool &Value )
- {
- const char *TrueStrings[]={ "true", "valid", "enable", "enabled" };
- const char *FalseStrings[]={ "false", "invalid", "disable", "disabled" };
- if( const char *Attribute=ElemAttr.GetValue() )
- {
- for( size_t i=0 ; i < size(TrueStrings) ; ++i )
- if( !stricmp(TrueStrings[i],Attribute) )
- return Value=true;
- for( size_t i=0 ; i < size(FalseStrings) ; ++i )
- if( !strcmpi(FalseStrings[i],Attribute) )
- return Value=false;
- int iValue;
- if( ExtractInt( Attribute, iValue ) )
- return Value=(iValue!=0);
- }
- return Value;
- }
- //////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////
- const char * operator << ( const XmlElementAttribute &ElemAttr, const char *Value )
- {
- if( ElemAttr.Element )
- ElemAttr.Element->SetAttribute(ElemAttr.Attribute,Value);
- return Value;
- }
- int operator << ( const XmlElementAttribute &ElemAttr, int Value )
- {
- if( ElemAttr.Element )
- ElemAttr.Element->SetAttribute( ElemAttr.Attribute, Value );
- return Value;
- }
- double operator << ( const XmlElementAttribute &ElemAttr, double Value )
- {
- if( ElemAttr.Element )
- ElemAttr.Element->SetDoubleAttribute( ElemAttr.Attribute, Value );
- return Value;
- }
- bool operator << ( const XmlElementAttribute &ElemAttr, bool Value )
- {
- if( ElemAttr.Element )
- ElemAttr.Element->SetAttribute( ElemAttr.Attribute, Value ? "true" : "false" );
- return Value;
- }
|
bon oki, const char*, strcmpi, sscanf, const char * blog[] ça pue du bec, mais c'était un essai.
des remarques ?
y'a moyen de simplifier ?
c'est de la mairde ça sert à rien ?
j'ai que du temps à perdre ?
sapu c pas libre ? (en fait si) Message édité par bjone le 02-05-2005 à 23:52:15
|