Esempio n. 1
0
int MIniFile::SetSectionName(const char *sectionName)
{
	IniSection* pSection = GetSection(sectionName);
	if(0 == pSection)
	{
		pSection = new IniSection;

		LineInfo* pSectionLineInfo = new LineInfo;

		pSection->SetSectionName(sectionName);

		pSection->m_nLineNo = m_nLineCount;

		pSectionLineInfo->nLineNo = pSection->m_nLineNo;
		pSectionLineInfo->strMainData = "[";
		pSectionLineInfo->strMainData += sectionName;
		pSectionLineInfo->strMainData += "]";

		AdjustAllLineInfo(pSectionLineInfo);

		AddSection(pSection);
	}
	else
	{
		pSection->SetSectionName(sectionName);
	}

	m_bChange = true;

	return 0;
}
Esempio n. 2
0
//向一个节中添加一个键,若没有这个节则新建一个节
int MIniFile::SetValue(const char* sectionName,const char* keyName,const char* value)
{
	IniSection* pSection = GetSection(sectionName);

	if (pSection != NULL)
	{
		IniKey* pKey = GetKey(sectionName,keyName);

		LineInfo* pLineInfo =NULL;

		if (pKey != NULL)
		{
			pKey->SetValue(value);
			pLineInfo =  FindLineInfo(pKey->m_nLineNo);

		}
		else
		{
			IniKey* pLastKey = pSection->GetLastKey();

			pKey = new IniKey(pSection);
			pSection->AddKey(pKey);
			pKey->SetKeyName(keyName);
			pKey->SetValue(value);

			if (pLastKey == NULL)
			{
				//该节没有键值对
				pKey->m_nLineNo = pSection->m_nLineNo + 1;
			}
			else
			{
				pKey->m_nLineNo = pLastKey->m_nLineNo+1;
			}

			pSection->m_nKeyCount++;
			pLineInfo = new LineInfo;
			pLineInfo->type = LINE_KEYPAIR;
			pLineInfo->nLineNo = pKey->m_nLineNo;

			AdjustAllLineInfo(pLineInfo);
		}

		pLineInfo->strMainData = "\t";
		pLineInfo->strMainData += pKey->m_Key;
		pLineInfo->strMainData += "\t=\t";
		pLineInfo->strMainData += pKey->m_Value;


	}
	else
	{
		//新增节和键
		pSection = new IniSection;
		IniKey* pKey = new IniKey(pSection);

		LineInfo* pSectionLineInfo = new LineInfo;
		LineInfo* pKeyLineInfo = new LineInfo;

		pSection->SetSectionName(sectionName);
		pKey->SetKeyName(keyName);
		pKey->SetValue(value);

		pSection->AddKey(pKey);

		pSection->m_nLineNo = m_nLineCount;
		pKey->m_nLineNo = m_nLineCount + 1;

		pSectionLineInfo->nLineNo = pSection->m_nLineNo;
		pSectionLineInfo->strMainData = "[";
		pSectionLineInfo->strMainData += sectionName;
		pSectionLineInfo->strMainData += "]";

		pKeyLineInfo->nLineNo = pKey->m_nLineNo;
		pKeyLineInfo->strMainData = "\t";
		pKeyLineInfo->strMainData += pKey->m_Key;
		pKeyLineInfo->strMainData += "\t=\t";
		pKeyLineInfo->strMainData += pKey->m_Value;

		AdjustAllLineInfo(pSectionLineInfo);
		AdjustAllLineInfo(pKeyLineInfo);

		AddSection(pSection);
	}

	m_bChange = true;

	return 0;
}
Esempio n. 3
0
void MIniFile::Adjust()//
{
	LineInfo* pInfo = 0;
	for(int i = 0 ; i < m_LineInfoArray.size(); ++i)
	{
		pInfo = m_LineInfoArray[i];
		if(0 == pInfo)
		{
			continue;
		}

		if (pInfo->type != LINE_SECTION)
		{
			continue;
		}

		string helpStr;

		helpStr = pInfo->strMainData;

		helpStr = Trim(helpStr);

		string sectionName = helpStr.substr(1,helpStr.size() - 1);
		sectionName = sectionName.substr(0,sectionName.size() - 1);

		IniSection* pSection = new IniSection;
		pSection->SetSectionName(sectionName.c_str());
		pSection->m_nLineNo = pInfo->nLineNo;


		AddSection(pSection);

		int nIndex = i+1;
		if(nIndex == m_LineInfoArray.size())
		{
			break;
		}

		for(int j = nIndex; j < m_LineInfoArray.size(); ++j)
		{
			LineInfo* pKeyLine = m_LineInfoArray[j];

			if(0 == pKeyLine)
			{
				continue;
			}

			if (pKeyLine->type == LINE_SECTION)
			{
				break;
			}

			if (pKeyLine->type == LINE_OTHER)
			{
				continue;
			}

			//剩下的都是LINE_PAIR
			pSection->m_nKeyCount++;


			IniKey* pKey = new IniKey;
			pKey->m_nLineNo = pKeyLine->nLineNo;

			//MString keyName,value,help;
			string help = pKeyLine->strMainData;
			help = Trim(help);


			int iIndex = help.find('=');
			string keyName = help.substr(0,iIndex);
			string value = help.substr(iIndex + 1);
			keyName = Trim(keyName);
			value = Trim(value);

			pKey->m_Key = keyName.c_str();
			pKey->m_Value = value.c_str();

			pSection->AddKey(pKey);
		}
	}
}