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

📄 jxinifile.cpp

📁 minica2的第2个版本
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	ASSERT(!m_bSectionMapDirty);

#ifdef _UNICODE
	DWORD dwRetSize;
	LPWSTR pTemp = _A2W((LPSTR)lpAppName, -1, dwRetSize);
    if(0 == m_mapSection.Lookup((LPCWSTR)pTemp, (LPVOID&)pSecHead))
		return 0;
#else
	if(0 == m_mapSection.Lookup(lpAppName, (LPVOID&)pSecHead))
		return 0;	
#endif

	LPSTR pSecTail = NULL; //point to next [
	LPSTR p1 = pSecHead;
	LPSTR p2;
	        
	BYTE wNULL = 0x00; 
	while(p1- pHead < (int)(m_dwUseSize/sizeof(char)) - 1)
	{
		p1++;
		if(*p1 == char('['))
		{
			pSecTail = p1;
			break;
		}
	}
	if(pSecHead == NULL) return -1;
	if(pSecTail == NULL)
		pSecTail = pHead + m_dwUseSize/sizeof(char);
	pSecTail--;
    
	//Enum Keys in Section
    p1 = pSecHead;
	int keyLen;
	while(p1 < pSecTail - 1)
	{
		while(p1 < pSecTail - 1 &&
			*p1 != char('\r') && *p1 != char('\n'))
			p1++;
		while(p1 < pSecTail - 1 &&
			(*p1 == char('\r') || *p1 == char('\n')))
			p1++;
		p2 = p1;
		while(p2 < pSecTail - 1 &&
			*p2 != char('='))
			p2++;
		if(p1 < pSecTail - 1 && p2 < pSecTail - 1)
		{			
			//Found A Key
			keyLen = p2 - p1;

//			CString str;
//			::lstrcpyn(str.GetBuffer(keyLen), p1, keyLen);
//			str.ReleaseBuffer(keyLen);
//			PopMsg(_T("Used %d KeyLen %d, %s"), posRet, keyLen, str);
            
			if(keyLen >= 1)
			{
				if(keyLen + 1 + posRet > (int)nSize) 
				{
					//PopMsg(_T("You Want Fetch Sections, Buffer %d, Keylen %d, Used %d, in %s"),
					//	nSize, keyLen, posRet, m_strFilename);
					return 0; //nSize - 1;
				}
				::strncpy(lpReturnedString + posRet, p1, keyLen);
                posRet += keyLen;
				//NULL = 0x00 , 1 Byte
				::memcpy(lpReturnedString + posRet, &wNULL, 1);
                posRet += 1;
			}			
		}
		p1 = p2 + 1;
	}
	//last NULL 
	//PopMsg(_T("%d"), posRet);
    if(1 + posRet > (int)nSize) return 0; //nSize - 1; 
    ::memcpy(lpReturnedString + posRet, &wNULL, 1);
	posRet += 1;
	return posRet;
}

