Benefits of using the Properties class

Source: Internet
Author: User

 

Many developers complainC ++ cannot bind the Properties class as Java does. The Properties class of Java contains a file used to read and write Properties in the Properties class. It can be written in the form of <name >=< value> (for example, ConnectToInternet = Use IE ).

The advantage of using the Properties class is that you can easily understand and modify them. In the first part of this article, you will see that we can also use the Properties class in C ++. The second part of this article will show you how easy it is to save data to the Properties class by using operators> and <.

Now we will introduce the structure of the C ++ Properties file. Each row of the file can be one of the following three situations:

Empty line (think it is part of the comment)

Comment row starting '#'

'<Name> = <value>'. This is the statement that assigns a value to an attribute.

Now let's take a look at the features of the Properties class:

Annotations are persistent (they are not lost when the Properties class is saved ). Note that each comment belongs to a certain attribute. The comment row on '<name >=< value>' belongs to this '<Name>' attribute.

After the Properties class is saved, the attributes are retained.

It is effective for various character types: char, wchar_t, etc.

The use of the Properties class is quite simple:

Save (): save attributes

Has_property (strPropertyName): If the class has this property, 'true' is returned'

String get_property (strPropertyName): returns the specified property (if the specified property does not exist, an exception is thrown)

Set_property (strPropertyName, strPropertyValue): Set the given property

Stringget_property_comment (strPropertyName): returns the comment that belongs to the specified attribute. (If the comment of the specified attribute does not exist, an exception is thrown)

Set_property_comment (strPropertyName, strPropertyComment): sets the comment for the specified attribute. (If the comment for the specified attribute does not exist, an exception is thrown)

The following is the code of the file_reader_writer class and the corresponding example. After running the command, copy the properties.txt file. Let's see how easy it is to access and modify it.

# Include exception>

# Include string>

# Include sstream>

# Include map>

# Include vector>

# Include fstream>

# Include algorithm>

# Include functional>

// Allow String Conversion

Template <class FromCharType, class ToCharType>

Inline std: basic_string <ToCharType> convert_string (const std: basic_string <FromCharType> & strSource)

{

Std: basic_string <ToCharType> strDest;

Int nSourceLen = strSource. length ();

StrDest. resize (nSourceLen );

For (int idxChar = 0; idxChar <nSourceLen; idxChar ++)

{StrDest [idxChar] = (ToCharType) strSource [idxChar];}

Return strDest;

}

// Used for trim_spaces;

Template <class CharType>

Struct is_char_in_str: public std: binary_function <CharType, std: basic_string <CharType>, bool>

{

Bool operator () (CharType ch, const std: basic_string <CharType> & strSource) const

{Return (strSource. find (ch )! = Std: basic_string <CharType >:: npos );}

};

// Remove spaces in the string

Template <class CharType>

Std: basic_string <CharType> trim_spaces (const std: basic_string <CharType> & strSource)

{

Std: basic_string <CharType> strSpaces; strSpaces ++ = (CharType );

Typedef std: basic_string <CharType> string_type;

String_type: const_iterator

ItFirst = std: find_if (strSource. begin (), strSource. end (),

Std: not1 (std: bind2nd (is_char_in_str <CharType> (), strSpaces )));

String_type: const_reverse_iterator

RitLast = std: find_if (strSource. rbegin (), strSource. rend (),

Std: not1 (std: bind2nd (is_char_in_str <CharType> (), strSpaces )));

String_type: const_iterator itLast = & * ritLast;

If (itFirst <= itLast)

If (itFirst! = StrSource. end ())
Return string_type (itFirst, itLast + 1 );

Return string_type ();

}

// Exception when reading/writing attributes

Class properties_exception: public std: exception

{

Public:

Properties_exception (const std: string & str): m_strDescription (str ){}

Const char * what () const {return m_strDescription.c_str ();}

Private:

Std: string m_strDescription;

};

// Read/write attributes from a file

Template <class CharType>

Class file_reader_writer

{

Typedef std: basic_string <CharType> string_type;

Public:

//... Needed within the basic_properties!

Typedef CharType char_type;

Public:

File_reader_writer (const char * strFileName)

: M_bIsDirty (false), m_strFileName (strFileName) {read_properties ();}

~ File_reader_writer () {save ();}

Void save ()

{Write_properties ();}

Bool has_property (const string_type & strPropertyName) const

{

PropertiesCollection: const_iterator itFound = m_collProperties.find (strPropertyName );

Return (itFound! = M_collProperties.end ());

}

Const string_type & get_property (const string_type & strPropertyName) const

{

PropertiesCollection: const_iterator itFound = m_collProperties.find (strPropertyName );

If (itFound! = M_collProperties.end ())

Return itFound-> second. m_strValue;

Else

Throw properties_exception (

"Cound not get property value for" + convert_string <char_type, char> (strPropertyName) +

", Since this property does not exist .");

}

Void set_property (const string_type & strProperty, const string_type & strPropertyValue)

{

PropertiesCollection: iterator itFound = m_collProperties.find (strProperty );

If (itFound = m_collProperties.end ())

// It is a new property

M_aProperties.push_back (strProperty );

M_collProperties [strProperty]. m_strValue = strPropertyValue;

M_bIsDirty = true;

}

Const string_type & get_property_comment (const string_type & strPropertyName) const

{

PropertiesCollection: const_iterator itFound = m_collProperties.find (strPropertyName );

If (itFound! = M_collProperties.end ())

Return itFound-> second. m_strComment;

Else

Throw properties_exception (

"Cound not get property comment for" + convert_string <char_type, char> (strPropertyName) +

", Since this property does not exist .");

}

Void set_property_comment (const string_type & strPropertyName, const string_type & strPropertyComment)

{

PropertiesCollection: iterator itFound = m_collProperties.find (strPropertyName );

If (itFound! = M_collProperties.end ())

ItFound-> second. m_strComment = strPropertyComment;

Else

Throw properties_exception (

"Cound not set property comment for" + convert_string <char_type, char> (strPropertyName) +

", Since this property does not exist .");

M_bIsDirty = true;

}

Private:

Static const char_type get_delimeter () {return = ;}

Static const char_type get_comment () {return #;}

Void read_properties ()

{

Const char DELIMETER = get_delimeter ();

Const char COMMENT = get_comment ();

Std: basic_ifstream <char_type> streamIn (m_strFileName.c_str ());

String_type strLine;

String_type strComment;

While (std: getline (streamIn, strLine ))

{

StrLine = trim_spaces (st

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.