00001 #include "MyIniFile.h"
00007
00008
00009
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
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
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)
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] == '[')
00070 {
00071 strLine[strlen(strLine) - 1] = '\0';
00072 Sections->Add(Section = new TMySection(strLine + 1));
00073 }
00074 else if (Section)
00075 Section->Add(strLine);
00076 }
00077 }
00078
00079
00080
00081
00082 TMyIniFile::~TMyIniFile()
00083 {
00084 TMySection *Section;
00085 int i, j;
00086
00087 if (changed)
00088 fseek(fIni, 0, SEEK_SET);
00089
00090 for (i = 0; i < Sections->count; i++)
00091 {
00092 Section = (TMySection *)Sections->Items(i);
00093 if (changed)
00094 {
00095 fprintf(fIni, "[%s]\n", Section->sectionName);
00096 for (j = 0; j < Section->count; j++)
00097 fprintf(fIni, "%s\n", Section->Strings(j));
00098 fprintf(fIni, "\n");
00099 }
00100 delete Section;
00101 }
00102 delete Sections;
00103 fclose(fIni);
00104 }
00113
00114
00115
00116
00117
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++)
00127 {
00128 Section = (TMySection *)Sections->Items(i);
00129 if (strcmp(Section->sectionName, section) == 0)
00130 {
00131 tempc = Section->Values(ident);
00132 return tempc[0] == '\0'? defaults : tempc;
00133 }
00134 }
00135 return defaults;
00136 }
00144
00145
00146
00147
00148
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++)
00161 {
00162 Section = (TMySection *)Sections->Items(i);
00163 if (strcmp(Section->sectionName, section) == 0)
00164 {
00165 for (j = 0; j < Section->count; j++)
00166 if (strcmp(Section->Names(j), ident) == 0)
00167 {
00168 Section->Delete(j);
00169 Section->Insert(j, str);
00170 flag = true;
00171 break;
00172 }
00173 if (!flag)
00174 {
00175 Section->Add(str);
00176 flag = true;
00177 }
00178 break;
00179 }
00180 }
00181 if (!flag)
00182 {
00183 Sections->Add(Section = new TMySection(section));
00184 Section->Add(str);
00185 }
00186 changed = true;
00187 }
00188