天下

记录修行的印记

IniFileGetValue,IniFileSetValue

static CString GetAppDirectory()
{
    CString sPath;
    GetModuleFileName(NULL,sPath.GetBuffer(MAX_PATH+1),MAX_PATH);
    //GetCurrentDirectory(MAX_PATH,sPath.GetBuffer(MAX_PATH+1));

    sPath.ReleaseBuffer ();
    int nPos;
    nPos=sPath.ReverseFind ('\\');
    sPath=sPath.Left(nPos+1);
    return sPath;
}

static CString IniFileGetValue(const CString& strFileName,const CString& strKeyPath,const CString& strDefault="")
{
    CStringArray strKeys;
    CString strValue;
    int last_pos = 0;
    int pos;
    int new_pos;
    CString strKey;
    while (1)
    {
        pos = strKeyPath.Find("/",last_pos);
        new_pos = pos;
        if (pos==-1)
            new_pos = strKeyPath.GetLength();
        strKey   = strKeyPath.Mid(last_pos,new_pos - last_pos);
        last_pos = pos+1;
        if (!strKey.IsEmpty())
            strKeys.Add(strKey);
        if (pos==-1)
            break;
    }
    if (strKeys.GetSize()!=2)
        return strDefault;
    GetPrivateProfileString(strKeys[0],strKeys[1],0,strValue.GetBuffer(4096),4095,strFileName);
    strValue.ReleaseBuffer();
    if (strValue.IsEmpty())
        return strDefault;
    return strValue;
}

static BOOL IniFileSetValue(const CString& strFileName,const CString& strKeyPath,const CString& strValue)
{
    CStringArray strKeys;
    int last_pos = 0;
    int pos;
    int new_pos;
    CString strKey;
    while (1)
    {
        pos = strKeyPath.Find("/",last_pos);
        new_pos = pos;
        if (pos==-1)
            new_pos = strKeyPath.GetLength();
        strKey   = strKeyPath.Mid(last_pos,new_pos - last_pos);
        last_pos = pos+1;
        if (!strKey.IsEmpty())
            strKeys.Add(strKey);
        if (pos==-1)
            break;
    }
    if (strKeys.GetSize()!=2)
        return FALSE;
    
    return WritePrivateProfileString(strKeys[0],strKeys[1],strValue,strFileName);
}

posted on 2015-07-10 17:25 天下 阅读(349) 评论(0)  编辑 收藏 引用 所属分类: Win32


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2011年3月>
272812345
6789101112
13141516171819
20212223242526
272829303112
3456789

导航

统计

常用链接

留言簿(4)

随笔分类(378)

随笔档案(329)

链接

最新随笔

搜索

最新评论