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

📄 iniwr.cpp

📁 wince evc ini读写 au1200
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//输出:Returns the number of bytes read.
//日期:2008-07-09
//备注:类封装
///////////////////////////////////////////////////////
DWORD CIniWR::GetString(LPCWSTR lpAppName, LPCWSTR lpKeyName, LPCWSTR lpReturnedString, DWORD nSize, LPCWSTR lpFileName)
{
	//WCHAR szFileName[MAX_PATH];
	DWORD dwSize , cchCopied;
	DWORD dwOffset = 0;
	TCHAR pLine[MAX_PATH] = {0} ;
	DWORD dwValLen = 0;

	if (!lpAppName || !lpFileName)
		return 0;

	//打开文件,将文件数据拷贝到缓冲区g_pData中,返回INI文件大小
	if (0 == (dwSize = ReadIniFile(lpFileName)))
	{
		ERRORMSG(1, (_T("GetString, Could not ReadIniFile INI file: %s\n"), lpFileName));
		return 0;
	}

	cchCopied = 0;
	while ( 0 != (dwOffset = GetLine( pLine , dwOffset , dwSize )))
	{
		//RETAILMSG(1,(_T("%s\n"),szLine));
		// 是不是注释行
		if (IsComment(pLine))
			continue;

		// 是不是段名
		if (IsSection(pLine))
		{
			// 是不是我们要找的段名
			if (IsSectionName(pLine,lpAppName))
			{
				// 寻找我们要的键名
				while ( 0 != (dwOffset = GetLine(pLine , dwOffset , dwSize)))
				{
					LPWSTR pValue=NULL;

					if (IsSection(pLine))
						break;

					if (IsKey(pLine , lpKeyName, &pValue, &dwValLen))
					{
						cchCopied = min(dwValLen, nSize-1);
					//	wcsncpy((wchar_t *)lpReturnedString, pValue, cchCopied);
						wcscpy((wchar_t *)lpReturnedString,pValue);
						//lpReturnedString[cchCopied] = 0;
						lpReturnedString = 0;
						// We're done.
						break;						
					}
				}
				break;
			}
		}
	}

	return cchCopied;
}
///////////////////////////////////////////////////////
//名称:GetPrivateProfileString
//描述:WCE下读取INI文件中某段名/键名的键值的字符串
//输入:
// lpAppName[in]         - points to section name
// lpKeyName[in]         - points to key name
// lpDefault[in]         - points to default string
// lpReturnedString[out] - points to destination buffer
// nSize[in]             - size of destination buffer "lpReturnedString"(in characters)
// lpFileName[in]        - points to initialization filename
//输出:The return value is the number of characters copied to the buffer, 
// not including the terminating null character.		
//日期:2008-07-09
//备注:1). 如果INI文件没有你关心的数据,返回默认值lpDefault
//      2). 作者XZP 类封装 Mercury Xu
///////////////////////////////////////////////////////
DWORD CIniWR::GetPrivateProfileString(LPCWSTR lpAppName, LPCWSTR lpKeyName, LPCWSTR lpDefault, LPWSTR lpReturnedString, DWORD Size, LPCWSTR lpFileName)
{
	DWORD dwRc = 0, dwReturn = 0;
	if(!lpAppName || !lpKeyName || !lpReturnedString || !lpFileName || Size<=0 )
		return 0;

	dwRc = GetString(lpAppName,lpKeyName,lpReturnedString,Size,lpFileName);
	if(dwRc != 0)
	{
		dwReturn = dwRc;
	}
	else
	{
		if(lpDefault)
		{
			wcsncpy(lpReturnedString, lpDefault, Size);
			lpReturnedString[Size-1] = NULL;
		}
		else
			*lpReturnedString = 0;
		dwReturn = wcslen(lpReturnedString);
	}

	// 释放内存
	if (NULL != g_pData)
	{
		free( g_pData );
		g_pData = NULL;
	}

	return dwReturn;
}
///////////////////////////////////////////////////////
//名称:GetPrivateProfileInt
//描述:retrieves an integer associated with a key in the
// specified section of the given initialization file
//输入:
// LPCTSTR lpAppName,  // address of section name
// LPCTSTR lpKeyName,  // address of key name
// INT nDefault,       // return value if key name is not found
// LPCTSTR lpFileName  // address of initialization filename
//输出:
// The return value is the integer equivalent of the string following 
// the specified key name in the specified initialization file. If the 
// key is not found, the return value is the specified default value. 
// If the value of the key is less than zero, the return value is zero. 
//日期:2008-07-09
//备注:作者:XZP 类封装 Mercury Xu
///////////////////////////////////////////////////////
UINT CIniWR::GetPrivateProfileInt(LPCTSTR lpAppName, LPCTSTR lpKeyName, int nDefault, LPCTSTR lpFileName)
{
	WCHAR szRet[80] ={0};

	if(!lpAppName || !lpKeyName || !lpFileName )
		return 0;

	DWORD cch = GetString(lpAppName, lpKeyName, szRet, sizeof(szRet)/sizeof(WCHAR), lpFileName);

	// 释放内存
	if (NULL != g_pData)
	{
		free( g_pData );
		g_pData = NULL;
	}

	if (cch)
		return _wtoi(szRet);
	else
		return nDefault;	
}
///////////////////////////////////////////////////////
//名称:WritePrivateProfileString
//描述:WCE环境下,向指定INI文件指定段名写入字符串数据
//输入:
// lpAppName[in]
// Pointer to a null-terminated string containing section name. If
// the section does not exit, it is created.
// lpKeyName[in]
// Pointer to a null-terminated string containing key name. If the
// key does not exit in the specified section pointed to by the lpAppName 
// parameter, it's created. If this parameter is NULL, the ertire section, 
// including all keys within the section, is deleted. When deleting a 
// section, leave the comments intact.
// lpString[in]
// pointer to a null-terminated string to be written to the file. 
// If this parameter is NULL, the key pointed to by the lpKeyName 
// parameter is deleted.
// lpFileName[in]
// Pointer to a null-terminated string that names the initialization file. 
//输出:
// FALSE - fail
// TRUE  - success
//日期:2008-07-09
//备注:
// 1). 先将要修改的INI文件的全部数据读取到全局内存g_pData中
// 2). 在g_pData中定位到我们要修改的位置,将其它数据和我们修改的数据写入一临时ini文件
// 3). 最后将临时ini文件覆盖原来的ini文件,再删除临时ini文件
// 4). 主要的API函数:
// CreateFile、ReadFile、WriteFile、SetEndOfFile、CopyFile、DeleteFile
// 5). 如果lpKeyName == NULL, 删除整个段, 如果lpString == NULL, 删除健
// 作者:XZP 封装类:Mercury Xu