//User Query All Keys in a Given Section
DWORD CJXINIFile::GetKeysExW(
		 LPCWSTR lpAppName,        // section name
         LPWSTR lpReturnedString,  // destination buffer
         DWORD nSize,              // size of destination buffer
         LPCWSTR lpFileName        // initialization file name
    )
{
	int posRet = 0;
	int secLen = ::wcslen(lpAppName);
	LPWSTR pHead = (LPWSTR)m_pTrueData;
	
	LPWSTR pSecHead = NULL; //pointing to [
	//Section Map Must be Available
	ASSERT(!m_bSectionMapDirty);
#ifdef _UNICODE
	if(0 == m_mapSection.Lookup(lpAppName, (LPVOID&)pSecHead))
		return 0;
#else
	DWORD dwRetSize;
	PSTR pTemp = _W2A((LPWSTR)lpAppName, -1, dwRetSize);
    if(0 == m_mapSection.Lookup((LPCSTR)pTemp, (LPVOID&)pSecHead))
		return 0;
#endif
	LPWSTR pSecTail = NULL; //point to next [
	LPWSTR p1 = pSecHead;
	LPWSTR p2;
	        
	WORD wNULL = 0x0000; 
	while(p1- pHead < (int)(m_dwUseSize/sizeof(WCHAR)) - 1)
	{
		p1++;
		if(*p1 == WCHAR('['))
		{
			pSecTail = p1;
			break;
		}
	}
	if(pSecHead == NULL) return -1;
	if(pSecTail == NULL)
		pSecTail = pHead + m_dwUseSize/sizeof(WCHAR);
	pSecTail--;
    
	//Enum Keys in Section
    p1 = pSecHead;
	int keyLen;
	while(p1 < pSecTail - 1)
	{
		while(p1 < pSecTail - 1 &&
			*p1 != WCHAR('\r') && *p1 != WCHAR('\n'))
			p1++;
		while(p1 < pSecTail - 1 &&
			(*p1 == WCHAR('\r') || *p1 == WCHAR('\n')))
			p1++;
		p2 = p1;
		while(p2 < pSecTail - 1 &&
			*p2 != WCHAR('='))
			p2++;
		if(p1 < pSecTail - 1 && p2 < pSecTail - 1)
		{
			
			//Found A Key
			keyLen = p2 - p1;

//			CString str;
//			::lstrcpyn(str.GetBuffer(keyLen), p1, keyLen);
//			str.ReleaseBuffer(keyLen);
//			PopMsg(_T("Used %d KeyLen %d, %s"), posRet, keyLen, str);
            
			if(keyLen >= 1)
			{
				if(keyLen + 1 + posRet > (int)nSize) 
				{
					//PopMsg(_T("You Want Fetch Sections, Buffer %d, Keylen %d, Used %d, in %s"),
					//	nSize, keyLen, posRet, m_strFilename);
					return 0; //nSize - 1;
				}
				::wcsncpy(lpReturnedString + posRet, p1, keyLen);
                posRet += keyLen;
				//NULL = 0x00 00, 2 Byte
				::memcpy(lpReturnedString + posRet, &wNULL, 2);
                posRet += 1;
			}			
		}
		p1 = p2 + 1;
	}
	//last NULL 
	//PopMsg(_T("%d"), posRet);
    if(1 + posRet > (int)nSize) return 0; //nSize - 1; 
    ::memcpy(lpReturnedString + posRet, &wNULL, 2);
	posRet += 1;
	return posRet;
}

// return 0 -- no key, size too small to put value
//return -1  -- error
//return string length (of course > 1)
DWORD CJXINIFile::GetPrivateProfileStringW(
         LPCWSTR lpAppName,        // section name
         LPCWSTR lpKeyName,        // key name
         LPCWSTR lpDefault,        // default string
         LPWSTR lpReturnedString,  // destination buffer
         DWORD nSize,              
		 // size of destination buffer,Specifies the size, in TCHARs, 
         LPCWSTR lpFileName        // initialization file name
    )
{
	BOOL bRet = LoadIniFile(lpFileName, 2);
	if(!bRet) return (UINT)-1;
	if(lpDefault == NULL) return -1;
	if(m_dwUseSize == 0) return -1;
	if(lpAppName == NULL)
		return GetSectionsW(lpReturnedString, nSize, lpFileName);
	if(lpKeyName == NULL)
		return GetKeysW(lpAppName, lpReturnedString, nSize, lpFileName);

	LPWSTR pHead = (LPWSTR)m_pTrueData;
	UINT uSec, uKey;
	uSec = (UINT) -1;
	uKey = (UINT)-1;
	uKey = GetKeyW(pHead, (UINT)m_dwUseSize, 
		lpAppName, lpKeyName, uSec);
    if(uKey == (UINT)-1) //not key
	{
		if(::wcslen(lpDefault) > (int)nSize) return -1;
		::wcsncpy(lpReturnedString, lpDefault, ::wcslen(lpDefault));
		//append a null
        lpReturnedString[::wcslen(lpDefault)+1] = WCHAR('\0');
		return 0;
	}
	WCHAR buffer[MAX_PATH];

    LPWSTR pValue = pHead + uKey;
	while(pValue - pHead < (int)((m_dwUseSize)/sizeof(WCHAR)) && 
		 *pValue != WCHAR('\r') && *pValue != WCHAR('\n'))
	{
		pValue++;  //in unicode, +2 byte
	}

	int len = pValue - pHead - uKey;
	pValue = pHead + uKey;
	::wcsncpy(buffer, pValue, len); 
	if(nSize <= (UINT)len)
	{
		::wcsncpy(lpReturnedString, pValue, nSize);
        lpReturnedString[nSize - 1] = WCHAR('\0');
		return 0;
	}
	else
	{
		::wcsncpy(lpReturnedString, pValue, len);
		//append null
        lpReturnedString[len] = WCHAR('\0');
		return len;			
	}
	return (UINT) -1;
}

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

	if(lpKeyName == NULL)
		return DeleteSectionExA(lpAppName);
	else if(lpString == NULL)
		return this->DeleteKeyExA(lpAppName, lpKeyName);

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

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

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

		delete []buffer;
		buffer = NULL;
        
		CString strSection;
