// IniFile.cpp: implementation of the CINI class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" //#include "Readini.h" #include "INI.h" #include #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif // //setINIFileName // Sets the INI File name to read and write. // //getKeyValue // Used to retrieve a key value given the section and key name. // //getSectionData // Used to retrieve all key/value pairs of a given section. // //getSectionNames // Used to retrieve all the sections in an ini file // //sectionExists // Used to find out if a given section exists. // //setKey // Used to modify the key value or to create a key value pair for the specified section. // ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CINI::CINI(CString strFile) { m_strFileName = strFile; m_sectionList = new CStringList(); m_sectionDataList = new CStringList(); } CINI::~CINI() { delete m_sectionList; delete m_sectionDataList; } // Used to retrieve a value give the section and key CString CINI::getKeyValue(CString strKey,CString strSection) { char ac_Result[MAX_KEY_DATA_SIZE]; // Get the info from the .ini file m_lRetValue = GetPrivateProfileString((LPCTSTR)strSection,(LPCTSTR)strKey, "",ac_Result, MAX_KEY_DATA_SIZE, (LPCTSTR)m_strFileName); CString strResult(ac_Result); return strResult; } // Used to add or set a key value pair to a section long CINI::setKey(CString strValue, CString strKey, CString strSection) { m_lRetValue = WritePrivateProfileString (strSection, strKey, strValue, m_strFileName); return m_lRetValue; } // Used to delete a key value pair to a section long CINI::DeleteKey(CString strKey, CString strSection) { m_lRetValue = WritePrivateProfileString (strSection, strKey, NULL, m_strFileName); return m_lRetValue; } // Used to delete a key value pair to a section long CINI::DeleteSection(CString strSection) { m_lRetValue = WritePrivateProfileString (strSection, NULL, NULL, m_strFileName); return m_lRetValue; } // Used to find out if a given section exists BOOL CINI::sectionExists(CString strSection) { char ac_Result[MAX_KEY_DATA_SIZE]; CString csAux; // Get the info from the .ini file m_lRetValue = GetPrivateProfileString((LPCTSTR)strSection,NULL, "",ac_Result, MAX_KEY_DATA_SIZE, (LPCTSTR)m_strFileName); // Return if we could retrieve any info from that section //csAux.Format("%d",m_lRetValue); //MessageBox(NULL,csAux,"",MB_OK); return (m_lRetValue > 0); } // Used to retrieve all of the section names in the ini file CStringList* CINI::getSectionNames() //returns collection of section names { char ac_Result[MAX_SECTION_SIZE]; m_sectionList->RemoveAll(); m_lRetValue = GetPrivateProfileSectionNames(ac_Result,MAX_SECTION_SIZE,(LPCTSTR)m_strFileName); CString strSectionName; for(int i=0; iInsertAfter(m_sectionList->GetTailPosition(),strSectionName); } strSectionName = ""; } } return m_sectionList; } // Used to retrieve all key/value pairs of a given section. CStringList* CINI::getSectionData(CString strSection) { char ac_Result[MAX_SECTION_SIZE]; //change size depending on needs m_sectionDataList->RemoveAll(); m_lRetValue = GetPrivateProfileSection((LPCTSTR)strSection, ac_Result, MAX_SECTION_SIZE, (LPCTSTR)m_strFileName); CString strSectionData; for(int i=0; iInsertAfter(m_sectionDataList->GetTailPosition(),strSectionData); } strSectionData = ""; } } return m_sectionDataList; } void CINI::setINIFileName(CString strINIFile) { m_strFileName = strINIFile; }