⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jxinifile.cpp

📁 minica2的第2个版本
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	            while(pos)
				{
					CString strKey;
		            LPVOID lpPos;
		            m_mapSection.GetNextAssoc(pos, strKey, lpPos);
					//adjust Section Map
				    if((LPSTR)lpPos - pHead > (int)uMoveHead)
					{
						LPSTR pTemp = (LPSTR)lpPos;
						pTemp -= charDiff;
						lpPos = (LPVOID)pTemp;
						m_mapSection.SetAt(strKey, lpPos);
					}
				}
			}
		}
		pValue = pHead + uKey;
		::memcpy(pValue, buffer, sizeof(char)*uCurLen);
        m_dwUseSize += (uCurLen-uPrevLen)*sizeof(char);

		delete buffer;
		buffer = NULL;
        bRet = TRUE;
	}

	
	if(!bRet) 
	{
		UnloadIniFile();
		return FALSE;
	}
	//If the File is large enough, flush the file
	if((int)(m_dwUseSize - m_dwSize) > JXFlushIncrement - 5*JXPage)
	{
		//Flush Back to HardDisk
		UnloadIniFile();
	}
	return TRUE;
}

BOOL CJXINIFile::WritePrivateProfileStringExW(
         LPCWSTR lpAppName,  // section name
         LPCWSTR lpKeyName,  // key name
         LPCWSTR lpString,   // string to add
         LPCWSTR lpFileName  // initialization file
    )
{
	BOOL bRet = this->LoadIniFile(lpFileName, 2);
	if(!bRet) return FALSE;
	m_bDirty = TRUE;

	if(lpKeyName == NULL)
		return DeleteSectionExW(lpAppName);
	else if(lpString == NULL)
		return this->DeleteKeyExW(lpAppName, lpKeyName);

	LPWSTR pHead = (LPWSTR)m_pTrueData;
	//2001/10/19 if lpString.len > MAX_PATH error 
	//WCHAR buffer[MAX_PATH];
	LPWSTR buffer = NULL;

	UINT uSec, uKey;  
	//Warning: uKey, uSec is measured in WCHARs!!!!!
	//in UNICODE now
	if(!m_bSectionMapDirty)
		uKey = GetKeyExW(pHead, (UINT)m_dwUseSize, 
		   lpAppName, lpKeyName, uSec);
	else if(!m_bSectionMapLock)
	{
		UpdateMapW();
		uKey = GetKeyExW(pHead, (UINT)m_dwUseSize, 
		   lpAppName, lpKeyName, uSec);
	}
	else
        uKey = GetKeyW(pHead, (UINT)m_dwUseSize, 
		   lpAppName, lpKeyName, uSec);

    if(uKey == (UINT)-1 && uSec == (UINT)-1) //no section
	{
		int len = wcslen(lpAppName) + wcslen(lpKeyName) +wcslen(lpString);
		len += 7;
		buffer = new WCHAR[len+1];
		::swprintf(buffer, L"[%s]\r\n%s=%s\r\n", lpAppName, lpKeyName, lpString);
		buffer[len] = 0;
		LPWSTR pStr = pHead;
		pStr += m_dwUseSize/sizeof(WCHAR);
		::memcpy(pStr, buffer, len*sizeof(WCHAR));
		m_dwUseSize += len*sizeof(WCHAR);

		delete []buffer;
		buffer = NULL;
        
		CString strSection;
#ifdef _UNICODE
		strSection = lpAppName;
#else
		DWORD dwRetSize;
		LPSTR pTemp = _W2A((LPWSTR)lpAppName, -1, dwRetSize);
        ::strncpy(strSection.GetBuffer(dwRetSize), pTemp, dwRetSize);
		strSection.ReleaseBuffer(dwRetSize);
#endif

		if(!m_bSectionMapLock)
		{
			m_mapSection.SetAt(strSection, (LPVOID)pStr);
		}
		else
			m_bSectionMapDirty = TRUE;
		//No Change //m_bSectionMapDirty = ;
		bRet = TRUE;
	}
	else if(uKey == (UINT)-1 && uSec != (UINT)-1) 
	//section exist but value do not exist
	{
		// Following Write Just After [section],
		//that new item become the first of the section
        //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
		//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
		/*
		LPWSTR pSection = pHead + uSec;
		while(pSection - pHead < (int)(m_dwUseSize/sizeof(WCHAR)) && 
	    	 *pSection != WCHAR('\r') && *pSection != WCHAR('\n'))
		{
			pSection++;
		}
    	while(*pSection == WCHAR('\r') || *pSection == WCHAR('\n'))
			pSection++;
		UINT uMoveHead = (UINT)(pSection - pHead); 
        //move memory
		::swprintf(buffer, L"%s=%s\r\n", lpKeyName, lpString);
		int len = ::wcslen(buffer);
		//also ok
		//::memmove(pHead + uMoveHead + len, pHead + uMoveHead, m_dwUseSize-uMoveHead*sizeof(WCHAR));
		for(int i = (int)(m_dwUseSize - sizeof(WCHAR)); 
		    i >= (int)(uMoveHead*sizeof(WCHAR)); i -= sizeof(WCHAR))
		{
			::memcpy(pHead + i/sizeof(WCHAR) + len, 
				pHead + i/sizeof(WCHAR), sizeof(WCHAR));
		}
		*/
		//Following Write new item the last item in the section
        //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
		//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
        LPWSTR pSection = pHead + uSec + 1;
		while(pSection - pHead < (int)(m_dwUseSize/sizeof(WCHAR)) && 
	    	 *pSection != WCHAR('['))
		{
			pSection++;
		}
//		::swprintf(buffer, L"%s=%s\r\n", lpKeyName, lpString);
//		int len = ::wcslen(buffer);

		int len = wcslen(lpKeyName) + wcslen(lpString);
		len += 3;
		buffer = new WCHAR[len+1];
		::swprintf(buffer, L"%s=%s\r\n", lpKeyName, lpString);
		buffer[len] = 0;
    	if(pSection - pHead == (int)(m_dwUseSize/sizeof(WCHAR)))
		{
			//no move needed, file end
			//no need to Update Section map
		}
		else
		{
			UINT uMoveHead = (UINT)(pSection - pHead); 
            //move memory
		    //also ok
		    //::memmove(pHead + uMoveHead + len, pHead + uMoveHead, m_dwUseSize-uMoveHead*sizeof(WCHAR));
		    for(int i = (int)(m_dwUseSize - sizeof(WCHAR)); 
		       i >= (int)(uMoveHead*sizeof(WCHAR)); i -= sizeof(WCHAR))
			{
				   ::memcpy(pHead + i/sizeof(WCHAR) + len, 
				     pHead + i/sizeof(WCHAR), sizeof(WCHAR));
			}
			if(m_bSectionMapLock)
			{ 
				m_bSectionMapDirty = TRUE;
			}
			else //update section map
			{
				POSITION pos = m_mapSection.GetStartPosition();
	            while(pos)
				{
					CString strKey;
		            LPVOID lpPos;
		            m_mapSection.GetNextAssoc(pos, strKey, lpPos);
					//adjust Section Map
				    if((LPWSTR)lpPos - pSection > 0)
					{
						LPWSTR pTemp = (LPWSTR)lpPos;
						pTemp += len;
						lpPos = (LPVOID)pTemp;
						m_mapSection.SetAt(strKey, lpPos);
					}
				}
			}
		}
		::memcpy(pSection, buffer, sizeof(WCHAR)*len);
		m_dwUseSize += len*sizeof(WCHAR);
		delete buffer;
		buffer = NULL;
		bRet = TRUE;
	}
	else //key exist
	{
		ASSERT(uKey != (UINT)-1);
        LPWSTR pValue = pHead + uKey;
	    while(pValue - pHead < (int)(m_dwUseSize/sizeof(WCHAR)) && 
		    *pValue != WCHAR('\r') && *pValue != WCHAR('\n'))
		{
			pValue++;
		}

	    while(*pValue == WCHAR('\r') || *pValue == WCHAR('\n'))
			pValue++;
		UINT uMoveHead = (UINT)(pValue - pHead);
		
		UINT uPrevLen = uMoveHead - uKey;

		int len = wcslen(lpString);
		len += 2;
		buffer = new WCHAR[len+1];
		::swprintf(buffer, L"%s\r\n", lpString);
        buffer[len] = 0;
		UINT uCurLen = len;
        //move have 2 direction
		if(uCurLen > uPrevLen)
		{
			int charDiff = uCurLen - uPrevLen;
			int begin = (int)(m_dwUseSize - sizeof(WCHAR));
			int end = (int)uMoveHead*sizeof(WCHAR);
			for(int i = begin; i >= end; i -= sizeof(WCHAR))
			{
				::memcpy(pHead + i/sizeof(WCHAR) + charDiff,
					pHead + i/sizeof(WCHAR), sizeof(WCHAR));
			}
			if(m_bSectionMapLock)
				m_bSectionMapDirty = TRUE;
			else //update section map
			{
				POSITION pos = m_mapSection.GetStartPosition();
	            while(pos)
				{
					CString strKey;
		            LPVOID lpPos;
		            m_mapSection.GetNextAssoc(pos, strKey, lpPos);
					//adjust Section Map
				    if((LPWSTR)lpPos - pHead > (int)uMoveHead)
					{
						LPWSTR pTemp = (LPWSTR)lpPos;
						pTemp += charDiff;
						lpPos = (LPVOID)pTemp;
						m_mapSection.SetAt(strKey, lpPos);
					}
				}
			}
		}
		else if(uCurLen < uPrevLen)
		{
			int charDiff = uPrevLen - uCurLen;
			int begin = (int)uMoveHead*sizeof(WCHAR);
			int end = (int)(m_dwUseSize - sizeof(WCHAR));
			for(int i = begin; i <= end;  i+=sizeof(WCHAR))
			{
				::memcpy(pHead + i/sizeof(WCHAR) - charDiff,
					pHead + i/sizeof(WCHAR), sizeof(WCHAR));
			}
			if(m_bSectionMapLock)
				m_bSectionMapDirty = TRUE;
			else //update section map
			{
				POSITION pos = m_mapSection.GetStartPosition();
	            while(pos)
				{
					CString strKey;
		            LPVOID lpPos;
		            m_mapSection.GetNextAssoc(pos, strKey, lpPos);
					//adjust Section Map
				    if((LPWSTR)lpPos - pHead > (int)uMoveHead)
					{
						LPWSTR pTemp = (LPWSTR)lpPos;
						pTemp -= charDiff;
						lpPos = (LPVOID)pTemp;
						m_mapSection.SetAt(strKey, lpPos);
					}
				}
			}
		}
		pValue = pHead + uKey;
		::memcpy(pValue, buffer, sizeof(WCHAR)*uCurLen);
        m_dwUseSize += (uCurLen-uPrevLen)*sizeof(WCHAR);

		delete buffer;
		buffer = NULL;
        bRet = TRUE;
	}

	
	if(!bRet) 
	{
		UnloadIniFile();
		return FALSE;
	}
	//If the File is large enough, flush the file
	if((int)(m_dwUseSize - m_dwSize) > JXFlushIncrement - 5*JXPage)
	{
		//Flush Back to HardDisk
		UnloadIniFile();
	}
	return TRUE;
}

