BOOL INI_FindSection (const char *sectionname, BOOL create) { if (ini == NULL) return FALSE; printf("INI_FindSection trying to find name for %s\n", sectionname); char line[256], section[64]; char *p; int i, sectionfound, ret; rewind (ini); last_line = 0; sectionfound = 0; while(!feof(ini)) { ret = 0; *line=0; fgets(line,255,ini); // remove enter i=strlen(line); // ZIGGY there was a bug here if EOL was unix like on a short line (i.e. a line // with just EOL), it would write into line[-1] if(i>=1 && line[i-1]==0xa) { ret=1; line[i-1]=0; if (i>=2 && line[i-2]==0xd) line[i-2]=0; } // remove comments p=line; while(*p) { if (p[0]=='/' && p[1]=='/') { p[0]=0; break; } p++; } // skip starting space p=line; while(*p<=' ' && *p) p++; // empty line if(!*p) continue; last_line=ftell(ini); // where to add if not found last_line_ret = ret; if(*p!='[') continue; p++; for (i=0;i<63;i++) { if(*p==']' || !*p) break; section[i]=*p++; } section[i]=0; #ifdef _WIN32 if(!stricmp(section,sectionname)) #else // _WIN32 if (!strcasecmp(section,sectionname)) #endif // _WIN32 { sectionstart=ftell(ini); sectionfound=1; return TRUE; } } if (!sectionfound && create) { // create the section fseek(ini,last_line,SEEK_SET); INI_InsertSpace ((!last_line_ret) * 2 + 6 + strlen(sectionname)); if (!last_line_ret) fwrite (&cr, 1, 2, ini); fwrite (&cr, 1, 2, ini); sprintf (section, "[%s]", sectionname); fwrite (section, 1, strlen(section), ini); fwrite (&cr, 1, 2, ini); sectionstart = ftell(ini); last_line = sectionstart; last_line_ret = 1; return TRUE; } return FALSE; }
// Reads the value of item 'itemname' as a string. void INI_WriteString (const char *itemname, const char *value) { char line[256], name[64]; char *p, *n; int ret, i; fseek(ini,sectionstart,SEEK_SET); while(!feof(ini)) { ret = 0; *line=0; fgets(line,255,ini); // remove enter i=strlen(line); // ZIGGY there was a bug here if EOL was unix like on a short line (i.e. a line // with just EOL), it would write into line[-1] // OLD CODE : if(line[i-1]=='\n') ret=1, line[i-2]=0; if(i>=1 && line[i-1]==0xa) { ret=1; line[i-1]=0; if (i>=2 && line[i-2]==0xd) line[i-2]=0; } // remove comments p=line; while(*p) { if (p[0]=='/' && p[1]=='/') { p[0]=0; break; } p++; } // skip starting space p=line; while(*p<=' ' && *p) p++; // empty line if(!*p) continue; // new section if(*p=='[') break; last_line=ftell(ini); // where to add if not found last_line_ret = ret; // read name n = name; while(*p && *p!='=' && *p>' ') *n++ = *p++; *n = 0; #ifdef _WIN32 if(!stricmp(name,itemname)) #else // _WIN32 if(!strcasecmp(name,itemname)) #endif // _WIN32 { INI_InsertSpace (-i + (strlen(itemname) + strlen(value) + 5)); sprintf (line, "%s = %s", itemname, value); fseek (ini, -i, SEEK_CUR); fwrite (line, 1, strlen(line), ini); fwrite (&cr, 1, 2, ini); last_line = ftell(ini); last_line_ret = 1; return; } } // uh-oh, not found. we need to create fseek(ini,last_line,SEEK_SET); INI_InsertSpace ((!last_line_ret) * 2 + strlen(itemname) + strlen(value) + 5); if (!last_line_ret) fwrite (&cr, 1, 2, ini); sprintf (line, "%s = %s", itemname, value); fwrite (line, 1, strlen(line), ini); fwrite (&cr, 1, 2, ini); last_line = ftell(ini); last_line_ret = 1; return; }
// Reads the value of item 'itemname' as a string. const char *INI_ReadString (const char *itemname, char *value, const char *def_value, BOOL create) { char line[256], name[64]; char *p, *n; int ret, i; *value = 0; fseek(ini,sectionstart,SEEK_SET); while(!feof(ini)) { ret = 0; *line=0; if (fgets(line,255,ini) == NULL) break; // remove enter i=strlen(line); // ZIGGY there was a bug here if EOL was unix like on a short line (i.e. a line // with just EOL), it would write into line[-1] // OLD CODE : if(line[i-1]=='\n') ret=1, line[i-2]=0; if(i>=1 && line[i-1]==0xa) { ret=1; line[i-1]=0; if (i>=2 && line[i-2]==0xd) line[i-2]=0; } // remove comments p=line; while(*p) { if (p[0]==';') { p[0]=0; break; } p++; } // skip starting space p=line; while(*p<=' ' && *p) p++; // empty line if(!*p) continue; // new section if(*p=='[') break; last_line=ftell(ini); // where to add if not found last_line_ret = ret; // read name n = name; while(*p && *p!='=' && *p>' ') *n++ = *p++; *n = 0; #ifdef _WIN32 if(!stricmp(name,itemname)) #else // _WIN32 if(!strcasecmp(name,itemname)) #endif // _WIN32 { // skip spaces/equal sign while(*p<=' ' || *p=='=') p++; // read value n = value; while(*p) *n++ = *p++; // remove trailing spaces while (*(n-1) == ' ') n--; *n=0; return value; } } // uh-oh, not found. we need to create if (create) { fseek(ini,last_line,SEEK_SET); INI_InsertSpace ((!last_line_ret) * 2 + strlen(itemname) + strlen(def_value) + 5); if (!last_line_ret) if (fwrite(&cr, 1, 2, ini) != 2) ERRLOG("Failed to write <CR><LF> to .ini file"); sprintf (line, "%s = %s", itemname, def_value); if (fwrite(line, 1, strlen(line), ini) != strlen(line) || fwrite(&cr, 1, 2, ini) != 2) ERRLOG("Failed to write key,value line to .ini file"); last_line = ftell(ini); last_line_ret = 1; } strcpy (value, def_value); return value; }