Added Locale
authorMichael Andres <ma@suse.de>
Mon, 19 Dec 2005 13:49:34 +0000 (13:49 +0000)
committerMichael Andres <ma@suse.de>
Mon, 19 Dec 2005 13:49:34 +0000 (13:49 +0000)
zypp/@Review/LangCode.cc [deleted file]
zypp/@Review/LangCode.h [deleted file]
zypp/CountryCode.h
zypp/LanguageCode.h
zypp/Locale.cc [new file with mode: 0644]
zypp/Locale.h [new file with mode: 0644]
zypp/Makefile.am
zypp/NeedAType.h
zypp/detail/ResObjectImplIf.h

diff --git a/zypp/@Review/LangCode.cc b/zypp/@Review/LangCode.cc
deleted file mode 100644 (file)
index c9cc63e..0000000
+++ /dev/null
@@ -1,89 +0,0 @@
-/*---------------------------------------------------------------------\
-|                                                                      |
-|                      __   __    ____ _____ ____                      |
-|                      \ \ / /_ _/ ___|_   _|___ \                     |
-|                       \ V / _` \___ \ | |   __) |                    |
-|                        | | (_| |___) || |  / __/                     |
-|                        |_|\__,_|____/ |_| |_____|                    |
-|                                                                      |
-|                               core system                            |
-|                                                        (C) SuSE GmbH |
-\----------------------------------------------------------------------/
-
-   File:       LangCode.cc
-
-   Author:     Michael Andres <ma@suse.de>
-   Maintainer: Michael Andres <ma@suse.de>
-
-/-*/
-
-#include <iostream>
-
-#include <y2util/LangCode.h>
-
-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 (file)
index 21d6730..0000000
+++ /dev/null
@@ -1,117 +0,0 @@
-/*---------------------------------------------------------------------\
-|                                                                      |
-|                      __   __    ____ _____ ____                      |
-|                      \ \ / /_ _/ ___|_   _|___ \                     |
-|                       \ V / _` \___ \ | |   __) |                    |
-|                        | | (_| |___) || |  / __/                     |
-|                        |_|\__,_|____/ |_| |_____|                    |
-|                                                                      |
-|                               core system                            |
-|                                                        (C) SuSE GmbH |
-\----------------------------------------------------------------------/
-
-   File:       LangCode.h
-
-   Author:     Michael Andres <ma@suse.de>
-   Maintainer: Michael Andres <ma@suse.de>
-
-/-*/
-#ifndef LangCode_h
-#define LangCode_h
-
-#include <iosfwd>
-
-#include <y2util/ISOLanguage.h>
-#include <y2util/ISOCountry.h>
-
-///////////////////////////////////////////////////////////////////
-//
-//     CLASS NAME : LangCode
-/**
- * Store ISO <code>language[_country]</code> 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<LangCode>::operator()( const LangCode & lhs,
-                                            const LangCode & rhs ) const
-    {
-      if ( less<ISOLanguage>()( lhs.language(), rhs.language() ) )
-        return true;
-      if ( less<ISOLanguage>()( rhs.language(), lhs.language() ) )
-        return false;
-      return less<ISOCountry>()( lhs.country(), rhs.country() );
-    }
-}
-
-///////////////////////////////////////////////////////////////////
-
-#endif // LangCode_h
index abaa7e0..00cb139 100644 (file)
 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;
 
+    /** <tt>*this != noCode</tt>. */
+    bool hasCode() const
+    { return *this != noCode; }
+
   private:
     /** Pointer to implementation */
     RW_pointer<Impl> _pimpl;
index 71f3f28..26a21f2 100644 (file)
 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;
 
+    /** <tt>*this != noCode</tt>. */
+    inline bool hasCode() const
+    { return *this != noCode; }
+
+    /** <tt>*this == useDefault</tt>. */
+    bool useDefaultCode() const
+    { return *this == useDefault; }
+
   private:
     /** Pointer to implementation */
     RW_pointer<Impl> _pimpl;
diff --git a/zypp/Locale.cc b/zypp/Locale.cc
new file mode 100644 (file)
index 0000000..95c6722
--- /dev/null
@@ -0,0 +1,184 @@
+/*---------------------------------------------------------------------\
+|                          ____ _   __ __ ___                          |
+|                         |__  / \ / / . \ . \                         |
+|                           / / \ V /|  _/  _/                         |
+|                          / /__ | | | | | |                           |
+|                         /_____||_| |_| |_|                           |
+|                                                                      |
+\---------------------------------------------------------------------*/
+/** \file      zypp/Locale.cc
+ *
+*/
+#include <iostream>
+//#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<Impl> nullimpl()
+    { if ( ! _nullimpl ) _nullimpl.reset( new Impl ); return _nullimpl; }
+
+  private:
+    /** Default Impl. */
+    static shared_ptr<Impl> _nullimpl;
+  };
+  ///////////////////////////////////////////////////////////////////
+
+  shared_ptr<Locale::Impl> 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 (file)
index 0000000..b2ac8f4
--- /dev/null
@@ -0,0 +1,126 @@
+/*---------------------------------------------------------------------\
+|                          ____ _   __ __ ___                          |
+|                         |__  / \ / / . \ . \                         |
+|                           / / \ V /|  _/  _/                         |
+|                          / /__ | | | | | |                           |
+|                         /_____||_| |_| |_|                           |
+|                                                                      |
+\---------------------------------------------------------------------*/
+/** \file      zypp/Locale.h
+ *
+*/
+#ifndef ZYPP_LOCALE_H
+#define ZYPP_LOCALE_H
+
+#include <iosfwd>
+
+#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<Impl> _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<zypp::Locale>::operator()( const zypp::Locale & lhs, const zypp::Locale & rhs ) const
+    { return lhs.code() < rhs.code(); }
+  /////////////////////////////////////////////////////////////////
+} // namespace std
+///////////////////////////////////////////////////////////////////
+#endif // ZYPP_LOCALE_H
index d6994e1..5fc1e08 100644 (file)
@@ -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   \
index 831dad6..8496355 100644 (file)
@@ -16,6 +16,7 @@
 #include <map>
 #include <list>
 #include <string>
+#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<class _Val>
index c48950f..90a444c 100644 (file)
@@ -16,6 +16,7 @@
 #include <string>
 
 #include "zypp/detail/ResObjectFactory.h"
+#include "zypp/Locale.h"
 #include "zypp/ByteCount.h"
 #include "zypp/Date.h"