BdD.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by Pierre Schmitt, Ludovic Giacomello, Nhi Ly   *
00003  *      *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #include "BdD.h"
00022 
00023 BdD::BdD(){
00024         mVerrou.Lock();
00025         Charger();
00026         mVerrou.Unlock();
00027 }
00028 
00029 BdD::~BdD(){
00030         Sauvegarder();
00031 }
00032 
00033 int BdD::AjouterUtilisateur(string login, string mdp){
00034         mVerrou.Lock();
00035         if(Rechercher(login)==-1){
00036                 UserInfo u;
00037                 u.login=login;
00038                 u.mdp=mdp;
00039                 u.score=0;
00040                 mBdD.ConsQueue(u);
00041                 mVerrou.Unlock();
00042                 return 1;
00043         } 
00044         mVerrou.Unlock();
00045         return -1;
00046 }
00047 
00048 int BdD::SupprimerUtilisateur(string login){
00049         mVerrou.Lock();
00050         int pos=Rechercher(login);
00051         if(pos!=-1) {
00052                 mBdD.Detruit(pos);
00053                 mVerrou.Unlock();
00054                 return 1;
00055         }
00056         mVerrou.Unlock();
00057         return -1;
00058 }
00059 
00060 bool BdD::EstUtilisateur(string login, string mdp){
00061         mVerrou.Lock();
00062         int pos=Rechercher(login);
00063         if(pos!=-1) if(mBdD[pos].mdp==mdp){
00064                 mVerrou.Unlock();
00065                 return true;
00066         }
00067         mVerrou.Unlock();
00068         return false;
00069 }
00070 
00071 int BdD::GetScore(string login){
00072         mVerrou.Lock();
00073         int pos=Rechercher(login);
00074         if(pos!=-1) {
00075                 mVerrou.Unlock();
00076                 return mBdD[pos].score;
00077         }
00078         mVerrou.Unlock();
00079         return -1;      
00080 }
00081 
00082 int BdD::SetScore(string login, unsigned int score){
00083         mVerrou.Lock();
00084         int pos=Rechercher(login);
00085         if(pos!=-1) {
00086                 UserInfo u=mBdD[pos];
00087                 u.score=score;
00088                 mBdD.Modifier(pos,u);
00089                 mVerrou.Unlock();
00090                 return 1;
00091         }
00092         mVerrou.Unlock();
00093         return -1;      
00094 }
00095 
00096 void  BdD::Charger(){
00097         //On ouvre users.bdd pour charger la structure de donnée 
00098         ifstream f("users.bdd");
00099         string line;
00100         if(f.is_open()){ 
00101                 while(getline(f,line)){
00102                         string login,mdp, score;
00103                         istringstream iss(line);
00104                         getline(iss,login,' '); getline(iss,mdp,' '); getline(iss,score,' '); //<=> Tokens sur ' '
00105                         istringstream conv(score);
00106                         UserInfo u;
00107                         u.login=login; u.mdp=mdp; conv>>u.score;
00108                         mBdD.ConsQueue(u);
00109                         
00110                 }       
00111         } else {
00112                 //Le fichier n'existe pas, donc on le créé
00113                 ofstream fCreationBdD("users.bdd");
00114                 fCreationBdD.close();
00115         }
00116         f.close();
00117 }
00118 
00119 void  BdD::Sauvegarder(){
00120         mVerrou.Lock();
00121         ofstream f("users.bdd");
00122         if(f.is_open()){
00123                 for(unsigned int i=0; i<mBdD.Card(); i++){
00124                         f<<mBdD[i].login<<" "<<mBdD[i].mdp<<" "<<mBdD[i].score<<endl;
00125                 }
00126         } else cerr<<"Impossible d'ouvrir le fichier en écriture !"<<endl;
00127         f.close();
00128         mVerrou.Unlock();
00129 }
00130 
00131 
00132 int BdD::Rechercher(string login){
00133         for(unsigned int i=0; i<mBdD.Card(); i++){
00134                 if(mBdD[i].login==login) {
00135                         return i;
00136                 }
00137         }
00138         return -1;
00139 }
00140 
00141 void BdD::Afficher(){
00142         mVerrou.Lock();
00143         for(unsigned int i=0; i<mBdD.Card(); i++){
00144                 cout<<mBdD[i].login<<" "<<mBdD[i].mdp<<" "<<mBdD[i].score<<endl;
00145         }
00146         mVerrou.Unlock();
00147 }
00148 
00149 string BdD::ListeScore(){
00150         string res="";
00151         for(unsigned int i=0; i<mBdD.Card(); i++){
00152                 res+=mBdD[i].login; res+=" ";
00153                 ostringstream oss;
00154                 oss<<mBdD[i].score;
00155                 res+=oss.str(); res+=" ";
00156                 if(i<mBdD.Card()-1) res+=": ";
00157         }
00158         return res;
00159 }

Generated on Wed Jan 2 14:01:41 2008 for Pacman by  doxygen 1.5.1