bool CIniFile::SetValue( string const keyname, string const valuename, string const value, bool const create) { long keyID = FindKey( keyname); if ( keyID == noID) { if ( create) keyID = long( AddKeyName( keyname)); else return false; } long valueID = FindValue( unsigned(keyID), valuename); if ( valueID == noID) { if ( !create) return false; keys[keyID].names.resize( keys[keyID].names.size() + 1, valuename); keys[keyID].values.resize( keys[keyID].values.size() + 1, value); } else keys[keyID].values[valueID] = value; return true; }
BOOL IniFile::SetValueS(LPCTSTR lpcszKey, LPCTSTR lpcszEntry, LPCTSTR lpcszValue, BOOL bCreate) { size_t nKey = FindKey(lpcszKey); if (nKey == InvalidIndex) { if (bCreate) nKey = AddKeyName(lpcszKey); else return FALSE; } size_t nEntry = FindEntry(nKey, lpcszEntry); if (nEntry == InvalidIndex) { if (!bCreate) return FALSE; Entry entry; entry.lpszEntry = new TCHAR[_tcslen(lpcszEntry)+1]; entry.lpszValue = new TCHAR[_tcslen(lpcszValue)+1]; _tcscpy(entry.lpszEntry, lpcszEntry); _tcscpy(entry.lpszValue, lpcszValue); m_dqKeys[nKey].dqEntries.resize(m_dqKeys[nKey].dqEntries.size() + 1, entry); } else { LPTSTR lpszValue2 = new TCHAR[_tcslen(lpcszValue)+1]; _tcscpy(lpszValue2, lpcszValue); delete[] m_dqKeys[nKey].dqEntries[nEntry].lpszValue; m_dqKeys[nKey].dqEntries[nEntry].lpszValue = lpszValue2; } return TRUE; }
BOOL IniFile::ReadFileA() { USES_CONVERSION; FILE *f = fopen(T2A(m_szPath), "rt"); if (!f) return FALSE; char szLine[MAX_FGETS_LINE]; char szKey[MAX_KEY]; LPSTR lpszSub, lpszSub2; szKey[0] = '\0'; while (fgets(szLine, MAX_FGETS_LINE-1, f)) { if (szLine[strlen(szLine) - 1] == '\r') szLine[strlen(szLine) - 1] = '\0'; if (szLine[strlen(szLine) - 1] == '\n') szLine[strlen(szLine) - 1] = '\0'; if (strlen(szLine)) { if (!isprint(szLine[0])) { fclose(f); return FALSE; } switch (szLine[0]) { case '[': lpszSub = strchr(szLine, ']'); if (lpszSub) { lpszSub[0] = '\0'; strcpy(szKey, szLine+1); AddKeyName(A2T(szKey)); } break; case ';': case '#': SetComment(A2T(szLine+1)); break; default: lpszSub = strchr(szLine, '='); if (lpszSub) { lpszSub2 = lpszSub; if (lpszSub2[0] == ' ') { while (lpszSub2[0] == ' ') lpszSub2--; lpszSub2++; } lpszSub2[0] = '\0'; while (lpszSub[0] == ' ') lpszSub++; lpszSub++; SetValueS(A2T(szKey), A2T(szLine), A2T(lpszSub)); } break; } } } fclose(f); return m_dqKeys.empty() ? FALSE : TRUE; }
// // further: \r\n까지 검색하여 길이 구해 line 버퍼수 결정 // BOOL IniFile::ReadFileW() { USES_CONVERSION; FILE *f = _tfopen(m_szPath, _T("rb")); if (!f) return FALSE; LPWSTR lpwszLine; WCHAR wszLine[MAX_FGETS_LINE] = {0}; fread(wszLine, 2, 1, f); if (wszLine[0] != 0xFEFF) { fclose(f); return FALSE; } size_t nLen; size_t nRead; WCHAR szKey[MAX_KEY]; LPWSTR lpwszSub, lpwszSub2; szKey[0] = L'\0'; while (1) { nLen = 0; while (1) { nRead = fread(wszLine+nLen, sizeof(WCHAR), 1, f); if (nRead > 0) nLen++; if (nLen >= 1 && wszLine[nLen-1] == L'\n') { wszLine[nLen-1] = L'\0'; nLen--; if (nLen >= 1 && wszLine[nLen-1] == L'\r') { wszLine[nLen-1] = L'\0'; nLen--; } break; } if (nRead <= 0) break; } lpwszLine = (LPWSTR)wszLine; if (wcslen(lpwszLine)) { switch (lpwszLine[0]) { case L'[': lpwszSub = wcsrchr(lpwszLine, L']'); // ex) [c:\[123]] if (lpwszSub) { lpwszSub[0] = _T('\0'); wcscpy(szKey, lpwszLine+1); AddKeyName(W2T(szKey)); } break; case L';': case L'#': SetComment(W2T(lpwszLine+1)); break; default: lpwszSub = wcschr(lpwszLine, L'='); if (lpwszSub) { lpwszSub2 = lpwszSub; if (lpwszSub2[0] == L' ') { while (lpwszSub2[0] == L' ') lpwszSub2--; lpwszSub2++; } lpwszSub2[0] = L'\0'; while (lpwszSub[0] == L' ') lpwszSub++; lpwszSub++; SetValueS(W2T(szKey), W2T(lpwszLine), W2T(lpwszSub)); } break; } } if (nRead <= 0) break; } fclose(f); return m_dqKeys.empty() ? FALSE : TRUE; }
bool CIniFile::ReadFile() { // Normally you would use ifstream, but the SGI CC compiler has // a few bugs with ifstream. So ... fstream used. fstream f; string line; string keyname, valuename, value; string::size_type pLeft, pRight; f.open( path.c_str(), ios::in); if ( f.fail()) return false; while( getline( f, line)) { // To be compatible with Win32, check for existence of '\r'. // Win32 files have the '\r' and Unix files don't at the end of a line. // Note that the '\r' will be written to INI files from // Unix so that the created INI file can be read under Win32 // without change. if ( line[line.length()==0?0:line.length()-1] == '\r') line = line.substr( 0, line.length() - 1); if ( line.length()) { // Check that the user hasn't openned a binary file by checking the first // character of each line! if ( !isprint( line[0])) { printf( "Failing on char %d\n", line[0]); f.close(); return false; } if (( pLeft = line.find_first_of(";#[=")) != string::npos) { switch ( line[pLeft]) { case '[': if ((pRight = line.find_last_of("]")) != string::npos && pRight > pLeft) { keyname = line.substr( pLeft + 1, pRight - pLeft - 1); AddKeyName( keyname); } break; case '=': valuename = line.substr( 0, pLeft); value = line.substr( pLeft + 1); SetValue( keyname, valuename, value); break; case ';': case '#': if ( !names.size()) HeaderComment( line.substr( pLeft + 1)); else KeyComment( keyname, line.substr( pLeft + 1)); break; } } } } f.close(); if ( names.size()) return true; return false; }