许多开发者抱怨C++不能像Java那样绑定Properties类。Java的Properties类内在包含一个文件,该文件用来读写Properties类中的属性,可以写成这样形式:<名字>=<数值>(例如:ConnectToInternet=Use IE)。
使用Properties类的好处就是你可以很轻松的理解和修改它们。在本文的第一部分中,你将看到我们也可以在C++中使用Properties类。本文的第二部分将向你演示通过使用操作符>>和<<把数据保存到Properties类中是多么的容易。
现在介绍C++ Properties文件的结构。该文件的每一行可以是下面三种情况中的某一种:
空行(认为它是注释中的一部分)
以‘#’ 开始的注释行
‘<名字>=<数值>’行,这是给一个属性赋值的语句
现在让我们再看看Properties类的的特点:
注释是持久性的(当保存Properties类时,它们不会丢失掉)。注意每一个注释都属于某个属性。在‘<名字>=<数值>’行上的注释行属于该‘<名字>’属性。
当保存Properties类后,属性仍然保留自己的位置。
它对各种字符类型都有效:char、wchar_t等等
Properties类的使用相当简单:
save():保存属性
has_property(strPropertyName):如果类中有该属性则返回‘真’
string get_property(strPropertyName):返回指定的属性(如果指定属性不存在,则抛出例外)
set_property(strPropertyName, strPropertyValue):设置给定属性
stringget_property_comment( strPropertyName):返回属于指定属性的注释(如果指定属性的注释不存在,则抛出例外)
set_property_comment(strPropertyName, strPropertyComment):设置指定属性的注释(如果指定属性的注释不存在,则抛出例外)
下面是file_reader_writer类以及相应例子的代码。运行它之后,请查看properties.txt文件。看看访问和修改它是多么容易的一件事。
#include exception>
#include string>
#include sstream>
#include map>
#include vector>
#include fstream>
#include algorithm>
#include functional>
//允许字符串转化
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); }
};
//消除字符串中的空格
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 <= itLast)
if ( itFirst != strSource.end())
return string_type( itFirst, itLast + 1);
return string_type();
}
// 当读写属性时的例外
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;
};
// 从文件中读写属性
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->se