In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about the benefits of using the Properties class in JAVA. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Many developers complain that C++ can't bind Properties classes like Java does. Java's Properties class inherently contains a file that is used to read and write properties in the Properties class, which can be written as follows: = (for example: ConnectToInternet=Use IE).
The advantage of using Properties classes 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 that by using the operators > > and
# 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; } // 用于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);}
}
/ / eliminate spaces in a string
Template
< class CharType>Std::basic_string
< CharType>Trim_spaces (const std::basic_string
< CharType>& strSource)
{
Std::basic_string
< CharType>StrSpaces; strSpaces + = (CharType)'; 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
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 (strLine)
Bool bIsComment = strLine.empty () | | (strLine [0] = = COMMENT)
If (bIsComment)
{strComment + = strLine; strComment + =';}
Else
{
Int idxDelimeter = strLine.find (DELIMETER)
If (idxDelimeter! = string_type::npos)
{
String_type strPropertyName = strLine.substr (0, idxDelimeter)
String_type strPropertyValue = strLine.substr (idxDelimeter + 1)
StrPropertyName = trim_spaces (strPropertyName)
StrPropertyValue = trim_spaces (strPropertyValue)
M_collProperties.insert (
Std::make_pair (strPropertyName, OneProperty (strPropertyValue, strComment)
M_aProperties.push_back (strPropertyName)
StrComment.erase ()
}
Else
Throw properties_exception (
"While reading from file'" + m_strFileName +
"', we encountered an invalid line:" + convert_string
< char_type, char>(strLine))
}
}
M_strLastComment = strComment
}
Void write_properties () const
{
If (! m_bIsDirty)
/ / No need to save
; return
Const char DELIMETER = get_delimeter ()
Std::basic_ofstream
< char_type>StreamOut (m_strFileName.c_str ())
PropertiesArray::const_iterator
ItFirst = m_aProperties.begin (), itLast = m_aProperties.end ()
While (itFirst! = itLast)
{
Const string_type & strPropertyName = * itFirst
Const OneProperty & property = m_collProperties.find (strPropertyName)-> second
Write_property_comment (streamOut, property.m_strComment)
String_type strToWrite = strPropertyName
StrToWrite + =''; strToWrite + = DELIMETER; strToWrite + =''
StreamOut strToWrite streamComment (strComment)
String_type strLine
While (std::getline (streamComment, strLine))
{
If (! strLine.empty ()
If (strLine [0] = = COMMENT)
StreamOut
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.