INI file read/write wrapper class

Source: Internet
Author: User
Tags ini wrapper


#ifndef _profile_h_
#define _profile_h_


/**
* Read and write INI file classes
*/
Class CProfile
{
Public
CProfile ();
~cprofile ();


Set INI file name
VOID setprofile (LPCTSTR szininame);


Public
A string that holds the INI file name
TCHAR M_szininame[max_path];


Takes the value of type float in the INI file
Float GetFloat (lpctstr szsection, lpctstr szName, float fdefault);


The key value of the integral type in the INI file is taken
Long Getlong (LPCTSTR szsection, LPCTSTR szName, long Ldefault);


Takes the key value of the unsigned 16 binary integer type in the INI file
DWORD Gethex (LPCTSTR szsection, LPCTSTR SzName, DWORD Dwdefault);


Takes the key value of the string type in the INI file
CString GetString (LPCTSTR strsection, LPCTSTR szName, LPCTSTR szdefault);


The key value of the string type in the Write INI file
BOOL SetString (LPCTSTR strsection, LPCTSTR szName, LPCTSTR szvalue);


The key value of the integral type in the Write INI file
BOOL Setlong (LPCTSTR strsection, LPCTSTR szName, long LValue);


The key value of the integral type in the Write INI file
BOOL sefloat (LPCTSTR strsection, lpctstr szName, float fvalue);


The key value of type float in the Write INI file
BOOL Sehex (LPCTSTR strsection, LPCTSTR szName, DWORD dwvalue);


};


#endif


//-----------------------------------------------------------------------------
File:Profile.cpp
//-----------------------------------------------------------------------------


#include "stdafx.h"
#include "profile.h"


//-----------------------------------------------------------------------------
Name:
Desc:
//-----------------------------------------------------------------------------
Cprofile::cprofile ()
{
GetModuleFileName (NULL, M_szininame, _max_path);
int ilen = Lstrlen (m_szininame);
M_szininame[ilen-3] = ' I ';
M_szininame[ilen-2] = ' n ';
M_szininame[ilen-1] = ' I ';
}


//-----------------------------------------------------------------------------
Name:
Desc:
//-----------------------------------------------------------------------------
Cprofile::~cprofile ()
{
}




//-----------------------------------------------------------------------------
Name:
Desc:this function reads our profiles into member variables of the profile object.
//-----------------------------------------------------------------------------
VOID cprofile::setprofile (LPCTSTR szininame)
{
lstrcpy (M_szininame, szininame);


}




//-----------------------------------------------------------------------------
Name:
Desc:
//-----------------------------------------------------------------------------
DWORD Cprofile::gethex (LPCTSTR strsection, LPCTSTR SzName, DWORD Dwdefault)
{
TCHAR Szbuffer[max_path];
DWORD dwresult = 0;
GetPrivateProfileString (Strsection, SzName, TEXT (""),
Szbuffer, sizeof (szbuffer), m_szininame);
if (szbuffer[0] = = ' + ')
return dwdefault;


if (_stscanf (Szbuffer, TEXT ("0x%x"), &dwresult)! = 1)
{
dwresult = 0;
}


return dwresult;
}




//-----------------------------------------------------------------------------
Name:
Desc:
//-----------------------------------------------------------------------------
Float Cprofile::getfloat (lpctstr strsection, lpctstr szName, float fdefault)
{
TCHAR Szbuffer[max_path];
GetPrivateProfileString (Strsection, SzName, TEXT (""),
Szbuffer, sizeof (szbuffer), m_szininame);
if (szbuffer[0] = = ' + ')
return fdefault;


return (float) _tcstod (Szbuffer,text (') ');
}




//-----------------------------------------------------------------------------
Name:
Desc:
//-----------------------------------------------------------------------------
Long Cprofile::getlong (LPCTSTR strsection, LPCTSTR szName, long Ldefault)
{
TCHAR Szbuffer[max_path];
GetPrivateProfileString (Strsection, SzName, TEXT (""),
Szbuffer, sizeof (szbuffer), m_szininame);
if (szbuffer[0] = = ' + ')
return ldefault;


Return _tcstol (' Szbuffer,text '), 10;
}




//-----------------------------------------------------------------------------
Name:
Desc:
//-----------------------------------------------------------------------------
CString cprofile::getstring (LPCTSTR strsection, LPCTSTR szName, LPCTSTR szdefault)
{
TCHAR Szbuffer[max_path];
CString szvalue = _t ("");


GetPrivateProfileString (Strsection, SzName, Szdefault,
Szbuffer, MAX_PATH, m_szininame);
int ilen = Lstrlen (szbuffer);
if (Ilen > 0)
Szvalue = Szbuffer;


return szvalue;


}


BOOL cprofile::setstring (LPCTSTR strsection, LPCTSTR szName, LPCTSTR szvalue)
{
Return WritePrivateProfileString (Strsection, SzName, Szvalue, m_szininame);
}


BOOL Cprofile::setlong (LPCTSTR strsection, LPCTSTR szName, long LValue)
{
CString strvalue;
Strvalue.format (_t ("%d"), lValue);
Return WritePrivateProfileString (Strsection, SzName, strvalue, m_szininame);
}


BOOL cprofile::sefloat (LPCTSTR strsection, lpctstr szName, float fvalue)
{
CString strvalue;
Strvalue.format (_t ("%f"), Fvalue);
Return WritePrivateProfileString (Strsection, SzName, strvalue, m_szininame);
}


BOOL Cprofile::sehex (LPCTSTR strsection, LPCTSTR szName, DWORD dwvalue)
{
CString strvalue;
Strvalue.format (_t ("%x"), dwvalue);
Return WritePrivateProfileString (Strsection, SzName, strvalue, m_szininame);
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.