1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
#include "xml_score.h"
CXMLScore::CXMLScore ()
{
}
CXMLScore::~CXMLScore ()
{
xmlFreeDoc (m_pDocument);
}
// Chargement du fichier XML contenant les niveaux
bool CXMLScore::Charger (const char* szFilename)
{
m_szFilename = std::string (szFilename);
// Parse le document XML et créé l'arbre DOM
m_pDocument = xmlParseFile (szFilename);
// Récupère l'élément racine du document
xmlNode* pScores = xmlDocGetRootElement (m_pDocument);
if ((!xmlStrcmp (pScores->name, (const xmlChar *)"scores")))
Scores (pScores);
return true;
}
bool CXMLScore::Sauver ()
{
// Sauvegarde du fichier XML
xmlSaveFile (m_szFilename.c_str (), m_pDocument);
return true;
}
bool CXMLScore::AjouterScore (SScore Score)
{
xmlNode* pScores = xmlDocGetRootElement (m_pDocument);
xmlNode* pText;
// Création d'un noeud text
pText = xmlNewText ((const xmlChar *) "\t");
// Ajout à l'élément pScores le noeud text
xmlAddChild (pScores, pText);
char szBuffer [16];
// Création d'un élément score
xmlNode* pScore = xmlNewChild (pScores, 0, (const xmlChar *) "score", 0);
// Ajout d'un nouvel élément nommé "score"
for (std::list<SJoueur>::const_iterator JoueurIt = Score.m_Joueur.begin ();
JoueurIt != Score.m_Joueur.end ();
JoueurIt++)
{
pText = xmlNewText ((const xmlChar *) "\n\t\t");
xmlAddChild (pScore, pText);
xmlNode* pJoueur = xmlNewChild (pScore, 0, (const xmlChar *) "joueur", 0);
xmlNewProp (pJoueur, (const xmlChar *) "nom", (const xmlChar *) JoueurIt->m_szNom.c_str ());
memset (szBuffer, '\0', 16);
sprintf (szBuffer, "%d", JoueurIt->m_uiScore);
xmlNewProp (pJoueur, (const xmlChar *) "score", (const xmlChar *) szBuffer);
memset (szBuffer, '\0', 16);
sprintf (szBuffer, "%d", JoueurIt->m_uiNiveau);
xmlNewProp (pJoueur, (const xmlChar *) "niveau", (const xmlChar *) szBuffer);
}
pText = xmlNewText ((const xmlChar *) "\n\t");
xmlAddChild (pScore, pText);
pText = xmlNewText ((const xmlChar *) "\n");
xmlAddChild (pScores, pText);
return true;
}
// Parcours un élément 'scores'
void CXMLScore::Scores (xmlNode* pScores)
{
xmlNode* pScore = pScores->children;
do
{
if (pScore->type == XML_ELEMENT_NODE)
if ((!xmlStrcmp (pScore->name, (const xmlChar *)"score")))
m_Scores.insert (Score (pScore));
}
while ((pScore = pScore->next) != 0);
}
// Parcours un élément 'score'
SScore CXMLScore::Score (xmlNode* pScore)
{
SScore Score;
xmlNode* pJoueur = pScore->children;
if (!pJoueur)
return Score;
do
{
if (pJoueur->type == XML_ELEMENT_NODE)
if ((!xmlStrcmp (pJoueur->name, (const xmlChar *)"joueur")))
Score.m_Joueur.push_back (Joueur (pJoueur));
}
while ((pJoueur = pJoueur->next) != 0);
return Score;
}
// Parcours un élément joueur
SJoueur CXMLScore::Joueur (xmlNode* pJoueur)
{
SJoueur Joueur;
xmlElement* pElementJoueur = (xmlElement*) pJoueur;
xmlAttribute* pAttribute = pElementJoueur->attributes;
if (!pAttribute)
return Joueur;
do
{
if (pAttribute->type == XML_ATTRIBUTE_NODE)
{
if ((!xmlStrcmp (pAttribute->name, (const xmlChar *)"nom")))
Joueur.m_szNom = std::string ((const char*) xmlGetProp (pJoueur, pAttribute->name));
else if ((!xmlStrcmp (pAttribute->name, (const xmlChar *)"score")))
Joueur.m_uiScore = atoi ((const char*) xmlGetProp (pJoueur, pAttribute->name));
else if ((!xmlStrcmp (pAttribute->name, (const xmlChar *)"niveau")))
Joueur.m_uiNiveau = atoi ((const char*) xmlGetProp (pJoueur, pAttribute->name));
}
}
while ((pAttribute = (xmlAttribute*) pAttribute->next) != 0);
return Joueur;
}
// Redéfinition de l'opérateur < pour comparer deux SScore afin que le conteneur set puisse trier les meilleurs scores
inline bool operator < (SScore Score1, SScore Score2)
{
unsigned int uiScore1 = 0, uiScore2 = 0;
for (std::list<SJoueur>::iterator it = Score1.m_Joueur.begin ();
it != Score1.m_Joueur.end (); it++)
uiScore1 += it->m_uiScore;
for (std::list<SJoueur>::iterator it = Score2.m_Joueur.begin ();
it != Score2.m_Joueur.end (); it++)
uiScore2 += it->m_uiScore;
return uiScore1 < uiScore2;
}
// Redéfinition de l'opérateur > pour comparer deux SScore afin que le conteneur set puisse trier les meilleurs scores
inline bool operator > (SScore Score1, SScore Score2)
{
unsigned int uiScore1 = 0, uiScore2 = 0;
for (std::list<SJoueur>::iterator it = Score1.m_Joueur.begin ();
it != Score1.m_Joueur.end (); it++)
uiScore1 += it->m_uiScore;
for (std::list<SJoueur>::iterator it = Score2.m_Joueur.begin ();
it != Score2.m_Joueur.end (); it++)
uiScore2 += it->m_uiScore;
return uiScore1 > uiScore2;
} |
Partager