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; }
/*========================================================================= FindpKey : Searches the chained list for a pKey under a given section Return Value: NULL at an error or a pointer to the ENTRY structure if succeed. *========================================================================*/ bool FindpKey (cchr * Section, cchr * pKey, EFIND * List) { char Search[130]; char Found[130]; char Text[255]; char *pText; struct ENTRY *pEntry; List->pSec = NULL; List->pKey = NULL; pEntry = FindSection (Section); if (pEntry == NULL) { return FALSE; } List->pSec = pEntry; List->KeyText[0] = 0; List->ValText[0] = 0; List->Comment[0] = 0; pEntry = pEntry->pNext; if (pEntry == NULL) { return FALSE; } sprintf (Search, "%s", pKey); //strupr (Search); while (pEntry != NULL) { if ((pEntry->Type == tpSECTION) || /* Stop after next section or EOF */ (pEntry->Type == tpNULL)) { return FALSE; } if (pEntry->Type == tpKEYVALUE) { strcpy (Text, pEntry->Text); pText = strchr (Text, ';'); if (pText != NULL) { strcpy (List->Comment, Text); *pText = 0; } pText = strchr (Text, '='); if (pText != NULL) { *pText = 0; strcpy (List->KeyText, Text); strcpy (Found, Text); *pText = '='; //strupr (Found); delblank(Found); /* printf ("%s,%s\n", Search, Found); */ if (strcmp (Found, Search) == 0) { strcpy (List->ValText, pText + 1); delblank(List->ValText); List->pKey = pEntry; return TRUE; } } } pEntry = pEntry->pNext; } return FALSE; }
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; }