etienne herbin / FileIni
Committer:
dumont
Date:
Sun Jun 10 17:19:21 2012 +0000
Revision:
0:79f68acc26ec

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dumont 0:79f68acc26ec 1 #include "mbed.h"
dumont 0:79f68acc26ec 2 #include "FileIni.h"
dumont 0:79f68acc26ec 3
dumont 0:79f68acc26ec 4 #ifndef _POSIX_VERSION
dumont 0:79f68acc26ec 5
dumont 0:79f68acc26ec 6 int _strupr(char* ptrSource)
dumont 0:79f68acc26ec 7 {
dumont 0:79f68acc26ec 8 int i=0;
dumont 0:79f68acc26ec 9 while(ptrSource[i]!='\0')
dumont 0:79f68acc26ec 10 {
dumont 0:79f68acc26ec 11 if (ptrSource[i]>='a' && ptrSource[i]<='z') ptrSource[i]=ptrSource[i]-48;
dumont 0:79f68acc26ec 12 i++;
dumont 0:79f68acc26ec 13 }
dumont 0:79f68acc26ec 14 return 0;
dumont 0:79f68acc26ec 15 }
dumont 0:79f68acc26ec 16 #endif
dumont 0:79f68acc26ec 17
dumont 0:79f68acc26ec 18
dumont 0:79f68acc26ec 19 LPINI IniLoad(char* ptrFileName)
dumont 0:79f68acc26ec 20 {
dumont 0:79f68acc26ec 21 /** declaration des variables */
dumont 0:79f68acc26ec 22 FILE* ptrFile=NULL;
dumont 0:79f68acc26ec 23 INI *lpinStart=NULL, *lpinCurrent=NULL, *lpinTmp=NULL;
dumont 0:79f68acc26ec 24 INIKEY *lpikKey=NULL, *lpikTmp=NULL;
dumont 0:79f68acc26ec 25 char ligne[MAX_PATH];
dumont 0:79f68acc26ec 26 char isSectionNew=2;
dumont 0:79f68acc26ec 27 char *ptrToken=NULL, *ptrLine=NULL;
dumont 0:79f68acc26ec 28 /** initialisation */
dumont 0:79f68acc26ec 29 lpinStart=(LPINI)malloc(sizeof(INI));
dumont 0:79f68acc26ec 30 if (lpinStart==NULL) return NULL;
dumont 0:79f68acc26ec 31 INI_ZERO(lpinStart);
dumont 0:79f68acc26ec 32 if (ptrFileName==NULL) return lpinStart;
dumont 0:79f68acc26ec 33 ptrFile=fopen(ptrFileName, "r");
dumont 0:79f68acc26ec 34 if (ptrFile==NULL) return NULL;
dumont 0:79f68acc26ec 35 lpinCurrent=lpinStart;
dumont 0:79f68acc26ec 36 /** parsage */
dumont 0:79f68acc26ec 37 wait(1);
dumont 0:79f68acc26ec 38 while (fgets(ligne, MAX_PATH, ptrFile)!=NULL)
dumont 0:79f68acc26ec 39 {
dumont 0:79f68acc26ec 40 /* on nettoie la ligne */
dumont 0:79f68acc26ec 41 if ((ptrToken=strchr(ligne, 13))) *ptrToken='\0';
dumont 0:79f68acc26ec 42 /* on supprime les commentaires */
dumont 0:79f68acc26ec 43 if ((ptrToken=strchr(ligne, ';'))) *ptrToken='\0';
dumont 0:79f68acc26ec 44 wait(0.5);
dumont 0:79f68acc26ec 45 if (*ligne=='[')
dumont 0:79f68acc26ec 46 {
dumont 0:79f68acc26ec 47 ptrLine=ligne+1;
dumont 0:79f68acc26ec 48 ptrToken=strchr(ptrLine, ']');
dumont 0:79f68acc26ec 49 if (ptrToken!=NULL) *ptrToken='\0';
dumont 0:79f68acc26ec 50 _strupr(ptrLine);
dumont 0:79f68acc26ec 51 /** On alloue une nouvelle structure */
dumont 0:79f68acc26ec 52 if (!(lpinTmp=(LPINI)malloc(sizeof(INI)))) EXIT_FREE
dumont 0:79f68acc26ec 53 INI_ZERO(lpinTmp);
dumont 0:79f68acc26ec 54 lpinCurrent->lpinNext=lpinTmp;
dumont 0:79f68acc26ec 55 lpinCurrent=lpinTmp;
dumont 0:79f68acc26ec 56 if (!(lpinCurrent->ptrSectionName=(char*)malloc(strlen(ptrLine)+1))) EXIT_FREE
dumont 0:79f68acc26ec 57 _strupr(ptrLine);
dumont 0:79f68acc26ec 58 strcpy(lpinCurrent->ptrSectionName, ptrLine);
dumont 0:79f68acc26ec 59 isSectionNew=1;
dumont 0:79f68acc26ec 60 /* il s'agit d'une Nouvelle Section */
dumont 0:79f68acc26ec 61 }
dumont 0:79f68acc26ec 62 else if (*ligne!='\0')
dumont 0:79f68acc26ec 63 {
dumont 0:79f68acc26ec 64 /* il s'agit d'une cl&#65533;&#65533; */
dumont 0:79f68acc26ec 65 if (ptrToken=strchr(ligne, '=')) *ptrToken='\0';
dumont 0:79f68acc26ec 66 ptrLine=ptrToken+1;
dumont 0:79f68acc26ec 67 if (!(lpikTmp=(LPINIKEY)malloc(sizeof(INIKEY)))) EXIT_FREE;
dumont 0:79f68acc26ec 68 INIKEY_ZERO(lpikTmp);
dumont 0:79f68acc26ec 69 if (isSectionNew==1)
dumont 0:79f68acc26ec 70 {
dumont 0:79f68acc26ec 71 lpinCurrent->lpikKey=lpikTmp; /* si il s'agit de la premi&#65533;re cl&#65533;e */
dumont 0:79f68acc26ec 72 lpikKey=lpikTmp;
dumont 0:79f68acc26ec 73 }
dumont 0:79f68acc26ec 74 else
dumont 0:79f68acc26ec 75 {
dumont 0:79f68acc26ec 76 lpikKey->lpikNext=lpikTmp;
dumont 0:79f68acc26ec 77 lpikKey=lpikTmp;
dumont 0:79f68acc26ec 78 }
dumont 0:79f68acc26ec 79 _strupr(ligne);
dumont 0:79f68acc26ec 80 if (((lpikKey->ptrValue=(char*)malloc(strlen(ptrLine)+1)) && (lpikKey->ptrKeyName=(char*)malloc(strlen(ligne)+1)))==0) EXIT_FREE
dumont 0:79f68acc26ec 81 strcpy(lpikKey->ptrValue, ptrLine);
dumont 0:79f68acc26ec 82 strcpy(lpikKey->ptrKeyName, ligne);
dumont 0:79f68acc26ec 83 isSectionNew=0;
dumont 0:79f68acc26ec 84 }
dumont 0:79f68acc26ec 85 }
dumont 0:79f68acc26ec 86 fclose(ptrFile);
dumont 0:79f68acc26ec 87 return lpinStart;
dumont 0:79f68acc26ec 88 }
dumont 0:79f68acc26ec 89
dumont 0:79f68acc26ec 90 LPINI IniGetSection(char* ptrSectionName, LPINI lpinIni)
dumont 0:79f68acc26ec 91 {
dumont 0:79f68acc26ec 92 while (lpinIni!=NULL)
dumont 0:79f68acc26ec 93 {
dumont 0:79f68acc26ec 94 if (lpinIni->ptrSectionName)
dumont 0:79f68acc26ec 95 {
dumont 0:79f68acc26ec 96 if (!strcmp(lpinIni->ptrSectionName, ptrSectionName)) return lpinIni;
dumont 0:79f68acc26ec 97 }
dumont 0:79f68acc26ec 98 lpinIni=lpinIni->lpinNext;
dumont 0:79f68acc26ec 99 }
dumont 0:79f68acc26ec 100 return NULL;
dumont 0:79f68acc26ec 101 }
dumont 0:79f68acc26ec 102
dumont 0:79f68acc26ec 103 LPINIKEY IniGetKey(char* ptrVarName, LPINI lpinSection)
dumont 0:79f68acc26ec 104 {
dumont 0:79f68acc26ec 105 LPINIKEY lpikSection=lpinSection->lpikKey;
dumont 0:79f68acc26ec 106 while (lpikSection)
dumont 0:79f68acc26ec 107 {
dumont 0:79f68acc26ec 108 if (lpikSection->ptrKeyName)
dumont 0:79f68acc26ec 109 {
dumont 0:79f68acc26ec 110 if(!strcmp(lpikSection->ptrKeyName, ptrVarName)) return lpikSection;
dumont 0:79f68acc26ec 111 }
dumont 0:79f68acc26ec 112 lpikSection=lpikSection->lpikNext;
dumont 0:79f68acc26ec 113 }
dumont 0:79f68acc26ec 114 return NULL;
dumont 0:79f68acc26ec 115 }
dumont 0:79f68acc26ec 116
dumont 0:79f68acc26ec 117 int IniGetValue(char* ptrValue, int iLength, LPINIKEY lpikIniKey)
dumont 0:79f68acc26ec 118 {
dumont 0:79f68acc26ec 119 if (lpikIniKey==NULL) return EXIT_FAILURE;
dumont 0:79f68acc26ec 120 strncpy(ptrValue, lpikIniKey->ptrValue, iLength);
dumont 0:79f68acc26ec 121 return 0;
dumont 0:79f68acc26ec 122 }
dumont 0:79f68acc26ec 123
dumont 0:79f68acc26ec 124 int IniSetValue(char* ptrValue, LPINIKEY lpikIniKey)
dumont 0:79f68acc26ec 125 {
dumont 0:79f68acc26ec 126 char* ptrTmp=NULL;
dumont 0:79f68acc26ec 127 if (lpikIniKey==NULL) return EXIT_FAILURE;
dumont 0:79f68acc26ec 128 ptrTmp=(char*)realloc(lpikIniKey->ptrValue, strlen(ptrValue));
dumont 0:79f68acc26ec 129 if (ptrTmp==NULL) return EXIT_FAILURE;
dumont 0:79f68acc26ec 130 strcpy(ptrTmp, ptrValue);
dumont 0:79f68acc26ec 131 lpikIniKey->ptrValue=ptrTmp;
dumont 0:79f68acc26ec 132 return 0;
dumont 0:79f68acc26ec 133 }
dumont 0:79f68acc26ec 134
dumont 0:79f68acc26ec 135 LPINIKEY IniCreateKey(char* ptrKeyName, char* ptrValue, LPINI lpinSection)
dumont 0:79f68acc26ec 136 {
dumont 0:79f68acc26ec 137 INIKEY *lpikTmp=NULL;
dumont 0:79f68acc26ec 138 LPINIKEY lpikSection=lpinSection->lpikKey;
dumont 0:79f68acc26ec 139 lpikTmp=(LPINIKEY)malloc(sizeof(INIKEY));
dumont 0:79f68acc26ec 140 if (lpikTmp==NULL) return NULL;
dumont 0:79f68acc26ec 141 INIKEY_ZERO(lpikTmp);
dumont 0:79f68acc26ec 142 if (lpinSection->lpikKey==NULL)
dumont 0:79f68acc26ec 143 {
dumont 0:79f68acc26ec 144 lpinSection->lpikKey=lpikTmp;
dumont 0:79f68acc26ec 145 }
dumont 0:79f68acc26ec 146 else
dumont 0:79f68acc26ec 147 {
dumont 0:79f68acc26ec 148 lpikTmp->lpikNext=lpikSection->lpikNext;
dumont 0:79f68acc26ec 149 lpikSection->lpikNext=lpikTmp;
dumont 0:79f68acc26ec 150 }
dumont 0:79f68acc26ec 151 lpikSection=lpikTmp;
dumont 0:79f68acc26ec 152 lpikSection->ptrKeyName=(char*)malloc(strlen(ptrKeyName)+1);
dumont 0:79f68acc26ec 153 lpikSection->ptrValue=(char*)malloc(strlen(ptrValue)+1);
dumont 0:79f68acc26ec 154 if (!(lpikSection->ptrValue && lpikSection->ptrKeyName)) return NULL;
dumont 0:79f68acc26ec 155 strcpy(lpikSection->ptrValue, ptrValue);
dumont 0:79f68acc26ec 156 strcpy(lpikSection->ptrKeyName, ptrKeyName);
dumont 0:79f68acc26ec 157 return lpikSection;
dumont 0:79f68acc26ec 158 }
dumont 0:79f68acc26ec 159
dumont 0:79f68acc26ec 160 LPINI IniCreateSection(char* ptrSectionName, LPINI lpinIni)
dumont 0:79f68acc26ec 161 {
dumont 0:79f68acc26ec 162 LPINI lpinTmp=NULL;
dumont 0:79f68acc26ec 163 if (lpinIni->ptrSectionName!=NULL)
dumont 0:79f68acc26ec 164 {
dumont 0:79f68acc26ec 165 lpinTmp=(LPINI)malloc(sizeof(INI));
dumont 0:79f68acc26ec 166 if (lpinTmp==NULL) return NULL;
dumont 0:79f68acc26ec 167 INI_ZERO(lpinTmp);
dumont 0:79f68acc26ec 168 lpinTmp->lpinNext=lpinIni->lpinNext;
dumont 0:79f68acc26ec 169 lpinIni->lpinNext=lpinTmp;
dumont 0:79f68acc26ec 170 lpinIni=lpinTmp;
dumont 0:79f68acc26ec 171 }
dumont 0:79f68acc26ec 172 lpinIni->ptrSectionName=(char*)malloc(strlen(ptrSectionName));
dumont 0:79f68acc26ec 173 if (lpinIni->ptrSectionName==NULL)
dumont 0:79f68acc26ec 174 {
dumont 0:79f68acc26ec 175 return 0;
dumont 0:79f68acc26ec 176 }
dumont 0:79f68acc26ec 177 strcpy(lpinIni->ptrSectionName, ptrSectionName);
dumont 0:79f68acc26ec 178 return lpinIni;
dumont 0:79f68acc26ec 179 }
dumont 0:79f68acc26ec 180
dumont 0:79f68acc26ec 181 int IniSave(char* ptrFileName, LPINI lpinIni)
dumont 0:79f68acc26ec 182 {
dumont 0:79f68acc26ec 183 FILE* ptrFile=NULL;
dumont 0:79f68acc26ec 184 LPINIKEY lpikKey=NULL;
dumont 0:79f68acc26ec 185 ptrFile=fopen(ptrFileName, "w");
dumont 0:79f68acc26ec 186 if (ptrFile==NULL) return EXIT_FAILURE;
dumont 0:79f68acc26ec 187 while (lpinIni)
dumont 0:79f68acc26ec 188 {
dumont 0:79f68acc26ec 189 if (lpinIni->ptrSectionName)
dumont 0:79f68acc26ec 190 {
dumont 0:79f68acc26ec 191 fprintf(ptrFile, "[%s]%c%c", lpinIni->ptrSectionName, 13 ,10);
dumont 0:79f68acc26ec 192 lpikKey=lpinIni->lpikKey;
dumont 0:79f68acc26ec 193 while (lpikKey)
dumont 0:79f68acc26ec 194 {
dumont 0:79f68acc26ec 195 if (lpikKey->ptrKeyName) fprintf(ptrFile, "%s=%s%c%c", lpikKey->ptrKeyName, lpikKey->ptrValue, 13,10);
dumont 0:79f68acc26ec 196 lpikKey=lpikKey->lpikNext;
dumont 0:79f68acc26ec 197 }
dumont 0:79f68acc26ec 198 }
dumont 0:79f68acc26ec 199 lpinIni=lpinIni->lpinNext;
dumont 0:79f68acc26ec 200 }
dumont 0:79f68acc26ec 201 fclose(ptrFile);
dumont 0:79f68acc26ec 202 return EXIT_SUCCESS;
dumont 0:79f68acc26ec 203 }
dumont 0:79f68acc26ec 204
dumont 0:79f68acc26ec 205 void IniDeleteSection(LPINI lpinSection)
dumont 0:79f68acc26ec 206 {
dumont 0:79f68acc26ec 207 free(lpinSection->ptrSectionName);
dumont 0:79f68acc26ec 208 lpinSection->ptrSectionName=NULL;
dumont 0:79f68acc26ec 209 }
dumont 0:79f68acc26ec 210
dumont 0:79f68acc26ec 211 void IniDeleteKey(LPINIKEY lpikKey)
dumont 0:79f68acc26ec 212 {
dumont 0:79f68acc26ec 213 free(lpikKey->ptrKeyName);
dumont 0:79f68acc26ec 214 lpikKey->ptrKeyName=NULL;
dumont 0:79f68acc26ec 215 }
dumont 0:79f68acc26ec 216
dumont 0:79f68acc26ec 217 void IniFree(LPINI lpinIni)
dumont 0:79f68acc26ec 218 {
dumont 0:79f68acc26ec 219 LPINIKEY lpikKey;
dumont 0:79f68acc26ec 220 LPINIKEY lpikTmp;
dumont 0:79f68acc26ec 221 LPINI lpinTmp;
dumont 0:79f68acc26ec 222 while (lpinIni!=NULL)
dumont 0:79f68acc26ec 223 {
dumont 0:79f68acc26ec 224 lpikKey=lpinIni->lpikKey;
dumont 0:79f68acc26ec 225 while (lpikKey!=NULL)
dumont 0:79f68acc26ec 226 {
dumont 0:79f68acc26ec 227 free(lpikKey->ptrKeyName);
dumont 0:79f68acc26ec 228 free(lpikKey->ptrValue);
dumont 0:79f68acc26ec 229 lpikTmp=lpikKey->lpikNext;
dumont 0:79f68acc26ec 230 free(lpikKey);
dumont 0:79f68acc26ec 231 lpikKey=lpikTmp;
dumont 0:79f68acc26ec 232 }
dumont 0:79f68acc26ec 233 free(lpinIni->ptrSectionName);
dumont 0:79f68acc26ec 234 lpinTmp=lpinIni->lpinNext;
dumont 0:79f68acc26ec 235 free(lpinIni);
dumont 0:79f68acc26ec 236 lpinIni=lpinTmp;
dumont 0:79f68acc26ec 237 }
dumont 0:79f68acc26ec 238 }