/*========================================================================*/ bool OpenIniFile(cchr * FileName) { char Str[255]; char *pStr; struct ENTRY *pEntry; FreeAllMem(); if (FileName == NULL) { return FALSE; } if ((IniFile = fopen(FileName, "r")) == NULL) { return FALSE; } while (fgets(Str, 255, IniFile) != NULL) { pStr = strchr(Str, '\n'); if (pStr != NULL) { *pStr = 0; } pEntry = MakeNewEntry(); if (pEntry == NULL) { return FALSE; } #ifdef INI_REMOVE_CR int Len = strlen(Str); if (Len > 0) { if (Str[Len - 1] == '\r') { Str[Len - 1] = '\0'; } } #endif pEntry->Text = (char *) malloc(strlen(Str) + 1); if (pEntry->Text == NULL) { FreeAllMem(); return FALSE; } strcpy(pEntry->Text, Str); pStr = strchr(Str, ';'); if (pStr != NULL) { *pStr = 0; } /* Cut all comments */ if ((strstr(Str, "[") > 0) && (strstr(Str, "]") > 0)) { /* Is Section */ pEntry->Type = tpSECTION; } else { if (strstr(Str, "=") > 0) { pEntry->Type = tpKEYVALUE; } else { pEntry->Type = tpCOMMENT; } } CurEntry = pEntry; } fclose(IniFile); IniFile = NULL; return TRUE; }
/*========================================================================*/ bool WriteIniFile(const char *FileName) { struct ENTRY *pEntry = Entry; //IniFile = NULL; if (IniFile != NULL) { fclose(IniFile); } if ((IniFile = fopen(FileName, "wb")) == NULL) { FreeAllMem(); return FALSE; } while (pEntry != NULL) { if (pEntry->Type != tpNULL) { #ifdef INI_REMOVE_CR fprintf(IniFile, "%s\n", pEntry->Text); #else fprintf(IniFile, "%s\r\n", pEntry->Text); #endif pEntry = pEntry->pNext; } } fflush(IniFile); fclose(IniFile); IniFile = NULL; return TRUE; }
/*========================================================================= WriteIniFile ------------------------------------------------------------------------- Job : Writes the iniFile to the disk and close it. Frees all memory allocated by WriteIniFile; *========================================================================*/ bool WriteIniFile (INIFILE* thiz, const char *FileName) { struct ENTRY *pEntry = thiz->entry; thiz->inifile = NULL; if (thiz->inifile != NULL) { fclose (thiz->inifile); } if ((thiz->inifile = fopen (FileName, "wb")) == NULL) { FreeAllMem (thiz); return FALSE; } while (pEntry != NULL) { if (pEntry->Type != tpNULL) { #ifdef INI_REMOVE_CR fprintf (thiz->inifile, "%s\n", pEntry->Text); #else fprintf (thiz->inifile, "%s\r\n", pEntry->Text); #endif pEntry = pEntry->pNext; } } fclose (thiz->inifile); thiz->inifile = NULL; return TRUE; }
/*========================================================================*/ void CloseIniFile(void) { FreeAllMem(); if (IniFile != NULL) { fclose(IniFile); IniFile = NULL; } }
/*========================================================================= * * CloseIniFile * ------------------------------------------------------------------------- * Job : Frees the memory and closes the ini file without any * modifications. If you want to write the file use * WriteIniFile instead. * ========================================================================*/ void CloseIniFile (INIFILE* thiz) { FreeAllMem (thiz); if (thiz->inifile != NULL) { fclose (thiz->inifile); thiz->inifile = NULL; } }
/*========================================================================= CloseIniFile ------------------------------------------------------------------------- Job : Frees the memory and closes the ini file without any modifications. If you want to write the file use WriteIniFile instead. *========================================================================*/ void CloseIniFile (void) { FreeAllMem (); if (IniFile != NULL) { fclose (IniFile); //memset(CurFile, '\0', 5 + strlen(CurFile)); // 100717 law CurFile[0] = 0; // 100717 law IniFile = NULL; } }
/*========================================================================= MakeNewEntry : Allocates the memory for a new entry. This is only the new empty structure, that must be filled from function like AddItem etc. Info : This is only a internal function. You dont have to call it from outside. *==========================================================================*/ struct ENTRY *MakeNewEntry(void) { struct ENTRY *pEntry; pEntry = (struct ENTRY *) malloc(sizeof(ENTRY)); if (pEntry == NULL) { FreeAllMem(); return NULL; } if (Entry == NULL) { Entry = pEntry; } pEntry->Type = tpNULL; pEntry->pPrev = CurEntry; pEntry->pNext = NULL; pEntry->Text = NULL; if (CurEntry != NULL) { CurEntry->pNext = pEntry; } return pEntry; }
struct ENTRY *CIniFile::MakeNewEntry (void) { struct ENTRY *pEntry; pEntry = (struct ENTRY *)malloc (sizeof (ENTRY)); if (pEntry == NULL) { FreeAllMem (); return NULL; } if (m_pEntry == NULL) { m_pEntry = pEntry; } pEntry->Type = tpNULL; pEntry->pPrev = m_pCurEntry; pEntry->pNext = NULL; pEntry->pText = NULL; if (m_pCurEntry != NULL) { m_pCurEntry->pNext = pEntry; } return pEntry; }
/*========================================================================= MakeNewEntry : Allocates the memory for a new entry. This is only the new empty structure, that must be filled from function like AddItem etc. Info : This is only a internal function. You dont have to call it from outside. *==========================================================================*/ struct ENTRY * MakeNewEntry (INIFILE* thiz) { struct ENTRY *pEntry; pEntry = (struct ENTRY *) malloc (sizeof (ENTRY)); if (pEntry == NULL) { FreeAllMem (thiz); return NULL; } if (thiz->entry == NULL) { thiz->entry = pEntry; } pEntry->Type = tpNULL; pEntry->pPrev = thiz->cur_entry; pEntry->pNext = NULL; pEntry->Text = NULL; if (thiz->cur_entry != NULL) { thiz->cur_entry->pNext = pEntry; } return pEntry; }
bool OpenIniFile (cchr * FileName) { char Str[255]; char *pStr; struct ENTRY *pEntry; //printf("Open File:%s\n",FileName); FreeAllMem (); if (FileName == NULL) { printf("FileName is NULL\n"); return FALSE; } if ((IniFile = fopen (FileName, "r")) == NULL) { printf("Error open File:%s\n",FileName); return FALSE; } while (fgets (Str, 255, IniFile) != NULL) { pStr = strchr (Str, '\n'); if (pStr != NULL) { *pStr = 0; } pEntry = MakeNewEntry (); if (pEntry == NULL) { printf("Get Entry Fail!\n"); return FALSE; } #ifdef INI_REMOVE_CR { int const Len = strlen(Str); if ( Len > 0 ) { if ( Str[Len-1] == '\r' ) { Str[Len-1] = '\0'; } } } #endif delblank(Str); pEntry->Text = (char *) malloc (strlen (Str) + 1); if (pEntry->Text == NULL) { printf("Get Text Fail!\n"); FreeAllMem (); return FALSE; } //printf("%s\n",Str); strcpy (pEntry->Text, Str); pStr = strchr (Str, ';'); if (pStr != NULL) { *pStr = 0; } /* Cut all comments */ if ((strstr (Str, "[") > 0) && (strstr (Str, "]") > 0)) /* Is Section */ { pEntry->Type = tpSECTION; } else { if (strstr (Str, "=") > 0) { pEntry->Type = tpKEYVALUE; } else { pEntry->Type = tpCOMMENT; } } CurEntry = pEntry; } fclose (IniFile); IniFile = NULL; // 100717 law strcpy(CurFile, FileName); //printf("ini filename %s\r\n", CurFile); //printf("End Open File!%d\n",true); return true; }
/*========================================================================= CIniFile : The destructor *========================================================================*/ CIniFile::~CIniFile (void) { FreeAllMem (); }
void child_clean() { FreeAllMem(); exit(0); }
INIFILE* OpenIniFile (cchr * FileName) { char str[255]; char *pstr; struct ENTRY *pEntry; int Len; INIFILE* thiz = create_inifile(); //printf("Open File:%s\n",FileName); FreeAllMem (thiz); if (FileName == NULL) { printf("FileName is NULL\n"); return NULL; } if ((thiz->inifile = fopen (FileName, "r")) == NULL) { printf("Error open File:%s\n",FileName); return NULL; } while (fgets (str, 255, thiz->inifile) != NULL) { pstr = strchr (str, '\n'); if (pstr != NULL) { *pstr = 0; } pEntry = MakeNewEntry (thiz); if (pEntry == NULL) { printf("Get Entry Fail!\n"); return NULL; } #ifdef INI_REMOVE_CR Len = strlen(str); if ( Len > 0 ) { if ( str[Len-1] == '\r' ) { str[Len-1] = '\0'; } } #endif delblank(str); pEntry->Text = (char *) malloc (strlen (str) + 1); if (pEntry->Text == NULL) { printf("Get Text Fail!\n"); FreeAllMem (thiz); return NULL; } //printf("%s\n",str); strcpy (pEntry->Text, str); pstr = strchr (str, ';'); if (pstr != NULL) { *pstr = 0; } /* Cut all comments */ if ((strstr (str, "[") > 0) && (strstr (str, "]") > 0)) /* Is Section */ { pEntry->Type = tpSECTION; } else { if (strstr (str, "=") > 0) { pEntry->Type = tpKEYVALUE; } else { pEntry->Type = tpCOMMENT; } } thiz->cur_entry = pEntry; } fclose (thiz->inifile); thiz->inifile = NULL; //printf("End Open File!%d\n",true); return thiz; }