[zypp-commit] r7067 - in /trunk/libzypp/zypp/base: String.cc String.h
Author: mlandres Date: Fri Aug 31 21:27:15 2007 New Revision: 7067 URL: http://svn.opensuse.org/viewcvs/zypp?rev=7067&view=rev Log: Added str::strToTrue, str::sstrToFalse and str::sstrToBool to parse boolean values from string. Modified: trunk/libzypp/zypp/base/String.cc trunk/libzypp/zypp/base/String.h Modified: trunk/libzypp/zypp/base/String.cc URL: http://svn.opensuse.org/viewcvs/zypp/trunk/libzypp/zypp/base/String.cc?rev=7067&r1=7066&r2=7067&view=diff ============================================================================== --- trunk/libzypp/zypp/base/String.cc (original) +++ trunk/libzypp/zypp/base/String.cc Fri Aug 31 21:27:15 2007 @@ -54,6 +54,36 @@ /****************************************************************** ** + ** FUNCTION NAME : strToTrue + ** FUNCTION TYPE : bool + */ + bool strToTrue( const std::string & str ) + { + std::string t( toLower( str ) ); + return( t == "1" + || t == "yes" + || t == "true" + || t == "on" + ); + } + + /****************************************************************** + ** + ** FUNCTION NAME : strToFalse + ** FUNCTION TYPE : bool + */ + bool strToFalse( const std::string & str ) + { + std::string t( toLower( str ) ); + return ! ( t == "0" + || t == "no" + || t == "false" + || t == "off" + ); + } + + /****************************************************************** + ** ** FUNCTION NAME : toLower ** FUNCTION TYPE : std::string */ @@ -160,26 +190,26 @@ string gsub(const string& sData, const string& sFrom, const string& sTo) { string sNew = sData; - + if (! sNew.empty()) { string::size_type toLen = sTo.length(); string::size_type frLen = sFrom.length(); string::size_type loc = 0; - + while (string::npos != (loc = sNew.find(sFrom, loc))) { sNew.replace(loc, frLen, sTo); loc += toLen; - + if (loc >= sNew.length()) break; } } - + return sNew; } - + /****************************************************************** ** ** Modified: trunk/libzypp/zypp/base/String.h URL: http://svn.opensuse.org/viewcvs/zypp/trunk/libzypp/zypp/base/String.h?rev=7067&r1=7066&r2=7067&view=diff ============================================================================== --- trunk/libzypp/zypp/base/String.h (original) +++ trunk/libzypp/zypp/base/String.h Fri Aug 31 21:27:15 2007 @@ -213,6 +213,24 @@ //@} /////////////////////////////////////////////////////////////////// + /** Parsing boolean from string. + */ + //@{ + /** Return \c true if str is <tt>1, true, yes, on</tt>. */ + bool strToTrue( const std::string & str ); + + /** Return \c false if str is <tt>0, false, no, off</tt>. */ + bool strToFalse( const std::string & str ); + + /** Parse \c str into a bool depending on the default value. + * If the \c default is true, look for a legal \c false string. + * If the \c default is false, look for a legal \c true string. + */ + inline bool strToBool( const std::string & str, bool default_r ) + { return( default_r ? strToFalse( str ) : strToTrue( str ) ); } + //@} + + /////////////////////////////////////////////////////////////////// /** \name Split. */ //@{ /** Split \a line_r into words. @@ -329,7 +347,7 @@ */ std::string gsub( const std::string& sData, const std::string& sFrom, const std::string& sTo); - + /////////////////////////////////////////////////////////////////// /** \name String prefix handling. -- To unsubscribe, e-mail: zypp-commit+unsubscribe@opensuse.org For additional commands, e-mail: zypp-commit+help@opensuse.org
participants (1)
-
mlandres@svn.opensuse.org