文章标题

Profile.h
// TextFile.h: interface for the CTextFile class.
//
//

if !defined(AFX_TEXTFILE_H__0B34BBD2_9145_4797_9DD1_BF7A9B8B2151__INCLUDED_)

define AFX_TEXTFILE_H__0B34BBD2_9145_4797_9DD1_BF7A9B8B2151__INCLUDED_

if _MSC_VER > 1000

pragma once

endif // _MSC_VER > 1000

include “IniFile.h”

class CGlobalInterface
{
protected:
static CString m_strSdScanDirectory;
static CString m_strLanguage;

public:
static void SetSdScanDirectory(CString szDirectory);
static CString GetSdScanDirectory();
static void SetLanguage(CString szLanguage);
static CString GetLanguage();

public:
static CString m_strEcuId;

/*
*  
*/
static TCHAR * PF_GetWordStr(const char *cStr);
/*
* CString to string
*/
static char * PF_GetCharStr(const TCHAR *wStr);

};

class CProfile
{
public:
static CString GetString(CString sFile,CString sSect,CString sKey
,CString sDftValue,BOOL bCaseSensitive=TRUE);
static INT GetInteger(CString sFile,CString sSect,CString sKey
,INT nDftValue,BOOL bCaseSensitive=TRUE);
static BOOL WriteString(CString sFile,CString sSect,CString sKey
,CString sValue,BOOL bCaseSensitive=TRUE);
static BOOL WriteInteger(CString sFile,CString sSect,CString sKey
,INT nValue,BOOL bCaseSensitive=TRUE);

private:
static CIniFile m_iniF;
static CString m_sLastFile;
};

endif // !defined(AFX_TEXTFILE_H__0B34BBD2_9145_4797_9DD1_BF7A9B8B2151__INCLUDED_)

Profile.cpp

include “stdafx.h”

include “Profile.h”

ifdef _DEBUG

undef THIS_FILE

static char THIS_FILE[]=FILE;

define new DEBUG_NEW

endif

CString CGlobalInterface::m_strSdScanDirectory;
CString CGlobalInterface::m_strLanguage;

define PLATFORM_MAX_BUFFER 4096

TCHAR g_WordBuf[PLATFORM_MAX_BUFFER];
char g_CharBuf[PLATFORM_MAX_BUFFER];

TCHAR * CGlobalInterface::PF_GetWordStr(const char *cStr)
{
int wlen;

wlen=MultiByteToWideChar(CP_ACP,0,cStr,strlen(cStr), (LPWSTR)g_WordBuf,PLATFORM_MAX_BUFFER);
g_WordBuf[wlen]=_T('\0');

return g_WordBuf;

}

char * CGlobalInterface::PF_GetCharStr(const TCHAR *wStr)
{
int len;

len=WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)wStr, wcslen((const wchar_t *)wStr), g_CharBuf, PLATFORM_MAX_BUFFER, NULL, NULL);
g_CharBuf[len]='\0';

return g_CharBuf;

}

/*—————————————————————————–
功 能:设置显示程序工作目录
参数说明:CString pDirectory-指向工作目录的指针
返 回 值:无
说 明:无
—————————————————————————–*/
void CGlobalInterface::SetSdScanDirectory(CString pDirectory)
{
m_strSdScanDirectory = pDirectory;
}

/*—————————————————————————–
功 能:取得显示程序工作目录
参数说明:无
返 回 值:工作目录绝对路径名
说 明:无
—————————————————————————–*/
CString CGlobalInterface::GetSdScanDirectory()
{
return m_strSdScanDirectory;
}

/*—————————————————————————–
功 能:设置语言代码
参数说明:CString pLanguage-指向设置语言代码字符串的指针
返 回 值:无
说 明:无
—————————————————————————–*/
void CGlobalInterface::SetLanguage(CString pLanguage)
{
m_strLanguage = pLanguage;
}

/*—————————————————————————–
功 能:取得语言代码
参数说明:无
返 回 值:语言代码字符串
说 明:无
—————————————————————————–*/
CString CGlobalInterface::GetLanguage()
{
return m_strLanguage;
}

