Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Reading of ini file

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Ini.h code

# ifndef INI_H

# define INI_H

# include

# include

Using namespace std

# define CONFIGLEN 256

Enum INI_RES

{

INI_SUCCESS, / / successful

INI_ERROR, / / Common error

INI_OPENFILE_ERROR, / / failed to open the file

INI_NO_ATTR / / has no corresponding key value

}

/ / subkey index subkey value

Typedef map KEYMAP

/ / Primary key index primary key value

Typedef map MAINKEYMAP

/ / basic operation class of config file

Class CIni

{

Public:

/ / Constructor

CIni ()

/ / Analysis of sufficient function

Virtual ~ CIni ()

Public:

/ / get the key value of × ×

Int GetInt (const char* mAttr, const char* cAttr)

/ / get the string of the key value

Char* GetStr (const char* mAttr, const char* cAttr)

/ / Open the config file

INI_RES OpenFile (const char* pathName, const char* type)

/ / close the config file

INI_RES CloseFile ()

Protected:

/ / read config file

INI_RES GetKey (const char* mAttr, const char* cAttr, char* value)

Protected:

/ / File handle opened

FILE* m_fp

Char m_ szKey[CONFIGLEN]

MAINKEYMAP m_Map

}

# endif

Ini.cpp code

# include "ini.h"

/ *

* function: constructor

* parameters: none

* return value: none

* remarks:

* * /

CIni::CIni ()

{

Memset (m_szKey, 0, sizeof (m_szKey))

M_fp = NULL

}

/ *

* function: destructor

* parameters: none

* return value: none

* remarks:

* * /

CIni::~CIni ()

{

M_Map.clear ()

}

/ *

* function: open file function

* parameters: none

* return value:

* remarks:

* * /

INI_RES CIni::OpenFile (const char* pathName, const char* type)

{

String szLine, szMainKey, szLastMainKey, szSubKey

Char strLine [CONFIGLEN] = {0}

KEYMAP mLastMap

Int nIndexPos =-1

Int nLeftPos =-1

Int nRightPos =-1

M_fp = fopen (pathName, type)

If (m_fp = = NULL)

{

Printf ("open inifile% s error!\ n", pathName)

Return INI_OPENFILE_ERROR

}

M_Map.clear ()

While (fgets (strLine, CONFIGLEN, m_fp))

{

SzLine.assign (strLine)

/ / remove unnecessary characters from a string

NLeftPos = szLine.find ("\ n")

If (string::npos! = nLeftPos)

{

SzLine.erase (nLeftPos, 1)

}

NLeftPos = szLine.find ("\ r")

If (string::npos! = nLeftPos)

{

SzLine.erase (nLeftPos, 1)

}

/ / determine whether it is a primary key

NLeftPos = szLine.find ("[")

NRightPos = szLine.find ("]")

If (nLeftPos! = string::npos & & nRightPos! = string::npos)

{

SzLine.erase (nLeftPos, 1)

NRightPos--

SzLine.erase (nRightPos, 1)

M _ Map [szLastMainKey] = mLastMap

MLastMap.clear ()

SzLastMainKey = szLine

}

Else

{

/ / whether it is a subkey

If (nIndexPos = szLine.find ("="), string::npos! = nIndexPos)

{

String szSubKey, szSubValue

SzSubKey = szLine.substr (0, nIndexPos)

SzSubValue = szLine.substr (nIndexPos + 1, szLine.length ()-nIndexPos-1)

MLastMap [szSubKey] = szSubValue

}

Else

{

/ / TODO: contents such as comments that do not conform to the ini key value template

}

}

}

/ / insert the last primary key

M _ Map [szLastMainKey] = mLastMap

Return INI_SUCCESS

}

/ *

* function: turn off file function

* parameters: none

* return value:

* remarks:

* * /

INI_RES CIni::CloseFile ()

{

If (m_fp! = NULL)

{

Fclose (m_fp)

M_fp = NULL

}

Return INI_SUCCESS

}

/ *

* function: a string that gets a key value under [SECTION]

* parameters:

* char* mAttr input parameter primary key

* char* cAttr input parameter subkey

* char* value output parameter subkey value

* return value:

* remarks:

* * /

INI_RES CIni::GetKey (const char* mAttr, const char* cAttr, char* pValue)

{

KEYMAP mKey = m _ map [mAttr]

String sTemp = mKey [cAttr]

Strcpy (pValue, sTemp.c_str ())

Return INI_SUCCESS

}

/ *

* function: obtain the key value of × ×

* parameters:

* cAttr primary key

* cAttr subkey

* return value: if the value is normal, if the corresponding value is not read successfully, 0 is returned (the key value itself is 0 does not conflict)

* remarks:

* * /

Int CIni::GetInt (const char* mAttr, const char* cAttr)

{

Int nRes = 0

Memset (m_szKey, 0, sizeof (m_szKey))

If (INI_SUCCESS = = GetKey (mAttr, cAttr, m_szKey))

{

NRes = atoi (m_szKey)

}

Return nRes

}

/ *

* function: the string that gets the key value

* parameters:

* cAttr primary key

* cAttr subkey

* return value: "NULL" is returned if the read subkey string is not read successfully.

* remarks:

* * /

Char* CIni::GetStr (const char* mAttr, const char* cAttr)

{

Memset (m_szKey, 0, sizeof (m_szKey))

If (INI_SUCCESS! = GetKey (mAttr, cAttr, m_szKey))

{

Strcpy (m_szKey, "NULL")

}

Return m_szKey

}

Config.ini

[cctv.thrift]

IP=192.168.37.123

Port=7001

Username=admin

Password=admin

test

Static void ReadIniFile ()

{

CIni ini

Ini.OpenFile ("config.ini", "r")

Char * pVal1 = ini.GetStr ("cctv.thrift", "IP")

Int nKey = ini.GetInt ("cctv.thrift", "Port")

}

Be careful

The string returned by GetStr must be saved to other variables immediately. If GetInt is called again at this time, the value of the above pVal1 will be changed.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report