/********************************************************************
created: 2009/10/10
filename: RdWrIni.cpp,RdWrIni.h
author: YRC
purpose: Read and Write Ini file
*********************************************************************/
#include "RdWrIni.h"
LPSTR g_pData = NULL; // 存储整个INI文件数据的缓冲区
/************************************************************************
**函数:CopyFile
**功能:The CopyFile function copies an existing file to a new file.
**参数:
lpExistingFileName[in] -Pointer to a null-terminated string
that specifies the name of an existing file.
lpNewFileName[in] - Pointer to a null-terminated string that specifies the name of the new file.
bFailIfExists[in] - Specifies how this operation is to proceed if a file of the same name as that
specified by lpNewFileName already exists.
If this parameter is TRUE and the new file already exists, the function fails.
If this parameter is FALSE and the new file already exists,
the function overwrites the existing file and succeeds.
**返回:
TRUE - success
FALSE - fail
**作者:YRC
**日期:09.10.20
**备注:
************************************************************************/
BOOL CopyFile(LPCSTR lpExistingFileName, // name of an existing file
LPCSTR lpNewFileName, // name of new file
BOOL bFailIfExists // operation if file exists
)
{
INT fdIn = 0, fdOut = 0;
struct stat statinfo;
DWORD dwSize =0;
void *pSrc =NULL, *pDst = NULL;
memset(&statinfo,0, sizeof(statinfo));
if (!lpExistingFileName || !lpExistingFileName)
{
return FALSE;
}
fdIn = open(lpExistingFileName, O_RDONLY);
if (fdIn<0)
{
perror("CopyFile open");
return FALSE;
}
// get file info
if (fstat(fdIn, &statinfo) < 0)
{
perror("CopyFile fstat ");
}
dwSize = statinfo.st_size;
//printf("statinfo.st_size = %d\n ", statinfo.st_size);
if (bFailIfExists)
{
fdOut = open(lpNewFileName, O_RDWR | O_CREAT | O_EXCL,S_IRWXU);
if (fdOut== -1)
{
perror("CopyFile new file fail");
return FALSE;
}
}
else
{
fdOut = open(lpNewFileName, O_RDWR | O_CREAT |O_TRUNC,S_IRWXU);
if (fdOut== -1)
{
perror("CopyFile new file fail ");
return FALSE;
}
}
//printf("CopyFile lseek\n");
if (lseek(fdOut, dwSize - 1, SEEK_SET) == -1)
{
perror("CopyFile lseek ");
return FALSE;
}
if (write(fdOut, "", 1) != 1)
{
perror("the file isn't writable ");
return FALSE;
}
//printf("CopyFile write\n");
if ((pSrc = mmap(NULL, dwSize, PROT_READ, MAP_SHARED, fdIn, 0)) == MAP_FAILED)
{
perror("mmap ");
return FALSE;
}
//printf("CopyFile mmap 1\n");
if ((pDst = mmap(NULL, dwSize, PROT_READ|PROT_WRITE, MAP_SHARED, fdOut, 0)) == MAP_FAILED)
{
perror("mmap ");
return FALSE;
}
//printf("CopyFile mmap 2\n");
//dose copy the file
memcpy(pDst,pSrc, dwSize);
close(fdOut);
close(fdIn);
// if (pSrc)
// {
// free(pSrc);
// }
// if (pDst)
// {
// free(pDst);
// }
// printf("CopyFile exit\n");
return TRUE;
}
/************************************************************************
**函数:GetLine
**功能:获取在g_pData中从dwOffset位置开始的一行数据并保存到pLine,同时把偏移量dwOffset
移到下一行行首
**参数:
pLine[out] - 接收一行数据(不包括\r\n)
dwOffset[in] - 要读取的那一行的开始位置
dwSize[in] - INI文件大小
**返回:
正确 - 下一行行首的位置
错误 - 0
**作者:YRC
**日期:09.10.19
**备注:
************************************************************************/
INT GetLine(LPSTR pLine, DWORD dwOffset, DWORD dwSize)
{
int len = 0;
int len2 = 0;
// Look for the end of the line.
while ( dwOffset + len < dwSize
&& '\r' != g_pData[dwOffset+len] && '\n' != g_pData[dwOffset+len])
{
if( g_pData[dwOffset+len]==0 )
break;
pLine[len] = g_pData[dwOffset+len] ;
++len;
}
pLine[len] = 0 ;
// Now push the internal offset past the newline.
// (We assume \r\n pairs are always in this order)
if (dwOffset + len + len2 < dwSize && '\r' == g_pData[dwOffset+len+len2])
++len2;
if (dwOffset + len + len2+1 < dwSize && '\n' == g_pData[dwOffset+len+len2])
++len2;
if (2 >= len + len2 && (dwOffset +2 >=dwSize) )
return 0;
dwOffset += len + len2;
return dwOffset;
}
/************************************************************************
**函数:IsComment
**功能:判断是不是注释行
**参数:
pLine[in] - INI的一行数据
**返回:
1 - 注释行
0 - 不是注释行
**备注:
1). 空行也视为注释行
************************************************************************/
BOOL IsComment(LPCSTR pLine)
{
if (!pLine || 0 == strlen(pLine) || ';' == *pLine)
return TRUE;
return FALSE;
}
/************************************************************************
**函数:IsSection
**功能:判断是不是段名
**参数:
pLine[in] - INI的一行数据
**返回:
1 - 是段名
0 - 不是
************************************************************************/
BOOL IsSection(LPCSTR pLine)
{
if (pLine && '[' == *pLine)
return TRUE;
return FALSE;
}
/************************************************************************
**函数:IsSectionName
**功能:判断是INI文件的一行(pLine)是不是要找的段名(pSection)
**参数:
pLine[in] - INI文件的一行数据
pSection[in] - 要找的段名
**返回:
1 - 是
0 - 不是
**备注:
************************************************************************/
BOOL IsSectionName(LPCSTR pLine, LPCSTR pSection)
{
if (IsSection(pLine))
{
DWORD len = strlen(pSection);
if (strlen(pLine) - 2 == len && 0 == strncasecmp(pLine+1, pSection, len))
return TRUE;
}
return FALSE;
}
/************************************************************************
**函数:IsKey
**功能:判断INI文件中一行的数据是不是要找的键名,如果是并读取键值
**参数:
pLine[in] - INI文件某行数据
pKeyName[in] - 要寻找的键名
pValue[out] - 键值
dwValLen[out] - 键值pValue大小(in bytes)
**返回:
1 - 是,同时pValue返回键值
0 - 不是,pValue为NULL
**作者:YRC
**日期:09.10.19
**备注:
************************************************************************/
BOOL IsKey(LPSTR pLine , LPCSTR pKeyName, LPSTR* pValue, DWORD* dwValLen )
{
LPSTR pEqual = NULL;
DWORD length = 0, len = 0;
if(!pLine || !pKeyName)
return FALSE;
// pLine是不是注释行
if (IsComment( pLine ))
return FALSE;
// 寻找"="号
pEqual = strchr(pLine, '=' );
if (!pEqual)
return FALSE;
// 寻找键名最后一字符的位置
//while (pEqual - 1 >= pLine && iswspace(*(pEqual-1)))
while(pEqual - 1 >= pLine && ((*(pEqual-1)) == ""))
--pEqual;
// Badly formed file.
if (pEqual - 1 < pLine)
return FALSE;
// 键名长度
length = pEqual - pLine;
len = strlen(pKeyName);
//if (len == length && 0 == _wcsnicmp(pLine, pKeyName, len))
if ( 0 == strncasecmp(pLine, pKeyName, len))
{
*pValue = strchr(pLine, '=' );
++(*pValue);
*dwValLen = strlen(pLine) - ((*pValue) - pLine);
// 去掉紧跟在"="号后的所有空格
//while (0 < *dwValLen && iswspace(**pValue))
while (0 < *dwValLen && ((**pValue) == ""))
{
++(*pValue);
--(*dwValLen);
}
while (0 < *dwV

yinrn815
- 粉丝: 0
最新资源
- 基于嵌入式系统的LCD电子时钟方案设计书.doc
- (源码)基于Python和Flask框架的学习经验分享平台.zip
- 全国PLC方案设计书师大学本科方案设计书2.doc
- 计算机应用技术对企业信息化的影响探究.docx
- 《C语言程序设计方案》练习题.doc
- 交通信号控制双向协调调试技术探讨.doc
- -单片机数字电子钟设计方案与研究报告-.doc
- 国家大学科技园徐州软件基地#楼工程量清单和招标控制价的编制毕业设计论文终稿.doc
- 互联网+社区养老模式构建对策.docx
- 操作系统学习课程设计方案指导书-.doc
- 分层教学法在中职计算机课程教学中的应用.docx
- 基于VB图书管理系统大学本科方案设计书.doc
- 试论网络思想政治教育的可能性分析.docx
- (源码)基于C++和SFML库的Tetris Clone游戏项目.zip
- 大学生应用计算机进行冶金相关软件开发的探究.docx
- 8.无线网络安全破解与防御.ppt
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



- 1
- 2
- 3
- 4
- 5
- 6
前往页