Use STL to read INI File Content

Source: Internet
Author: User
Tags read ini file

The test. ini file is as follows:

 

[Section1]
Key1 = value1
Key2 = value2

[Section2]
Key1 = value1
Key2 = value2 # comment

 

The usage is as follows:

 

Cinifile ini;
If (! INI. Open ("test. ini "))
Return;
String strvalue = ini. Read ("Section1", "key1 ");
If (strvalue. Empty ())
STD: cout <"can't find [Section1] key1" <Endl;
Else
STD: cout <"value =" <strvalue <Endl;

 

Output: value = value1

 

The source code is as follows:

(The following source code for http://www.stlchina.org/I only made some small changes)

# If! Defined (_ ini_file1_included _)
# DEFINE _ ini_file1_included _

# If _ msc_ver> 1000
# Pragma once
# Endif // _ msc_ver> 1000

# Pragma warning (Disable: 4786)

# Include <map>
# Include <vector>
# Include <string>
# Include <algorithm>
# Include <fstream>

Using namespace STD;

Typedef Map <string, String, less <string> map_string;
Typedef map_string: iterator it_map_string;

Const char * const middlestring = "_____***_______";

Struct analyzeini
{
Analyzeini (map_string & strmap): pmap (& strmap ){}

Void operator () (const string & Strini)
{
Int first = Strini. Find ('[');
Int last = Strini. rfind (']');
If (first! = String: NPOs & last! = String: NPOs & first! = Last + 1)
{
Strsect = Strini. substr (first + 1, last-first-1 );
Return;
}
If (strsect. Empty ())
Return;
If (first = Strini. Find ('=') = string: NPOs)
Return;
String strtmp1 = Strini. substr (0, first );
String strtmp2 = Strini. substr (first + 1, string: NPOs );
First = strtmp1.find _ first_not_of ("\ t ");
Last = strtmp1.find _ last_not_of ("\ t ");
If (first = string: NPOs | last = string: NPOs)
Return;
String strkey = strtmp1.substr (first, last-first + 1 );
First = strtmp2.find _ first_not_of ("\ t ");
If (last = strtmp2.find ("\ t #", first ))! = String: NPOs) |
(Last = strtmp2.find ("#", first ))! = String: NPOs) |
(Last = strtmp2.find ("\ t //", first ))! = String: NPOs) |
(Last = strtmp2.find ("//", first ))! = String: NPOs ))
{
Strtmp2 = strtmp2.substr (0, last-first );
}
Last = strtmp2.find _ last_not_of ("\ t ");
If (first = string: NPOs | last = string: NPOs)
Return;
String value = strtmp2.substr (first, last-first + 1 );
String mapkey = strsect + middlestring;
Mapkey + = strkey;
(* Pmap) [mapkey] = value;
Return;
}

PRIVATE:
String strsect;
Map_string * pmap;
};

Class cinifile
{
Public:
Cinifile (){};
~ Cinifile (){};

Bool open (const char * pinipath)
{
If (! Isvalid (pinipath ))
{
Return false;
}

Return do_open (pinipath );
}

String read (const char * expect CT, const char * pkey)
{
If (! Isvalid (reverse CT) |! Isvalid (pkey ))
{
Return string ();
}

String mapkey = Country CT;
Mapkey + = middlestring;
Mapkey + = pkey;
It_map_string it = c_inimap.find (mapkey );
If (IT = c_inimap.end ())
Return string ();
Else
Return it-> second;
}

Static bool isvalid (const char * psz)
{
Return! (Psz = NULL & strlen (psz) = 0 );
}

Protected:
Bool do_open (const char * pinipath)
{
Ifstream fin (pinipath );
If (! Fin. is_open ())
Return false;
Vector <string> strvect;
While (! Fin. EOF ())
{
String inbuf;
Getline (FIN, inbuf, '\ n ');
Strvect. push_back (inbuf );
}
If (strvect. Empty ())
Return false;
For_each (strvect. Begin (), strvect. End (), analyzeini (c_inimap ));
Return! C_inimap.empty ();
}

Map_string c_inimap;
};

# Endif //! Defined (_ ini_file1_included _)

 

 

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.