Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 0:79f68acc26ec, committed 2012-06-10
- Comitter:
- dumont
- Date:
- Sun Jun 10 17:19:21 2012 +0000
- Commit message:
Changed in this revision
| FileIni.c | Show annotated file Show diff for this revision Revisions of this file |
| FileIni.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/FileIni.c Sun Jun 10 17:19:21 2012 +0000
@@ -0,0 +1,238 @@
+#include "mbed.h"
+#include "FileIni.h"
+
+#ifndef _POSIX_VERSION
+
+int _strupr(char* ptrSource)
+{
+ int i=0;
+ while(ptrSource[i]!='\0')
+ {
+ if (ptrSource[i]>='a' && ptrSource[i]<='z') ptrSource[i]=ptrSource[i]-48;
+ i++;
+ }
+ return 0;
+}
+#endif
+
+
+LPINI IniLoad(char* ptrFileName)
+{
+ /** declaration des variables */
+ FILE* ptrFile=NULL;
+ INI *lpinStart=NULL, *lpinCurrent=NULL, *lpinTmp=NULL;
+ INIKEY *lpikKey=NULL, *lpikTmp=NULL;
+ char ligne[MAX_PATH];
+ char isSectionNew=2;
+ char *ptrToken=NULL, *ptrLine=NULL;
+ /** initialisation */
+ lpinStart=(LPINI)malloc(sizeof(INI));
+ if (lpinStart==NULL) return NULL;
+ INI_ZERO(lpinStart);
+ if (ptrFileName==NULL) return lpinStart;
+ ptrFile=fopen(ptrFileName, "r");
+ if (ptrFile==NULL) return NULL;
+ lpinCurrent=lpinStart;
+ /** parsage */
+ wait(1);
+ while (fgets(ligne, MAX_PATH, ptrFile)!=NULL)
+ {
+ /* on nettoie la ligne */
+ if ((ptrToken=strchr(ligne, 13))) *ptrToken='\0';
+ /* on supprime les commentaires */
+ if ((ptrToken=strchr(ligne, ';'))) *ptrToken='\0';
+ wait(0.5);
+ if (*ligne=='[')
+ {
+ ptrLine=ligne+1;
+ ptrToken=strchr(ptrLine, ']');
+ if (ptrToken!=NULL) *ptrToken='\0';
+ _strupr(ptrLine);
+ /** On alloue une nouvelle structure */
+ if (!(lpinTmp=(LPINI)malloc(sizeof(INI)))) EXIT_FREE
+ INI_ZERO(lpinTmp);
+ lpinCurrent->lpinNext=lpinTmp;
+ lpinCurrent=lpinTmp;
+ if (!(lpinCurrent->ptrSectionName=(char*)malloc(strlen(ptrLine)+1))) EXIT_FREE
+ _strupr(ptrLine);
+ strcpy(lpinCurrent->ptrSectionName, ptrLine);
+ isSectionNew=1;
+ /* il s'agit d'une Nouvelle Section */
+ }
+ else if (*ligne!='\0')
+ {
+ /* il s'agit d'une cl�� */
+ if (ptrToken=strchr(ligne, '=')) *ptrToken='\0';
+ ptrLine=ptrToken+1;
+ if (!(lpikTmp=(LPINIKEY)malloc(sizeof(INIKEY)))) EXIT_FREE;
+ INIKEY_ZERO(lpikTmp);
+ if (isSectionNew==1)
+ {
+ lpinCurrent->lpikKey=lpikTmp; /* si il s'agit de la premi�re cl�e */
+ lpikKey=lpikTmp;
+ }
+ else
+ {
+ lpikKey->lpikNext=lpikTmp;
+ lpikKey=lpikTmp;
+ }
+ _strupr(ligne);
+ if (((lpikKey->ptrValue=(char*)malloc(strlen(ptrLine)+1)) && (lpikKey->ptrKeyName=(char*)malloc(strlen(ligne)+1)))==0) EXIT_FREE
+ strcpy(lpikKey->ptrValue, ptrLine);
+ strcpy(lpikKey->ptrKeyName, ligne);
+ isSectionNew=0;
+ }
+ }
+ fclose(ptrFile);
+ return lpinStart;
+}
+
+LPINI IniGetSection(char* ptrSectionName, LPINI lpinIni)
+{
+ while (lpinIni!=NULL)
+ {
+ if (lpinIni->ptrSectionName)
+ {
+ if (!strcmp(lpinIni->ptrSectionName, ptrSectionName)) return lpinIni;
+ }
+ lpinIni=lpinIni->lpinNext;
+ }
+ return NULL;
+}
+
+LPINIKEY IniGetKey(char* ptrVarName, LPINI lpinSection)
+{
+ LPINIKEY lpikSection=lpinSection->lpikKey;
+ while (lpikSection)
+ {
+ if (lpikSection->ptrKeyName)
+ {
+ if(!strcmp(lpikSection->ptrKeyName, ptrVarName)) return lpikSection;
+ }
+ lpikSection=lpikSection->lpikNext;
+ }
+ return NULL;
+}
+
+int IniGetValue(char* ptrValue, int iLength, LPINIKEY lpikIniKey)
+{
+ if (lpikIniKey==NULL) return EXIT_FAILURE;
+ strncpy(ptrValue, lpikIniKey->ptrValue, iLength);
+ return 0;
+}
+
+int IniSetValue(char* ptrValue, LPINIKEY lpikIniKey)
+{
+ char* ptrTmp=NULL;
+ if (lpikIniKey==NULL) return EXIT_FAILURE;
+ ptrTmp=(char*)realloc(lpikIniKey->ptrValue, strlen(ptrValue));
+ if (ptrTmp==NULL) return EXIT_FAILURE;
+ strcpy(ptrTmp, ptrValue);
+ lpikIniKey->ptrValue=ptrTmp;
+ return 0;
+}
+
+LPINIKEY IniCreateKey(char* ptrKeyName, char* ptrValue, LPINI lpinSection)
+{
+ INIKEY *lpikTmp=NULL;
+ LPINIKEY lpikSection=lpinSection->lpikKey;
+ lpikTmp=(LPINIKEY)malloc(sizeof(INIKEY));
+ if (lpikTmp==NULL) return NULL;
+ INIKEY_ZERO(lpikTmp);
+ if (lpinSection->lpikKey==NULL)
+ {
+ lpinSection->lpikKey=lpikTmp;
+ }
+ else
+ {
+ lpikTmp->lpikNext=lpikSection->lpikNext;
+ lpikSection->lpikNext=lpikTmp;
+ }
+ lpikSection=lpikTmp;
+ lpikSection->ptrKeyName=(char*)malloc(strlen(ptrKeyName)+1);
+ lpikSection->ptrValue=(char*)malloc(strlen(ptrValue)+1);
+ if (!(lpikSection->ptrValue && lpikSection->ptrKeyName)) return NULL;
+ strcpy(lpikSection->ptrValue, ptrValue);
+ strcpy(lpikSection->ptrKeyName, ptrKeyName);
+ return lpikSection;
+}
+
+LPINI IniCreateSection(char* ptrSectionName, LPINI lpinIni)
+{
+ LPINI lpinTmp=NULL;
+ if (lpinIni->ptrSectionName!=NULL)
+ {
+ lpinTmp=(LPINI)malloc(sizeof(INI));
+ if (lpinTmp==NULL) return NULL;
+ INI_ZERO(lpinTmp);
+ lpinTmp->lpinNext=lpinIni->lpinNext;
+ lpinIni->lpinNext=lpinTmp;
+ lpinIni=lpinTmp;
+ }
+ lpinIni->ptrSectionName=(char*)malloc(strlen(ptrSectionName));
+ if (lpinIni->ptrSectionName==NULL)
+ {
+ return 0;
+ }
+ strcpy(lpinIni->ptrSectionName, ptrSectionName);
+ return lpinIni;
+}
+
+int IniSave(char* ptrFileName, LPINI lpinIni)
+{
+ FILE* ptrFile=NULL;
+ LPINIKEY lpikKey=NULL;
+ ptrFile=fopen(ptrFileName, "w");
+ if (ptrFile==NULL) return EXIT_FAILURE;
+ while (lpinIni)
+ {
+ if (lpinIni->ptrSectionName)
+ {
+ fprintf(ptrFile, "[%s]%c%c", lpinIni->ptrSectionName, 13 ,10);
+ lpikKey=lpinIni->lpikKey;
+ while (lpikKey)
+ {
+ if (lpikKey->ptrKeyName) fprintf(ptrFile, "%s=%s%c%c", lpikKey->ptrKeyName, lpikKey->ptrValue, 13,10);
+ lpikKey=lpikKey->lpikNext;
+ }
+ }
+ lpinIni=lpinIni->lpinNext;
+ }
+ fclose(ptrFile);
+ return EXIT_SUCCESS;
+}
+
+void IniDeleteSection(LPINI lpinSection)
+{
+ free(lpinSection->ptrSectionName);
+ lpinSection->ptrSectionName=NULL;
+}
+
+void IniDeleteKey(LPINIKEY lpikKey)
+{
+ free(lpikKey->ptrKeyName);
+ lpikKey->ptrKeyName=NULL;
+}
+
+void IniFree(LPINI lpinIni)
+{
+ LPINIKEY lpikKey;
+ LPINIKEY lpikTmp;
+ LPINI lpinTmp;
+ while (lpinIni!=NULL)
+ {
+ lpikKey=lpinIni->lpikKey;
+ while (lpikKey!=NULL)
+ {
+ free(lpikKey->ptrKeyName);
+ free(lpikKey->ptrValue);
+ lpikTmp=lpikKey->lpikNext;
+ free(lpikKey);
+ lpikKey=lpikTmp;
+ }
+ free(lpinIni->ptrSectionName);
+ lpinTmp=lpinIni->lpinNext;
+ free(lpinIni);
+ lpinIni=lpinTmp;
+ }
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/FileIni.h Sun Jun 10 17:19:21 2012 +0000
@@ -0,0 +1,137 @@
+#ifndef FILEINI_INCLUDED
+#define FILEINI_INCLUDED
+
+#define MAX_PATH 256
+
+/**
+ * @file
+ */
+typedef struct INIKEY INIKEY;
+typedef struct INIKEY* LPINIKEY;
+
+
+/** A structure that stores a configuration key node. */
+struct INIKEY {
+ char* ptrKeyName;
+ char* ptrValue;
+ LPINIKEY lpikNext;
+};
+
+
+typedef struct INI INI;
+typedef struct INI* LPINI;
+
+/** A structure that represent a section node */
+struct INI {
+ char* ptrSectionName;
+ LPINIKEY lpikKey;
+ LPINI lpinNext;
+};
+
+/** Sets string characters to upper case.
+ * @param ptrSource A pointer to the string to transform to upper case.
+ */
+int _strupr(char* ptrSource);
+
+/** @memberof INI
+ * Loads and parses an Ini file into memory.
+ * @param ptrFile A pointer to a NULL-terminated string that contains the name of the file to load.
+ * If this parameters is NULL, the function create a void Ini file in memory.
+ * @return Returns a pointer to an INI structure that represents the loaded file. If a error occurs, the return value is NULL.
+ */
+LPINI IniLoad(char* ptrFileName);
+
+/** @memberof INI
+ * Gets a pointer to a section.
+ * @param ptrSectionFile A pointer to a NULL-terminated string that stores the name of the needed section.
+ * @param lpinIni A pointer to an INI structure created with IniLoad().
+ * @return Returns a pointer to an INI structure that represents the needed section. If a error occurs, the return value is NULL.
+ */
+LPINI IniGetSection(char* ptrSectionName, LPINI lpinIni);
+
+LPINIKEY IniGetKey(char* ptrVarName, LPINI lpinSection);
+/** \fn LPINIKEY IniGetKey(char* ptrVarName, LPINI lpinSection);
+ @memberof INI
+ \brief Gets a pointer to a key.
+ \param[in] ptrVarName A pointer to a NULL-terminated string that stores the name of the needed key.
+ \param[in] lpinSection A pointer to an INI structure that represents a section (opened with IniGetSection() or IniCreateSection()).
+ \return Returns a pointer to an INIKEY structure that represents the needed key. If a error occurs, the return value is NULL.
+*/
+
+int IniGetValue(char* ptrValue, int iLength, LPINIKEY lpikIniKey);
+/** \fn int IniGetValue(char* ptrValue, int iLength, LPINIKEY lpikIniKey);
+ @memberof INIKEY
+ \brief Gets a key value.
+ \param[out] ptrValue A pointer to a buffer that will receive the key content.
+ \param[in] iLenght The length of the buffer pointed by ptrValue.
+ \param[in] lpikIniKey A pointer to an INIKEY structure that represents a key (opened with IniGetKey() or IniCreateKey()).
+ \return Returns 0 if no error occurs, 1 else.
+*/
+
+int IniSetValue(char* ptrValue, LPINIKEY lpikIniKey);
+/** \fn int IniSetValue(char* ptrValue, LPINIKEY lpikIniKey);
+ @memberof INIKEY
+ \brief Sets a key value.
+ \param[in] ptrValue A pointer to a NULL-terminated string that stores the value to be set.
+ \param[in] lpikIniKey A pointer to an INIKEY structure that represents a key (opened with IniGetKey() or IniCreateKey()).
+ \return Returns 0 if no error occurs, 1 else.
+*/
+LPINIKEY IniCreateKey(char* ptrKeyName, char* ptrValue, LPINI lpinSection);
+/** @extends INIKEY
+ \fn LPINIKEY IniCreateKey(char* ptrKeyName, char* ptrValue, LPINI lpinSection);
+ \brief Creates a key.
+ \param[in] ptrKeyName A pointer to a NULL-terminated string that stores the name of the key to be created.
+ \param[in] ptrValue The length of the NULL-terminated string that stores the value of the key to be created.
+ \param[in] lpinSection A pointer to an INI structure that represents a section (opened with IniGetSection() or IniCreateSection()).
+ \return Returns a pointer to an INIKEY structure that represents the created key. If a error occurs, the return value is NULL.
+*/
+LPINI IniCreateSection(char* ptrSectionName, LPINI lpinIni);
+/** @extends INI
+ \fn LPINI IniCreateSection(char* ptrSectionName, LPINI lpinIni);
+ \brief Creates a section.
+ \param[in] ptrSectionName A pointer to a NULL-terminated string that stores the name of the section to be created.
+ \param[in] lpinIni A pointer to an INI structure that represents a file (opened with IniLoad()).
+ \return Returns a pointer to an INI structure that represents the created section. If a error occurs, the return value is NULL.
+*/
+int IniSave(char* ptrFileName, LPINI lpinIni);
+/** @extends INI
+ \fn int IniSave(char* ptrFileName, LPINI lpinIni);
+ \brief Saves an Ini file .
+ \param[in] ptrFileName A pointer to a NULL-terminated string that stores the name of the file to be saved.
+ \param[in] lpinIni A pointer to an INI structure that represents a file (opened with IniLoad()).
+ \returnsReturns 0 if no error occurs, 1 else.
+*/
+void IniDeleteSection(LPINI lpinSection);
+/** @extends INI
+ \fn void IniDeleteSection(LPINI lpinSection);
+ \brief Deletes a section.
+ \param[in] lpinSection A pointer to an INI structure that represents the section to be deleted (opened with IniGetSection() or IniCreateSection()).
+ \returns No return value avaliable.
+*/
+void IniDeleteKey(LPINIKEY lpikKey);
+/** @extends INIKEY
+ \fn void IniDeleteKey(LPINIKEY lpikKey);
+ * \brief Deletes a key.
+ * \param[in] lpinSection A pointer to an INI structure that represents the key to be deleted (opened with IniGetKey() or IniCreateKey()).
+ * \return No return value avaliable.
+ */
+void IniFree(LPINI lpinIni);
+/** @extends INI
+ \fn void IniFree(LPINI lpinIni);
+ * \brief Frees an Ini file.
+ * \param[in] lpinIni A pointer to an INI structure that represents the file (opened with IniLoad()) to be freed.
+ * \return No return value avaliable.
+ */
+
+#define INI_ZERO(a) a->ptrSectionName=NULL; \
+ a->lpikKey=NULL; \
+ a->lpinNext=NULL
+
+#define INIKEY_ZERO(a) a->ptrKeyName=NULL; \
+ a->ptrValue=NULL; \
+ a->lpikNext=NULL
+
+#define EXIT_FREE {fclose(ptrFile);return 0;}
+
+#endif
+