///////////////////////////////////////////////////////////////////
// Ini.cpp
//
// "CIni" is a simple API wrap class used for ini file access.
// The purpose of this class is to make ini file access more
// convenient than direct API calls.
//
// This file is distributed "as is" and without any expressed or implied
// warranties. The author holds no responsibilities for any possible damages
// or loss of data that are caused by use of this file. The user must assume
// the entire risk of using this file.
//
// 7/08/2002 Bin Liu
//
// Update history:
//
// 7/08/2002 -- Initial release.
// 7/14/2002 -- Added "IncreaseInt" and "AppendString"
// 9/02/2002 -- Added "removeProfileSection" and "RemoveProfileEntry"
// 2/09/2003 -- The class has been made unicode-compliant
// 11/04/2003 -- Integrated MFC support, added in new member functions
// for accessing arrays.
// 11/08/2003 -- Fixed "GetString" and "GetPathName" method, changed parameter
// from "LPSTR" to "LPTSTR"
// 11/10/2003 -- Renamed method "GetKeys" to "GetKeyLines",
// Added method "GetKeyNames"
// Added parameter "bTrimString" to method "GetArray"
// 11/14/2003 -- Use "__AFXWIN_H__" instead of "_AFXDLL" to determine MFC presence
// Removed length limit on "m_pszPathName"
// Removed "GetStruct" and "WriteStruct"
// Added "GetDataBlock", "WriteDataBlock", "AppendDataBlock"
// Added "GetChar" and "WriteChar"
//
///////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
// Cini Class Implementation
/////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h" // include if you got "fatal error C1010: unexpected end of file..."
#include "Ini.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#define DEF_PROFILE_NUM_LEN 64 // numeric string length, could be quite long for binary format
#define DEF_PROFILE_THRESHOLD 512 // temporary string length
#define DEF_PROFILE_DELIMITER _T(",") // default string delimiter
#define DEF_PROFILE_TESTSTRING _T("{63788286-AE30-4D6B-95DF-3B451C1C79F9}") // Uuid for internal use
// struct used to be passed to __KeyPairProc as a LPVOID parameter
struct STR_LIMIT
{
LPTSTR lpTarget;
DWORD dwRemain;
DWORD dwTotalCopied;
};
/////////////////////////////////////////////////////////////////////////////////
// Constructors & Destructor
/////////////////////////////////////////////////////////////////////////////////
CIni::CIni()
{
m_pszPathName = NULL;
}
CIni::CIni(LPCTSTR lpPathName)
{
m_pszPathName = NULL;
SetPathName(lpPathName);
}
CIni::~CIni()
{
if (m_pszPathName != NULL)
delete [] m_pszPathName;
}
/////////////////////////////////////////////////////////////////////////////////
// Ini File Path Access
/////////////////////////////////////////////////////////////////////////////////
// Assign ini file path name
void CIni::SetPathName(LPCTSTR lpPathName)
{
if (lpPathName == NULL)
{
if (m_pszPathName != NULL)
*m_pszPathName = _T('\0');
}
else
{
if (m_pszPathName != NULL)
delete [] m_pszPathName;
m_pszPathName = _tcsdup(lpPathName);
}
}
// Retrieve ini file path name
DWORD CIni::GetPathName(LPTSTR lpBuffer, DWORD dwBufSize) const
{
*lpBuffer = _T('\0');
DWORD dwLen = 0;
if (lpBuffer != NULL)
{
_tcsncpy(lpBuffer, m_pszPathName, dwBufSize);
dwLen = _tcslen(lpBuffer);
}
else
{
// just calculate the required buffer size
dwLen = _tcslen(m_pszPathName);
}
return dwLen;
}
#ifdef __AFXWIN_H__
CString CIni::GetPathName() const
{
return CString(m_pszPathName);
}
#endif
/////////////////////////////////////////////////////////////////////////////////
// Raw String Access
/////////////////////////////////////////////////////////////////////////////////
// Get a profile string value, if the buffer size is not large enough, the result
// may be truncated.
DWORD CIni::GetString(LPCTSTR lpSection, LPCTSTR lpKey, LPTSTR lpBuffer, DWORD dwBufSize, LPCTSTR lpDefault) const
{
if (lpBuffer != NULL)
*lpBuffer = _T('\0');
LPTSTR psz = __GetStringDynamic(lpSection, lpKey, lpDefault);
DWORD dwLen = _tcslen(psz);
if (lpBuffer != NULL)
{
_tcsncpy(lpBuffer, psz, dwBufSize);
dwLen = min(dwLen, dwBufSize);
}
delete [] psz;
return dwLen;
}
#ifdef __AFXWIN_H__
CString CIni::GetString(LPCTSTR lpSection, LPCTSTR lpKey, LPCTSTR lpDefault) const
{
LPTSTR psz = __GetStringDynamic(lpSection, lpKey, lpDefault);
CString str(psz);
delete [] psz;
return str;
}
#endif
// Write a string value to the ini file
BOOL CIni::WriteString(LPCTSTR lpSection, LPCTSTR lpKey, LPCTSTR lpValue) const
{
if (lpSection == NULL || lpKey == NULL)
return FALSE;
return ::WritePrivateProfileString(lpSection, lpKey, lpValue == NULL ? _T("") : lpValue, m_pszPathName);
}
// Read a string value from the ini file, append another string after it and then write it
// back to the ini file
BOOL CIni::AppendString(LPCTSTR lpSection, LPCTSTR lpKey, LPCTSTR lpString) const
{
if (lpString == NULL)
return FALSE;
TCHAR* psz = __GetStringDynamic(lpSection, lpKey);
TCHAR* pNewString = new TCHAR[_tcslen(psz) + _tcslen(lpString) + 1];
_stprintf(pNewString, _T("%s%s"), psz, lpString);
const BOOL RES = WriteString(lpSection, lpKey, pNewString);
delete [] pNewString;
delete [] psz;
return RES;
}
/////////////////////////////////////////////////////////////////////////////////
// Ini File String Array Access
/////////////////////////////////////////////////////////////////////////////////
// Get an array of string
DWORD CIni::GetArray(LPCTSTR lpSection, LPCTSTR lpKey, LPTSTR lpBuffer, DWORD dwBufSize, LPCTSTR lpDelimiter, BOOL bTrimString) const
{
if (lpBuffer != NULL)
*lpBuffer = _T('\0');
if (lpSection == NULL || lpKey == NULL)
return 0;
LPTSTR psz = __GetStringDynamic(lpSection, lpKey);
DWORD dwCopied = 0;
if (*psz != _T('\0'))
{
if (lpBuffer == NULL)
{
// just calculate the required buffer size
const DWORD MAX_LEN = _tcslen(psz) + 2;
LPTSTR p = new TCHAR[MAX_LEN + 1];
dwCopied = __StringSplit(psz, p, MAX_LEN, lpDelimiter, bTrimString);
delete [] p;
}
else
{
dwCopied = __StringSplit(psz, lpBuffer, dwBufSize, lpDelimiter, bTrimString);
}
}
delete [] psz;
return dwCopied;
}
#ifdef __AFXWIN_H__
void CIni::GetArray(LPCTSTR lpSection, LPCTSTR lpKey, CStringArray *pArray, LPCTSTR lpDelimiter, BOOL bTrimString) const
{
if (pArray != NULL)
pArray->RemoveAll();
const DWORD LEN = GetArray(lpSection, lpKey, NULL, 0, lpDelimiter);
if (LEN == 0)
return;
LPTSTR psz = new TCHAR[LEN + 3];
GetArray(lpSection, lpKey, psz, LEN + 2, lpDelimiter);
ParseDNTString(psz, __SubStrAdd, (LPVOID)pArray);
delete [] psz;
}
#endif
#ifdef __AFXWIN_H__
BOOL CIni::WriteArray(LPCTSTR lpSection, LPCTSTR lpKey, const CStringArray *pArray, int nWriteCount, LPCTSTR lpDelimiter) const
{
if (pArray == NULL)
return FALSE;
if (nWriteCount < 0)
nWriteCount = pArray->GetSize();
else
nWriteCount = min(nWriteCount, pArray->GetSize());
const CString DELIMITER = (lpDelimiter == NULL || *lpDelimiter == _T('\0')) ? _T(",") : lpDelimiter;
CString sLine;
for (int i = 0; i < nWriteCount; i++)
{
sLine += pArray->GetAt(i);
if (i != nWriteCount - 1)
sLine += DELIMITER;
}
return WriteString(lpSection, lpKey, sLine);
}
#endif
/////////////////////////////////////////////////////////////////////////////////
// Primitive Data Type Access
/////////////////////////////////////////////////////////////////////////////////
// Get a signed integral value
int CIni::GetInt(LPCTSTR lpSection, LPCTSTR lpK
很好的INI文件操作类
4星 · 超过85%的资源 需积分: 0 98 浏览量
2007-08-03
13:41:45
上传
评论
收藏 9KB RAR 举报
WELLTEN
- 粉丝: 95
- 资源: 10
最新资源
- Python闭包:深入理解与应用场景解析.zip
- Win64OpenSSL-3-3-0.exe
- 课高分程设计-基于C++实现的民航飞行与地图简易管理系统-南京航空航天大学
- 航天器遥测数据故障检测系统python源码+文档说明+数据库(课程设计)
- 北京航空航天大学操作系统课设+ppt+实验报告
- 基于Vue+Echarts实现风力发电机中传感器的数据展示监控可视化系统+源代码+文档说明(高分课程设计)
- 基于单片机的风力发电机转速控制源码
- 基于C++实现的风力发电气动平衡监测系统+源代码+测量数据(高分课程设计)
- 毕业设计- 基于STM32F103C8T6 单片机,物联网技术的太阳能发电装置+源代码+文档说明+架构图+界面截图
- 基于 LSTM(长短期记忆)(即改进的循环神经网络)预测风力发电厂中风力涡轮机产生的功率+源代码+文档说明
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论2