#include "IniUtil.h" CIniUtil::CIniUtil(void) { //m_ValSection = _T("DP5 Configuration Values"); } CIniUtil::~CIniUtil(void) { } BOOL CIniUtil::GetDP5Data(CString m_strINIFile, CString m_strSection, CString cmdKey, CString *cmdData, CString *Comment) { CString m_strSectionName; CString m_strKeyValue; int ScPos; CString strData; CString strComment; m_strSectionName = m_strSection; if(m_strINIFile.IsEmpty()) { return FALSE; } //Specify INI File name if(m_strSectionName.IsEmpty()) { return FALSE; } //Specify section name ini.setINIFileName(m_strINIFile); BOOL bExists = ini.sectionExists(m_strSectionName); if(!bExists) { return FALSE; } //Section doesn't exist strData.Empty(); m_strKeyValue = ini.getKeyValue(cmdKey,m_strSectionName); m_strKeyValue.Trim(); if (m_strKeyValue.GetLength() == 0) { return FALSE; } strComment = ""; ScPos = m_strKeyValue.Find(";",0); if (ScPos > 0) { // save the value if comment strData = m_strKeyValue.Left(ScPos); strComment = m_strKeyValue.Mid(ScPos + 1); } else { strData = m_strKeyValue; } *cmdData = strData; *Comment = strComment; return TRUE; } BOOL CIniUtil::SaveDP5Data(CString m_strINIFile, CString m_strSection, CString cmdKey, CString cmdData, CString Comment) { CString m_strSectionName; CString m_strKeyValue; CString strData; BOOL m_lRetValue; m_strSectionName = m_strSection; //if(m_strINIFile.IsEmpty()) { return FALSE; } //Specify INI File name //if(m_strSectionName.IsEmpty()) { return FALSE; } //Specify section name ini.setINIFileName(m_strINIFile); //BOOL bExists = ini.sectionExists(m_strSectionName); //if(!bExists) { return FALSE; } //Section doesn't exist strData = cmdData + ";" + Comment; m_lRetValue = ini.setKey(strData,cmdKey,m_strSection); return m_lRetValue; } BOOL CIniUtil::GetDP5Section(CString m_strINIFile, CString m_strSection, CStringList* m_strListDP5section) { if(m_strINIFile.IsEmpty()) { //AfxMessageBox("INI File name error"); return FALSE; } if(m_strSection.IsEmpty()) { //AfxMessageBox("Section name error"); return FALSE; } ini.setINIFileName(m_strINIFile); BOOL bExists = ini.sectionExists(m_strSection); if(!bExists) { //AfxMessageBox("Section doesn't exist error"); return FALSE; } CStringList* strlstSection = ini.getSectionData(m_strSection); POSITION posCmd; //BOOL isComment; //CString strKey; //int EqPos; m_strListDP5section->RemoveAll(); CString strCMD; for(posCmd = strlstSection->GetHeadPosition(); posCmd != NULL; ) { strCMD = strlstSection->GetNext(posCmd); //if (isComment) { // strKey = myOtherList->GetNext(position); // EqPos = strKey.Find("=",0); // strKey = strKey.Left(EqPos); // m_ListSectionData.AddString(strKey); //} else { if(strCMD != "") { m_strListDP5section->InsertAfter(m_strListDP5section->GetTailPosition(),strCMD); } strCMD = ""; //} } return TRUE; } CString CIniUtil::ConvertCmdListToCString(CStringList* strCmdList, BOOL AddNewLines, BOOL RemoveComments) { CString strCmdCStr; POSITION posCmd; int ComPos; CString strCMD; strCmdCStr = ""; strCMD = ""; for(posCmd = strCmdList->GetHeadPosition(); posCmd != NULL; ) { strCMD = strCmdList->GetNext(posCmd); strCMD = strCMD.Trim(); if (RemoveComments && (strCMD != "")) { ComPos = strCMD.Find(";",0); //CMDV=*; //0123456 if (ComPos > 5) { // remove the comments strCMD = strCMD.Left(ComPos); strCMD = strCMD.Trim() + ";"; } else if (ComPos >= 0) { // error strCMD = ""; } else { // check for MS style ini entry strCMD = GetMsIniItem(strCMD); } } if(strCMD != "") { if (AddNewLines) { strCMD += "\r\n"; } strCmdCStr += strCMD; } strCMD = ""; } return strCmdCStr; } CString CIniUtil::GetMsIniItem(CString strCmdIn) { CString strMsCmd; int iStart,iEnd; CString strKey; CString strData; CString strCMD; strCMD = strCmdIn.Trim(); // test for valid command parameters if (strCMD.GetLength() < 6) { return ""; } // no data possible iStart = strCMD.Find("=",0); if (iStart != 4) { return ""; } // data start not found iEnd = strCMD.Find(";",0); if (iEnd != -1) { // terminator found, just strip comments strMsCmd = strCMD.Left(iEnd+1); return strMsCmd; } // appear to have ms ini key data pair, terminate cmd, ret strMsCmd = strCMD + ";"; return strMsCmd; } BOOL CIniUtil::DeleteDP5Key(CString m_strINIFile, CString m_strSection, CString cmdKey) { CString m_strSectionName; CString m_strKeyValue; m_strSectionName = m_strSection; if(m_strINIFile.IsEmpty()) { return FALSE; } //Specify INI File name if(m_strSectionName.IsEmpty()) { return FALSE; } //Specify section name ini.setINIFileName(m_strINIFile); BOOL bExists = ini.sectionExists(m_strSectionName); if(!bExists) { return FALSE; } //Section doesn't exist BOOL isDeleted = ini.DeleteKey(cmdKey,m_strSection); return TRUE; } //void CIniUtil::ListSections() //{ // if(m_strINIFile.IsEmpty()) // { // AfxMessageBox("Specify INI File name"); // return; // } // // ini.setINIFileName(m_strINIFile); // m_ListSections.ResetContent(); // m_ListSectionData.ResetContent(); // // m_strKeyValue.Empty(); // UpdateData(FALSE); // // //load listbox from collection returned from getSectionNames(); // CStringList* strSectionList = ini.getSectionNames(); // POSITION pos; // for(pos = myStringList->GetHeadPosition(); pos != NULL; ) { // m_ListSections.AddString(myStringList->GetNext(pos)); // } //}