From ea3f3d491929b2a94c53f7d0be48ecfa33985c2b Mon Sep 17 00:00:00 2001 From: Michael Andres Date: Mon, 19 Dec 2005 13:49:34 +0000 Subject: [PATCH] Added Locale --- zypp/@Review/LangCode.cc | 89 -------------------- zypp/@Review/LangCode.h | 117 --------------------------- zypp/CountryCode.h | 8 ++ zypp/LanguageCode.h | 13 ++- zypp/Locale.cc | 184 ++++++++++++++++++++++++++++++++++++++++++ zypp/Locale.h | 126 +++++++++++++++++++++++++++++ zypp/Makefile.am | 2 + zypp/NeedAType.h | 11 +-- zypp/detail/ResObjectImplIf.h | 1 + 9 files changed, 334 insertions(+), 217 deletions(-) delete mode 100644 zypp/@Review/LangCode.cc delete mode 100644 zypp/@Review/LangCode.h create mode 100644 zypp/Locale.cc create mode 100644 zypp/Locale.h diff --git a/zypp/@Review/LangCode.cc b/zypp/@Review/LangCode.cc deleted file mode 100644 index c9cc63e..0000000 --- a/zypp/@Review/LangCode.cc +++ /dev/null @@ -1,89 +0,0 @@ -/*---------------------------------------------------------------------\ -| | -| __ __ ____ _____ ____ | -| \ \ / /_ _/ ___|_ _|___ \ | -| \ V / _` \___ \ | | __) | | -| | | (_| |___) || | / __/ | -| |_|\__,_|____/ |_| |_____| | -| | -| core system | -| (C) SuSE GmbH | -\----------------------------------------------------------------------/ - - File: LangCode.cc - - Author: Michael Andres - Maintainer: Michael Andres - -/-*/ - -#include - -#include - -using namespace std; - -/////////////////////////////////////////////////////////////////// -// -// -// METHOD NAME : LangCode::LangCode -// METHOD TYPE : Constructor -// -LangCode::LangCode( const std::string & code_r ) -{ - string t; - string::size_type sep = code_r.find_first_of( "@." ); - if ( sep == string::npos ) { - t = code_r; - } else { - t = code_r.substr( 0, sep ); - } - - sep = t.find( '_' ); - if ( sep == string::npos ) { - _language = ISOLanguage( t ); - } else { - _language = ISOLanguage( t.substr( 0, sep ) ); - _country = ISOCountry( t.substr( sep+1 ) ); - } - -} - -/////////////////////////////////////////////////////////////////// -// -// -// METHOD NAME : LangCode::code -// METHOD TYPE : std::string -// -std::string LangCode::code() const -{ - string ret( languageCode() ); - if ( hasCountry() ) - ret += "_" + countryCode(); - return ret; -} - -/////////////////////////////////////////////////////////////////// -// -// -// METHOD NAME : LangCode::name -// METHOD TYPE : std::string -// -std::string LangCode::name() const -{ - string ret( languageName() ); - if ( hasCountry() ) - ret += " (" + countryName() + ")"; - return ret; -} - -/****************************************************************** -** -** -** FUNCTION NAME : operator<< -** FUNCTION TYPE : std::ostream & -*/ -std::ostream & operator<<( std::ostream & str, const LangCode & obj ) -{ - return str << obj.code(); -} diff --git a/zypp/@Review/LangCode.h b/zypp/@Review/LangCode.h deleted file mode 100644 index 21d6730..0000000 --- a/zypp/@Review/LangCode.h +++ /dev/null @@ -1,117 +0,0 @@ -/*---------------------------------------------------------------------\ -| | -| __ __ ____ _____ ____ | -| \ \ / /_ _/ ___|_ _|___ \ | -| \ V / _` \___ \ | | __) | | -| | | (_| |___) || | / __/ | -| |_|\__,_|____/ |_| |_____| | -| | -| core system | -| (C) SuSE GmbH | -\----------------------------------------------------------------------/ - - File: LangCode.h - - Author: Michael Andres - Maintainer: Michael Andres - -/-*/ -#ifndef LangCode_h -#define LangCode_h - -#include - -#include -#include - -/////////////////////////////////////////////////////////////////// -// -// CLASS NAME : LangCode -/** - * Store ISO language[_country] codes. - **/ -class LangCode { - - private: - - ISOLanguage _language; - ISOCountry _country; - - public: - - LangCode() {} - - explicit LangCode( const std::string & code_r ); - - LangCode( const ISOLanguage & language_r, - const ISOCountry & country_r = ISOCountry() ) - : _language( language_r ) - , _country( country_r ) - {} - - ~LangCode() {} - - bool isSet() const { return( _language.isSet() || _country.isSet() ); } - - bool hasLanguage() const { return _language.isSet(); } - bool hasCountry() const { return _country.isSet(); } - - std::string code() const; - std::string languageCode() const { return _language.code(); } - std::string countryCode() const { return _country.code(); } - - std::string name() const; - std::string languageName() const { return _language.name(); } - std::string countryName() const { return _country.name(); } - - public: - - ISOLanguage language() const { return _language; } - ISOCountry country() const { return _country; } -}; - -/////////////////////////////////////////////////////////////////// - -std::ostream & operator<<( std::ostream & str, const LangCode & obj ); - -/////////////////////////////////////////////////////////////////// - -inline bool operator==( const LangCode & lhs, const LangCode & rhs ) { - return( lhs.code() == rhs.code() ); -} -inline bool operator==( const std::string & lhs, const LangCode & rhs ) { - return( lhs == rhs.code() ); -} -inline bool operator==( const LangCode & lhs, const std::string & rhs ) { - return( lhs.code() == rhs ); -} - -inline bool operator!=( const LangCode & lhs, const LangCode & rhs ) { - return( ! operator==( lhs, rhs ) ); -} -inline bool operator!=( const std::string & lhs, const LangCode & rhs ) { - return( ! operator==( lhs, rhs ) ); -} -inline bool operator!=( const LangCode & lhs, const std::string & rhs ) { - return( ! operator==( lhs, rhs ) ); -} - -/////////////////////////////////////////////////////////////////// - - -namespace std { - template<> - inline bool less::operator()( const LangCode & lhs, - const LangCode & rhs ) const - { - if ( less()( lhs.language(), rhs.language() ) ) - return true; - if ( less()( rhs.language(), lhs.language() ) ) - return false; - return less()( lhs.country(), rhs.country() ); - } -} - -/////////////////////////////////////////////////////////////////// - -#endif // LangCode_h diff --git a/zypp/CountryCode.h b/zypp/CountryCode.h index abaa7e0..00cb139 100644 --- a/zypp/CountryCode.h +++ b/zypp/CountryCode.h @@ -21,6 +21,10 @@ namespace zypp { ///////////////////////////////////////////////////////////////// + class CountryCode; + inline bool operator==( const CountryCode & lhs, const CountryCode & rhs ); + inline bool operator!=( const CountryCode & lhs, const CountryCode & rhs ); + /////////////////////////////////////////////////////////////////// // // CLASS NAME : CountryCode @@ -64,6 +68,10 @@ namespace zypp /** Return the country name; if not available the country code. */ std::string name() const; + /** *this != noCode. */ + bool hasCode() const + { return *this != noCode; } + private: /** Pointer to implementation */ RW_pointer _pimpl; diff --git a/zypp/LanguageCode.h b/zypp/LanguageCode.h index 71f3f28..26a21f2 100644 --- a/zypp/LanguageCode.h +++ b/zypp/LanguageCode.h @@ -21,6 +21,10 @@ namespace zypp { ///////////////////////////////////////////////////////////////// + class LanguageCode; + inline bool operator==( const LanguageCode & lhs, const LanguageCode & rhs ); + inline bool operator!=( const LanguageCode & lhs, const LanguageCode & rhs ); + /////////////////////////////////////////////////////////////////// // // CLASS NAME : LanguageCode @@ -50,7 +54,6 @@ namespace zypp ~LanguageCode(); public: - /** \name LanguageCode constants. */ //@{ /** No or empty code. */ @@ -66,6 +69,14 @@ namespace zypp /** Return the language name; if not available the language code. */ std::string name() const; + /** *this != noCode. */ + inline bool hasCode() const + { return *this != noCode; } + + /** *this == useDefault. */ + bool useDefaultCode() const + { return *this == useDefault; } + private: /** Pointer to implementation */ RW_pointer _pimpl; diff --git a/zypp/Locale.cc b/zypp/Locale.cc new file mode 100644 index 0000000..95c6722 --- /dev/null +++ b/zypp/Locale.cc @@ -0,0 +1,184 @@ +/*---------------------------------------------------------------------\ +| ____ _ __ __ ___ | +| |__ / \ / / . \ . \ | +| / / \ V /| _/ _/ | +| / /__ | | | | | | | +| /_____||_| |_| |_| | +| | +\---------------------------------------------------------------------*/ +/** \file zypp/Locale.cc + * +*/ +#include +//#include "zypp/base/Logger.h" + +#include "zypp/Locale.h" + +using std::endl; + +/////////////////////////////////////////////////////////////////// +namespace zypp +{ ///////////////////////////////////////////////////////////////// + + /////////////////////////////////////////////////////////////////// + // + // CLASS NAME : Locale::Impl + // + /** Locale implementation. */ + struct Locale::Impl + { + Impl() + {} + + Impl( const std::string & code_r ) + { + std::string t; + std::string::size_type sep = code_r.find_first_of( "@." ); + if ( sep == std::string::npos ) { + t = code_r; + } else { + t = code_r.substr( 0, sep ); + } + + sep = t.find( '_' ); + if ( sep == std::string::npos ) { + _language = LanguageCode( t ); + } else { + _language = LanguageCode( t.substr( 0, sep ) ); + _country = CountryCode( t.substr( sep+1 ) ); + } + } + + Impl( const LanguageCode & language_r, + const CountryCode & country_r ) + : _language( language_r ) + , _country( country_r ) + {} + + const LanguageCode & language() const + { return _language; } + + const CountryCode & country() const + { return _country; } + + std::string code() const + { + std::string ret( _language.code() ); + if ( _country.hasCode() ) + ret += "_" + _country.code(); + return ret; + } + + std::string name() const + { + std::string ret( _language.name() ); + if ( _country.hasCode() ) + ret += " (" + _country.name() + ")"; + return ret; + } + + private: + + LanguageCode _language; + CountryCode _country; + + public: + /** Offer default Impl. */ + static shared_ptr nullimpl() + { if ( ! _nullimpl ) _nullimpl.reset( new Impl ); return _nullimpl; } + + private: + /** Default Impl. */ + static shared_ptr _nullimpl; + }; + /////////////////////////////////////////////////////////////////// + + shared_ptr Locale::Impl::_nullimpl; + + /////////////////////////////////////////////////////////////////// + + /** \relates Locale::Impl Stream output */ + inline std::ostream & operator<<( std::ostream & str, const Locale::Impl & obj ) + { + return str << "Locale::Impl"; + } + + /////////////////////////////////////////////////////////////////// + // + // CLASS NAME : Locale + // + /////////////////////////////////////////////////////////////////// + + const Locale Locale::noCode; + + /////////////////////////////////////////////////////////////////// + // + // METHOD NAME : Locale::Locale + // METHOD TYPE : Ctor + // + Locale::Locale() + : _pimpl( Impl::nullimpl() ) + {} + + /////////////////////////////////////////////////////////////////// + // + // METHOD NAME : Locale::Locale + // METHOD TYPE : Ctor + // + Locale::Locale( const std::string & code_r ) + : _pimpl( new Impl( code_r ) ) + {} + + /////////////////////////////////////////////////////////////////// + // + // METHOD NAME : Locale::Locale + // METHOD TYPE : Ctor + // + Locale::Locale( const LanguageCode & language_r, + const CountryCode & country_r ) + : _pimpl( new Impl( language_r, country_r ) ) + {} + + /////////////////////////////////////////////////////////////////// + // + // METHOD NAME : Locale::~Locale + // METHOD TYPE : Dtor + // + Locale::~Locale() + {} + + /////////////////////////////////////////////////////////////////// + // + // METHOD NAME : Locale:: + // METHOD TYPE : + // + const LanguageCode & Locale::language() const + { return _pimpl->language(); } + + /////////////////////////////////////////////////////////////////// + // + // METHOD NAME : Locale:: + // METHOD TYPE : + // + const CountryCode & Locale::country() const + { return _pimpl->country(); } + + /////////////////////////////////////////////////////////////////// + // + // METHOD NAME : Locale:: + // METHOD TYPE : + // + std::string Locale::code() const + { return _pimpl->code(); } + + /////////////////////////////////////////////////////////////////// + // + // METHOD NAME : Locale:: + // METHOD TYPE : + // + std::string Locale::name() const + { return _pimpl->name(); } + + ///////////////////////////////////////////////////////////////// +} // namespace zypp +/////////////////////////////////////////////////////////////////// diff --git a/zypp/Locale.h b/zypp/Locale.h new file mode 100644 index 0000000..b2ac8f4 --- /dev/null +++ b/zypp/Locale.h @@ -0,0 +1,126 @@ +/*---------------------------------------------------------------------\ +| ____ _ __ __ ___ | +| |__ / \ / / . \ . \ | +| / / \ V /| _/ _/ | +| / /__ | | | | | | | +| /_____||_| |_| |_| | +| | +\---------------------------------------------------------------------*/ +/** \file zypp/Locale.h + * +*/ +#ifndef ZYPP_LOCALE_H +#define ZYPP_LOCALE_H + +#include + +#include "zypp/base/PtrTypes.h" + +#include "zypp/LanguageCode.h" +#include "zypp/CountryCode.h" + +/////////////////////////////////////////////////////////////////// +namespace zypp +{ ///////////////////////////////////////////////////////////////// + + /////////////////////////////////////////////////////////////////// + // + // CLASS NAME : Locale + // + /** */ + class Locale + { + friend std::ostream & operator<<( std::ostream & str, const Locale & obj ); + + public: + /** Implementation */ + class Impl; + + public: + /** Default ctor */ + Locale(); + + /** Ctor taking a string. */ + explicit + Locale( const std::string & code_r ); + + /** Ctor taking LanguageCode and optional CountryCode. */ + Locale( const LanguageCode & language_r, + const CountryCode & country_r = CountryCode() ); + + /** Dtor */ + ~Locale(); + + public: + /** \name Locale constants. */ + //@{ + /** No or empty code. */ + static const Locale noCode; + //@} + + public: + /** */ + const LanguageCode & language() const; + /** */ + const CountryCode & country() const; + + /** Return the locale code. */ + std::string code() const; + + /** Return the name made of language and country name. */ + std::string name() const; + + private: + /** Pointer to implementation */ + RW_pointer _pimpl; + }; + /////////////////////////////////////////////////////////////////// + + /** \relates Locale Stream output */ + inline std::ostream & operator<<( std::ostream & str, const Locale & obj ) + { return str << obj.code(); } + + /** Comparison based on string value. */ + //@{ + /** \relates Locale */ + inline bool operator==( const Locale & lhs, const Locale & rhs ) { + return( lhs.code() == rhs.code() ); + } + /** \relates Locale */ + inline bool operator==( const std::string & lhs, const Locale & rhs ) { + return( lhs == rhs.code() ); + } + /** \relates Locale */ + inline bool operator==( const Locale & lhs, const std::string & rhs ) { + return( lhs.code() == rhs ); + } + + /** \relates Locale */ + inline bool operator!=( const Locale & lhs, const Locale & rhs ) { + return( ! operator==( lhs, rhs ) ); + } + /** \relates Locale */ + inline bool operator!=( const std::string & lhs, const Locale & rhs ) { + return( ! operator==( lhs, rhs ) ); + } + /** \relates Locale */ + inline bool operator!=( const Locale & lhs, const std::string & rhs ) { + return( ! operator==( lhs, rhs ) ); + } + //@} + + ///////////////////////////////////////////////////////////////// +} // namespace zypp +/////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////// +namespace std +{ ///////////////////////////////////////////////////////////////// + /** \relates Locale Default order for std::container based on code string value.*/ + template<> + inline bool less::operator()( const zypp::Locale & lhs, const zypp::Locale & rhs ) const + { return lhs.code() < rhs.code(); } + ///////////////////////////////////////////////////////////////// +} // namespace std +/////////////////////////////////////////////////////////////////// +#endif // ZYPP_LOCALE_H diff --git a/zypp/Makefile.am b/zypp/Makefile.am index d6994e1..5fc1e08 100644 --- a/zypp/Makefile.am +++ b/zypp/Makefile.am @@ -16,6 +16,7 @@ include_HEADERS = NeedAType.h \ Dependencies.h \ Edition.h \ LanguageCode.h \ + Locale.h \ Rel.h \ ResObject.h \ Resolvable.h \ @@ -54,6 +55,7 @@ lib@PACKAGE@_la_SOURCES = \ Dependencies.cc \ Edition.cc \ LanguageCode.cc \ + Locale.cc \ Rel.cc \ ResObject.cc \ Resolvable.cc \ diff --git a/zypp/NeedAType.h b/zypp/NeedAType.h index 831dad6..8496355 100644 --- a/zypp/NeedAType.h +++ b/zypp/NeedAType.h @@ -16,6 +16,7 @@ #include #include #include +#include "zypp/Locale.h" /////////////////////////////////////////////////////////////////// namespace zypp @@ -72,16 +73,6 @@ namespace zypp /** Vendor. Worth a typedef. Maybe a class unifying the strings. */ typedef std::string Vendor; - /** Handle Strings like "en_US". Offerrs their CountryCode and - LanguageCode parts. */ - typedef std::string Locale; - - /** CountryCode of locale (from libutil incl. translations) */ - typedef std::string CountryCode; - - /** LanguageCode of locale (from libutil incl. translations) */ - typedef std::string LanguageCode; - /** Handle data depending on a locale. Translated strings, maybe other too. */ template diff --git a/zypp/detail/ResObjectImplIf.h b/zypp/detail/ResObjectImplIf.h index c48950f..90a444c 100644 --- a/zypp/detail/ResObjectImplIf.h +++ b/zypp/detail/ResObjectImplIf.h @@ -16,6 +16,7 @@ #include #include "zypp/detail/ResObjectFactory.h" +#include "zypp/Locale.h" #include "zypp/ByteCount.h" #include "zypp/Date.h" -- 2.7.4