#ifdef _UNICODE
		DWORD dwRetSize;
		LPWSTR pTemp = _A2W((LPSTR)lpAppName, -1, dwRetSize);
        ::wcsncpy(strSection.GetBuffer(dwRetSize), pTemp, dwRetSize);
		strSection.ReleaseBuffer(dwRetSize);
#else
		strSection = lpAppName;		
#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
        //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
		//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
        LPSTR pSection = pHead + uSec + 1;
		while(pSection - pHead < (int)(m_dwUseSize/sizeof(char)) && 
	    	 *pSection != char('['))
		{
			pSection++;
		}
//		::swprintf(buffer, L"%s=%s\r\n", lpKeyName, lpString);
//		int len = ::wcslen(buffer);

		int len = strlen(lpKeyName) + strlen(lpString);
		len += 3;
		buffer = new char[len+1];
		::sprintf(buffer, "%s=%s\r\n", lpKeyName, lpString);
		buffer[len] = 0;
    	if(pSection - pHead == (int)(m_dwUseSize/sizeof(char)))
		{
			//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(char)); 
		       i >= (int)(uMoveHead*sizeof(char)); i -= sizeof(char))
			{
				   ::memcpy(pHead + i/sizeof(char) + len, 
				     pHead + i/sizeof(char), sizeof(char));
			}
			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((LPSTR)lpPos - pSection > 0)
					{
						LPWSTR pTemp = (LPWSTR)lpPos;
						pTemp += len;
						lpPos = (LPVOID)pTemp;
						m_mapSection.SetAt(strKey, lpPos);
					}
				}
			}
		}
		::memcpy(pSection, buffer, sizeof(char)*len);
		m_dwUseSize += len*sizeof(char);
		delete buffer;
		buffer = NULL;
		bRet = TRUE;
	}
	else //key exist
	{
		ASSERT(uKey != (UINT)-1);
        LPSTR pValue = pHead + uKey;
	    while(pValue - pHead < (int)(m_dwUseSize/sizeof(char)) && 
		    *pValue != char('\r') && *pValue != char('\n'))
		{
			pValue++;
		}

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

		int len = strlen(lpString);
		len += 2;
		buffer = new char[len+1];
		::sprintf(buffer, "%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(char));
			int end = (int)uMoveHead*sizeof(char);
			for(int i = begin; i >= end; i -= sizeof(char))
			{
				::memcpy(pHead + i/sizeof(char) + charDiff,
					pHead + i/sizeof(char), sizeof(char));
			}
			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((LPSTR)lpPos - pHead > (int)uMoveHead)
					{
						LPSTR pTemp = (LPSTR)lpPos;
						pTemp += charDiff;
						lpPos = (LPVOID)pTemp;
						m_mapSection.SetAt(strKey, lpPos);
					}
				}
			}
		}
		else if(uCurLen < uPrevLen)
		{
			int charDiff = uPrevLen - uCurLen;
			int begin = (int)uMoveHead*sizeof(char);
			int end = (int)(m_dwUseSize - sizeof(char));
			for(int i = begin; i <= end;  i+=sizeof(char))
			{
				::memcpy(pHead + i/sizeof(char) - charDiff,
					pHead + i/sizeof(char), sizeof(char));
			}
			if(m_bSectionMapLock)
				m_bSectionMapDirty = TRUE;
			else //update section map
			{
				POSITION pos = m_mapSection.GetStartPosition();

⌨️ 快捷键说明

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