///////////////////////////////////////////////////////
bool CIniWR::WritePrivateProfileString(LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpString, LPCTSTR lpFileName)
{
	DWORD dwSize, dwOffset;                // dwSize - ini文件大小, dwOffset - 偏移量
	BOOL  bReadLine = TRUE;
	BOOL  bWrote = FALSE;
	TCHAR pszLine[MAX_PATH] = {0};         // 存储一行的数据
	TCHAR pszIniFileTemp[MAX_PATH] = {0};  // 临时ini文件的名称(包括路径)
	HANDLE hOutputFile ;
	LPWSTR pValue;
	DWORD dwValLen;
	LPWSTR pName;

	dwOffset = 0;
	if (!lpFileName)
		return FALSE;

	// 读取INI文件内容到全局变量g_pData内存中
	dwSize = ReadIniFile(lpFileName);

	//RETAILMSG(1, (TEXT("lpFileName=[%s], dwSize=[%d]"), lpFileName, dwSize));

	// Create the output file.
	//wcscpy(pszIniFileTemp, lpFileName);
	pName = _tcsrchr(lpFileName, L'\\');
	if(pName)
	{
		pName++;
		wsprintf(pszIniFileTemp,TEXT("\\Windows\\%s.tmp"),pName);
	}
	else
	{ 
		wsprintf(pszIniFileTemp, TEXT("\\Windows\\%s.ini.tmp"),lpAppName);
	}

	hOutputFile = CreateFile(pszIniFileTemp,
		GENERIC_WRITE,
		0,(LPSECURITY_ATTRIBUTES)0, 
		CREATE_ALWAYS,
		FILE_ATTRIBUTE_NORMAL,
		0);
	if (INVALID_HANDLE_VALUE == hOutputFile)
	{
		ERRORMSG(1, (TEXT("Could not open output file: %s\n"), pszIniFileTemp));
		return FALSE;
	}

	// 将所有数据写入临时ini文件
	for (;;)
	{
		// The bReadLine flag is used to not read a new line after we break
		// out of the inner loop. We've already got a line to process.
		if (bReadLine)
		{
			dwOffset = GetLine( pszLine , dwOffset , dwSize );
			if (!dwOffset)
				break;
		}
		bReadLine = TRUE;
		// Skip past comments.
		if (IsComment(pszLine))
		{
			WriteLine(hOutputFile , pszLine);
			continue;
		}
		// Found a section name.
		if (IsSection(pszLine))
		{
			// It's the section we want.
			if (IsSectionName(pszLine , lpAppName))
			{
				// 以下如果lpKeyName为NULL,删除整个段
				if (lpKeyName)
					WriteLine(hOutputFile , pszLine);

				// Process the whole section.
				while (0 != (dwOffset = GetLine( pszLine , dwOffset , dwSize )))
				{
					// Reached the end of the section.
					if (IsSection(pszLine))
					{
						bReadLine = FALSE;
						// This line will be written in the outer loop.
						break;
					}
					// When deleting a section, leave the comments intact.
					else if (IsComment(pszLine))
					{
						WriteLine(hOutputFile , pszLine);
						continue;
					}
					// Got the value we want.
					if (!bWrote && IsKey(pszLine , lpKeyName, &pValue, &dwValLen))
					{
						bWrote = TRUE;
						// 如果lpString为NULL,删除健lpKeyName
						if(lpString)
							WriteValue(hOutputFile , NULL, lpKeyName, lpString);
					}
					else
					{
						if (lpKeyName)
							WriteLine(hOutputFile , pszLine);
					}

					if(dwOffset >= dwSize)
						break ;
				}

				// 如果在段名lpAppName下键名lpKeyName不存在,则新建键名lpKeyName和键值lpString
				if (!bWrote)
				{
					bWrote = TRUE;
					WriteValue(hOutputFile, NULL, lpKeyName, lpString);
				}
			}
			else
				WriteLine(hOutputFile , pszLine);
		}
		else
			WriteLine(hOutputFile , pszLine);

		if(dwOffset ==0) 
			break;
	}

	// 如果指定的段名lpAppName不存在,则新建段名lpAppName及键名lpKeyName和键值lpString
	if (!bWrote && lpKeyName && lpString)
	{
		WriteValue(hOutputFile , lpAppName, lpKeyName, lpString);
	}

	// 用临时ini文件覆盖原来的ini文件并删除临时ini文件
	if (INVALID_HANDLE_VALUE != hOutputFile)
	{
		SetEndOfFile(hOutputFile );
		CloseHandle(hOutputFile);
		CopyFile(pszIniFileTemp, lpFileName, FALSE);
		DeleteFile(pszIniFileTemp);
	}

	// 释放ReadIniFile函数的全局内存
	if (NULL != g_pData)
	{
		free( g_pData ) ;
		g_pData = NULL ;
	}

	return TRUE;
}

