Page principale | Liste alphabétique | Liste des classes | Liste des fichiers | Membres de classe | Membres de fichier | Pages associées

Ini.cpp

Aller à la documentation de ce fichier.
00001 /*! \file Ini.cpp
00002     \brief Gestion de fichier ini.
00003 
00004     Utilisation de fichier structurer .ini.
00005 
00006     \author     aerith (http://aerith.fr)
00007     \version    1.0
00008     \date       01/07/2008
00009 */
00010 
00011 #include    "Ini.h"
00012 
00013 MUTEX           Ini::iniMutex;
00014 unsigned int    Ini::iniInstance = 0;
00015 
00016 Ini::Ini(const char *pName)
00017 {
00018     if(!iniInstance)
00019         MutexInit(&iniMutex);
00020 
00021     iniInstance++;
00022 
00023     fileName = pName;
00024     sectionVide = new IniSection("");
00025 }
00026 
00027 Ini::~Ini()
00028 {
00029     unsigned int    i;
00030 
00031     for(i = 0; i < sections.size(); i++)
00032         delete sections[i];
00033     sections.clear();
00034 
00035     delete sectionVide;
00036 
00037     iniInstance--;
00038 
00039     if(!iniInstance)
00040         MutexStop(&iniMutex);
00041 }
00042 
00043 void    Ini::Load()
00044 {
00045     FILE    *fichier;
00046     char    data[INI_BUFFERSIZE];
00047     char    *p;
00048     char    c;
00049     int     m;
00050     TRY
00051     
00052     MutexLock(&iniMutex);
00053     p = data;
00054     m = 0;
00055 
00056     fichier = fopen(fileName.data(), "r");
00057     if(!fichier)
00058         THROW(ERROR_C_DATA_FILE)
00059 
00060     while(!feof(fichier))
00061     {
00062         c = fgetc(fichier);
00063         if(c)
00064         {
00065             *p = '\0';
00066 
00067             switch(c)
00068             {
00069                 case    '#' :
00070                 case    ';' :
00071                     if(m == 0)
00072                         m = 10;
00073                     else
00074                         *p++ = c;
00075                     break;
00076                 case    '[' :
00077                     if(m == 0)
00078                         p = data;
00079                     else
00080                         *p++ = c;
00081                     break;
00082                 case    ']' :
00083                     if(m == 0)
00084                     {
00085                         if(IsAlphaNum(data))
00086                             AddSection(data);
00087 
00088                         p = data;
00089                     }
00090                     else
00091                         *p++ = c;
00092                     break;
00093                 case    '=' :
00094                     if(m == 0)
00095                     {
00096                         if(IsAlphaNum(data) && sections.size())
00097                         {
00098                             sections.back()->AddItem(data);
00099                             m = 1;
00100                             p = data;
00101                         }
00102                     }
00103                     else
00104                         *p++ = c;
00105                     break;
00106                 case    '\0':
00107                 case    '\n':
00108                 case    '\r':
00109                     if(m == 1)
00110                         sections.back()->GetItem()->SetData(data);
00111 
00112                     m = 0;
00113                     p = data;
00114                     break;
00115                 default     :
00116                     *p++ = c;
00117             }
00118         }
00119     }
00120 
00121     fclose(fichier);
00122 
00123     MutexUnLock(&iniMutex);
00124     
00125     CATCH
00126 }
00127 
00128 void    Ini::Save()
00129 {
00130     FILE            *fichier;
00131     unsigned int    i, j;
00132     TRY
00133     
00134     MutexLock(&iniMutex);
00135 
00136     fichier = fopen(fileName.data(), "w");
00137     if(fichier)
00138         THROW(ERROR_C_DATA_FILE)
00139 
00140     for(i = 0; i < sections.size(); i++)
00141     {
00142         fprintf(fichier, "[%s]\n", sections[i]->GetName());
00143 
00144         for(j = 0; j < sections[i]->GetNbItem(); j++)
00145             fprintf(fichier, "%s=%s\n", sections[i]->GetItem(j)->GetName(), sections[i]->GetItem(j)->GetData());
00146     }
00147 
00148     fclose(fichier);
00149 
00150     MutexUnLock(&iniMutex);
00151     
00152     CATCH
00153 }
00154 
00155 void    Ini::AddSection(const char *pName)
00156 {
00157     sections.push_back(new IniSection(pName));
00158 }
00159 
00160 void    Ini::DelSection(unsigned int iPos)
00161 {
00162     if(iPos < sections.size())
00163         sections.erase(sections.begin() + iPos);
00164 }
00165 
00166 void    Ini::DelSection(const char *pName)
00167 {
00168     for(unsigned int i = 0; i < sections.size(); i++)
00169     {
00170         if(!strcmp(sections[i]->GetName(), pName))
00171         {
00172             sections.erase(sections.begin() + i);
00173             return;
00174         }
00175     }
00176 }
00177 
00178 IniSection  *Ini::GetSection(unsigned int iPos)
00179 {
00180     if(iPos < sections.size())
00181         return sections[iPos];
00182 
00183     return sectionVide;
00184 }
00185 
00186 IniSection  *Ini::GetSection(const char *pName, unsigned int iOcur)
00187 {
00188     unsigned int    ocur = 0, i;
00189 
00190     for(i = 0; i < sections.size(); i++)
00191     {
00192         if(!strcmp(sections[i]->GetName(), pName))
00193         {
00194             if(ocur == iOcur)
00195                 return sections[i];
00196 
00197             ocur++;
00198         }
00199     }
00200 
00201     return sectionVide;
00202 }
00203 
00204 const char  *Ini::GetSectionName(unsigned int iPos)
00205 {
00206     if(iPos < sections.size())
00207         return sections[iPos]->GetName();
00208 
00209     return NULL;
00210 };
00211 
00212 unsigned int    Ini::GetNbSection(const char *pName)
00213 {
00214     unsigned int    nb = 0, i;
00215 
00216     for(i = 0; i < sections.size(); i++)
00217     {
00218         if(!strcmp(sections[i]->GetName(), pName))
00219             nb++;
00220     }
00221 
00222     return nb;
00223 }
00224 

Généré le Thu Jun 12 09:12:29 2008 pour A.I.F. par  doxygen 1.3.9.1