00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026
00027 #include "CfgFile.hh"
00028
00029
00030 void clCfgFile::ReadFile (const char *cpFileName)
00031 {
00032 char cpInBuf[CFGF_BUFSIZE + 1];
00033 char cpName[CFGF_BUFSIZE + 1];
00034 char cpValue[CFGF_BUFSIZE + 1];
00035 char *cpTokPtr;
00036 FILE *FpCfgFile;
00037
00038 iEntryCount = 0;
00039 FpCfgFile = fopen(cpFileName, "rt");
00040 if (FpCfgFile == NULL)
00041 {
00042 return;
00043 }
00044 do {
00045 if (iEntryCount >= CFGF_MAX_ENTRIES - 2)
00046 {
00047 break;
00048 }
00049 fgets(cpInBuf, CFGF_BUFSIZE, FpCfgFile);
00050 cpTokPtr = strtok(cpInBuf, " \t\n");
00051 if (cpTokPtr != NULL)
00052 {
00053 strcpy(cpName, cpTokPtr);
00054 cpTokPtr = strtok(NULL, " \t\n");
00055 if (cpTokPtr != NULL)
00056 {
00057 strcpy(cpValue, cpTokPtr);
00058 Names[iEntryCount].Size(strlen(cpName) + 1);
00059 Values[iEntryCount].Size(strlen(cpValue) + 1);
00060 strcpy(Names[iEntryCount], cpName);
00061 strcpy(Values[iEntryCount], cpValue);
00062 iEntryCount++;
00063 }
00064 }
00065 } while (feof(FpCfgFile) == 0);
00066 fclose(FpCfgFile);
00067 }
00068
00069
00070 void clCfgFile::FreeAll ()
00071 {
00072 int iLoopCntr;
00073
00074 for (iLoopCntr = 0; iLoopCntr < iEntryCount; iLoopCntr++)
00075 {
00076 Names[iLoopCntr].Free();
00077 Values[iLoopCntr].Free();
00078 }
00079 iEntryCount = 0;
00080 }
00081
00082
00083 clCfgFile::clCfgFile ()
00084 {
00085 iEntryCount = 0;
00086 }
00087
00088
00089 clCfgFile::clCfgFile (const char *cpFileName)
00090 {
00091 iEntryCount = 0;
00092 ReadFile(cpFileName);
00093 }
00094
00095
00096 void clCfgFile::SetFileName (const char *cpFileName)
00097 {
00098 FreeAll();
00099 ReadFile(cpFileName);
00100 }
00101
00102
00103 clCfgFile::~clCfgFile ()
00104 {
00105 try
00106 {
00107 FreeAll();
00108 }
00109 catch (...)
00110 {
00111 }
00112 }
00113
00114
00115 bool clCfgFile::GetStr (const char *cpKey, char *cpVal)
00116 {
00117 int iLoopCntr;
00118
00119 for (iLoopCntr = 0; iLoopCntr < iEntryCount; iLoopCntr++)
00120 {
00121 if (strcmp(Names[iLoopCntr], cpKey) == 0)
00122 {
00123 strcpy(cpVal, Values[iLoopCntr]);
00124 return true;
00125 }
00126 }
00127 return false;
00128 }
00129
00130
00131 bool clCfgFile::GetInt (const char *cpKey, int *ipVal)
00132 {
00133 int iLoopCntr;
00134
00135 for (iLoopCntr = 0; iLoopCntr < iEntryCount; iLoopCntr++)
00136 {
00137 if (strcmp(Names[iLoopCntr], cpKey) == 0)
00138 {
00139 sscanf(Values[iLoopCntr], "%i", ipVal);
00140 return true;
00141 }
00142 }
00143 return false;
00144 }
00145
00146
00147 bool clCfgFile::GetInt (const char *cpKey, long *lpVal)
00148 {
00149 int iLoopCntr;
00150
00151 for (iLoopCntr = 0; iLoopCntr < iEntryCount; iLoopCntr++)
00152 {
00153 if (strcmp(Names[iLoopCntr], cpKey) == 0)
00154 {
00155 sscanf(Values[iLoopCntr], "%li", lpVal);
00156 return true;
00157 }
00158 }
00159 return false;
00160 }
00161
00162
00163 bool clCfgFile::GetFlt (const char *cpKey, float *fpVal)
00164 {
00165 int iLoopCntr;
00166
00167 for (iLoopCntr = 0; iLoopCntr < iEntryCount; iLoopCntr++)
00168 {
00169 if (strcmp(Names[iLoopCntr], cpKey) == 0)
00170 {
00171 sscanf(Values[iLoopCntr], "%f", fpVal);
00172 return true;
00173 }
00174 }
00175 return false;
00176 }
00177
00178
00179 bool clCfgFile::GetFlt (const char *cpKey, double *dpVal)
00180 {
00181 int iLoopCntr;
00182
00183 for (iLoopCntr = 0; iLoopCntr < iEntryCount; iLoopCntr++)
00184 {
00185 if (strcmp(Names[iLoopCntr], cpKey) == 0)
00186 {
00187 sscanf(Values[iLoopCntr], "%lf", dpVal);
00188 return true;
00189 }
00190 }
00191 return false;
00192 }
00193
00194
00195 int clCfgFile::GetFltArray (const char *cpKey, float *fpVals)
00196 {
00197 int iLoopCntr;
00198 int iValCntr = 0;
00199 char *cpTokPtr;
00200
00201 for (iLoopCntr = 0; iLoopCntr < iEntryCount; iLoopCntr++)
00202 {
00203 if (strcmp(Names[iLoopCntr], cpKey) == 0)
00204 {
00205 cpTokPtr = strtok(Values[iLoopCntr], ", \t\n");
00206 while (cpTokPtr != NULL)
00207 {
00208 sscanf(cpTokPtr, "%f", &fpVals[iValCntr++]);
00209 cpTokPtr = strtok(NULL, ", \t\n");
00210 }
00211 }
00212 }
00213 return iValCntr;
00214 }
00215