int _WritePrivateProfileString( const char * lpAppName, // pointer to section name const char * lpKeyName, // pointer to key name const char * lpString, // pointer to string to add const char * lpFileName // pointer to initialization filename ) { char *Input = NULL, *Data = NULL, * Pos, CurrentSection[300]; int DataLen = 0, DataLeft, result, count; //static long Fpos = 0; long WritePos; FILE * fInput; fInput = fopen(lpFileName,"r+b"); if (fInput == NULL) { fInput = fopen(lpFileName,"w+b"); if (fInput == NULL) { return 0; } } CurrentSection[0] = 0; //test to see if Fpos is same as current section; /* fseek(fInput,Fpos,SEEK_SET); result = fGetString2(fInput,&Input,&Data,&DataLen,&DataLeft); if (result > 1) { Pos = Input; while (Pos != NULL) { Pos = strchr(Pos,'/'); if (Pos != NULL) { if (Pos[1] == '/') { Pos[0] = 0; } else { Pos += 1; } } } for (count = strlen(&Input[0]) - 1; count >= 0; count --) { if (Input[count] != ' ' && Input[count] != '\r') { break; } Input[count] = 0; } //stip leading spaces if (strlen(Input) > 1) { if (Input[0] == '[' && Input[strlen(Input) - 1] == ']') { strcpy(CurrentSection,&Input[1]); CurrentSection[strlen(CurrentSection) - 1] = 0; } } } if (strcmp(lpAppName,CurrentSection) != 0) { DataLen = 0; DataLeft = 0; free(Data); Data = NULL; fseek(fInput,0,SEEK_SET); }*/ do { /* if (strcmp(lpAppName,CurrentSection) != 0) { Fpos = ftell(fInput) - DataLeft; } */ result = fGetString2(fInput,&Input,(BYTE**)&Data,&DataLen,&DataLeft); if (result <= 1) { continue; } Pos = Input; while (Pos != NULL) { Pos = strchr(Pos,'/'); if (Pos != NULL) { if (Pos[1] == '/') { Pos[0] = 0; } else { Pos += 1; } } } for (count = strlen(&Input[0]) - 1; count >= 0; count --) { if (Input[count] != ' ' && Input[count] != '\r') { break; } Input[count] = 0; } //stip leading spaces if (strlen(Input) <= 1) { continue; } if (Input[0] == '[') { if (Input[strlen(Input) - 1] != ']') { continue; } if (strcmp(lpAppName,CurrentSection) == 0) { result = -1; continue; } strcpy(CurrentSection,&Input[1]); CurrentSection[strlen(CurrentSection) - 1] = 0; WritePos = ftell(fInput) - DataLeft; continue; } if (strcmp(lpAppName,CurrentSection) != 0) { continue; } Pos = strchr(Input,'='); if (Pos == NULL) { continue; } Pos[0] = 0; if (strcmp(Input,lpKeyName) != 0) { WritePos = ftell(fInput) - DataLeft; continue; } { long OldLen = (ftell(fInput) - DataLeft) - WritePos; int Newlen = strlen(lpKeyName) + strlen(lpString) + strlen(LineFeed) + 1; if (OldLen != Newlen) { fInsertSpaces(fInput,WritePos,Newlen - OldLen); } fseek(fInput,WritePos,SEEK_SET); fprintf(fInput,"%s=%s%s",lpKeyName,lpString,LineFeed); fflush(fInput); if ((Newlen - OldLen < 0)) { //copy all file excluding size diff } fclose(fInput); } if (Input) { free(Input); Input = NULL; } if (Data) { free(Data); Data = NULL; } return 0; } while (result >= 0); if (strcmp(lpAppName,CurrentSection) == 0) { int len = strlen(lpKeyName) + strlen(lpString) + strlen(LineFeed) + 1; fInsertSpaces(fInput,WritePos,len); fseek(fInput,WritePos,SEEK_SET); fprintf(fInput,"%s=%s%s",lpKeyName,lpString,LineFeed); fflush(fInput); fclose(fInput); } else { fseek(fInput,0,SEEK_END); fprintf(fInput,"%s[%s]%s%s=%s%s",LineFeed,lpAppName,LineFeed, lpKeyName,lpString,LineFeed); fflush(fInput); fclose(fInput); } if (Input) { free(Input); Input = NULL; } if (Data) { free(Data); Data = NULL; } return 0; }
void CIniFileBase::SaveCurrentSection ( void ) { if (!m_CurrentSectionDirty) { return; } m_CurrentSectionDirty = false; if (m_CurrentSection.length() == 0) { m_CurrentSection = "default"; } int lineFeedLen = (int)strlen(m_LineFeed); if (m_CurrentSectionFilePos == -1) { //Section has not been added yet m_File.Seek(0,CFileBase::end); int len = (int)m_CurrentSection.length() + (lineFeedLen * 2) + 5; AUTO_PTR<char> SectionName(new char[len]); if (m_File.GetLength() < (int)strlen(m_LineFeed)) { sprintf(SectionName.get(),"[%s]%s",m_CurrentSection.c_str(),m_LineFeed); } else { sprintf(SectionName.get(),"%s[%s]%s",m_LineFeed,m_CurrentSection.c_str(),m_LineFeed); } m_File.Write(SectionName.get(),(int)strlen(SectionName.get())); m_CurrentSectionFilePos = m_File.GetPosition(); m_SectionsPos.insert(FILELOC::value_type(m_CurrentSection,m_CurrentSectionFilePos)); } else { //increase/decrease space needed int NeededBufferLen = 0; { AUTO_PTR<char> LineData(NULL); int len = 0; for (KeyValueList::iterator iter = m_CurrentSectionData.begin(); iter != m_CurrentSectionData.end(); iter++) { int newLen = (int)iter->first.length() + (int)iter->second.length() + lineFeedLen + 5; if (newLen > len) { LineData.reset(new char[newLen]); len = newLen; } sprintf(LineData.get(),"%s=%s%s",iter->first.c_str(),iter->second.c_str(),m_LineFeed); NeededBufferLen += (int)strlen(LineData.get()); } } int currentLen = 0; m_File.Seek(m_CurrentSectionFilePos, CFileBase::begin); int MaxDataSize = 0, DataSize = 0, ReadPos = 0, result; char *Input = NULL, *Data = NULL; //Skip first line as it is the section name int StartPos = m_CurrentSectionFilePos; int EndPos = StartPos; do { result = GetStringFromFile(Input,Data,MaxDataSize,DataSize,ReadPos); if (result <= 1) { continue; } if (strlen(CleanLine(Input)) <= 1 || Input[0] != '[') { EndPos = ((m_File.GetPosition() - DataSize) + ReadPos); continue; } if (Input[0] == '[') { NeededBufferLen += lineFeedLen; } break; } while (result >= 0); currentLen = EndPos - StartPos; if (Data) { delete [] Data; Data = NULL; } if (NeededBufferLen != currentLen) { fInsertSpaces(StartPos,NeededBufferLen - currentLen); m_File.Flush(); ClearSectionPosList(StartPos); } //set pointer to beginning of the start pos m_File.Seek(StartPos, CFileBase::begin); } { AUTO_PTR<char> LineData(NULL); int len = 0; for (KeyValueList::iterator iter = m_CurrentSectionData.begin(); iter != m_CurrentSectionData.end(); iter++) { int newLen = (int)iter->first.length() + (int)iter->second.length() + lineFeedLen + 5; if (newLen > len) { LineData.reset(new char[newLen]); len = newLen; } sprintf(LineData.get(),"%s=%s%s",iter->first.c_str(),iter->second.c_str(),m_LineFeed); m_File.Write(LineData.get(),(int)strlen(LineData.get())); } } m_File.Flush(); }
/*int _WritePrivateProfileString( const char * lpAppName, // pointer to section name const char * lpKeyName, // pointer to key name const char * lpString, // pointer to string to add const char * lpFileName // pointer to initialization filename ) { char Input[300], CurrentSection[300]; int result, count; long WritePos; FILE * fInput; //open the file for reading fInput = fopen(lpFileName,"r+b"); if (fInput == NULL) { return 0; } fseek(fInput,0,SEEK_SET); CurrentSection[0] = 0; do { char * Pos; result = fGetString(fInput,Input,sizeof(Input)); if (result <= 1) { continue; } if (Input[strlen(Input) - 1] == '\r') { Input[strlen(Input) - 1] = 0; } Pos = Input; while (Pos != NULL) { Pos = strchr(Pos,'/'); if (Pos != NULL) { if (Pos[1] == '/') { Pos[0] = 0; } else { Pos += 1; } } } for (count = strlen(&Input[0]) - 1; count >= 0; count --) { if (Input[count] != ' ') { break; } Input[count] = 0; } //stip leading spaces if (strlen(Input) <= 1) { continue; } if (Input[0] == '[') { if (Input[strlen(Input) - 1] != ']') { continue; } if (strcmp(lpAppName,CurrentSection) == 0) { result = -1; continue; } strcpy(CurrentSection,&Input[1]); CurrentSection[strlen(CurrentSection) - 1] = 0; WritePos = ftell(fInput); continue; } if (strcmp(lpAppName,CurrentSection) != 0) { continue; } Pos = strchr(Input,'='); if (Pos == NULL) { continue; } Pos[0] = 0; if (strcmp(Input,lpKeyName) != 0) { WritePos = ftell(fInput); continue; } { long OldLen = ftell(fInput) - WritePos; int Newlen = strlen(lpKeyName) + strlen(lpString) + strlen(LineFeed) + 1; if (OldLen != Newlen) { fInsertSpaces(fInput,WritePos,Newlen - OldLen); } fseek(fInput,WritePos,SEEK_SET); fprintf(fInput,"%s=%s%s",lpKeyName,lpString,LineFeed); if ((Newlen - OldLen < 0)) { //copy all file excluding size diff } fclose(fInput); } return 0; } while (result >= 0); if (strcmp(lpAppName,CurrentSection) == 0) { int len = strlen(lpKeyName) + strlen(lpString) + strlen(LineFeed) + 1; fInsertSpaces(fInput,WritePos,len); fseek(fInput,WritePos,SEEK_SET); fprintf(fInput,"%s=%s%s",lpKeyName,lpString,LineFeed); fclose(fInput); } else { fseek(fInput,0,SEEK_END); fprintf(fInput,"%s[%s]%s%s=%s%s",LineFeed,lpAppName,LineFeed, lpKeyName,lpString,LineFeed); fclose(fInput); } return 0; } */ int _DeletePrivateProfileString( const char * lpAppName, // pointer to section name const char * lpKeyName, // pointer to key name const char * lpFileName // pointer to initialization filename ) { char *Input = NULL, *Data = NULL, * Pos, CurrentSection[300]; int DataLen = 0, DataLeft, result, count; long WritePos; FILE * fInput; fInput = fopen(lpFileName,"r+b"); if (fInput == NULL) { fInput = fopen(lpFileName,"w+b"); if (fInput == NULL) { return 0; } } CurrentSection[0] = 0; do { result = fGetString2(fInput,&Input,(BYTE**)&Data,&DataLen,&DataLeft); if (result <= 1) { continue; } Pos = Input; while (Pos != NULL) { Pos = strchr(Pos,'/'); if (Pos != NULL) { if (Pos[1] == '/') { Pos[0] = 0; } else { Pos += 1; } } } for (count = strlen(&Input[0]) - 1; count >= 0; count --) { if (Input[count] != ' ' && Input[count] != '\r') { break; } Input[count] = 0; } //stip leading spaces if (strlen(Input) <= 1) { continue; } if (Input[0] == '[') { if (Input[strlen(Input) - 1] != ']') { continue; } if (strcmp(lpAppName,CurrentSection) == 0) { result = -1; continue; } strcpy(CurrentSection,&Input[1]); CurrentSection[strlen(CurrentSection) - 1] = 0; WritePos = ftell(fInput) - DataLeft; continue; } if (strcmp(lpAppName,CurrentSection) != 0) { continue; } Pos = strchr(Input,'='); if (Pos == NULL) { continue; } Pos[0] = 0; if (strcmp(Input,lpKeyName) != 0) { WritePos = ftell(fInput) - DataLeft; continue; } { long OldLen = (ftell(fInput) - DataLeft) - WritePos; int Newlen = 0; if (OldLen != Newlen) { fInsertSpaces(fInput,WritePos,Newlen - OldLen); } fclose(fInput); } if (Input) { free(Input); Input = NULL; } if (Data) { free(Data); Data = NULL; } return 0; } while (result >= 0); fclose(fInput); if (Input) { free(Input); Input = NULL; } if (Data) { free(Data); Data = NULL; } return 0; }