D:/work/code_by/ListTools/MyIniFile.cpp

查看本檔案說明文件.
00001 #include "MyIniFile.h"
00007 /*===========================================================================
00008 功能:將字串左右的空白及換行符號去掉
00009 傳入:string為字串
00010 ===========================================================================*/
00011 void trim(char *string)
00012 {
00013   int i, right, left;
00014 
00015   for (right = strlen(string) - 1; right >= 0; right--)
00016     if (string[right] != ' ' && string[right] != '\n')
00017       break;
00018   string[++right] = '\0';
00019   for (left = 0; left < right, string[left] == ' '; left++);
00020   if (left > 0)
00021     for (i = 0; i <= right - left; i++)
00022       string[i] = string[i + left];
00023 }
00024 
00025 /*===========================================================================
00026 功能:建構子
00027 傳入:name為區塊名字
00028 ===========================================================================*/
00029 TMySection::TMySection(char *name) : TMyStringList()
00030 {
00031   sectionName = new char[strlen(name) + 1];
00032   strcpy(sectionName, name);
00033 }
00034 
00035 /*===========================================================================
00036 功能:解構子,有將每個字串所佔的記憶體清掉
00037 ===========================================================================*/
00038 TMySection::~TMySection()
00039 {
00040   delete [] sectionName;
00041 }
00042 
00043 /*===========================================================================
00044 功能:建構子
00045 傳入:fname為ini檔名
00046 ===========================================================================*/
00047 TMyIniFile::TMyIniFile(char *fname)
00048 {
00049   char strLine[201];
00050   TMySection *Section;
00051 
00052   strcpy(fileName, fname);
00053   changed = false;
00054   if ((fIni = fopen(fileName,"rt")) == NULL)  //開啟ini檔
00055     fIni = fopen(fileName, "wt");
00056   else
00057   {
00058     fclose(fIni);
00059     fIni = fopen(fileName, "r+t");
00060   }
00061 
00062   Sections = new TMyList();
00063   while (fgets(strLine, 200, fIni) != NULL)  //讀取一行
00064   {
00065     strLine[200] = '\0';
00066     trim(strLine);  //去掉頭尾的空白及換行符號
00067     if (strLine[0] == '\0' || strLine[0] == ';')  //空白行或註解
00068       continue;
00069     if (strLine[0] == '[')  //此行是一個section
00070     {
00071       strLine[strlen(strLine) - 1] = '\0';  //去掉尾部的']'
00072       Sections->Add(Section = new TMySection(strLine + 1));  //新增一個section
00073     }
00074     else if (Section)
00075       Section->Add(strLine);  //在此section新增一個ident
00076   }
00077 }
00078 
00079 /*===========================================================================
00080 功能:解構子
00081 ===========================================================================*/
00082 TMyIniFile::~TMyIniFile()
00083 {
00084   TMySection *Section;
00085   int i, j;
00086 
00087   if (changed)  //ini檔被修改過
00088     fseek(fIni, 0, SEEK_SET);  
00089 
00090   for (i = 0; i < Sections->count; i++)  //對每個section
00091   {
00092     Section = (TMySection *)Sections->Items(i);
00093     if (changed)  //ini檔被修改過
00094     {
00095       fprintf(fIni, "[%s]\n", Section->sectionName);  //寫入一筆section
00096       for (j = 0; j < Section->count; j++)  //對此section中的每個ident
00097         fprintf(fIni, "%s\n", Section->Strings(j));  //寫入一筆ident
00098       fprintf(fIni, "\n");
00099     }
00100     delete Section;
00101   }
00102   delete Sections;
00103   fclose(fIni);
00104 }
00113 /*===========================================================================
00114 功能:讀取某一個區塊的某一個值
00115 傳入:section為區塊名
00116       ident為名字索引
00117       defaults為預設值
00118 傳出:該區塊中的某一個值
00119 ===========================================================================*/
00120 char *TMyIniFile::ReadString(char *section, char *ident, char *defaults)
00121 {
00122   TMySection *Section;
00123   char *tempc;
00124   int i;
00125 
00126   for (i = 0; i < Sections->count; i++)  //對每一個section
00127   {
00128     Section = (TMySection *)Sections->Items(i);
00129     if (strcmp(Section->sectionName, section) == 0)  //找出名字相符的section
00130     {
00131       tempc = Section->Values(ident);  //找出名字相符的ident的值
00132       return tempc[0] == '\0'? defaults : tempc;
00133     }
00134   }
00135   return defaults;
00136 }
00144 /*===========================================================================
00145 功能:將一個值寫入某一個區塊中
00146 傳入:section為區塊名
00147       ident為名字索引
00148       value為字串值
00149 ===========================================================================*/
00150 void TMyIniFile::WriteString(char *section, char *ident, char *value)
00151 {
00152   TMySection *Section;
00153   char str[201];
00154   bool flag = false;
00155   int i, j;
00156 
00157   strcpy(str, ident);
00158   strcat(str, "=");
00159   strcat(str, value);
00160   for (i = 0; i < Sections->count; i++)  //對每一個section
00161   {
00162     Section = (TMySection *)Sections->Items(i);
00163     if (strcmp(Section->sectionName, section) == 0)  //找出名字相符的section
00164     {
00165       for (j = 0; j < Section->count; j++)  //對此section的每一個ident
00166         if (strcmp(Section->Names(j), ident) == 0)  //找出名字相符的ident
00167         {
00168           Section->Delete(j);  //刪除此ident
00169           Section->Insert(j, str);  //在原位置插入新ident
00170           flag = true;
00171           break;
00172         }
00173       if (!flag)  //在此section中找不到相符的ident
00174       {
00175         Section->Add(str);  //在此section中新增ident
00176         flag = true;
00177       }
00178       break;
00179     }
00180   }
00181   if (!flag)  //找不到相符的section
00182   {
00183     Sections->Add(Section = new TMySection(section));  //新增section
00184     Section->Add(str);  //在此section中新增ident
00185   }
00186   changed = true;
00187 }
00188 

產生日期:Tue Jul 11 11:52:19 2006, 專案:cbmr, 產生器:  doxygen 1.4.7