BOOL CJXINIFile::WritePrivateProfileStringW(
         LPCWSTR lpAppName,  // section name
         LPCWSTR lpKeyName,  // key name
         LPCWSTR lpString,   // string to add
         LPCWSTR lpFileName  // initialization file
    )
{
	BOOL bRet = this->LoadIniFile(lpFileName, 2);
	if(!bRet) return FALSE;
	m_bDirty = TRUE;

	if(lpKeyName == NULL)
		return DeleteSectionW(lpAppName);
	else if(lpString == NULL)
		return this->DeleteKeyW(lpAppName, lpKeyName);

	LPWSTR pHead = (LPWSTR)m_pTrueData;
	//2001/10/19 if lpString.len > MAX_PATH error 
	//WCHAR buffer[MAX_PATH];
	LPWSTR buffer = NULL;

	UINT uSec, uKey;  
	//Warning: uKey, uSec is measured in WCHARs!!!!!
	//in UNICODE now
	uKey = GetKeyW(pHead, (UINT)m_dwUseSize, 
		lpAppName, lpKeyName, uSec);
    if(uKey == (UINT)-1 && uSec == (UINT)-1) //no section
	{
//		::swprintf(buffer, L"[%s]\r\n%s=%s\r\n", lpAppName, lpKeyName, lpString);
//		int len = ::wcslen(buffer);
//		LPWSTR pStr = pHead;
//		pStr += m_dwUseSize/sizeof(WCHAR);
//		::memcpy(pStr, buffer, len*sizeof(WCHAR));
//		m_dwUseSize += len*sizeof(WCHAR);

		int len = wcslen(lpAppName) + wcslen(lpKeyName) +wcslen(lpString);
		len += 7;
		buffer = new WCHAR[len+1];
		::swprintf(buffer, L"[%s]\r\n%s=%s\r\n", lpAppName, lpKeyName, lpString);
		buffer[len] = 0;
		LPWSTR pStr = pHead;
		pStr += m_dwUseSize/sizeof(WCHAR);
		::memcpy(pStr, buffer, len*sizeof(WCHAR));
		m_dwUseSize += len*sizeof(WCHAR);

		delete []buffer;
		buffer = NULL;
		bRet = TRUE;
	}
	else if(uKey == (UINT)-1 && uSec != (UINT)-1) 
	//section exist but value do not exist
	{
		// Following Write Just After [section],
		//that new item become the first of the section
        //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
		//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
		/*
		LPWSTR pSection = pHead + uSec;
		while(pSection - pHead < (int)(m_dwUseSize/sizeof(WCHAR)) && 
	    	 *pSection != WCHAR('\r') && *pSection != WCHAR('\n'))
		{
			pSection++;
		}
    	while(*pSection == WCHAR('\r') || *pSection == WCHAR('\n'))
			pSection++;
		UINT uMoveHead = (UINT)(pSection - pHead); 
        //move memory
		::swprintf(buffer, L"%s=%s\r\n", lpKeyName, lpString);
		int len = ::wcslen(buffer);
		//also ok
		//::memmove(pHead + uMoveHead + len, pHead + uMoveHead, m_dwUseSize-uMoveHead*sizeof(WCHAR));
		for(int i = (int)(m_dwUseSize - sizeof(WCHAR)); 
		    i >= (int)(uMoveHead*sizeof(WCHAR)); i -= sizeof(WCHAR))
		{
			::memcpy(pHead + i/sizeof(WCHAR) + len, 
				pHead + i/sizeof(WCHAR), sizeof(WCHAR));
		}
		*/
		//Following Write new item the last item in the section
        //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
		//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
        LPWSTR pSection = pHead + uSec + 1;
		while(pSection - pHead < (int)(m_dwUseSize/sizeof(WCHAR)) && 
	    	 *pSection != WCHAR('['))
		{
			pSection++;
		}
//		::swprintf(buffer, L"%s=%s\r\n", lpKeyName, lpString);
//		int len = ::wcslen(buffer);

		int len = wcslen(lpKeyName) + wcslen(lpString);
		len += 3;
		buffer = new WCHAR[len+1];
		::swprintf(buffer, L"%s=%s\r\n", lpKeyName, lpString);
		buffer[len] = 0;
    	if(pSection - pHead == (int)(m_dwUseSize/sizeof(WCHAR)))
		{
			//no move needed, file end
		}
		else
		{
			UINT uMoveHead = (UINT)(pSection - pHead); 
            //move memory
		    //also ok
		    //::memmove(pHead + uMoveHead + len, pHead + uMoveHead, m_dwUseSize-uMoveHead*sizeof(WCHAR));
		    for(int i = (int)(m_dwUseSize - sizeof(WCHAR)); 
		       i >= (int)(uMoveHead*sizeof(WCHAR)); i -= sizeof(WCHAR))
			{
				   ::memcpy(pHead + i/sizeof(WCHAR) + len, 
				     pHead + i/sizeof(WCHAR), sizeof(WCHAR));
			}
		}
				
		::memcpy(pSection, buffer, sizeof(WCHAR)*len);
		
		m_dwUseSize += len*sizeof(WCHAR);
		delete buffer;
		buffer = NULL;
		bRet = TRUE;
	}
	else //key exist
	{
		ASSERT(uKey != (UINT)-1);
        LPWSTR pValue = pHead + uKey;
	    while(pValue - pHead < (int)(m_dwUseSize/sizeof(WCHAR)) && 
		    *pValue != WCHAR('\r') && *pValue != WCHAR('\n'))
		{
			pValue++;
		}

	    while(*pValue == WCHAR('\r') || *pValue == WCHAR('\n'))
			pValue++;
		UINT uMoveHead = (UINT)(pValue - pHead);
		
		UINT uPrevLen = uMoveHead - uKey;

		int len = wcslen(lpString);
		len += 2;
		buffer = new WCHAR[len+1];
		::swprintf(buffer, L"%s\r\n", lpString);
        buffer[len] = 0;
		UINT uCurLen = len;
        //move have 2 direction
		if(uCurLen > uPrevLen)
		{
			int charDiff = uCurLen - uPrevLen;
			int begin = (int)(m_dwUseSize - sizeof(WCHAR));
			int end = (int)uMoveHead*sizeof(WCHAR);
			for(int i = begin; i >= end; i -= sizeof(WCHAR))
			{
				::memcpy(pHead + i/sizeof(WCHAR) + charDiff,
					pHead + i/sizeof(WCHAR), sizeof(WCHAR));
			}
		}
		else if(uCurLen < uPrevLen)
		{
			int charDiff = uPrevLen - uCurLen;
			int begin = (int)uMoveHead*sizeof(WCHAR);
			int end = (int)(m_dwUseSize - sizeof(WCHAR));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -