00001
00002
00009
00010
00011
#include "xml_score.h"
00012
#include <stdio.h>
00013
00014 CXMLScore::CXMLScore ()
00015 {
00016
00017 }
00018
00019 CXMLScore::~CXMLScore ()
00020 {
00021 xmlFreeDoc (m_pDocument);
00022 }
00023
00024
00025 bool CXMLScore::Charger (
const char* szFilename)
00026 {
00027 m_szFilename =
std::string (szFilename);
00028
00029 m_pDocument = xmlParseFile (szFilename);
00030
00031
00032 xmlNode* pScores = xmlDocGetRootElement (m_pDocument);
00033
if ((!xmlStrcmp (pScores->name, (
const xmlChar *)
"scores")))
00034 Scores (pScores);
00035
00036
return true;
00037 }
00038
00039 bool CXMLScore::Sauver ()
00040 {
00041
00042 xmlSaveFile (m_szFilename.c_str (), m_pDocument);
00043
00044
return true;
00045 }
00046
00047 bool CXMLScore::AjouterScore (
SScore Score)
00048 {
00049 xmlNode* pScores = xmlDocGetRootElement (m_pDocument);
00050 xmlNode* pText;
00051
00052
00053 pText = xmlNewText ((
const xmlChar *)
"\t");
00054
00055 xmlAddChild (pScores, pText);
00056
char szBuffer [16];
00057
00058
00059 xmlNode* pScore = xmlNewChild (pScores, 0, (
const xmlChar *)
"score", 0);
00060
00061
00062
for (std::list<SJoueur>::const_iterator JoueurIt = Score.
m_Joueur.begin ();
00063 JoueurIt != Score.
m_Joueur.end ();
00064 JoueurIt++)
00065 {
00066 pText = xmlNewText ((
const xmlChar *)
"\n\t\t");
00067 xmlAddChild (pScore, pText);
00068
00069 xmlNode* pJoueur = xmlNewChild (pScore, 0, (
const xmlChar *)
"joueur", 0);
00070 xmlNewProp (pJoueur, (
const xmlChar *)
"nom", (
const xmlChar *) JoueurIt->m_szNom.c_str ());
00071 memset (szBuffer,
'\0', 16);
00072 sprintf (szBuffer,
"%d", JoueurIt->m_uiScore);
00073 xmlNewProp (pJoueur, (
const xmlChar *)
"score", (
const xmlChar *) szBuffer);
00074 memset (szBuffer,
'\0', 16);
00075 sprintf (szBuffer,
"%d", JoueurIt->m_uiNiveau);
00076 xmlNewProp (pJoueur, (
const xmlChar *)
"niveau", (
const xmlChar *) szBuffer);
00077 }
00078 pText = xmlNewText ((
const xmlChar *)
"\n\t");
00079 xmlAddChild (pScore, pText);
00080
00081 pText = xmlNewText ((
const xmlChar *)
"\n");
00082 xmlAddChild (pScores, pText);
00083
00084
return true;
00085 }
00086
00087
00088
void CXMLScore::Scores (xmlNode* pScores)
00089 {
00090 xmlNode* pScore = pScores->children;
00091
do
00092 {
00093
if (pScore->type == XML_ELEMENT_NODE)
00094
if ((!xmlStrcmp (pScore->name, (
const xmlChar *)
"score")))
00095 m_Scores.insert (Score (pScore));
00096 }
00097
while ((pScore = pScore->next) != 0);
00098 }
00099
00100
00101
SScore CXMLScore::Score (xmlNode* pScore)
00102 {
00103
SScore Score;
00104
00105 xmlNode* pJoueur = pScore->children;
00106
if (!pJoueur)
00107
return Score;
00108
00109
do
00110 {
00111
if (pJoueur->type == XML_ELEMENT_NODE)
00112
if ((!xmlStrcmp (pJoueur->name, (
const xmlChar *)
"joueur")))
00113 Score.
m_Joueur.push_back (Joueur (pJoueur));
00114 }
00115
while ((pJoueur = pJoueur->next) != 0);
00116
00117
return Score;
00118 }
00119
00120
00121
SJoueur CXMLScore::Joueur (xmlNode* pJoueur)
00122 {
00123
SJoueur Joueur;
00124
00125 xmlElement* pElementJoueur = (xmlElement*) pJoueur;
00126 xmlAttribute* pAttribute = pElementJoueur->attributes;
00127
if (!pAttribute)
00128
return Joueur;
00129
00130
do
00131 {
00132
if (pAttribute->type == XML_ATTRIBUTE_NODE)
00133 {
00134
if ((!xmlStrcmp (pAttribute->name, (
const xmlChar *)
"nom")))
00135 Joueur.
m_szNom =
std::string ((
const char*) xmlGetProp (pJoueur, pAttribute->name));
00136
else if ((!xmlStrcmp (pAttribute->name, (
const xmlChar *)
"score")))
00137 Joueur.
m_uiScore = atoi ((
const char*) xmlGetProp (pJoueur, pAttribute->name));
00138
else if ((!xmlStrcmp (pAttribute->name, (
const xmlChar *)
"niveau")))
00139 Joueur.
m_uiNiveau = atoi ((
const char*) xmlGetProp (pJoueur, pAttribute->name));
00140 }
00141 }
00142
while ((pAttribute = (xmlAttribute*) pAttribute->next) != 0);
00143
00144
return Joueur;
00145 }
00146
00147
00148 inline bool operator < (
SScore Score1,
SScore Score2)
00149 {
00150
unsigned int uiScore1 = 0, uiScore2 = 0;
00151
00152
for (std::list<SJoueur>::iterator it = Score1.
m_Joueur.begin ();
00153 it != Score1.
m_Joueur.end (); it++)
00154 uiScore1 += it->m_uiScore;
00155
00156
for (std::list<SJoueur>::iterator it = Score2.
m_Joueur.begin ();
00157 it != Score2.
m_Joueur.end (); it++)
00158 uiScore2 += it->m_uiScore;
00159
00160
return uiScore1 < uiScore2;
00161 }
00162
00163
00164 inline bool operator > (
SScore Score1,
SScore Score2)
00165 {
00166
unsigned int uiScore1 = 0, uiScore2 = 0;
00167
00168
for (std::list<SJoueur>::iterator it = Score1.
m_Joueur.begin ();
00169 it != Score1.
m_Joueur.end (); it++)
00170 uiScore1 += it->m_uiScore;
00171
00172
for (std::list<SJoueur>::iterator it = Score2.
m_Joueur.begin ();
00173 it != Score2.
m_Joueur.end (); it++)
00174 uiScore2 += it->m_uiScore;
00175
00176
return uiScore1 > uiScore2;
00177 }