/static /CIniFile CProfile::m_iniF;
/static /CString CProfile::m_sLastFile(SZ_EMPTY);

/*—————————————————————————–
功 能:读取配置串
参数说明:CString sFile 配置文件名,
CString sSect 段名,
CString sKey 键值,
CString sDftValue 缺省串
返 回 值:读入的字符串
说 明:无
—————————————————————————–*/
/static /
CString CProfile::GetString(CString sFile,CString sSect,CString sKey
,CString sDftValue,BOOL bCaseSensitive/=TRUE/)
{
// sanity check
if(sFile.IsEmpty()||sSect.IsEmpty()||sKey.IsEmpty())
{
return sDftValue;
}

if(sFile.CompareNoCase(m_sLastFile)!=0)
{
    if(m_iniF.ReadFile(CGlobalInterface::PF_GetCharStr(sFile),bCaseSensitive))
    {
        m_sLastFile = sFile;
    }
    else
    {
        return sDftValue;
    }
}

string s_sect = CGlobalInterface::PF_GetCharStr(sSect);
string s_key = CGlobalInterface::PF_GetCharStr(sKey);
string s_dft_v = CGlobalInterface::PF_GetCharStr(sDftValue);
LPCSTR lpStrRet = m_iniF.GetValue(s_sect.c_str(),s_key.c_str(),s_dft_v.c_str(),bCaseSensitive);
return (CString)lpStrRet;

}

/*—————————————————————————–
功 能:读取配置数
参数说明:CString sFile 配置文件名,
CString sSect 段名,
CString sKey 键值,
INT iDefaultValue 缺省值
返 回 值:读入的数值
说 明:无
—————————————————————————–*/
/static /
INT CProfile::GetInteger(CString sFile,CString sSect,CString sKey,INT nDftValue
,BOOL bCaseSensitive/=TRUE/)
{
CString s_dft_value;
s_dft_value.Format(_T(“%d”),nDftValue);

CString s_value = GetString(sFile,sSect,sKey,s_dft_value,bCaseSensitive);

return atoi(CGlobalInterface::PF_GetCharStr(s_value));

}

/*—————————————————————————–
功 能:写入配置串
参数说明:CString sFile 配置文件名,
CString sSect 段名,
CString sKey 键值,
CString sValue 写入串
返 回 值:成功–TRUE 失败–FALSE
说 明:无
—————————————————————————–*/
/static /
BOOL CProfile::WriteString(CString sFile,CString sSect,CString sKey
,CString sValue,BOOL bCaseSensitive/=TRUE/)
{
// sanity check
if(sFile.IsEmpty()) return FALSE;
if(sSect.IsEmpty()) return FALSE;
if(sKey.IsEmpty()) return FALSE;

if(m_iniF.ReadFile(CGlobalInterface::PF_GetCharStr(sFile),bCaseSensitive))
{
    m_sLastFile = sFile;
}
else
{
    return FALSE;
}

// always create new if section/key's not existed
string s_sect = CGlobalInterface::PF_GetCharStr(sSect);
string s_key = CGlobalInterface::PF_GetCharStr(sKey);
string s_v = CGlobalInterface::PF_GetCharStr(sValue);
BOOL bRet = m_iniF.SetValue(s_sect.c_str(),s_key.c_str(),s_v.c_str(),TRUE,bCaseSensitive);
if (!bRet)
{
    return bRet;
}
// have to write to file every time
bRet = m_iniF.WriteFile(CGlobalInterface::PF_GetCharStr(m_sLastFile));
if (!bRet)
{
    return bRet;
}   

return TRUE;

}

/*—————————————————————————–
功 能:写入配置数
参数说明:CString sFile 配置文件名,
CString sSect 段名,
CString sKey 键值,
INT nValue 写入值
返 回 值:成功–TRUE 失败–FALSE
说 明:无
—————————————————————————–*/
/static /
BOOL CProfile::WriteInteger(CString sFile,CString sSect,CString sKey,INT nValue
,BOOL bCaseSensitive/=TRUE/)
{
CString s_value;
s_value.Format(_T(“%d”),nValue);

return WriteString(sFile,sSect,sKey,s_value,bCaseSensitive);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值