Cprofile: read/write private ini configuration file

Source: Internet
Author: User

Note: This is a small one I usually write.Code, May not be useful and in line with the design principles. If anything is inappropriate, please advise.

 

Note:

Although ini configuration files are not recommended by Microsoft, they must be written in C ++ at least in vc6.ProgramHowever, using INI has its advantages: a simple INI is more intuitive than a simple XML (but the opposite is true in complex cases). Windows provides a dedicated API to read and write ini configurations, you only need one statement.

However, some problems related to the API reading and writing of private configuration files have also been found in several recently written small programs:

1. You need to obtain the Global Path of the configuration file and send it to the API each time;

2. Windows does not provide an API for writing integer data;

3. getprivateprofilexxx is often written as getprifilexxx.

 

Therefore, a class encapsulated by myself is actually a very simple encapsulation. The function name is shorter and the function parameters are less. Well, I often do this, and make some very simple encapsulation for some trivial (many others should think so) Beautiful code or simplified calling.

Key points are as follows:

1. Two member variables are provided to save the path and segment name of the configuration file, so that the published read/write interface does not have to carry these two parameters. At least for most of my applets, the two data items are unique.

2. Provides a default configuration file path: in the directory where the current module is located, it has the same name as the current module and is suffixed with INI.

3. provides an interface for writing int-type data, which is used online. Convert to a string before writing.

4. The provided getstring directly returns the cstring object, and the caller does not need to allocate memory.

 

Header file:

 Class Cprofile
{
Public :
Static DWORD getdefaprofile profile (lptstr lpbuffer, uint usize );

Cprofile ();
Cprofile (lpctstr strfile );
Virtual ~ Cprofile ();

Public :

Bool getstruct (lpctstr key, lpvoid lpstruct, uint usize, lpctstr section = NULL );
DWORD getsectionnames (lptstr lpreturnedstring, DWORD nsize );
DWORD getsection (lptstr lpreturnedstring, DWORD nsize, lpctstr section = NULL );
Cstring getstring (maid key, maid strdefault = NULL, maid section = NULL );
Int getint (maid key, int ndefault = 0, maid section = NULL );

Bool writestruct (lpctstr key, lpvoid lpstruct, uint usize, lpctstr section = NULL );
Bool writeint (maid key, int ivalue, maid section = NULL );
Bool writestring (maid key, maid lpstring, maid section = NULL );
Bool writesection (maid, maid section = NULL );

Void Setcurrentsection (maid );
Void Setcurrentfile (lpctstr lpfile );

Lpctstr getcurrentsection ();
Lpctstr getcurrentfile ();

Protected :

Tchar m_strsection [max_sectionname_size];
Tchar m_strfile [max_path];
};

Code:

 

Code # Define Cprofile_preparesection \
If (Null = Section) Section = M_strsection ;\
Assert (null ! = Section)

//////////////////////////////////////// ///////////////////////////// /
// Construction/destruction
//////////////////////////////////////// ///////////////////////////// /

Cprofile: cprofile ()
{
Getdefaultprofile (m_strfile, max_path );
Memset (m_strsection, 0 , 50   *   Sizeof (Tchar ));
}

Cprofile: cprofile (lpctstr lpfile)
{
Setcurrentfile (lpfile );
Memset (m_strsection,0,50 * Sizeof(Tchar ));
}

Cprofile ::~Cprofile ()
{
}

DWORD cprofile: getdefaultprofile (lptstr lpbuffer, uint usize)
{
DWORD ipathlen=: Getmodulefilename (null, lpbuffer, usize );

If ( 0 = ipathlen) return 0 ;

while ( ' . ' ! = lpbuffer [ -- ipathlen] & ipathlen >= 0 );

If(0 =Ipathlen)Return 0;

Ipathlen=Min (usize- 5, Ipathlen );

Wsprintf (lpbuffer+Ipathlen,". Ini \ 0");

ReturnIpathlen+ 5;
}

Int cprofile: getint (lpctstr key, int ndefault, lpctstr Section)
{
Cprofile_preparesection;

Return: Getprivateprofileint (section, key, ndefault, m_strfile );
}

Cstring cprofile: getstring (maid key, maid)
{
StaticTchar buffer [max_profilestring_size];

Memset (buffer,0, Max_profilestring_size* Sizeof(Tchar ));

Cprofile_preparesection;

: Getprivateprofilestring (section, key, lpdefault, buffer, max_profilestring_size, m_strfile );

ReturnCstring (buffer );
}

DWORD cprofile: getsection (lptstr lpreturnedstring, DWORD nsize, lpctstr Section)
{
Cprofile_preparesection;

Return: Getprivateprofilesection (section, lpreturnedstring, nsize, m_strfile );
}

DWORD cprofile: getsectionnames (lptstr lpreturnedstring, DWORD nsize)
{
Return: Getprivateprofilesectionnames (lpreturnedstring, nsize, m_strfile );
}

Bool cprofile: getstruct (lpctstr key, lpvoid lpstruct, uint usize, lpctstr Section)
{
Cprofile_preparesection;

Return: Getprivateprofilestruct (section, key, lpstruct, usize, m_strfile );
}

Bool cprofile: writesection (lpstring, lptstr Section)
{
Cprofile_preparesection;

Return: Writeprivateprofilesection (section, lpstring, m_strfile );
}

Bool cprofile: writestring (maid key, maid)
{
Cprofile_preparesection;

Return: Writeprivateprofilestring (section, key, lpstring, m_strfile );
}

Bool cprofile: writeint (lpctstr key, int ivalue, lpctstr Section)
{
Cprofile_preparesection;

Tchar buffer [ 8 ];

Memset (buffer, 0 , 4   *   Sizeof (Tchar ));

Wsprintf (buffer, " % I " , Ivalue );

Return : Writeprivateprofilestring (section, key, buffer, m_strfile );
}

Bool cprofile: writestruct (lpctstr key, lpvoid lpstruct, uint usize, lpctstr Section)
{
Cprofile_preparesection;

Return: Writeprivateprofilestruct (section, key, lpstruct, usize, m_strfile );
}

VoidCprofile: setcurrentfile (lpctstr lpfile)
{
Strcpyn (m_strfile, lpfile, max_path );
}

VoidCprofile: setcurrentsection (maid)
{
Strcpyn (m_strsection, lction ction, max_sectionname_size );
}

Lpctstr cprofile: getcurrentsection ()
{
ReturnM_strsection;
}
Lpctstr cprofile: getcurrentfile ()
{
ReturnM_strfile;
}

 

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.