/************************************************************************
**函数:WriteLine
**功能:向文件写入一行数据(包括回车换行符)
**参数:
hOutput[in] - 已打开的文件句柄
pLine[in]   - 要写入的一行数据
**返回:NONE
**作者:XZP
**类封装:Mercury Xu
**日期:08.1.2
**修改:2008-7-9
**备注:
1). 写入一行数据,也包括写入行末的"\r\n"两个字符
2). 注意区分是不是UNICODE版本的INI文件,如果不是,要将WCHAR转为char再写入INI文件
3). 注意不要将结束符也写入文件
************************************************************************/

void CIniWR::WriteLine(HANDLE hOutput, LPCWSTR pLine)
{
	DWORD dwWrote = 0;
	WCHAR wBuffer[MAX_PATH] = {0};
	char buffer[MAX_PATH] ={0};
	DWORD dwlen = wcslen(pLine) ;
	BOOL bUnicode = FALSE ;

	if(bUnicode)
	{
		if (pLine)
		{
			WriteFile(hOutput, pLine, wcslen(pLine)*sizeof(WCHAR), &dwWrote, NULL);
			WriteFile(hOutput, L"\r\n", 2*sizeof(WCHAR), &dwWrote, NULL);
		}
	}
	else
	{
		if (pLine)
		{
			int bsize ;
			TCHAR szTempLine[MAX_PATH] = {0} ;
			wcsncpy( szTempLine, pLine, dwlen);

			bsize=WideCharToMultiByte(CP_ACP,0,szTempLine,-1,NULL,0,NULL,NULL);
			WideCharToMultiByte(CP_ACP,0,szTempLine,-1,buffer, bsize,NULL,NULL);
			buffer[bsize] = 0 ;

			WriteFile(hOutput, buffer, bsize-1 , &dwWrote, NULL); // 注意不要将结束符也写入
			WriteFile(hOutput, "\r\n", 2, &dwWrote, NULL);
		}
	}
}
/************************************************************************
**函数:WritePrivateProfileInt
**功能:WCE环境下,向指定INI文件指定段名写入整型数据
**参数:参考WritePrivateProfileString函数
**返回:
FALSE - fail
TRUE  - success
**备注:
************************************************************************/
bool CIniWR::WritePrivateProfileInt(LPCTSTR lpAppName, LPCTSTR lpKeyName, int Value, LPCTSTR lpFileName)
{
	TCHAR ValBuf[16]={0};    

	wsprintf( ValBuf, TEXT( "%i" ), Value);    
	return( WritePrivateProfileString(lpAppName, lpKeyName, ValBuf, lpFileName) ); 
}

⌨️ 快捷键说明

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